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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2012.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2012 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 disclaim 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 AVR32 UC3 microcontrollers.
  28. *
  29. * Clock management driver for the AVR32 UC3 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_UC3
  33. * \defgroup Group_PlatformDrivers_UC3Clocks Clock Management Driver - LUFA/Platform/UC3/ClockManagement.h
  34. * \brief Module Clock Driver for the AVR32 UC3 microcontrollers.
  35. *
  36. * \section Sec_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_ModDescription Module Description
  41. * Clock management driver for the AVR32 UC3 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/UC3/ClockManagement.h>
  47. *
  48. * void main(void)
  49. * {
  50. * // Start the master external oscillator which will be used as the main clock reference
  51. * AVR32CLK_StartExternalOscillator(0, EXOSC_MODE_8MHZ_OR_MORE, EXOSC_START_0CLK);
  52. *
  53. * // Start the PLL for the CPU clock, switch CPU to it
  54. * AVR32CLK_StartPLL(0, CLOCK_SRC_OSC0, 12000000, F_CPU);
  55. * AVR32CLK_SetCPUClockSource(CLOCK_SRC_PLL0, F_CPU);
  56. *
  57. * // Start the PLL for the USB Generic Clock module
  58. * AVR32CLK_StartPLL(1, CLOCK_SRC_OSC0, 12000000, 48000000);
  59. * }
  60. * \endcode
  61. *
  62. * @{
  63. */
  64. #ifndef _UC3_CLOCK_MANAGEMENT_H_
  65. #define _UC3_CLOCK_MANAGEMENT_H_
  66. /* Includes: */
  67. #include "../../Common/Common.h"
  68. /* Enable C linkage for C++ Compilers: */
  69. #if defined(__cplusplus)
  70. extern "C" {
  71. #endif
  72. /* Public Interface - May be used in end-application: */
  73. /* Macros: */
  74. /** Enum for the possible external oscillator types. */
  75. enum UC3_Extern_OSC_ClockTypes_t
  76. {
  77. EXOSC_MODE_CLOCK = AVR32_PM_OSCCTRL0_MODE_EXT_CLOCK, /**< External clock (non-crystal) mode. */
  78. EXOSC_MODE_900KHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G0, /**< External crystal oscillator equal to or slower than 900KHz. */
  79. EXOSC_MODE_3MHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G1, /**< External crystal oscillator equal to or slower than 3MHz. */
  80. EXOSC_MODE_8MHZ_MAX = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G2, /**< External crystal oscillator equal to or slower than 8MHz. */
  81. EXOSC_MODE_8MHZ_OR_MORE = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G3, /**< External crystal oscillator equal to or faster than 8MHz. */
  82. };
  83. /** Enum for the possible external oscillator startup times. */
  84. enum UC3_Extern_OSC_ClockStartup_t
  85. {
  86. EXOSC_START_0CLK = AVR32_PM_OSCCTRL0_STARTUP_0_RCOSC, /**< Immediate startup, no delay. */
  87. EXOSC_START_64CLK = AVR32_PM_OSCCTRL0_STARTUP_64_RCOSC, /**< Wait 64 clock cycles before startup for stability. */
  88. EXOSC_START_128CLK = AVR32_PM_OSCCTRL0_STARTUP_128_RCOSC, /**< Wait 128 clock cycles before startup for stability. */
  89. EXOSC_START_2048CLK = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC, /**< Wait 2048 clock cycles before startup for stability. */
  90. EXOSC_START_4096CLK = AVR32_PM_OSCCTRL0_STARTUP_4096_RCOSC, /**< Wait 4096 clock cycles before startup for stability. */
  91. EXOSC_START_8192CLK = AVR32_PM_OSCCTRL0_STARTUP_8192_RCOSC, /**< Wait 8192 clock cycles before startup for stability. */
  92. EXOSC_START_16384CLK = AVR32_PM_OSCCTRL0_STARTUP_16384_RCOSC, /**< Wait 16384 clock cycles before startup for stability. */
  93. };
  94. /** Enum for the possible module clock sources. */
  95. enum UC3_System_ClockSource_t
  96. {
  97. CLOCK_SRC_SLOW_CLK = 0, /**< Clock sourced from the internal slow clock. */
  98. CLOCK_SRC_OSC0 = 1, /**< Clock sourced from the Oscillator 0 clock. */
  99. CLOCK_SRC_OSC1 = 2, /**< Clock sourced from the Oscillator 1 clock. */
  100. CLOCK_SRC_PLL0 = 3, /**< Clock sourced from the PLL 0 clock. */
  101. CLOCK_SRC_PLL1 = 4, /**< Clock sourced from the PLL 1 clock. */
  102. };
  103. /* Inline Functions: */
  104. /** Starts the given external oscillator of the UC3 microcontroller, with the given options. This routine blocks until
  105. * the oscillator is ready for use.
  106. *
  107. * \param[in] Channel Index of the external oscillator to start.
  108. * \param[in] Type Type of clock attached to the given oscillator channel, a value from \ref UC3_Extern_OSC_ClockTypes_t.
  109. * \param[in] Startup Statup time of the external oscillator, a value from \ref UC3_Extern_OSC_ClockStartup_t.
  110. *
  111. * \return Boolean \c true if the external oscillator was successfully started, \c false if invalid parameters specified.
  112. */
  113. static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,
  114. const uint8_t Type,
  115. const uint8_t Startup) ATTR_ALWAYS_INLINE;
  116. static inline bool AVR32CLK_StartExternalOscillator(const uint8_t Channel,
  117. const uint8_t Type,
  118. const uint8_t Startup)
  119. {
  120. switch (Channel)
  121. {
  122. case 0:
  123. AVR32_PM.OSCCTRL0.startup = Startup;
  124. AVR32_PM.OSCCTRL0.mode = Type;
  125. break;
  126. case 1:
  127. AVR32_PM.OSCCTRL1.startup = Startup;
  128. AVR32_PM.OSCCTRL1.mode = Type;
  129. break;
  130. default:
  131. return false;
  132. }
  133. AVR32_PM.mcctrl |= (1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));
  134. while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_OSC0RDY_OFFSET + Channel))));
  135. return true;
  136. }
  137. /** Stops the given external oscillator of the UC3 microcontroller.
  138. *
  139. * \param[in] Channel Index of the external oscillator to stop.
  140. */
  141. static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel) ATTR_ALWAYS_INLINE;
  142. static inline void AVR32CLK_StopExternalOscillator(const uint8_t Channel)
  143. {
  144. AVR32_PM.mcctrl &= ~(1 << (AVR32_PM_MCCTRL_OSC0EN_OFFSET + Channel));
  145. }
  146. /** Starts the given PLL of the UC3 microcontroller, with the given options. This routine blocks until the PLL is ready for use.
  147. *
  148. * \attention The output frequency must be equal to or greater than the source frequency.
  149. *
  150. * \param[in] Channel Index of the PLL to start.
  151. * \param[in] Source Clock source for the PLL, a value from \ref UC3_System_ClockSource_t.
  152. * \param[in] SourceFreq Frequency of the PLL's clock source, in Hz.
  153. * \param[in] Frequency Target frequency of the PLL's output.
  154. *
  155. * \return Boolean \c true if the PLL was successfully started, \c false if invalid parameters specified.
  156. */
  157. static inline bool AVR32CLK_StartPLL(const uint8_t Channel,
  158. const uint8_t Source,
  159. const uint32_t SourceFreq,
  160. const uint32_t Frequency) ATTR_ALWAYS_INLINE;
  161. static inline bool AVR32CLK_StartPLL(const uint8_t Channel,
  162. const uint8_t Source,
  163. const uint32_t SourceFreq,
  164. const uint32_t Frequency)
  165. {
  166. if (SourceFreq > Frequency)
  167. return false;
  168. switch (Source)
  169. {
  170. case CLOCK_SRC_OSC0:
  171. AVR32_PM.PLL[Channel].pllosc = 0;
  172. break;
  173. case CLOCK_SRC_OSC1:
  174. AVR32_PM.PLL[Channel].pllosc = 1;
  175. break;
  176. default:
  177. return false;
  178. }
  179. AVR32_PM.PLL[Channel].pllmul = (Frequency / SourceFreq) ? (((Frequency / SourceFreq) - 1) / 2) : 0;
  180. AVR32_PM.PLL[Channel].plldiv = 0;
  181. AVR32_PM.PLL[Channel].pllen = true;
  182. while (!(AVR32_PM.poscsr & (1 << (AVR32_PM_POSCSR_LOCK0_OFFSET + Channel))));
  183. return true;
  184. }
  185. /** Stops the given PLL of the UC3 microcontroller.
  186. *
  187. * \param[in] Channel Index of the PLL to stop.
  188. */
  189. static inline void AVR32CLK_StopPLL(const uint8_t Channel) ATTR_ALWAYS_INLINE;
  190. static inline void AVR32CLK_StopPLL(const uint8_t Channel)
  191. {
  192. AVR32_PM.PLL[Channel].pllen = false;
  193. }
  194. /** Starts the given Generic Clock of the UC3 microcontroller, with the given options.
  195. *
  196. * \param[in] Channel Index of the Generic Clock to start.
  197. * \param[in] Source Clock source for the Generic Clock, a value from \ref UC3_System_ClockSource_t.
  198. * \param[in] SourceFreq Frequency of the Generic Clock's clock source, in Hz.
  199. * \param[in] Frequency Target frequency of the Generic Clock's output.
  200. *
  201. * \return Boolean \c true if the Generic Clock was successfully started, \c false if invalid parameters specified.
  202. */
  203. static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,
  204. const uint8_t Source,
  205. const uint32_t SourceFreq,
  206. const uint32_t Frequency) ATTR_ALWAYS_INLINE;
  207. static inline bool AVR32CLK_StartGenericClock(const uint8_t Channel,
  208. const uint8_t Source,
  209. const uint32_t SourceFreq,
  210. const uint32_t Frequency)
  211. {
  212. if (Channel >= AVR32_PM_GCLK_NUM)
  213. return false;
  214. if (SourceFreq < Frequency)
  215. return false;
  216. switch (Source)
  217. {
  218. case CLOCK_SRC_OSC0:
  219. AVR32_PM.GCCTRL[Channel].pllsel = false;
  220. AVR32_PM.GCCTRL[Channel].oscsel = 0;
  221. break;
  222. case CLOCK_SRC_OSC1:
  223. AVR32_PM.GCCTRL[Channel].pllsel = false;
  224. AVR32_PM.GCCTRL[Channel].oscsel = 1;
  225. break;
  226. case CLOCK_SRC_PLL0:
  227. AVR32_PM.GCCTRL[Channel].pllsel = true;
  228. AVR32_PM.GCCTRL[Channel].oscsel = 0;
  229. break;
  230. case CLOCK_SRC_PLL1:
  231. AVR32_PM.GCCTRL[Channel].pllsel = true;
  232. AVR32_PM.GCCTRL[Channel].oscsel = 1;
  233. break;
  234. default:
  235. return false;
  236. }
  237. AVR32_PM.GCCTRL[Channel].diven = (SourceFreq > Frequency) ? true : false;
  238. AVR32_PM.GCCTRL[Channel].div = (((SourceFreq / Frequency) - 1) / 2);
  239. AVR32_PM.GCCTRL[Channel].cen = true;
  240. return true;
  241. }
  242. /** Stops the given generic clock of the UC3 microcontroller.
  243. *
  244. * \param[in] Channel Index of the generic clock to stop.
  245. *
  246. * \return Boolean \c true if the generic clock was sucessfully stopped, \c false if invalid parameters specified.
  247. */
  248. static inline bool AVR32CLK_StopGenericClock(const uint8_t Channel) ATTR_ALWAYS_INLINE;
  249. static inline bool AVR32CLK_StopGenericClock(const uint8_t Channel)
  250. {
  251. if (Channel >= AVR32_PM_GCLK_NUM)
  252. return false;
  253. AVR32_PM.GCCTRL[Channel].cen = false;
  254. return true;
  255. }
  256. /** Sets the clock source for the main microcontroller core. The given clock source should be configured
  257. * and ready for use before this function is called.
  258. *
  259. * This function will configure the FLASH controller's wait states automatically to suit the given clock source.
  260. *
  261. * \param[in] Source Clock source for the CPU core, a value from \ref UC3_System_ClockSource_t.
  262. * \param[in] SourceFreq Frequency of the CPU core's clock source, in Hz.
  263. *
  264. * \return Boolean \c true if the CPU core clock was sucessfully altered, \c false if invalid parameters specified.
  265. */
  266. static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,
  267. const uint32_t SourceFreq) ATTR_ALWAYS_INLINE;
  268. static inline bool AVR32CLK_SetCPUClockSource(const uint8_t Source,
  269. const uint32_t SourceFreq)
  270. {
  271. if (SourceFreq > AVR32_PM_CPU_MAX_FREQ)
  272. return false;
  273. AVR32_FLASHC.FCR.fws = (SourceFreq > AVR32_FLASHC_FWS_0_MAX_FREQ) ? true : false;
  274. switch (Source)
  275. {
  276. #if defined(AVR32_PM_MCCTRL_MCSEL_SLOW)
  277. case CLOCK_SRC_SLOW_CLK:
  278. AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_SLOW;
  279. break;
  280. #endif
  281. #if defined(AVR32_PM_MCCTRL_MCSEL_OSC0)
  282. case CLOCK_SRC_OSC0:
  283. AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_OSC0;
  284. break;
  285. #endif
  286. #if defined(AVR32_PM_MCCTRL_MCSEL_PLL0)
  287. case CLOCK_SRC_PLL0:
  288. AVR32_PM.MCCTRL.mcsel = AVR32_PM_MCCTRL_MCSEL_PLL0;
  289. break;
  290. #endif
  291. default:
  292. return false;
  293. }
  294. return true;
  295. }
  296. /* Disable C linkage for C++ Compilers: */
  297. #if defined(__cplusplus)
  298. }
  299. #endif
  300. #endif
  301. /** @} */