clock_5410x.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * @brief LPC5410X clock driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licenser disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __CLOCK_5410X_H_
  32. #define __CLOCK_5410X_H_
  33. #include "pll_5410x.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /** @defgroup CLOCK_5410X CHIP: LPC5410X Clock Driver
  38. * @ingroup CHIP_5410X_DRIVERS
  39. * @{
  40. */
  41. /* Internal oscillator frequency */
  42. #define SYSCON_IRC_FREQ (12000000)
  43. #define SYSCON_WDTOSC_FREQ (500000)
  44. #define SYSCON_RTC_FREQ (32768)
  45. /**
  46. * @brief Returns the internal oscillator (IRC) clock rate
  47. * @return internal oscillator (IRC) clock rate
  48. */
  49. STATIC INLINE uint32_t Chip_Clock_GetIntOscRate(void)
  50. {
  51. return SYSCON_IRC_FREQ;
  52. }
  53. /**
  54. * @brief Returns the external clock input rate
  55. * @return External clock input rate
  56. */
  57. STATIC INLINE uint32_t Chip_Clock_GetExtClockInRate(void)
  58. {
  59. return ExtClockIn;
  60. }
  61. /**
  62. * @brief Returns the RTC clock rate
  63. * @return RTC oscillator clock rate in Hz
  64. */
  65. STATIC INLINE uint32_t Chip_Clock_GetRTCOscRate(void)
  66. {
  67. return SYSCON_RTC_FREQ;
  68. }
  69. /**
  70. * @brief Return estimated watchdog oscillator rate
  71. * @return Estimated watchdog oscillator rate
  72. * @note This rate is accurate to plus or minus 40%.
  73. */
  74. STATIC INLINE uint32_t Chip_Clock_GetWDTOSCRate(void)
  75. {
  76. return SYSCON_WDTOSC_FREQ;
  77. }
  78. /**
  79. * Clock source selections for only the main A system clock. The main A system
  80. * clock is used as an input into the main B system clock selector. Main clock A
  81. * only needs to be setup if the main clock A input is used in the main clock
  82. * system selector.
  83. */
  84. typedef enum {
  85. SYSCON_MAIN_A_CLKSRC_IRC = 0, /*!< Internal oscillator */
  86. SYSCON_MAIN_A_CLKSRCA_CLKIN, /*!< Crystal (main) oscillator in */
  87. SYSCON_MAIN_A_CLKSRCA_WDTOSC, /*!< Watchdog oscillator rate */
  88. } CHIP_SYSCON_MAIN_A_CLKSRC_T;
  89. /**
  90. * @brief Set main A system clock source
  91. * @param src : Clock source for main A
  92. * @return Nothing
  93. * @note This function only needs to be setup if main clock A will be
  94. * selected in the Chip_Clock_GetMain_B_ClockRate() function.
  95. */
  96. STATIC INLINE void Chip_Clock_SetMain_A_ClockSource(CHIP_SYSCON_MAIN_A_CLKSRC_T src)
  97. {
  98. LPC_SYSCON->MAINCLKSELA = (uint32_t) src;
  99. }
  100. /**
  101. * @brief Returns the main A clock source
  102. * @return Returns which clock is used for the main A
  103. */
  104. STATIC INLINE CHIP_SYSCON_MAIN_A_CLKSRC_T Chip_Clock_GetMain_A_ClockSource(void)
  105. {
  106. return (CHIP_SYSCON_MAIN_A_CLKSRC_T) (LPC_SYSCON->MAINCLKSELA);
  107. }
  108. /**
  109. * @brief Return main A clock rate
  110. * @return main A clock rate in Hz
  111. */
  112. uint32_t Chip_Clock_GetMain_A_ClockRate(void);
  113. /**
  114. * Clock sources for only main B system clock
  115. */
  116. typedef enum {
  117. SYSCON_MAIN_B_CLKSRC_MAINCLKSELA = 0, /*!< main clock A */
  118. SYSCON_MAIN_B_CLKSRC_SYSPLLIN, /*!< System PLL input */
  119. SYSCON_MAIN_B_CLKSRC_SYSPLLOUT, /*!< System PLL output */
  120. SYSCON_MAIN_B_CLKSRC_RTC, /*!< RTC oscillator 32KHz output */
  121. } CHIP_SYSCON_MAIN_B_CLKSRC_T;
  122. /**
  123. * @brief Set main B system clock source
  124. * @param src : Clock source for main B
  125. * @return Nothing
  126. */
  127. STATIC INLINE void Chip_Clock_SetMain_B_ClockSource(CHIP_SYSCON_MAIN_B_CLKSRC_T src)
  128. {
  129. LPC_SYSCON->MAINCLKSELB = (uint32_t) src;
  130. }
  131. /**
  132. * @brief Returns the main B clock source
  133. * @return Returns which clock is used for the main B
  134. */
  135. STATIC INLINE CHIP_SYSCON_MAIN_B_CLKSRC_T Chip_Clock_GetMain_B_ClockSource(void)
  136. {
  137. return (CHIP_SYSCON_MAIN_B_CLKSRC_T) (LPC_SYSCON->MAINCLKSELB);
  138. }
  139. /**
  140. * @brief Return main B clock rate
  141. * @return main B clock rate
  142. */
  143. uint32_t Chip_Clock_GetMain_B_ClockRate(void);
  144. /**
  145. * Clock sources for CLKOUT
  146. */
  147. typedef enum {
  148. SYSCON_CLKOUTSRC_MAINCLK = 0, /*!< Main system clock for CLKOUT */
  149. SYSCON_CLKOUTSRC_CLKIN, /*!< CLKIN for CLKOUT */
  150. SYSCON_CLKOUTSRC_WDTOSC, /*!< Watchdog oscillator for CLKOUT */
  151. SYSCON_CLKOUTSRC_IRC, /*!< Internal oscillator for CLKOUT */
  152. SYSCON_CLKOUTSRCA_OUTPUT, /*!< clkoutA output route to input of clkoutB */
  153. SYSCON_CLKOUTSRC_RTC = 7 /*!< RTC oscillator 32KHz for CLKOUT */
  154. } CHIP_SYSCON_CLKOUTSRC_T;
  155. /**
  156. * @brief Set CLKOUT clock source and divider
  157. * @param src : Clock source for CLKOUT
  158. * @param div : divider for CLKOUT clock
  159. * @return Nothing
  160. * @note Use 0 to disable, or a divider value of 1 to 255. The CLKOUT clock
  161. * rate is the clock source divided by the divider. This function will
  162. * also toggle the clock source update register to update the clock
  163. * source.
  164. */
  165. void Chip_Clock_SetCLKOUTSource(CHIP_SYSCON_CLKOUTSRC_T src, uint32_t div);
  166. /**
  167. * System and peripheral clocks enum
  168. */
  169. typedef enum CHIP_SYSCON_CLOCK {
  170. /* Peripheral clock enables for SYSAHBCLKCTRL0 */
  171. SYSCON_CLOCK_ROM = 1, /*!< ROM clock */
  172. SYSCON_CLOCK_SRAM1 = 3, /*!< SRAM1 clock */
  173. SYSCON_CLOCK_SRAM2, /*!< SRAM2 clock */
  174. SYSCON_CLOCK_FLASH = 7, /*!< FLASH controller clock */
  175. SYSCON_CLOCK_FMC, /*!< FMC clock */
  176. SYSCON_CLOCK_INPUTMUX = 11, /*!< Input mux clock */
  177. SYSCON_CLOCK_IOCON = 13, /*!< IOCON clock */
  178. SYSCON_CLOCK_GPIO0, /*!< GPIO0 clock */
  179. SYSCON_CLOCK_GPIO1, /*!< GPIO1 clock */
  180. SYSCON_CLOCK_PINT = 18, /*!< PININT clock */
  181. SYSCON_CLOCK_GINT, /*!< grouped pin interrupt block clock */
  182. SYSCON_CLOCK_DMA, /*!< DMA clock */
  183. SYSCON_CLOCK_CRC, /*!< CRC clock */
  184. SYSCON_CLOCK_WWDT, /*!< WDT clock */
  185. SYSCON_CLOCK_RTC, /*!< RTC clock */
  186. SYSCON_CLOCK_MAILBOX = 26, /*!< Mailbox clock */
  187. SYSCON_CLOCK_ADC0, /*!< ADC0 clock */
  188. /* Peripheral clock enables for SYSAHBCLKCTRL1 */
  189. SYSCON_CLOCK_MRT = 32, /*!< multi-rate timer clock */
  190. SYSCON_CLOCK_RIT, /*!< Repetitive interval timer clock */
  191. SYSCON_CLOCK_SCT0, /*!< SCT0 clock */
  192. SYSCON_CLOCK_FIFO = 32 + 9, /*!< System FIFO clock */
  193. SYSCON_CLOCK_UTICK, /*!< UTICK clock */
  194. SYSCON_CLOCK_TIMER2 = 32 + 22, /*!< TIMER2 clock */
  195. SYSCON_CLOCK_TIMER3 = 32 + 26, /*!< TIMER3 clock */
  196. SYSCON_CLOCK_TIMER4, /*!< TIMER4 clock */
  197. /* Peripheral clock enables for ASYNCAPBCLKCTRLCLR */
  198. SYSCON_CLOCK_USART0 = 128 + 1, /*!< USART0 clock */
  199. SYSCON_CLOCK_USART1, /*!< USART1 clock */
  200. SYSCON_CLOCK_USART2, /*!< USART2 clock */
  201. SYSCON_CLOCK_USART3, /*!< USART3 clock */
  202. SYSCON_CLOCK_I2C0, /*!< I2C0 clock */
  203. SYSCON_CLOCK_I2C1, /*!< I2C1 clock */
  204. SYSCON_CLOCK_I2C2, /*!< I2C2 clock */
  205. SYSCON_CLOCK_SPI0 = 128 + 9, /*!< SPI0 clock */
  206. SYSCON_CLOCK_SPI1, /*!< SPI1 clock */
  207. SYSCON_CLOCK_TIMER0 = 128 + 13, /*!< TIMER0 clock */
  208. SYSCON_CLOCK_TIMER1, /*!< TIMER1 clock */
  209. SYSCON_CLOCK_FRG /*!< FRG clock */
  210. } CHIP_SYSCON_CLOCK_T;
  211. /**
  212. * @brief Enable a system or peripheral clock
  213. * @param clk : Clock to enable
  214. * @return Nothing
  215. */
  216. void Chip_Clock_EnablePeriphClock(CHIP_SYSCON_CLOCK_T clk);
  217. /**
  218. * @brief Disable a system or peripheral clock
  219. * @param clk : Clock to disable
  220. * @return Nothing
  221. */
  222. void Chip_Clock_DisablePeriphClock(CHIP_SYSCON_CLOCK_T clk);
  223. /**
  224. * @brief Set system tick clock divider (external CLKIN as SYSTICK reference only)
  225. * @param div : divider for system clock
  226. * @return Nothing
  227. * @note Use 0 to disable, or a divider value of 1 to 255. The system tick
  228. * rate is the external CLKIN rate divided by this value. The extern CLKIN pin
  229. * signal, divided by the SYSTICKCLKDIV divider, is selected by clearing
  230. * CLKSOURCE bit 2 in the System Tick CSR register. The core clock must be at least
  231. * 2.5 times faster than the reference system tick clock otherwise the count
  232. * values are unpredictable.
  233. */
  234. STATIC INLINE void Chip_Clock_SetSysTickClockDiv(uint32_t div)
  235. {
  236. LPC_SYSCON->SYSTICKCLKDIV = div;
  237. }
  238. /**
  239. * @brief Returns system tick clock divider
  240. * @return system tick clock divider
  241. */
  242. STATIC INLINE uint32_t Chip_Clock_GetSysTickClockDiv(void)
  243. {
  244. return LPC_SYSCON->SYSTICKCLKDIV;
  245. }
  246. /**
  247. * @brief Returns the system tick rate as used with the system tick divider
  248. * @return the system tick rate
  249. */
  250. uint32_t Chip_Clock_GetSysTickClockRate(void);
  251. /**
  252. * @brief Set system clock divider
  253. * @param div : divider for system clock
  254. * @return Nothing
  255. * @note Use 0 to disable, or a divider value of 1 to 255. The system clock
  256. * rate is the main system clock divided by this value.
  257. */
  258. STATIC INLINE void Chip_Clock_SetSysClockDiv(uint32_t div)
  259. {
  260. LPC_SYSCON->AHBCLKDIV = div;
  261. }
  262. /**
  263. * @brief Set system tick clock divider
  264. * @param div : divider for system clock
  265. * @return Nothing
  266. * @note Use 0 to disable, or a divider value of 1 to 255. The system tick
  267. * rate is the main system clock divided by this value. Use caution when using
  268. * the CMSIS SysTick_Config() functions as they typically use SystemCoreClock
  269. * for setup.
  270. */
  271. STATIC INLINE void Chip_Clock_SetADCClockDiv(uint32_t div)
  272. {
  273. LPC_SYSCON->ADCCLKDIV = div;
  274. }
  275. /**
  276. * @brief Returns ADC clock divider
  277. * @return ADC clock divider, 0 = disabled
  278. */
  279. STATIC INLINE uint32_t Chip_Clock_GetADCClockDiv(void)
  280. {
  281. return LPC_SYSCON->ADCCLKDIV;
  282. }
  283. /**
  284. * Clock sources for ADC clock source select
  285. */
  286. typedef enum {
  287. SYSCON_ADCCLKSELSRC_MAINCLK = 0, /*!< Main clock */
  288. SYSCON_ADCCLKSELSRC_SYSPLLOUT, /*!< PLL output */
  289. SYSCON_ADCCLKSELSRC_IRC /*!< Internal oscillator */
  290. } CHIP_SYSCON_ADCCLKSELSRC_T;
  291. /**
  292. * @brief Set the ADC clock source
  293. * @param src : ADC clock source
  294. * @return Nothing
  295. */
  296. STATIC INLINE void Chip_Clock_SetADCClockSource(CHIP_SYSCON_ADCCLKSELSRC_T src)
  297. {
  298. LPC_SYSCON->ADCCLKSEL = (uint32_t) src;
  299. }
  300. /**
  301. * @brief Returns the ADC clock source
  302. * @return Returns which clock is used for the ADC clock source
  303. */
  304. STATIC INLINE CHIP_SYSCON_ADCCLKSELSRC_T Chip_Clock_GetADCClockSource(void)
  305. {
  306. return (CHIP_SYSCON_ADCCLKSELSRC_T) (LPC_SYSCON->ADCCLKSEL);
  307. }
  308. /**
  309. * @brief Return ADC clock rate
  310. * @return ADC clock rate
  311. */
  312. uint32_t Chip_Clock_GetADCClockRate(void);
  313. /**
  314. * @brief Enable the RTC 32KHz output
  315. * @return Nothing
  316. * @note This clock can be used for the main clock directly, but
  317. * do not use this clock with the system PLL.
  318. */
  319. STATIC INLINE void Chip_Clock_EnableRTCOsc(void)
  320. {
  321. LPC_SYSCON->RTCOSCCTRL = 1;
  322. }
  323. /**
  324. * @brief Disable the RTC 32KHz output
  325. * @return Nothing
  326. */
  327. STATIC INLINE void Chip_Clock_DisableRTCOsc(void)
  328. {
  329. LPC_SYSCON->RTCOSCCTRL = 0;
  330. }
  331. /**
  332. * Clock source selections for the asynchronous APB clock
  333. */
  334. typedef enum {
  335. SYSCON_ASYNC_IRC = 0, /*!< IRC input */
  336. SYSCON_ASYNC_WDTOSC, /*!< Watchdog oscillator */
  337. SYSCON_ASYNC_MAINCLK = 4, /*!< Main clock */
  338. SYSCON_ASYNC_CLKIN, /*!< external CLK input */
  339. SYSCON_ASYNC_SYSPLLOUT /*!< System PLL output */
  340. } CHIP_ASYNC_SYSCON_SRC_T;
  341. /**
  342. * @brief Set asynchronous APB clock source
  343. * @param src : Clock source for asynchronous APB clock
  344. * @return Nothing
  345. */
  346. void Chip_Clock_SetAsyncSysconClockSource(CHIP_ASYNC_SYSCON_SRC_T src);
  347. /**
  348. * @brief Get asynchronous APB clock source
  349. * @return Clock source for asynchronous APB clock
  350. */
  351. CHIP_ASYNC_SYSCON_SRC_T Chip_Clock_GetAsyncSysconClockSource(void);
  352. /**
  353. * @brief Return asynchronous APB clock rate
  354. * @return Asynchronous APB clock rate
  355. * @note Includes adjustments by Async clock divider (ASYNCCLKDIV).
  356. */
  357. uint32_t Chip_Clock_GetAsyncSyscon_ClockRate(void);
  358. /**
  359. * @brief Set UART divider clock
  360. * @param div : divider for UART clock
  361. * @return Nothing
  362. * @note Use 0 to disable, or a divider value of 1 to 255. The UART clock
  363. * rate is the main system clock divided by this value.
  364. */
  365. STATIC INLINE void Chip_Clock_SetAsyncSysconClockDiv(uint32_t div)
  366. {
  367. LPC_ASYNC_SYSCON->ASYNCCLKDIV = div;
  368. }
  369. /**
  370. * Clock sources for main system clock. This is a mix of both main clock A
  371. * and B selections.
  372. */
  373. typedef enum {
  374. SYSCON_MAINCLKSRC_IRC = 0, /*!< Internal oscillator */
  375. SYSCON_MAINCLKSRC_CLKIN, /*!< Crystal (main) oscillator in */
  376. SYSCON_MAINCLKSRC_WDTOSC, /*!< Watchdog oscillator rate */
  377. SYSCON_MAINCLKSRC_PLLIN = 5, /*!< System PLL input */
  378. SYSCON_MAINCLKSRC_PLLOUT, /*!< System PLL output */
  379. SYSCON_MAINCLKSRC_RTC /*!< RTC oscillator 32KHz output */
  380. } CHIP_SYSCON_MAINCLKSRC_T;
  381. /**
  382. * @brief Set main system clock source
  383. * @param src : Clock source for main system
  384. * @return Nothing
  385. */
  386. void Chip_Clock_SetMainClockSource(CHIP_SYSCON_MAINCLKSRC_T src);
  387. /**
  388. * @brief Get main system clock source
  389. * @return Clock source for main system
  390. * @note
  391. */
  392. CHIP_SYSCON_MAINCLKSRC_T Chip_Clock_GetMainClockSource(void);
  393. /**
  394. * @brief Return main clock rate
  395. * @return main clock rate
  396. */
  397. uint32_t Chip_Clock_GetMainClockRate(void);
  398. /**
  399. * @brief Return system clock rate
  400. * @return system clock rate
  401. * @note This is the main clock rate divided by AHBCLKDIV.
  402. */
  403. uint32_t Chip_Clock_GetSystemClockRate(void);
  404. /**
  405. * @brief Get UART base clock rate
  406. * @return UART base clock rate
  407. */
  408. uint32_t Chip_Clock_GetUARTBaseClockRate(void);
  409. /**
  410. * @brief Get UART base clock rate using FRG
  411. * @return Actual UART base clock rate
  412. * @note It's recommended to set a base rate at least 16x the
  413. * expected maximum UART transfer bit rate.
  414. */
  415. uint32_t Chip_Clock_SetUARTBaseClockRate(uint32_t rate);
  416. /**
  417. * @}
  418. */
  419. #ifdef __cplusplus
  420. }
  421. #endif
  422. #endif /* __CLOCK_5410X_H_ */