Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClockManagement.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers.
  28. *
  29. * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration
  30. * of the various clocks within the device to clock the various peripherals.
  31. */
  32. /** \ingroup Group_PlatformDrivers_XMEGA
  33. * \defgroup Group_PlatformDrivers_XMEGAClocks Clock Management Driver - LUFA/Platform/XMEGA/ClockManagement.h
  34. * \brief Module Clock Driver for the AVR USB XMEGA microcontrollers.
  35. *
  36. * \section Sec_PlatformDrivers_XMEGAClocks_Dependencies Module Source Dependencies
  37. * The following files must be built with any user project that uses this module:
  38. * - None
  39. *
  40. * \section Sec_PlatformDrivers_XMEGAClocks_ModDescription Module Description
  41. * Clock management driver for the AVR USB XMEGA microcontrollers. This driver allows for the configuration
  42. * of the various clocks within the device to clock the various peripherals.
  43. *
  44. * Usage Example:
  45. * \code
  46. * #include <LUFA/Platform/XMEGA/ClockManagement.h>
  47. *
  48. * void main(void)
  49. * {
  50. * // Start the PLL to multiply the 2MHz RC oscillator to F_CPU and switch the CPU core to run from it
  51. * XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
  52. * XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
  53. *
  54. * // Start the 32MHz internal RC oscillator and start the DFLL to increase it to F_USB using the USB SOF as a reference
  55. * XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
  56. * XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
  57. * }
  58. * \endcode
  59. *
  60. * @{
  61. */
  62. #ifndef _XMEGA_CLOCK_MANAGEMENT_H_
  63. #define _XMEGA_CLOCK_MANAGEMENT_H_
  64. /* Includes: */
  65. #include "../../Common/Common.h"
  66. /* Enable C linkage for C++ Compilers: */
  67. #if defined(__cplusplus)
  68. extern "C" {
  69. #endif
  70. /* Public Interface - May be used in end-application: */
  71. /* Macros: */
  72. /** Enum for the possible external oscillator frequency ranges. */
  73. enum XMEGA_Extern_OSC_ClockFrequency_t
  74. {
  75. EXOSC_FREQ_2MHZ_MAX = OSC_FRQRANGE_04TO2_gc, /**< External crystal oscillator equal to or slower than 2MHz. */
  76. EXOSC_FREQ_9MHZ_MAX = OSC_FRQRANGE_2TO9_gc, /**< External crystal oscillator equal to or slower than 9MHz. */
  77. EXOSC_FREQ_12MHZ_MAX = OSC_FRQRANGE_9TO12_gc, /**< External crystal oscillator equal to or slower than 12MHz. */
  78. EXOSC_FREQ_16MHZ_MAX = OSC_FRQRANGE_12TO16_gc, /**< External crystal oscillator equal to or slower than 16MHz. */
  79. };
  80. /** Enum for the possible external oscillator startup times. */
  81. enum XMEGA_Extern_OSC_ClockStartup_t
  82. {
  83. EXOSC_START_6CLK = OSC_XOSCSEL_EXTCLK_gc, /**< Wait 6 clock cycles before startup (external clock). */
  84. EXOSC_START_32KCLK = OSC_XOSCSEL_32KHz_gc, /**< Wait 32K clock cycles before startup (32.768KHz crystal). */
  85. EXOSC_START_256CLK = OSC_XOSCSEL_XTAL_256CLK_gc, /**< Wait 256 clock cycles before startup. */
  86. EXOSC_START_1KCLK = OSC_XOSCSEL_XTAL_1KCLK_gc, /**< Wait 1K clock cycles before startup. */
  87. EXOSC_START_16KCLK = OSC_XOSCSEL_XTAL_16KCLK_gc, /**< Wait 16K clock cycles before startup. */
  88. };
  89. /** Enum for the possible module clock sources. */
  90. enum XMEGA_System_ClockSource_t
  91. {
  92. CLOCK_SRC_INT_RC2MHZ = 0, /**< Clock sourced from the Internal 2MHz RC Oscillator clock. */
  93. CLOCK_SRC_INT_RC32MHZ = 1, /**< Clock sourced from the Internal 32MHz RC Oscillator clock. */
  94. CLOCK_SRC_INT_RC32KHZ = 2, /**< Clock sourced from the Internal 32KHz RC Oscillator clock. */
  95. CLOCK_SRC_XOSC = 3, /**< Clock sourced from the External Oscillator clock. */
  96. CLOCK_SRC_PLL = 4, /**< Clock sourced from the Internal PLL clock. */
  97. };
  98. /** Enum for the possible DFLL clock reference sources. */
  99. enum XMEGA_System_DFLLReference_t
  100. {
  101. DFLL_REF_INT_RC32KHZ = 0, /**< Reference clock sourced from the Internal 32KHz RC Oscillator clock. */
  102. DFLL_REF_EXT_RC32KHZ = 1, /**< Reference clock sourced from the External 32KHz RC Oscillator clock connected to TOSC pins. */
  103. DFLL_REF_INT_USBSOF = 2, /**< Reference clock sourced from the USB Start Of Frame packets. */
  104. };
  105. /* Inline Functions: */
  106. /** Write a value to a location protected by the XMEGA CCP protection mechanism. This function uses inline assembly to ensure that
  107. * the protected address is written to within four clock cycles of the CCP key being written.
  108. *
  109. * \param[in] Address Address to write to, a memory address protected by the CCP mechanism
  110. * \param[in] Value Value to write to the protected location
  111. */
  112. static inline void XMEGACLK_CCP_Write(volatile void* Address, const uint8_t Value) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
  113. static inline void XMEGACLK_CCP_Write(volatile void* Address, const uint8_t Value)
  114. {
  115. __asm__ __volatile__ (
  116. "out %0, __zero_reg__" "\n\t" /* Zero RAMPZ using fixed zero value register */
  117. "movw r30, %1" "\n\t" /* Copy address to Z register pair */
  118. "out %2, %3" "\n\t" /* Write key to CCP register */
  119. "st Z, %4" "\n\t" /* Indirectly write value to address */
  120. : /* No output operands */
  121. : /* Input operands: */ "m" (RAMPZ), "e" (Address), "m" (CCP), "r" (CCP_IOREG_gc), "r" (Value)
  122. : /* Clobbered registers: */ "r30", "r31"
  123. );
  124. }
  125. /** Starts the external oscillator of the XMEGA microcontroller, with the given options. This routine blocks until
  126. * the oscillator is ready for use.
  127. *
  128. * \param[in] FreqRange Frequency range of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockFrequency_t.
  129. * \param[in] Startup Startup time of the external oscillator, a value from \ref XMEGA_Extern_OSC_ClockStartup_t.
  130. *
  131. * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.
  132. */
  133. static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange,
  134. const uint8_t Startup) ATTR_ALWAYS_INLINE;
  135. static inline bool XMEGACLK_StartExternalOscillator(const uint8_t FreqRange,
  136. const uint8_t Startup)
  137. {
  138. OSC.XOSCCTRL = (FreqRange | ((Startup == EXOSC_START_32KCLK) ? OSC_X32KLPM_bm : 0) | Startup);
  139. OSC.CTRL |= OSC_XOSCEN_bm;
  140. while (!(OSC.STATUS & OSC_XOSCRDY_bm));
  141. return true;
  142. }
  143. /** Stops the external oscillator of the XMEGA microcontroller. */
  144. static inline void XMEGACLK_StopExternalOscillator(void) ATTR_ALWAYS_INLINE;
  145. static inline void XMEGACLK_StopExternalOscillator(void)
  146. {
  147. OSC.CTRL &= ~OSC_XOSCEN_bm;
  148. }
  149. /** Starts the given internal oscillator of the XMEGA microcontroller, with the given options. This routine blocks until
  150. * the oscillator is ready for use.
  151. *
  152. * \param[in] Source Internal oscillator to start, a value from \ref XMEGA_System_ClockSource_t.
  153. *
  154. * \return Boolean \c true if the internal oscillator was successfully started, \c false if invalid parameters specified.
  155. */
  156. static inline bool XMEGACLK_StartInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;
  157. static inline bool XMEGACLK_StartInternalOscillator(const uint8_t Source)
  158. {
  159. switch (Source)
  160. {
  161. case CLOCK_SRC_INT_RC2MHZ:
  162. OSC.CTRL |= OSC_RC2MEN_bm;
  163. while (!(OSC.STATUS & OSC_RC2MRDY_bm));
  164. return true;
  165. case CLOCK_SRC_INT_RC32MHZ:
  166. OSC.CTRL |= OSC_RC32MEN_bm;
  167. while (!(OSC.STATUS & OSC_RC32MRDY_bm));
  168. return true;
  169. case CLOCK_SRC_INT_RC32KHZ:
  170. OSC.CTRL |= OSC_RC32KEN_bm;
  171. while (!(OSC.STATUS & OSC_RC32KRDY_bm));
  172. return true;
  173. default:
  174. return false;
  175. }
  176. }
  177. /** Stops the given internal oscillator of the XMEGA microcontroller.
  178. *
  179. * \param[in] Source Internal oscillator to stop, a value from \ref XMEGA_System_ClockSource_t.
  180. *
  181. * \return Boolean \c true if the internal oscillator was successfully stopped, \c false if invalid parameters specified.
  182. */
  183. static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source) ATTR_ALWAYS_INLINE;
  184. static inline bool XMEGACLK_StopInternalOscillator(const uint8_t Source)
  185. {
  186. switch (Source)
  187. {
  188. case CLOCK_SRC_INT_RC2MHZ:
  189. OSC.CTRL &= ~OSC_RC2MEN_bm;
  190. return true;
  191. case CLOCK_SRC_INT_RC32MHZ:
  192. OSC.CTRL &= ~OSC_RC32MEN_bm;
  193. return true;
  194. case CLOCK_SRC_INT_RC32KHZ:
  195. OSC.CTRL &= ~OSC_RC32KEN_bm;
  196. return true;
  197. default:
  198. return false;
  199. }
  200. }
  201. /** Starts the PLL of the XMEGA microcontroller, with the given options. This routine blocks until the PLL is ready for use.
  202. *
  203. * \attention The output frequency must be equal to or greater than the source frequency.
  204. *
  205. * \param[in] Source Clock source for the PLL, a value from \ref XMEGA_System_ClockSource_t.
  206. * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz.
  207. * \param[in] Frequency Target frequency of the PLL's output.
  208. *
  209. * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.
  210. */
  211. static inline bool XMEGACLK_StartPLL(const uint8_t Source,
  212. const uint32_t SourceFreq,
  213. const uint32_t Frequency) ATTR_ALWAYS_INLINE;
  214. static inline bool XMEGACLK_StartPLL(const uint8_t Source,
  215. const uint32_t SourceFreq,
  216. const uint32_t Frequency)
  217. {
  218. uint8_t MulFactor = (Frequency / SourceFreq);
  219. if (SourceFreq > Frequency)
  220. return false;
  221. if (MulFactor > 31)
  222. return false;
  223. switch (Source)
  224. {
  225. case CLOCK_SRC_INT_RC2MHZ:
  226. OSC.PLLCTRL = (OSC_PLLSRC_RC2M_gc | MulFactor);
  227. break;
  228. case CLOCK_SRC_INT_RC32MHZ:
  229. OSC.PLLCTRL = (OSC_PLLSRC_RC32M_gc | MulFactor);
  230. break;
  231. case CLOCK_SRC_XOSC:
  232. OSC.PLLCTRL = (OSC_PLLSRC_XOSC_gc | MulFactor);
  233. break;
  234. default:
  235. return false;
  236. }
  237. OSC.CTRL |= OSC_PLLEN_bm;
  238. while (!(OSC.STATUS & OSC_PLLRDY_bm));
  239. return true;
  240. }
  241. /** Stops the PLL of the XMEGA microcontroller. */
  242. static inline void XMEGACLK_StopPLL(void) ATTR_ALWAYS_INLINE;
  243. static inline void XMEGACLK_StopPLL(void)
  244. {
  245. OSC.CTRL &= ~OSC_PLLEN_bm;
  246. }
  247. /** Starts the DFLL of the XMEGA microcontroller, with the given options.
  248. *
  249. * \param[in] Source RC Clock source for the DFLL, a value from \ref XMEGA_System_ClockSource_t.
  250. * \param[in] Reference Reference clock source for the DFLL, an value from \ref XMEGA_System_DFLLReference_t.
  251. * \param[in] Frequency Target frequency of the DFLL's output.
  252. *
  253. * \return Boolean \c true if the DFLL was successfully started, \c false if invalid parameters specified.
  254. */
  255. static inline bool XMEGACLK_StartDFLL(const uint8_t Source,
  256. const uint8_t Reference,
  257. const uint32_t Frequency) ATTR_ALWAYS_INLINE;
  258. static inline bool XMEGACLK_StartDFLL(const uint8_t Source,
  259. const uint8_t Reference,
  260. const uint32_t Frequency)
  261. {
  262. uint16_t DFLLCompare = (Frequency / 1024);
  263. switch (Source)
  264. {
  265. case CLOCK_SRC_INT_RC2MHZ:
  266. OSC.DFLLCTRL |= (Reference << OSC_RC2MCREF_bp);
  267. DFLLRC2M.COMP1 = (DFLLCompare & 0xFF);
  268. DFLLRC2M.COMP2 = (DFLLCompare >> 8);
  269. DFLLRC2M.CTRL = DFLL_ENABLE_bm;
  270. break;
  271. case CLOCK_SRC_INT_RC32MHZ:
  272. OSC.DFLLCTRL |= (Reference << OSC_RC32MCREF_gp);
  273. DFLLRC32M.COMP1 = (DFLLCompare & 0xFF);
  274. DFLLRC32M.COMP2 = (DFLLCompare >> 8);
  275. if (Reference == DFLL_REF_INT_USBSOF)
  276. {
  277. NVM.CMD = NVM_CMD_READ_CALIB_ROW_gc;
  278. DFLLRC32M.CALA = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSCA));
  279. DFLLRC32M.CALB = pgm_read_byte(offsetof(NVM_PROD_SIGNATURES_t, USBRCOSC));
  280. NVM.CMD = 0;
  281. }
  282. DFLLRC32M.CTRL = DFLL_ENABLE_bm;
  283. break;
  284. default:
  285. return false;
  286. }
  287. return true;
  288. }
  289. /** Stops the given DFLL of the XMEGA microcontroller.
  290. *
  291. * \param[in] Source RC Clock source for the DFLL to be stopped, a value from \ref XMEGA_System_ClockSource_t.
  292. *
  293. * \return Boolean \c true if the DFLL was successfully stopped, \c false if invalid parameters specified.
  294. */
  295. static inline bool XMEGACLK_StopDFLL(const uint8_t Source) ATTR_ALWAYS_INLINE;
  296. static inline bool XMEGACLK_StopDFLL(const uint8_t Source)
  297. {
  298. switch (Source)
  299. {
  300. case CLOCK_SRC_INT_RC2MHZ:
  301. DFLLRC2M.CTRL = 0;
  302. break;
  303. case CLOCK_SRC_INT_RC32MHZ:
  304. DFLLRC32M.CTRL = 0;
  305. break;
  306. default:
  307. return false;
  308. }
  309. return true;
  310. }
  311. /** Sets the clock source for the main microcontroller core. The given clock source should be configured
  312. * and ready for use before this function is called.
  313. *
  314. * \param[in] Source Clock source for the CPU core, a value from \ref XMEGA_System_ClockSource_t.
  315. *
  316. * \return Boolean \c true if the CPU core clock was successfully altered, \c false if invalid parameters specified.
  317. */
  318. static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source) ATTR_ALWAYS_INLINE;
  319. static inline bool XMEGACLK_SetCPUClockSource(const uint8_t Source)
  320. {
  321. uint8_t ClockSourceMask = 0;
  322. switch (Source)
  323. {
  324. case CLOCK_SRC_INT_RC2MHZ:
  325. ClockSourceMask = CLK_SCLKSEL_RC2M_gc;
  326. break;
  327. case CLOCK_SRC_INT_RC32MHZ:
  328. ClockSourceMask = CLK_SCLKSEL_RC32M_gc;
  329. break;
  330. case CLOCK_SRC_INT_RC32KHZ:
  331. ClockSourceMask = CLK_SCLKSEL_RC32K_gc;
  332. break;
  333. case CLOCK_SRC_XOSC:
  334. ClockSourceMask = CLK_SCLKSEL_XOSC_gc;
  335. break;
  336. case CLOCK_SRC_PLL:
  337. ClockSourceMask = CLK_SCLKSEL_PLL_gc;
  338. break;
  339. default:
  340. return false;
  341. }
  342. uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
  343. GlobalInterruptDisable();
  344. XMEGACLK_CCP_Write(&CLK.CTRL, ClockSourceMask);
  345. SetGlobalInterruptMask(CurrentGlobalInt);
  346. Delay_MS(1);
  347. return (CLK.CTRL == ClockSourceMask);
  348. }
  349. /* Disable C linkage for C++ Compilers: */
  350. #if defined(__cplusplus)
  351. }
  352. #endif
  353. #endif
  354. /** @} */