em_cmu.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Clock management unit (CMU) API
  4. * @author Energy Micro AS
  5. * @version 3.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  22. * obligation to support this Software. Energy Micro AS is providing the
  23. * Software "AS IS", with no express or implied warranties of any kind,
  24. * including, but not limited to, any implied warranties of merchantability
  25. * or fitness for any particular purpose or warranties against infringement
  26. * of any proprietary rights of a third party.
  27. *
  28. * Energy Micro AS will not be liable for any consequential, incidental, or
  29. * special damages, or any other relief, or for any claim by any third party,
  30. * arising from your use of this Software.
  31. *
  32. ******************************************************************************/
  33. #ifndef __EM_CMU_H
  34. #define __EM_CMU_H
  35. #include <stdbool.h>
  36. #include "em_part.h"
  37. #include "em_bitband.h"
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /***************************************************************************//**
  42. * @addtogroup EM_Library
  43. * @{
  44. ******************************************************************************/
  45. /***************************************************************************//**
  46. * @addtogroup CMU
  47. * @{
  48. ******************************************************************************/
  49. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  50. /* Select register ids, for internal use */
  51. #define CMU_NOSEL_REG 0
  52. #define CMU_HFCLKSEL_REG 1
  53. #define CMU_LFACLKSEL_REG 2
  54. #define CMU_LFBCLKSEL_REG 3
  55. #define CMU_DBGCLKSEL_REG 4
  56. #if defined (_EFM32_GIANT_FAMILY)
  57. #define CMU_USBCCLKSEL_REG 5
  58. #endif
  59. #define CMU_SEL_REG_POS 0
  60. #define CMU_SEL_REG_MASK 0xf
  61. /* Divisor register ids, for internal use */
  62. #define CMU_NODIV_REG 0
  63. #define CMU_HFPERCLKDIV_REG 1
  64. #define CMU_HFCORECLKDIV_REG 2
  65. #define CMU_LFAPRESC0_REG 3
  66. #define CMU_LFBPRESC0_REG 4
  67. #if defined (_EFM32_GIANT_FAMILY)
  68. #define CMU_HFCLKDIV_REG 5
  69. #endif
  70. #define CMU_DIV_REG_POS 4
  71. #define CMU_DIV_REG_MASK 0xf
  72. /* Enable register ids, for internal use */
  73. #define CMU_NO_EN_REG 0
  74. #define CMU_HFPERCLKDIV_EN_REG 1
  75. #define CMU_HFPERCLKEN0_EN_REG 2
  76. #define CMU_HFCORECLKEN0_EN_REG 3
  77. #define CMU_LFACLKEN0_EN_REG 4
  78. #define CMU_LFBCLKEN0_EN_REG 5
  79. #define CMU_PCNT_EN_REG 6
  80. #define CMU_EN_REG_POS 8
  81. #define CMU_EN_REG_MASK 0xf
  82. /* Enable register bit position, for internal use */
  83. #define CMU_EN_BIT_POS 12
  84. #define CMU_EN_BIT_MASK 0x1f
  85. /* Clock branch bitfield position, for internal use */
  86. #define CMU_HF_CLK_BRANCH 0
  87. #define CMU_HFPER_CLK_BRANCH 1
  88. #define CMU_HFCORE_CLK_BRANCH 2
  89. #define CMU_LFA_CLK_BRANCH 3
  90. #define CMU_RTC_CLK_BRANCH 4
  91. #define CMU_LETIMER_CLK_BRANCH 5
  92. #define CMU_LCDPRE_CLK_BRANCH 6
  93. #define CMU_LCD_CLK_BRANCH 7
  94. #define CMU_LESENSE_CLK_BRANCH 8
  95. #define CMU_LFB_CLK_BRANCH 9
  96. #define CMU_LEUART0_CLK_BRANCH 10
  97. #define CMU_LEUART1_CLK_BRANCH 11
  98. #define CMU_DBG_CLK_BRANCH 12
  99. #define CMU_AUX_CLK_BRANCH 13
  100. #define CMU_USBC_CLK_BRANCH 14
  101. #define CMU_CLK_BRANCH_POS 17
  102. #define CMU_CLK_BRANCH_MASK 0x1f
  103. /** @endcond */
  104. /*******************************************************************************
  105. ******************************** ENUMS ************************************
  106. ******************************************************************************/
  107. /** Clock divisors. These values are valid for prescalers. */
  108. #define cmuClkDiv_1 1 /**< Divide clock by 1. */
  109. #define cmuClkDiv_2 2 /**< Divide clock by 2. */
  110. #define cmuClkDiv_4 4 /**< Divide clock by 4. */
  111. #define cmuClkDiv_8 8 /**< Divide clock by 8. */
  112. #define cmuClkDiv_16 16 /**< Divide clock by 16. */
  113. #define cmuClkDiv_32 32 /**< Divide clock by 32. */
  114. #define cmuClkDiv_64 64 /**< Divide clock by 64. */
  115. #define cmuClkDiv_128 128 /**< Divide clock by 128. */
  116. #define cmuClkDiv_256 256 /**< Divide clock by 256. */
  117. #define cmuClkDiv_512 512 /**< Divide clock by 512. */
  118. #define cmuClkDiv_1024 1024 /**< Divide clock by 1024. */
  119. #define cmuClkDiv_2048 2048 /**< Divide clock by 2048. */
  120. #define cmuClkDiv_4096 4096 /**< Divide clock by 4096. */
  121. #define cmuClkDiv_8192 8192 /**< Divide clock by 8192. */
  122. #define cmuClkDiv_16384 16384 /**< Divide clock by 16384. */
  123. #define cmuClkDiv_32768 32768 /**< Divide clock by 32768. */
  124. /** Clock divider configuration */
  125. typedef uint32_t CMU_ClkDiv_TypeDef;
  126. /** High frequency RC bands. */
  127. typedef enum
  128. {
  129. /** 1MHz RC band. */
  130. cmuHFRCOBand_1MHz = _CMU_HFRCOCTRL_BAND_1MHZ,
  131. /** 7MHz RC band. */
  132. cmuHFRCOBand_7MHz = _CMU_HFRCOCTRL_BAND_7MHZ,
  133. /** 11MHz RC band. */
  134. cmuHFRCOBand_11MHz = _CMU_HFRCOCTRL_BAND_11MHZ,
  135. /** 14MHz RC band. */
  136. cmuHFRCOBand_14MHz = _CMU_HFRCOCTRL_BAND_14MHZ,
  137. /** 21MHz RC band. */
  138. cmuHFRCOBand_21MHz = _CMU_HFRCOCTRL_BAND_21MHZ,
  139. /** 28MHz RC band. */
  140. cmuHFRCOBand_28MHz = _CMU_HFRCOCTRL_BAND_28MHZ
  141. } CMU_HFRCOBand_TypeDef;
  142. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  143. /** AUX High frequency RC bands. */
  144. typedef enum
  145. {
  146. /** 1MHz RC band. */
  147. cmuAUXHFRCOBand_1MHz = _CMU_AUXHFRCOCTRL_BAND_1MHZ,
  148. /** 7MHz RC band. */
  149. cmuAUXHFRCOBand_7MHz = _CMU_AUXHFRCOCTRL_BAND_7MHZ,
  150. /** 11MHz RC band. */
  151. cmuAUXHFRCOBand_11MHz = _CMU_AUXHFRCOCTRL_BAND_11MHZ,
  152. /** 14MHz RC band. */
  153. cmuAUXHFRCOBand_14MHz = _CMU_AUXHFRCOCTRL_BAND_14MHZ,
  154. /** 21MHz RC band. */
  155. cmuAUXHFRCOBand_21MHz = _CMU_AUXHFRCOCTRL_BAND_21MHZ,
  156. /** 28MHz RC band. */
  157. cmuAUXHFRCOBand_28MHz = _CMU_AUXHFRCOCTRL_BAND_28MHZ
  158. } CMU_AUXHFRCOBand_TypeDef;
  159. #endif
  160. /** Clock points in CMU. Please refer to CMU overview in reference manual. */
  161. typedef enum
  162. {
  163. /*******************/
  164. /* HF clock branch */
  165. /*******************/
  166. /** High frequency clock */
  167. #if defined(_EFM32_GIANT_FAMILY)
  168. cmuClock_HF = (CMU_HFCLKDIV_REG << CMU_DIV_REG_POS) |
  169. (CMU_HFCLKSEL_REG << CMU_SEL_REG_POS) |
  170. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  171. (0 << CMU_EN_BIT_POS) |
  172. (CMU_HF_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  173. #else
  174. cmuClock_HF = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  175. (CMU_HFCLKSEL_REG << CMU_SEL_REG_POS) |
  176. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  177. (0 << CMU_EN_BIT_POS) |
  178. (CMU_HF_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  179. #endif
  180. /** Debug clock */
  181. cmuClock_DBG = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  182. (CMU_DBGCLKSEL_REG << CMU_SEL_REG_POS) |
  183. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  184. (0 << CMU_EN_BIT_POS) |
  185. (CMU_DBG_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  186. /** AUX clock */
  187. cmuClock_AUX = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  188. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  189. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  190. (0 << CMU_EN_BIT_POS) |
  191. (CMU_AUX_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  192. /**********************************/
  193. /* HF peripheral clock sub-branch */
  194. /**********************************/
  195. /** High frequency peripheral clock */
  196. cmuClock_HFPER = (CMU_HFPERCLKDIV_REG << CMU_DIV_REG_POS) |
  197. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  198. (CMU_HFPERCLKDIV_EN_REG << CMU_EN_REG_POS) |
  199. (_CMU_HFPERCLKDIV_HFPERCLKEN_SHIFT << CMU_EN_BIT_POS) |
  200. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  201. /** Universal sync/async receiver/transmitter 0 clock. */
  202. #if defined(_CMU_HFPERCLKEN0_USART0_MASK)
  203. cmuClock_USART0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  204. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  205. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  206. (_CMU_HFPERCLKEN0_USART0_SHIFT << CMU_EN_BIT_POS) |
  207. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  208. #endif
  209. /** Universal sync/async receiver/transmitter 1 clock. */
  210. #if defined(_CMU_HFPERCLKEN0_USART1_MASK)
  211. cmuClock_USART1 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  212. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  213. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  214. (_CMU_HFPERCLKEN0_USART1_SHIFT << CMU_EN_BIT_POS) |
  215. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  216. #endif
  217. /** Universal sync/async receiver/transmitter 2 clock. */
  218. #if defined(_CMU_HFPERCLKEN0_USART2_MASK)
  219. cmuClock_USART2 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  220. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  221. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  222. (_CMU_HFPERCLKEN0_USART2_SHIFT << CMU_EN_BIT_POS) |
  223. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  224. #endif
  225. /** Universal async receiver/transmitter 0 clock. */
  226. #if defined(_CMU_HFPERCLKEN0_UART0_MASK)
  227. cmuClock_UART0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  228. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  229. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  230. (_CMU_HFPERCLKEN0_UART0_SHIFT << CMU_EN_BIT_POS) |
  231. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  232. #endif
  233. /** Universal async receiver/transmitter 1 clock. */
  234. #if defined(_CMU_HFPERCLKEN0_UART1_MASK)
  235. cmuClock_UART1 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  236. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  237. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  238. (_CMU_HFPERCLKEN0_UART1_SHIFT << CMU_EN_BIT_POS) |
  239. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  240. #endif
  241. /** Timer 0 clock. */
  242. #if defined(_CMU_HFPERCLKEN0_TIMER0_MASK)
  243. cmuClock_TIMER0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  244. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  245. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  246. (_CMU_HFPERCLKEN0_TIMER0_SHIFT << CMU_EN_BIT_POS) |
  247. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  248. #endif
  249. /** Timer 1 clock. */
  250. #if defined(_CMU_HFPERCLKEN0_TIMER1_MASK)
  251. cmuClock_TIMER1 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  252. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  253. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  254. (_CMU_HFPERCLKEN0_TIMER1_SHIFT << CMU_EN_BIT_POS) |
  255. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  256. #endif
  257. /** Timer 2 clock. */
  258. #if defined(_CMU_HFPERCLKEN0_TIMER2_MASK)
  259. cmuClock_TIMER2 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  260. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  261. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  262. (_CMU_HFPERCLKEN0_TIMER2_SHIFT << CMU_EN_BIT_POS) |
  263. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  264. #endif
  265. /** Timer 3 clock. */
  266. #if defined(_CMU_HFPERCLKEN0_TIMER3_MASK)
  267. cmuClock_TIMER3 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  268. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  269. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  270. (_CMU_HFPERCLKEN0_TIMER3_SHIFT << CMU_EN_BIT_POS) |
  271. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  272. #endif
  273. /** Analog comparator 0 clock. */
  274. #if defined(_CMU_HFPERCLKEN0_ACMP0_MASK)
  275. cmuClock_ACMP0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  276. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  277. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  278. (_CMU_HFPERCLKEN0_ACMP0_SHIFT << CMU_EN_BIT_POS) |
  279. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  280. #endif
  281. /** Analog comparator 1 clock. */
  282. #if defined(_CMU_HFPERCLKEN0_ACMP1_MASK)
  283. cmuClock_ACMP1 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  284. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  285. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  286. (_CMU_HFPERCLKEN0_ACMP1_SHIFT << CMU_EN_BIT_POS) |
  287. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  288. #endif
  289. /** Peripheral reflex system clock. */
  290. #if defined(_CMU_HFPERCLKEN0_PRS_MASK)
  291. cmuClock_PRS = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  292. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  293. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  294. (_CMU_HFPERCLKEN0_PRS_SHIFT << CMU_EN_BIT_POS) |
  295. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  296. #endif
  297. /** Digital to analog converter 0 clock. */
  298. #if defined(_CMU_HFPERCLKEN0_DAC0_MASK)
  299. cmuClock_DAC0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  300. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  301. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  302. (_CMU_HFPERCLKEN0_DAC0_SHIFT << CMU_EN_BIT_POS) |
  303. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  304. #endif
  305. /** General purpose input/output clock. */
  306. #if defined(GPIO_PRESENT)
  307. cmuClock_GPIO = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  308. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  309. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  310. (_CMU_HFPERCLKEN0_GPIO_SHIFT << CMU_EN_BIT_POS) |
  311. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  312. #endif
  313. /** Voltage comparator clock. */
  314. #if defined(VCMP_PRESENT)
  315. cmuClock_VCMP = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  316. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  317. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  318. (_CMU_HFPERCLKEN0_VCMP_SHIFT << CMU_EN_BIT_POS) |
  319. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  320. #endif
  321. /** Analog to digital converter 0 clock. */
  322. #if defined(_CMU_HFPERCLKEN0_ADC0_MASK)
  323. cmuClock_ADC0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  324. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  325. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  326. (_CMU_HFPERCLKEN0_ADC0_SHIFT << CMU_EN_BIT_POS) |
  327. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  328. #endif
  329. /** I2C 0 clock. */
  330. #if defined(_CMU_HFPERCLKEN0_I2C0_MASK)
  331. cmuClock_I2C0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  332. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  333. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  334. (_CMU_HFPERCLKEN0_I2C0_SHIFT << CMU_EN_BIT_POS) |
  335. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  336. #endif
  337. /** I2C 1 clock. */
  338. #if defined(_CMU_HFPERCLKEN0_I2C1_MASK)
  339. cmuClock_I2C1 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  340. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  341. (CMU_HFPERCLKEN0_EN_REG << CMU_EN_REG_POS) |
  342. (_CMU_HFPERCLKEN0_I2C1_SHIFT << CMU_EN_BIT_POS) |
  343. (CMU_HFPER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  344. #endif
  345. /**********************/
  346. /* HF core sub-branch */
  347. /**********************/
  348. /** Core clock */
  349. cmuClock_CORE = (CMU_HFCORECLKDIV_REG << CMU_DIV_REG_POS) |
  350. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  351. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  352. (0 << CMU_EN_BIT_POS) |
  353. (CMU_HFCORE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  354. /** Advanced encryption standard accelerator clock. */
  355. #if defined(AES_PRESENT)
  356. cmuClock_AES = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  357. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  358. (CMU_HFCORECLKEN0_EN_REG << CMU_EN_REG_POS) |
  359. (_CMU_HFCORECLKEN0_AES_SHIFT << CMU_EN_BIT_POS) |
  360. (CMU_HFCORE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  361. #endif
  362. /** Direct memory access controller clock. */
  363. #if defined(DMA_PRESENT)
  364. cmuClock_DMA = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  365. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  366. (CMU_HFCORECLKEN0_EN_REG << CMU_EN_REG_POS) |
  367. (_CMU_HFCORECLKEN0_DMA_SHIFT << CMU_EN_BIT_POS) |
  368. (CMU_HFCORE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  369. #endif
  370. /** Low energy clocking module clock. */
  371. cmuClock_CORELE = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  372. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  373. (CMU_HFCORECLKEN0_EN_REG << CMU_EN_REG_POS) |
  374. (_CMU_HFCORECLKEN0_LE_SHIFT << CMU_EN_BIT_POS) |
  375. (CMU_HFCORE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  376. /** External bus interface clock. */
  377. #if defined(EBI_PRESENT)
  378. cmuClock_EBI = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  379. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  380. (CMU_HFCORECLKEN0_EN_REG << CMU_EN_REG_POS) |
  381. (_CMU_HFCORECLKEN0_EBI_SHIFT << CMU_EN_BIT_POS) |
  382. (CMU_HFCORE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  383. #endif
  384. #if defined(USB_PRESENT)
  385. cmuClock_USBC = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  386. (CMU_USBCCLKSEL_REG << CMU_SEL_REG_POS) |
  387. (CMU_HFCORECLKEN0_EN_REG << CMU_EN_REG_POS) |
  388. (_CMU_HFCORECLKEN0_USBC_SHIFT << CMU_EN_BIT_POS) |
  389. (CMU_USBC_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  390. #endif
  391. #if defined(USB_PRESENT)
  392. cmuClock_USB = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  393. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  394. (CMU_HFCORECLKEN0_EN_REG << CMU_EN_REG_POS) |
  395. (_CMU_HFCORECLKEN0_USB_SHIFT << CMU_EN_BIT_POS) |
  396. (CMU_HFCORE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  397. #endif
  398. /***************/
  399. /* LF A branch */
  400. /***************/
  401. /** Low frequency A clock */
  402. cmuClock_LFA = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  403. (CMU_LFACLKSEL_REG << CMU_SEL_REG_POS) |
  404. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  405. (0 << CMU_EN_BIT_POS) |
  406. (CMU_LFA_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  407. /** Real time counter clock. */
  408. #if defined(RTC_PRESENT)
  409. cmuClock_RTC = (CMU_LFAPRESC0_REG << CMU_DIV_REG_POS) |
  410. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  411. (CMU_LFACLKEN0_EN_REG << CMU_EN_REG_POS) |
  412. (_CMU_LFACLKEN0_RTC_SHIFT << CMU_EN_BIT_POS) |
  413. (CMU_RTC_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  414. #endif
  415. /** Low energy timer 0 clock. */
  416. #if defined(_CMU_LFACLKEN0_LETIMER0_MASK)
  417. cmuClock_LETIMER0 = (CMU_LFAPRESC0_REG << CMU_DIV_REG_POS) |
  418. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  419. (CMU_LFACLKEN0_EN_REG << CMU_EN_REG_POS) |
  420. (_CMU_LFACLKEN0_LETIMER0_SHIFT << CMU_EN_BIT_POS) |
  421. (CMU_LETIMER_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  422. #endif
  423. /** Liquid crystal display, pre FDIV clock. */
  424. #if defined(_CMU_LFACLKEN0_LCD_MASK)
  425. cmuClock_LCDpre = (CMU_LFAPRESC0_REG << CMU_DIV_REG_POS) |
  426. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  427. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  428. (0 << CMU_EN_BIT_POS) |
  429. (CMU_LCDPRE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  430. /** Liquid crystal display clock. Please notice that FDIV prescaler
  431. * must be set by special API. */
  432. cmuClock_LCD = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  433. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  434. (CMU_LFACLKEN0_EN_REG << CMU_EN_REG_POS) |
  435. (_CMU_LFACLKEN0_LCD_SHIFT << CMU_EN_BIT_POS) |
  436. (CMU_LCD_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  437. #endif
  438. /** Pulse counter 0 clock. */
  439. #if defined(_CMU_PCNTCTRL_PCNT0CLKEN_MASK)
  440. cmuClock_PCNT0 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  441. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  442. (CMU_PCNT_EN_REG << CMU_EN_REG_POS) |
  443. (_CMU_PCNTCTRL_PCNT0CLKEN_SHIFT << CMU_EN_BIT_POS) |
  444. (CMU_LFA_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  445. #endif
  446. /** Pulse counter 1 clock. */
  447. #if defined(_CMU_PCNTCTRL_PCNT1CLKEN_MASK)
  448. cmuClock_PCNT1 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  449. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  450. (CMU_PCNT_EN_REG << CMU_EN_REG_POS) |
  451. (_CMU_PCNTCTRL_PCNT1CLKEN_SHIFT << CMU_EN_BIT_POS) |
  452. (CMU_LFA_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  453. #endif
  454. /** Pulse counter 2 clock. */
  455. #if defined(_CMU_PCNTCTRL_PCNT2CLKEN_MASK)
  456. cmuClock_PCNT2 = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  457. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  458. (CMU_PCNT_EN_REG << CMU_EN_REG_POS) |
  459. (_CMU_PCNTCTRL_PCNT2CLKEN_SHIFT << CMU_EN_BIT_POS) |
  460. (CMU_LFA_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  461. #endif
  462. /** LESENSE clock. */
  463. #if defined(_CMU_LFACLKEN0_LESENSE_MASK)
  464. cmuClock_LESENSE = (CMU_LFAPRESC0_REG << CMU_DIV_REG_POS) |
  465. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  466. (CMU_LFACLKEN0_EN_REG << CMU_EN_REG_POS) |
  467. (_CMU_LFACLKEN0_LESENSE_SHIFT << CMU_EN_BIT_POS) |
  468. (CMU_LESENSE_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  469. #endif
  470. /***************/
  471. /* LF B branch */
  472. /***************/
  473. /** Low frequency B clock */
  474. cmuClock_LFB = (CMU_NODIV_REG << CMU_DIV_REG_POS) |
  475. (CMU_LFBCLKSEL_REG << CMU_SEL_REG_POS) |
  476. (CMU_NO_EN_REG << CMU_EN_REG_POS) |
  477. (0 << CMU_EN_BIT_POS) |
  478. (CMU_LFB_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  479. /** Low energy universal asynchronous receiver/transmitter 0 clock. */
  480. #if defined(_CMU_LFBCLKEN0_LEUART0_MASK)
  481. cmuClock_LEUART0 = (CMU_LFBPRESC0_REG << CMU_DIV_REG_POS) |
  482. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  483. (CMU_LFBCLKEN0_EN_REG << CMU_EN_REG_POS) |
  484. (_CMU_LFBCLKEN0_LEUART0_SHIFT << CMU_EN_BIT_POS) |
  485. (CMU_LEUART0_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  486. #endif
  487. /** Low energy universal asynchronous receiver/transmitter 1 clock. */
  488. #if defined(_CMU_LFBCLKEN0_LEUART1_MASK)
  489. cmuClock_LEUART1 = (CMU_LFBPRESC0_REG << CMU_DIV_REG_POS) |
  490. (CMU_NOSEL_REG << CMU_SEL_REG_POS) |
  491. (CMU_LFBCLKEN0_EN_REG << CMU_EN_REG_POS) |
  492. (_CMU_LFBCLKEN0_LEUART1_SHIFT << CMU_EN_BIT_POS) |
  493. (CMU_LEUART1_CLK_BRANCH << CMU_CLK_BRANCH_POS),
  494. #endif
  495. } CMU_Clock_TypeDef;
  496. /** Oscillator types. */
  497. typedef enum
  498. {
  499. cmuOsc_LFXO, /**< Low frequency crystal oscillator. */
  500. cmuOsc_LFRCO, /**< Low frequency RC oscillator. */
  501. cmuOsc_HFXO, /**< High frequency crystal oscillator. */
  502. cmuOsc_HFRCO, /**< High frequency RC oscillator. */
  503. cmuOsc_AUXHFRCO, /**< Auxiliary high frequency RC oscillator. */
  504. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  505. cmuOsc_ULFRCO /**< Ultra low frequency RC oscillator. */
  506. #endif
  507. } CMU_Osc_TypeDef;
  508. /** Selectable clock sources. */
  509. typedef enum
  510. {
  511. cmuSelect_Error, /**< Usage error. */
  512. cmuSelect_Disabled, /**< Clock selector disabled. */
  513. cmuSelect_LFXO, /**< Low frequency crystal oscillator. */
  514. cmuSelect_LFRCO, /**< Low frequency RC oscillator. */
  515. cmuSelect_HFXO, /**< High frequency crystal oscillator. */
  516. cmuSelect_HFRCO, /**< High frequency RC oscillator. */
  517. cmuSelect_CORELEDIV2, /**< Core low energy clock divided by 2. */
  518. cmuSelect_AUXHFRCO, /**< Auxilliary clock source can be used for debug clock */
  519. cmuSelect_HFCLK, /**< Divided HFCLK on Giant for debug clock, undivided on Tiny Gecko and for USBC (not used on Gecko) */
  520. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  521. cmuSelect_ULFRCO, /**< Ultra low frequency RC oscillator. */
  522. #endif
  523. } CMU_Select_TypeDef;
  524. /*******************************************************************************
  525. ***************************** PROTOTYPES **********************************
  526. ******************************************************************************/
  527. void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable);
  528. uint32_t CMU_ClockFreqGet(CMU_Clock_TypeDef clock);
  529. CMU_ClkDiv_TypeDef CMU_ClockDivGet(CMU_Clock_TypeDef clock);
  530. CMU_Select_TypeDef CMU_ClockSelectGet(CMU_Clock_TypeDef clock);
  531. void CMU_ClockDivSet(CMU_Clock_TypeDef clock, CMU_ClkDiv_TypeDef div);
  532. void CMU_ClockSelectSet(CMU_Clock_TypeDef clock, CMU_Select_TypeDef ref);
  533. CMU_HFRCOBand_TypeDef CMU_HFRCOBandGet(void);
  534. void CMU_HFRCOBandSet(CMU_HFRCOBand_TypeDef band);
  535. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  536. CMU_AUXHFRCOBand_TypeDef CMU_AUXHFRCOBandGet(void);
  537. void CMU_AUXHFRCOBandSet(CMU_AUXHFRCOBand_TypeDef band);
  538. #endif
  539. void CMU_HFRCOStartupDelaySet(uint32_t delay);
  540. uint32_t CMU_HFRCOStartupDelayGet(void);
  541. void CMU_OscillatorEnable(CMU_Osc_TypeDef osc, bool enable, bool wait);
  542. uint32_t CMU_OscillatorTuningGet(CMU_Osc_TypeDef osc);
  543. void CMU_OscillatorTuningSet(CMU_Osc_TypeDef osc, uint32_t val);
  544. bool CMU_PCNTClockExternalGet(unsigned int inst);
  545. void CMU_PCNTClockExternalSet(unsigned int inst, bool external);
  546. uint32_t CMU_LCDClkFDIVGet(void);
  547. void CMU_LCDClkFDIVSet(uint32_t div);
  548. void CMU_FreezeEnable(bool enable);
  549. uint32_t CMU_Calibrate(uint32_t HFCycles, CMU_Osc_TypeDef reference);
  550. void CMU_CalibrateConfig(uint32_t downCycles, CMU_Osc_TypeDef downSel,
  551. CMU_Osc_TypeDef upSel);
  552. /***************************************************************************//**
  553. * @brief
  554. * Clear one or more pending CMU interrupts.
  555. *
  556. * @param[in] flags
  557. * CMU interrupt sources to clear.
  558. ******************************************************************************/
  559. __STATIC_INLINE void CMU_IntClear(uint32_t flags)
  560. {
  561. CMU->IFC = flags;
  562. }
  563. /***************************************************************************//**
  564. * @brief
  565. * Disable one or more CMU interrupts.
  566. *
  567. * @param[in] flags
  568. * CMU interrupt sources to disable.
  569. ******************************************************************************/
  570. __STATIC_INLINE void CMU_IntDisable(uint32_t flags)
  571. {
  572. CMU->IEN &= ~flags;
  573. }
  574. /***************************************************************************//**
  575. * @brief
  576. * Enable one or more CMU interrupts.
  577. *
  578. * @note
  579. * Depending on the use, a pending interrupt may already be set prior to
  580. * enabling the interrupt. Consider using CMU_IntClear() prior to enabling
  581. * if such a pending interrupt should be ignored.
  582. *
  583. * @param[in] flags
  584. * CMU interrupt sources to enable.
  585. ******************************************************************************/
  586. __STATIC_INLINE void CMU_IntEnable(uint32_t flags)
  587. {
  588. CMU->IEN |= flags;
  589. }
  590. /***************************************************************************//**
  591. * @brief
  592. * Get pending CMU interrupts.
  593. *
  594. * @return
  595. * CMU interrupt sources pending.
  596. ******************************************************************************/
  597. __STATIC_INLINE uint32_t CMU_IntGet(void)
  598. {
  599. return CMU->IF;
  600. }
  601. /***************************************************************************//**
  602. * @brief
  603. * Get enabled and pending CMU interrupt flags.
  604. *
  605. * @details
  606. * Useful for handling more interrupt sources in the same interrupt handler.
  607. *
  608. * @note
  609. * The event bits are not cleared by the use of this function.
  610. *
  611. * @return
  612. * Pending and enabled CMU interrupt sources.
  613. * The return value is the bitwise AND combination of
  614. * - the OR combination of enabled interrupt sources in CMU_IEN_nnn
  615. * register (CMU_IEN_nnn) and
  616. * - the OR combination of valid interrupt flags of the CMU module
  617. * (CMU_IF_nnn).
  618. ******************************************************************************/
  619. __STATIC_INLINE uint32_t CMU_IntGetEnabled(void)
  620. {
  621. uint32_t tmp = 0U;
  622. /* Store LESENSE->IEN in temporary variable in order to define explicit order
  623. * of volatile accesses. */
  624. tmp = CMU->IEN;
  625. /* Bitwise AND of pending and enabled interrupts */
  626. return CMU->IF & tmp;
  627. }
  628. /**************************************************************************//**
  629. * @brief
  630. * Set one or more pending CMU interrupts from SW.
  631. *
  632. * @param[in] flags
  633. * CMU interrupt sources to set to pending.
  634. *****************************************************************************/
  635. __STATIC_INLINE void CMU_IntSet(uint32_t flags)
  636. {
  637. CMU->IFS = flags;
  638. }
  639. /***************************************************************************//**
  640. * @brief
  641. * Lock the CMU in order to protect some of its registers against unintended
  642. * modification.
  643. *
  644. * @details
  645. * Please refer to the reference manual for CMU registers that will be
  646. * locked.
  647. *
  648. * @note
  649. * If locking the CMU registers, they must be unlocked prior to using any
  650. * CMU API functions modifying CMU registers protected by the lock.
  651. ******************************************************************************/
  652. __STATIC_INLINE void CMU_Lock(void)
  653. {
  654. CMU->LOCK = CMU_LOCK_LOCKKEY_LOCK;
  655. }
  656. /***************************************************************************//**
  657. * @brief
  658. * Unlock the CMU so that writing to locked registers again is possible.
  659. ******************************************************************************/
  660. __STATIC_INLINE void CMU_Unlock(void)
  661. {
  662. CMU->LOCK = CMU_LOCK_LOCKKEY_UNLOCK;
  663. }
  664. /***************************************************************************//**
  665. * @brief
  666. * Get calibration count register
  667. * @note
  668. * If continuous calibrartion mode is active, calibration busy will allmost
  669. * always be on, and we just need to read the value, where the normal case
  670. * would be that this function call has been triggered by the CALRDY
  671. * interrupt flag.
  672. * @return
  673. * Calibration count, the number of UPSEL clocks (see CMU_CalibrateConfig)
  674. * in the period of DOWNSEL oscillator clock cycles configured by a previous
  675. * write operation to CMU->CALCNT
  676. ******************************************************************************/
  677. __STATIC_INLINE uint32_t CMU_CalibrateCountGet(void)
  678. {
  679. /* Wait until calibration completes, UNLESS continuous calibration mode is */
  680. /* active */
  681. #if defined (_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  682. if (!(CMU->CALCTRL & CMU_CALCTRL_CONT))
  683. {
  684. while (CMU->STATUS & CMU_STATUS_CALBSY)
  685. ;
  686. }
  687. #else
  688. while (CMU->STATUS & CMU_STATUS_CALBSY)
  689. ;
  690. #endif
  691. return CMU->CALCNT;
  692. }
  693. /***************************************************************************//**
  694. * @brief
  695. * Starts calibration
  696. * @note
  697. * This call is usually invoked after CMU_CalibrateConfig() and possibly
  698. * CMU_CalibrateCont()
  699. ******************************************************************************/
  700. __STATIC_INLINE void CMU_CalibrateStart(void)
  701. {
  702. CMU->CMD = CMU_CMD_CALSTART;
  703. }
  704. #if defined (_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  705. /***************************************************************************//**
  706. * @brief
  707. * Stop the calibration counters
  708. ******************************************************************************/
  709. __STATIC_INLINE void CMU_CalibrateStop(void)
  710. {
  711. CMU->CMD = CMU_CMD_CALSTOP;
  712. }
  713. /***************************************************************************//**
  714. * @brief
  715. * Configures continuous calibration mode
  716. * @param[in] enable
  717. * If true, enables continuous calibration, if false disables continuous
  718. * calibrartion
  719. ******************************************************************************/
  720. __STATIC_INLINE void CMU_CalibrateCont(bool enable)
  721. {
  722. BITBAND_Peripheral(&(CMU->CALCTRL), _CMU_CALCTRL_CONT_SHIFT, enable);
  723. }
  724. #endif
  725. /** @} (end addtogroup CMU) */
  726. /** @} (end addtogroup EM_Library) */
  727. #ifdef __cplusplus
  728. }
  729. #endif
  730. #endif /* __EM_CMU_H */