]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Platform/UC3/ClockManagement.h
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Platform / UC3 / ClockManagement.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief Module Clock Driver for the AVR32 UC3 microcontrollers.\r
33  *\r
34  *  Clock management driver for the AVR32 UC3 microcontrollers. This driver allows for the configuration\r
35  *  of the various clocks within the device to clock the various peripherals.\r
36  */\r
37 \r
38 /** \ingroup Group_PlatformDrivers_UC3\r
39  *  \defgroup Group_PlatformDrivers_UC3Clocks Clock Management Driver - LUFA/Platform/UC3/ClockManagement.h\r
40  *  \brief Module Clock Driver for the AVR32 UC3 microcontrollers.\r
41  *\r
42  *  \section Sec_Dependencies Module Source Dependencies\r
43  *  The following files must be built with any user project that uses this module:\r
44  *    - None\r
45  *\r
46  *  \section Sec_ModDescription Module Description\r
47  *  Clock management driver for the AVR32 UC3 microcontrollers. This driver allows for the configuration\r
48  *  of the various clocks within the device to clock the various peripherals.\r
49  *\r
50  *  Usage Example:\r
51  *  \code\r
52  *              #include <LUFA/Platform/UC3/ClockManagement.h>\r
53  *      \r
54  *              void main(void)\r
55  *              {\r
56  *                      // Start the master external oscillator which will be used as the main clock reference\r
57  *                      AVR32CLK_StartExternalOscillator(0, EXOSC_MODE_8MHZ_OR_MORE, EXOSC_START_0CLK);\r
58  *          \r
59  *                      // Start the PLL for the CPU clock, switch CPU to it\r
60  *                      AVR32CLK_StartPLL(0, CLOCK_SRC_OSC0, 12000000, F_CPU);\r
61  *                      AVR32CLK_SetCPUClockSource(CLOCK_SRC_PLL0, F_CPU);\r
62  *          \r
63  *                      // Start the PLL for the USB Generic Clock module\r
64  *                      AVR32CLK_StartPLL(1, CLOCK_SRC_OSC0, 12000000, 48000000);\r
65  *              }\r
66  *  \endcode\r
67  *\r
68  *  @{\r
69  */\r
70 \r
71 #ifndef _UC3_CLOCK_MANAGEMENT_H_\r
72 #define _UC3_CLOCK_MANAGEMENT_H_\r
73 \r
74         /* Includes: */\r
75                 #include "../../Common/Common.h"\r
76 \r
77         /* Enable C linkage for C++ Compilers: */\r
78                 #if defined(__cplusplus)\r
79                         extern "C" {\r
80                 #endif\r
81 \r
82         /* Public Interface - May be used in end-application: */\r
83                 /* Macros: */\r
84                         /** Enum for the possible external oscillator types. */\r
85                         enum UC3_Extern_OSC_ClockTypes_t\r
86                         {\r
87                                 EXOSC_MODE_CLOCK         = AVR32_PM_OSCCTRL0_MODE_EXT_CLOCK,  /**< External clock (non-crystal) mode. */\r
88                                 EXOSC_MODE_900KHZ_MAX    = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G0, /**< External crystal oscillator equal to or slower than 900KHz. */\r
89                                 EXOSC_MODE_3MHZ_MAX      = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G1, /**< External crystal oscillator equal to or slower than 3MHz. */\r
90                                 EXOSC_MODE_8MHZ_MAX      = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G2, /**< External crystal oscillator equal to or slower than 8MHz. */\r
91                                 EXOSC_MODE_8MHZ_OR_MORE  = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G3, /**< External crystal oscillator equal to or faster than 8MHz. */\r
92                         };\r
93 \r
94                         /** Enum for the possible external oscillator startup times. */\r
95                         enum UC3_Extern_OSC_ClockStartup_t\r
96                         {\r
97                                 EXOSC_START_0CLK         = AVR32_PM_OSCCTRL0_STARTUP_0_RCOSC,     /**< Immediate startup, no delay. */\r
98                                 EXOSC_START_64CLK        = AVR32_PM_OSCCTRL0_STARTUP_64_RCOSC,    /**< Wait 64 clock cycles before startup for stability. */\r
99                                 EXOSC_START_128CLK       = AVR32_PM_OSCCTRL0_STARTUP_128_RCOSC,   /**< Wait 128 clock cycles before startup for stability. */\r
100                                 EXOSC_START_2048CLK      = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC,  /**< Wait 2048 clock cycles before startup for stability. */\r
101                                 EXOSC_START_4096CLK      = AVR32_PM_OSCCTRL0_STARTUP_4096_RCOSC,  /**< Wait 4096 clock cycles before startup for stability. */\r
102                                 EXOSC_START_8192CLK      = AVR32_PM_OSCCTRL0_STARTUP_8192_RCOSC,  /**< Wait 8192 clock cycles before startup for stability. */\r
103                                 EXOSC_START_16384CLK     = AVR32_PM_OSCCTRL0_STARTUP_16384_RCOSC, /**< Wait 16384 clock cycles before startup for stability. */\r
104                         };\r
105 \r
106                         /** Enum for the possible module clock sources. */\r
107                         enum UC3_System_ClockSource_t\r
108                         {\r
109                                 CLOCK_SRC_SLOW_CLK       = 0, /**< Clock sourced from the internal slow clock. */\r
110                                 CLOCK_SRC_OSC0           = 1, /**< Clock sourced from the Oscillator 0 clock. */\r
111                                 CLOCK_SRC_OSC1           = 2, /**< Clock sourced from the Oscillator 1 clock. */\r
112                                 CLOCK_SRC_PLL0           = 3, /**< Clock sourced from the PLL 0 clock. */\r
113                                 CLOCK_SRC_PLL1           = 4, /**< Clock sourced from the PLL 1 clock. */\r
114                         };\r
115 \r
116                 /* Inline Functions: */\r
117                         /** Starts the given external oscillator of the UC3 microcontroller, with the given options. This routine blocks until\r
118                          *  the oscillator is ready for use.\r
119                          *\r
120                          *  \param[in] Channel  Index of the external oscillator to start.\r
121                          *  \param[in] Type     Type of clock attached to the given oscillator channel, a value from \ref UC3_Extern_OSC_ClockTypes_t.\r
122                          *  \param[in] Startup  Statup time of the external oscillator, a value from \ref UC3_Extern_OSC_ClockStartup_t.\r
123                          *\r
124                          *  \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.\r
125                          */\r
126                         static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
127                                                                             const uint8_t Type,\r
128                                                                             const uint8_t Startup) ATTR_ALWAYS_INLINE;\r
129                         static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,\r
130                                                                             const uint8_t Type,\r
131                                                                             const uint8_t Startup)\r
132                         {\r
133                                 switch (Channel)\r
134                                 {\r
135                                         case 0:\r
136                                                 AVR32_PM.OSCCTRL0.startup = Startup;\r
137                                                 AVR32_PM.OSCCTRL0.mode    = Type;\r
138                                                 break;\r
139                                         case 1:\r
140                                                 AVR32_PM.OSCCTRL1.startup = Startup;\r
141                                                 AVR32_PM.OSCCTRL1.mode    = Type;\r
142                                                 break;\r
143                                         default:\r
144                                                 return false;\r
145                                 }\r
146 \r
147                                 AVR32_PM.mcctrl |= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));\r
148 \r
149                                 while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET + Channel))));\r
150                                 return true;\r
151                         }\r
152 \r
153                         /** Stops the given external oscillator of the UC3 microcontroller.\r
154                          *\r
155                          *  \param[in] Channel  Index of the external oscillator to stop.\r
156                          */\r
157                         static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE;\r
158                         static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel)\r
159                         {\r
160                                 AVR32_PM.mcctrl &= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));\r
161                         }\r
162 \r
163                         /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use.\r
164                          *\r
165                          *  \attention The output frequency must be equal to or greater than the source frequency.\r
166                          *\r
167                          *  \param[in] Channel     Index of the PLL to start.\r
168                          *  \param[in] Source      Clock source for the PLL, a value from \ref UC3_System_ClockSource_t.\r
169                          *  \param[in] SourceFreq  Frequency of the PLL's clock source, in Hz.\r
170                          *  \param[in] Frequency   Target frequency of the PLL's output.\r
171                          *\r
172                          *  \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.\r
173                          */\r
174                         static inline bool AVR32CLK_StartPLL(const uint8_t Channel,\r
175                                                              const uint8_t Source,\r
176                                                              const uint32_t SourceFreq,\r
177                                                              const uint32_t Frequency) ATTR_ALWAYS_INLINE;\r
178                         static inline bool AVR32CLK_StartPLL(const uint8_t Channel,\r
179                                                              const uint8_t Source,\r
180                                                              const uint32_t SourceFreq,\r
181                                                              const uint32_t Frequency)\r
182                         {\r
183                                 if (SourceFreq > Frequency)\r
184                                   return false;\r
185 \r
186                                 switch (Source)\r
187                                 {\r
188                                         case CLOCK_SRC_OSC0:\r
189                                                 AVR32_PM.PLL[Channel].pllosc = 0;\r
190                                                 break;\r
191                                         case CLOCK_SRC_OSC1:\r
192                                                 AVR32_PM.PLL[Channel].pllosc = 1;\r
193                                                 break;\r
194                                         default:\r
195                                                 return false;\r
196                                 }\r
197 \r
198                                 AVR32_PM.PLL[Channel].pllmul = (Frequency / SourceFreq) ? (((Frequency / SourceFreq) - 1) / 2) : 0;\r
199                                 AVR32_PM.PLL[Channel].plldiv = 0;\r
200                                 AVR32_PM.PLL[Channel].pllen  = true;\r
201 \r
202                                 while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET + Channel))));\r
203                                 return true;\r
204                         }\r
205 \r
206                         /** Stops the given PLL of the UC3 microcontroller.\r
207                          *\r
208                          *  \param[in] Channel  Index of the PLL to stop.\r
209                          */\r
210                         static inline void AVR32CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE;\r
211                         static inline void AVR32CLK_StopPLL(const uint8_t Channel)\r
212                         {\r
213                                 AVR32_PM.PLL[Channel].pllen = false;\r
214                         }\r
215 \r
216                         /** Starts the given Generic Clock of the UC3 microcontroller, with the given options.\r
217                          *\r
218                          *  \param[in] Channel     Index of the Generic Clock to start.\r
219                          *  \param[in] Source      Clock source for the Generic Clock, a value from \ref UC3_System_ClockSource_t.\r
220                          *  \param[in] SourceFreq  Frequency of the Generic Clock's clock source, in Hz.\r
221                          *  \param[in] Frequency   Target frequency of the Generic Clock's output.\r
222                          *\r
223                          *  \return Boolean \c true if the Generic Clock was successfully started, \c false if invalid parameters specified.\r
224                          */\r
225                         static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,\r
226                                                                       const uint8_t Source,\r
227                                                                       const uint32_t SourceFreq,\r
228                                                                       const uint32_t Frequency) ATTR_ALWAYS_INLINE;\r
229                         static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,\r
230                                                                       const uint8_t Source,\r
231                                                                       const uint32_t SourceFreq,\r
232                                                                       const uint32_t Frequency)\r
233                         {\r
234                                 if (Channel >= AVR32_PM_GCLK_NUM)\r
235                                   return false;\r
236 \r
237                                 if (SourceFreq < Frequency)\r
238                                   return false;\r
239 \r
240                                 switch (Source)\r
241                                 {\r
242                                         case CLOCK_SRC_OSC0:\r
243                                                 AVR32_PM.GCCTRL[Channel].pllsel = false;\r
244                                                 AVR32_PM.GCCTRL[Channel].oscsel = 0;\r
245                                                 break;\r
246                                         case CLOCK_SRC_OSC1:\r
247                                                 AVR32_PM.GCCTRL[Channel].pllsel = false;\r
248                                                 AVR32_PM.GCCTRL[Channel].oscsel = 1;\r
249                                                 break;\r
250                                         case CLOCK_SRC_PLL0:\r
251                                                 AVR32_PM.GCCTRL[Channel].pllsel = true;\r
252                                                 AVR32_PM.GCCTRL[Channel].oscsel = 0;\r
253                                                 break;\r
254                                         case CLOCK_SRC_PLL1:\r
255                                                 AVR32_PM.GCCTRL[Channel].pllsel = true;\r
256                                                 AVR32_PM.GCCTRL[Channel].oscsel = 1;\r
257                                                 break;\r
258                                         default:\r
259                                                 return false;\r
260                                 }\r
261 \r
262                                 AVR32_PM.GCCTRL[Channel].diven = (SourceFreq > Frequency) ? true : false;\r
263                                 AVR32_PM.GCCTRL[Channel].div   = (((SourceFreq / Frequency) - 1) / 2);\r
264                                 AVR32_PM.GCCTRL[Channel].cen   = true;\r
265 \r
266                                 return true;\r
267                         }\r
268 \r
269                         /** Stops the given generic clock of the UC3 microcontroller.\r
270                          *\r
271                          *  \param[in] Channel  Index of the generic clock to stop.\r
272                          *\r
273                          *  \return Boolean \c true if the generic clock was sucessfully stopped, \c false if invalid parameters specified.\r
274                          */\r
275                         static inline bool AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE;\r
276                         static inline bool AVR32CLK_StopGenericClock(const uint8_t Channel)\r
277                         {\r
278                                 if (Channel >= AVR32_PM_GCLK_NUM)\r
279                                   return false;\r
280                         \r
281                                 AVR32_PM.GCCTRL[Channel].cen = false;\r
282                                 \r
283                                 return true;\r
284                         }\r
285 \r
286                         /** Sets the clock source for the main microcontroller core. The given clock source should be configured\r
287                          *  and ready for use before this function is called.\r
288                          *\r
289                          *  This function will configure the FLASH controller's wait states automatically to suit the given clock source.\r
290                          *\r
291                          *  \param[in] Source      Clock source for the CPU core, a value from \ref UC3_System_ClockSource_t.\r
292                          *  \param[in] SourceFreq  Frequency of the CPU core's clock source, in Hz.\r
293                          *\r
294                          *  \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified.\r
295                          */\r
296                         static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,\r
297                                                                       const uint32_t SourceFreq) ATTR_ALWAYS_INLINE;\r
298                         static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,\r
299                                                                       const uint32_t SourceFreq)\r
300                         {\r
301                                 if (SourceFreq > AVR32_PM_CPU_MAX_FREQ)\r
302                                   return false;\r
303 \r
304                                 AVR32_FLASHC.FCR.fws = (SourceFreq > AVR32_FLASHC_FWS_0_MAX_FREQ) ? true : false;\r
305                                 \r
306                                 switch (Source)\r
307                                 {\r
308                                         #if defined(AVR32_PM_MCCTRL_MCSEL_SLOW)\r
309                                         case CLOCK_SRC_SLOW_CLK:\r
310                                                 AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_SLOW;\r
311                                                 break;\r
312                                         #endif\r
313                                         #if defined(AVR32_PM_MCCTRL_MCSEL_OSC0)\r
314                                         case CLOCK_SRC_OSC0:\r
315                                                 AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_OSC0;\r
316                                                 break;\r
317                                         #endif\r
318                                         #if defined(AVR32_PM_MCCTRL_MCSEL_PLL0)\r
319                                         case CLOCK_SRC_PLL0:\r
320                                                 AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_PLL0;\r
321                                                 break;\r
322                                         #endif\r
323                                         default:\r
324                                                 return false;\r
325                                 }\r
326 \r
327                                 return true;\r
328                         }\r
329 \r
330         /* Disable C linkage for C++ Compilers: */\r
331                 #if defined(__cplusplus)\r
332                         }\r
333                 #endif\r
334 \r
335 #endif\r
336 \r
337 /** @} */\r
338 \r