]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Platform/XMEGA/ClockManagement.h
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Platform / XMEGA / 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 AVR USB XMEGA microcontrollers.\r
33  *\r
34  *  Clock management driver for the AVR USB XMEGA 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_XMEGA\r
39  *  \defgroup Group_PlatformDrivers_XMEGAClocks Clock Management Driver - LUFA/Platform/XMEGA/ClockManagement.h\r
40  *  \brief Module Clock Driver for the AVR USB XMEGA 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 AVR USB XMEGA 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/XMEGA/ClockManagement.h>\r
53  *      \r
54  *      void main(void)\r
55  *      {\r
56  *              // Start the PLL to multiply the 2MHz RC oscillator to F_CPU and switch the CPU core to run from it\r
57  *              XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);\r
58  *              XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);\r
59  *          \r
60  *              // Start the 32MHz internal RC oscillator and start the DFLL to increase it to F_USB using the USB SOF as a reference\r
61  *              XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);\r
62  *              XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);\r
63  *      }\r
64  *  \endcode\r
65  *\r
66  *  @{\r
67  */\r
68 \r
69 #ifndef _XMEGA_CLOCK_MANAGEMENT_H_\r
70 #define _XMEGA_CLOCK_MANAGEMENT_H_\r
71 \r
72         /* Includes: */\r
73                 #include "../../Common/Common.h"\r
74 \r
75         /* Enable C linkage for C++ Compilers: */\r
76                 #if defined(__cplusplus)\r
77                         extern "C" {\r
78                 #endif\r
79 \r
80         /* Public Interface - May be used in end-application: */\r
81                 /* Macros: */\r
82                         /** Enum for the possible external oscillator frequency ranges. */\r
83                         enum XMEGA_Extern_OSC_ClockFrequency_t\r
84                         {\r
85                                 EXOSC_FREQ_2MHZ_MAX      = OSC_FRQRANGE_04TO2_gc,  /**< External crystal oscillator equal to or slower than 2MHz. */\r
86                                 EXOSC_FREQ_9MHZ_MAX      = OSC_FRQRANGE_2TO9_gc,   /**< External crystal oscillator equal to or slower than 9MHz. */\r
87                                 EXOSC_FREQ_12MHZ_MAX     = OSC_FRQRANGE_9TO12_gc,  /**< External crystal oscillator equal to or slower than 12MHz. */\r
88                                 EXOSC_FREQ_16MHZ_MAX     = OSC_FRQRANGE_12TO16_gc, /**< External crystal oscillator equal to or slower than 16MHz. */\r
89                         };\r
90 \r
91                         /** Enum for the possible external oscillator startup times. */\r
92                         enum XMEGA_Extern_OSC_ClockStartup_t\r
93                         {\r
94                                 EXOSC_START_6CLK         = OSC_XOSCSEL_EXTCLK_gc,      /**< Wait 6 clock cycles before startup (external clock). */\r
95                                 EXOSC_START_32KCLK       = OSC_XOSCSEL_32KHz_gc,       /**< Wait 32K clock cycles before startup (32.768KHz crystal). */\r
96                                 EXOSC_START_256CLK       = OSC_XOSCSEL_XTAL_256CLK_gc, /**< Wait 256 clock cycles before startup. */\r
97                                 EXOSC_START_1KCLK        = OSC_XOSCSEL_XTAL_1KCLK_gc,  /**< Wait 1K clock cycles before startup. */\r
98                                 EXOSC_START_16KCLK       = OSC_XOSCSEL_XTAL_16KCLK_gc, /**< Wait 16K clock cycles before startup. */\r
99                         };\r
100 \r
101                         /** Enum for the possible module clock sources. */\r
102                         enum XMEGA_System_ClockSource_t\r
103                         {\r
104                                 CLOCK_SRC_INT_RC2MHZ    = 0, /**< Clock sourced from the Internal 2MHz RC Oscillator clock. */\r
105                                 CLOCK_SRC_INT_RC32MHZ   = 1, /**< Clock sourced from the Internal 32MHz RC Oscillator clock. */\r
106                                 CLOCK_SRC_INT_RC32KHZ   = 2, /**< Clock sourced from the Internal 32KHz RC Oscillator clock. */\r
107                                 CLOCK_SRC_XOSC          = 3, /**< Clock sourced from the External Oscillator clock. */\r
108                                 CLOCK_SRC_PLL           = 4, /**< Clock sourced from the Internal PLL clock. */\r
109                         };\r
110 \r
111                         /** Enum for the possible DFLL clock reference sources. */\r
112                         enum XMEGA_System_DFLLReference_t\r
113                         {\r
114                                 DFLL_REF_INT_RC32KHZ   = 0, /**< Reference clock sourced from the Internal 32KHz RC Oscillator clock. */\r
115                                 DFLL_REF_EXT_RC32KHZ   = 1, /**< Reference clock sourced from the External 32KHz RC Oscillator clock connected to TOSC pins. */\r
116                                 DFLL_REF_INT_USBSOF    = 2, /**< Reference clock sourced from the USB Start Of Frame packets. */\r
117                         };\r
118 \r
119                 /* Inline Functions: */\r
120                         /** Write a value to a location protected by the XMEGA CCP protection mechanism. This function uses inline assembly to ensure that\r
121                          *  the protected address is written to within four clock cycles of the CCP key being written.\r
122                          *\r
123                          *  \param[in] Address  Address to write to, a memory address protected by the CCP mechanism\r
124                          *  \param[in] Value    Value to write to the protected location\r
125                          */\r
126                         static inline void XMEGACLK_CCP_Write(volatile void* Address, const uint8_t Value) ATTR_ALWAYS_INLINE;\r
127                         static inline void XMEGACLK_CCP_Write(volatile void* Address, const uint8_t Value)\r
128                         {\r
129                                 __asm__ __volatile__ (\r
130                                         "out %0, __zero_reg__" "\n\t" /* Zero RAMPZ using fixed zero value register */\r
131                                         "movw r30, %1"         "\n\t" /* Copy address to Z register pair */\r
132                                         "out %2, %3"           "\n\t" /* Write key to CCP register */\r
133                                         "st Z, %4"             "\n\t" /* Indirectly write value to address */\r
134                                         : /* No output operands */\r
135                                         : /* Input operands: */ "m" (RAMPZ), "e" (Address), "m" (CCP), "r" (CCP_IOREG_gc), "r" (Value)\r
136                                         : /* Clobbered registers: */ "r30", "r31"\r
137                                 ); \r
138                         }\r
139 \r
140                         /** Starts the external oscillator of the XMEGA microcontroller, with the given options. This routine blocks until\r
141                          *  the oscillator is ready for use.\r
142                          *\r
143                          *  \param[in] FreqRange  Frequency range of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockFrequency_t.\r
144                          *  \param[in] Startup    Startup time of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockStartup_t.\r
145                          *\r
146                          *  \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.\r
147                          */\r
148                         static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange,\r
149                                                                             const uint8_t Startup) ATTR_ALWAYS_INLINE;\r
150                         static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange,\r
151                                                                             const uint8_t Startup)\r
152                         {\r
153                                 OSC.XOSCCTRL  = (FreqRange | ((Startup == EXOSC_START_32KCLK) ? OSC_X32KLPM_bm : 0) | Startup);\r
154                                 OSC.CTRL     |= OSC_XOSCEN_bm;\r
155 \r
156                                 while (!(OSC.STATUS & OSC_XOSCRDY_bm));\r
157                                 return true;\r
158                         }\r
159 \r
160                         /** Stops the external oscillator of the XMEGA microcontroller. */\r
161                         static inline void XMEGACLK_StopExternalOscillator(void) ATTR_ALWAYS_INLINE;\r
162                         static inline void XMEGACLK_StopExternalOscillator(void)\r
163                         {\r
164                                 OSC.CTRL     &= ~OSC_XOSCEN_bm;\r
165                         }\r
166 \r
167                         /** Starts the given internal oscillator of the XMEGA microcontroller, with the given options. This routine blocks until\r
168                          *  the oscillator is ready for use.\r
169                          *\r
170                          *  \param[in] Source  Internal oscillator to start, a value from \ref XMEGA_System_ClockSource_t.\r
171                          *\r
172                          *  \return Boolean \c true if the internal oscillator was successfully started, \c false if invalid parameters specified.\r
173                          */\r
174                         static inline uint8_t XMEGACLK_StartInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;\r
175                         static inline uint8_t XMEGACLK_StartInternalOscillator(const uint8_t Source)\r
176                         {\r
177                                 switch (Source)\r
178                                 {\r
179                                         case CLOCK_SRC_INT_RC2MHZ:\r
180                                                 OSC.CTRL |= OSC_RC2MEN_bm;\r
181                                                 while (!(OSC.STATUS & OSC_RC2MRDY_bm));\r
182                                                 return true;\r
183                                         case CLOCK_SRC_INT_RC32MHZ:\r
184                                                 OSC.CTRL |= OSC_RC32MEN_bm;\r
185                                                 while (!(OSC.STATUS & OSC_RC32MRDY_bm));\r
186                                                 return true;\r
187                                         case CLOCK_SRC_INT_RC32KHZ:\r
188                                                 OSC.CTRL |= OSC_RC32KEN_bm;\r
189                                                 while (!(OSC.STATUS & OSC_RC32KRDY_bm));\r
190                                                 return true;\r
191                                 }\r
192 \r
193                                 return false;\r
194                         }\r
195 \r
196                         /** Stops the given internal oscillator of the XMEGA microcontroller.\r
197                          *\r
198                          *  \param[in] Source  Internal oscillator to stop, a value from \ref XMEGA_System_ClockSource_t.\r
199                          *\r
200                          *  \return Boolean \c true if the internal oscillator was successfully stopped, \c false if invalid parameters specified.\r
201                          */\r
202                         static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;\r
203                         static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source)\r
204                         {\r
205                                 switch (Source)\r
206                                 {\r
207                                         case CLOCK_SRC_INT_RC2MHZ:\r
208                                                 OSC.CTRL &= ~OSC_RC2MEN_bm;\r
209                                                 return true;\r
210                                         case CLOCK_SRC_INT_RC32MHZ:\r
211                                                 OSC.CTRL &= ~OSC_RC32MEN_bm;\r
212                                                 return true;\r
213                                         case CLOCK_SRC_INT_RC32KHZ:\r
214                                                 OSC.CTRL &= ~OSC_RC32KEN_bm;\r
215                                                 return true;\r
216                                 }\r
217 \r
218                                 return false;\r
219                         }\r
220 \r
221                         /** Starts the PLL of the XMEGA microcontroller, with the given options. This routine blocks until the PLL is ready for use.\r
222                          *\r
223                          *  \attention The output frequency must be equal to or greater than the source frequency.\r
224                          *\r
225                          *  \param[in] Source       Clock source for the PLL, a value from \ref XMEGA_System_ClockSource_t.\r
226                          *  \param[in] SourceFreq   Frequency of the PLL's clock source, in Hz.\r
227                          *  \param[in] Frequency    Target frequency of the PLL's output.\r
228                          *\r
229                          *  \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.\r
230                          */\r
231                         static inline bool XMEGACLK_StartPLL(const uint8_t Source,\r
232                                                              const uint32_t SourceFreq,\r
233                                                              const uint32_t Frequency) ATTR_ALWAYS_INLINE;\r
234                         static inline bool XMEGACLK_StartPLL(const uint8_t Source,\r
235                                                              const uint32_t SourceFreq,\r
236                                                              const uint32_t Frequency)\r
237                         {\r
238                                 uint8_t MulFactor = (Frequency / SourceFreq);\r
239 \r
240                                 if (SourceFreq > Frequency)\r
241                                   return false;\r
242                                   \r
243                                 if (MulFactor > 31)\r
244                                   return false;\r
245 \r
246                                 switch (Source)\r
247                                 {\r
248                                         case CLOCK_SRC_INT_RC2MHZ:\r
249                                                 OSC.PLLCTRL = (OSC_PLLSRC_RC2M_gc  | MulFactor);\r
250                                                 break;\r
251                                         case CLOCK_SRC_INT_RC32MHZ:\r
252                                                 OSC.PLLCTRL = (OSC_PLLSRC_RC32M_gc | MulFactor);\r
253                                                 break;\r
254                                         case CLOCK_SRC_XOSC:\r
255                                                 OSC.PLLCTRL = (OSC_PLLSRC_XOSC_gc  | MulFactor);\r
256                                                 break;\r
257                                         default:\r
258                                                 return false;\r
259                                 }\r
260 \r
261                                 OSC.CTRL |= OSC_PLLEN_bm;\r
262 \r
263                                 while (!(OSC.STATUS & OSC_PLLRDY_bm));\r
264                                 return true;\r
265                         }\r
266 \r
267                         /** Stops the PLL of the XMEGA microcontroller. */\r
268                         static inline void XMEGACLK_StopPLL(void) ATTR_ALWAYS_INLINE;\r
269                         static inline void XMEGACLK_StopPLL(void)\r
270                         {\r
271                                 OSC.CTRL &= ~OSC_PLLEN_bm;\r
272                         }\r
273 \r
274                         /** Starts the DFLL of the XMEGA microcontroller, with the given options.\r
275                          *\r
276                          *  \param[in] Source     RC Clock source for the DFLL, a value from \ref XMEGA_System_ClockSource_t.\r
277                          *  \param[in] Reference  Reference clock source for the DFLL, an value from \ref XMEGA_System_DFLLReference_t.\r
278                          *  \param[in] Frequency  Target frequency of the DFLL's output.\r
279                          *\r
280                          *  \return Boolean \c true if the DFLL was successfully started, \c false if invalid parameters specified.\r
281                          */\r
282                         static inline bool XMEGACLK_StartDFLL(const uint8_t Source,\r
283                                                               const uint8_t Reference,\r
284                                                               const uint32_t Frequency) ATTR_ALWAYS_INLINE;\r
285                         static inline bool XMEGACLK_StartDFLL(const uint8_t Source,\r
286                                                               const uint8_t Reference,\r
287                                                               const uint32_t Frequency)\r
288                         {\r
289                                 uint16_t DFLLCompare = (Frequency / 1000);\r
290 \r
291                                 switch (Source)\r
292                                 {\r
293                                         case CLOCK_SRC_INT_RC2MHZ:\r
294                                                 OSC.DFLLCTRL   |= (Reference << OSC_RC2MCREF_bp);\r
295                                                 DFLLRC2M.COMP1  = (DFLLCompare & 0xFF);\r
296                                                 DFLLRC2M.COMP2  = (DFLLCompare >> 8);\r
297                                                 DFLLRC2M.CTRL   = DFLL_ENABLE_bm;\r
298                                                 break;\r
299                                         case CLOCK_SRC_INT_RC32MHZ:\r
300                                                 OSC.DFLLCTRL   |= (Reference << OSC_RC32MCREF_gp);\r
301                                                 DFLLRC32M.COMP1 = (DFLLCompare & 0xFF);\r
302                                                 DFLLRC32M.COMP2 = (DFLLCompare >> 8);\r
303 \r
304                                                 if (Reference == DFLL_REF_INT_USBSOF)\r
305                                                 {\r
306                                                         NVM.CMD        = NVM_CMD_READ_CALIB_ROW_gc;\r
307                                                         DFLLRC32M.CALA = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSCA));\r
308                                                         DFLLRC32M.CALB = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSC));\r
309                                                         NVM.CMD        = 0;\r
310                                                 }\r
311 \r
312                                                 DFLLRC32M.CTRL  = DFLL_ENABLE_bm;\r
313                                                 break;\r
314                                         default:\r
315                                                 return false;\r
316                                 }\r
317 \r
318                                 return true;\r
319                         }\r
320 \r
321                         /** Stops the given DFLL of the XMEGA microcontroller.\r
322                          *\r
323                          *  \param[in] Source  RC Clock source for the DFLL to be stopped, a value from \ref XMEGA_System_ClockSource_t.\r
324                          *\r
325                          *  \return Boolean \c true if the DFLL was successfully stopped, \c false if invalid parameters specified.\r
326                          */\r
327                         static inline bool XMEGACLK_StopDFLL(const uint8_t Source) ATTR_ALWAYS_INLINE;\r
328                         static inline bool XMEGACLK_StopDFLL(const uint8_t Source)\r
329                         {\r
330                                 switch (Source)\r
331                                 {\r
332                                         case CLOCK_SRC_INT_RC2MHZ:\r
333                                                 DFLLRC2M.CTRL = 0;\r
334                                                 break;\r
335                                         case CLOCK_SRC_INT_RC32MHZ:\r
336                                                 DFLLRC32M.CTRL = 0;\r
337                                                 break;\r
338                                         default:\r
339                                                 return false;\r
340                                 }\r
341 \r
342                                 return true;\r
343                         }\r
344 \r
345                         /** Sets the clock source for the main microcontroller core. The given clock source should be configured\r
346                          *  and ready for use before this function is called.\r
347                          *\r
348                          *  \param[in] Source      Clock source for the CPU core, a value from \ref XMEGA_System_ClockSource_t.\r
349                          *\r
350                          *  \return Boolean \c true if the CPU core clock was successfully altered, \c false if invalid parameters specified.\r
351                          */\r
352                         static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source) ATTR_ALWAYS_INLINE;\r
353                         static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source)\r
354                         {\r
355                                 uint8_t ClockSourceMask = 0;\r
356 \r
357                                 switch (Source)\r
358                                 {\r
359                                         case CLOCK_SRC_INT_RC2MHZ:\r
360                                                 ClockSourceMask = CLK_SCLKSEL_RC2M_gc;\r
361                                                 break;\r
362                                         case CLOCK_SRC_INT_RC32MHZ:\r
363                                                 ClockSourceMask = CLK_SCLKSEL_RC32M_gc;\r
364                                                 break;\r
365                                         case CLOCK_SRC_INT_RC32KHZ:\r
366                                                 ClockSourceMask = CLK_SCLKSEL_RC32K_gc;\r
367                                                 break;\r
368                                         case CLOCK_SRC_XOSC:\r
369                                                 ClockSourceMask = CLK_SCLKSEL_XOSC_gc;\r
370                                                 break;\r
371                                         case CLOCK_SRC_PLL:\r
372                                                 ClockSourceMask = CLK_SCLKSEL_PLL_gc;\r
373                                                 break;\r
374                                         default:\r
375                                                 return false;\r
376                                 }\r
377 \r
378                                 uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();\r
379                                 GlobalInterruptDisable();\r
380 \r
381                                 XMEGACLK_CCP_Write(&CLK.CTRL, ClockSourceMask);\r
382 \r
383                                 SetGlobalInterruptMask(CurrentGlobalInt);\r
384 \r
385                                 Delay_MS(1);\r
386                                 return (CLK.CTRL == ClockSourceMask);\r
387                         }\r
388 \r
389         /* Disable C linkage for C++ Compilers: */\r
390                 #if defined(__cplusplus)\r
391                         }\r
392                 #endif\r
393 \r
394 #endif\r
395 \r
396 /** @} */\r
397 \r