clock_8xx.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * @brief LPC8xx clock driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  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_8XX_H_
  32. #define __CLOCK_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup CLOCK_8XX CHIP: LPC8xx Clock Driver
  37. * @ingroup CHIP_8XX_Drivers
  38. * @{
  39. */
  40. /* Internal oscillator frequency */
  41. #define SYSCTL_IRC_FREQ (12000000)
  42. #ifndef MAX_CLOCK_FREQ
  43. #define MAX_CLOCK_FREQ (30000000)
  44. #endif
  45. /**
  46. * Clock sources for system and USB PLLs
  47. */
  48. typedef enum CHIP_SYSCTL_PLLCLKSRC {
  49. SYSCTL_PLLCLKSRC_IRC = 0, /*!< Internal oscillator */
  50. SYSCTL_PLLCLKSRC_SYSOSC, /*!< Crystal (system) oscillator */
  51. SYSCTL_PLLCLKSRC_RESERVED,
  52. SYSCTL_PLLCLKSRC_EXT_CLKIN, /*!< External clock input */
  53. } CHIP_SYSCTL_PLLCLKSRC_T;
  54. /**
  55. * Watchdog oscillator analog output frequency selection
  56. * values enum (plus or minus 40%)
  57. */
  58. typedef enum CHIP_WDTLFO_OSC {
  59. WDTLFO_OSC_ILLEGAL,
  60. WDTLFO_OSC_0_60, /*!< 0.6 MHz watchdog/LFO rate */
  61. WDTLFO_OSC_1_05, /*!< 1.05 MHz watchdog/LFO rate */
  62. WDTLFO_OSC_1_40, /*!< 1.4 MHz watchdog/LFO rate */
  63. WDTLFO_OSC_1_75, /*!< 1.75 MHz watchdog/LFO rate */
  64. WDTLFO_OSC_2_10, /*!< 2.1 MHz watchdog/LFO rate */
  65. WDTLFO_OSC_2_40, /*!< 2.4 MHz watchdog/LFO rate */
  66. WDTLFO_OSC_2_70, /*!< 2.7 MHz watchdog/LFO rate */
  67. WDTLFO_OSC_3_00, /*!< 3.0 MHz watchdog/LFO rate */
  68. WDTLFO_OSC_3_25, /*!< 3.25 MHz watchdog/LFO rate */
  69. WDTLFO_OSC_3_50, /*!< 3.5 MHz watchdog/LFO rate */
  70. WDTLFO_OSC_3_75, /*!< 3.75 MHz watchdog/LFO rate */
  71. WDTLFO_OSC_4_00, /*!< 4.0 MHz watchdog/LFO rate */
  72. WDTLFO_OSC_4_20, /*!< 4.2 MHz watchdog/LFO rate */
  73. WDTLFO_OSC_4_40, /*!< 4.4 MHz watchdog/LFO rate */
  74. WDTLFO_OSC_4_60 /*!< 4.6 MHz watchdog/LFO rate */
  75. } CHIP_WDTLFO_OSC_T;
  76. /**
  77. * Clock sources for main system clock
  78. */
  79. typedef enum CHIP_SYSCTL_MAINCLKSRC {
  80. SYSCTL_MAINCLKSRC_IRC = 0, /*!< Internal oscillator */
  81. SYSCTL_MAINCLKSRC_PLLIN, /*!< System PLL input */
  82. SYSCTL_MAINCLKSRC_WDTOSC, /*!< Watchdog oscillator rate */
  83. SYSCTL_MAINCLKSRC_PLLOUT, /*!< System PLL output */
  84. } CHIP_SYSCTL_MAINCLKSRC_T;
  85. /**
  86. * System and peripheral clocks enum
  87. */
  88. typedef enum CHIP_SYSCTL_CLOCK {
  89. SYSCTL_CLOCK_SYS = 0, /*!< System clock */
  90. SYSCTL_CLOCK_ROM, /*!< ROM clock */
  91. SYSCTL_CLOCK_RAM, /*!< RAM clock */
  92. SYSCTL_CLOCK_FLASHREG, /*!< FLASH register interface clock */
  93. SYSCTL_CLOCK_FLASH, /*!< FLASH array access clock */
  94. SYSCTL_CLOCK_I2C0, /*!< I2C0 clock */
  95. SYSCTL_CLOCK_GPIO, /*!< GPIO clock */
  96. SYSCTL_CLOCK_SWM, /*!< Switch matrix clock */
  97. SYSCTL_CLOCK_SCT, /*!< State configurable timer clock */
  98. SYSCTL_CLOCK_WKT, /*!< Self wake-up timer clock */
  99. SYSCTL_CLOCK_MRT, /*!< Multi-rate timer clock */
  100. SYSCTL_CLOCK_SPI0, /*!< SPI0 clock */
  101. SYSCTL_CLOCK_SPI1, /*!< SPI01 clock */
  102. SYSCTL_CLOCK_CRC, /*!< CRC clock */
  103. SYSCTL_CLOCK_UART0, /*!< UART0 clock */
  104. SYSCTL_CLOCK_UART1, /*!< UART1 clock */
  105. SYSCTL_CLOCK_UART2, /*!< UART2 clock */
  106. SYSCTL_CLOCK_WWDT, /*!< Watchdog clock */
  107. SYSCTL_CLOCK_IOCON, /*!< IOCON clock */
  108. SYSCTL_CLOCK_ACOMP, /*!< Analog comparator clock */
  109. /* LPC82x Specific Clocks */
  110. SYSCTL_CLOCK_I2C1 = 21, /*!< I2C1 Clock */
  111. SYSCTL_CLOCK_I2C2, /*!< I2C2 Clock */
  112. SYSCTL_CLOCK_I2C3, /*!< I2C3 Clock */
  113. SYSCTL_CLOCK_ADC, /*!< 12-Bit ADC Clock */
  114. SYSCTL_CLOCK_MTB = 26, /*!< Macro Trace Buffer [USED FOR DEBUGGING] */
  115. SYSCTL_CLOCK_DMA = 29, /*!< DMA Clock */
  116. } CHIP_SYSCTL_CLOCK_T;
  117. /* Clock name alias */
  118. #define SYSCTL_CLOCK_I2C SYSCTL_CLOCK_I2C0
  119. #define SYSCTL_CLOCK_ACMP SYSCTL_CLOCK_ACOMP
  120. /**
  121. * Clock sources for CLKOUT
  122. */
  123. typedef enum CHIP_SYSCTL_CLKOUTSRC {
  124. SYSCTL_CLKOUTSRC_IRC = 0, /*!< Internal oscillator for CLKOUT */
  125. SYSCTL_CLKOUTSRC_SYSOSC, /*!< System oscillator for CLKOUT */
  126. SYSCTL_CLKOUTSRC_WDTOSC, /*!< Watchdog oscillator for CLKOUT */
  127. SYSCTL_CLKOUTSRC_MAINSYSCLK, /*!< Main system clock for CLKOUT */
  128. } CHIP_SYSCTL_CLKOUTSRC_T;
  129. /**
  130. * @brief Set System PLL divider values
  131. * @param msel : PLL feedback divider value
  132. * @param psel : PLL post divider value
  133. * @return Nothing
  134. * @note See the user manual for how to setup the PLL
  135. */
  136. STATIC INLINE void Chip_Clock_SetupSystemPLL(uint8_t msel, uint8_t psel)
  137. {
  138. LPC_SYSCTL->SYSPLLCTRL = (msel & 0x1F) | ((psel & 0x3) << 5);
  139. }
  140. /**
  141. * @brief Read System PLL status
  142. * @return true if the PLL is locked, false if not locked
  143. */
  144. STATIC INLINE bool Chip_Clock_IsSystemPLLLocked(void)
  145. {
  146. return (bool) ((LPC_SYSCTL->SYSPLLSTAT & 1) != 0);
  147. }
  148. /**
  149. * @brief Setup Watchdog oscillator rate and divider
  150. * @param wdtclk : Selected watchdog clock rate
  151. * @param div : Watchdog divider value, even value between 2 and 64
  152. * @return Nothing
  153. * @note Watchdog rate = selected rate divided by divider rate
  154. */
  155. STATIC INLINE void Chip_Clock_SetWDTOSC(CHIP_WDTLFO_OSC_T wdtclk, uint8_t div)
  156. {
  157. LPC_SYSCTL->WDTOSCCTRL = (((uint32_t) wdtclk) << 5) | ((div >> 1) - 1);
  158. }
  159. /**
  160. * @brief Returns the main clock source
  161. * @return Main clock source
  162. */
  163. STATIC INLINE CHIP_SYSCTL_MAINCLKSRC_T Chip_Clock_GetMainClockSource(void)
  164. {
  165. return (CHIP_SYSCTL_MAINCLKSRC_T) (LPC_SYSCTL->MAINCLKSEL & ~SYSCTL_MAINCLKSEL_RESERVED);
  166. }
  167. /**
  168. * @brief Set system clock divider
  169. * @param div : divider for system clock
  170. * @return Nothing
  171. * @note Use 0 to disable, or a divider value of 1 to 255. The system clock
  172. * rate is the main system clock divided by this value.
  173. */
  174. STATIC INLINE void Chip_Clock_SetSysClockDiv(uint32_t div)
  175. {
  176. LPC_SYSCTL->SYSAHBCLKDIV = div;
  177. }
  178. /**
  179. * @brief Enable system or peripheral clock
  180. * @param clk : Clock to enable
  181. * @return Nothing
  182. */
  183. STATIC INLINE void Chip_Clock_EnablePeriphClock(CHIP_SYSCTL_CLOCK_T clk)
  184. {
  185. LPC_SYSCTL->SYSAHBCLKCTRL = (1 << clk) | (LPC_SYSCTL->SYSAHBCLKCTRL & ~SYSCTL_SYSAHBCLKCTRL_RESERVED);
  186. }
  187. /**
  188. * @brief Disable system or peripheral clock
  189. * @param clk : Clock to disable
  190. * @return Nothing
  191. */
  192. STATIC INLINE void Chip_Clock_DisablePeriphClock(CHIP_SYSCTL_CLOCK_T clk)
  193. {
  194. LPC_SYSCTL->SYSAHBCLKCTRL &= ~((1 << clk) | SYSCTL_SYSAHBCLKCTRL_RESERVED);
  195. }
  196. /**
  197. * @brief Set UART divider clock
  198. * @param div : divider for UART clock
  199. * @return Nothing
  200. * @note Use 0 to disable, or a divider value of 1 to 255. The UART clock
  201. * rate is the main system clock divided by this value.
  202. */
  203. STATIC INLINE void Chip_Clock_SetUARTClockDiv(uint32_t div)
  204. {
  205. LPC_SYSCTL->UARTCLKDIV = div;
  206. }
  207. /**
  208. * @brief Return UART divider
  209. * @return divider for UART clock
  210. * @note A value of 0 means the clock is disabled.
  211. */
  212. STATIC INLINE uint32_t Chip_Clock_GetUARTClockDiv(void)
  213. {
  214. return LPC_SYSCTL->UARTCLKDIV & ~SYSCTL_UARTCLKDIV_RESERVED;
  215. }
  216. /**
  217. * @brief Set The USART Fractional Generator Divider
  218. * @param div : Fractional Generator Divider value, should be 0xFF
  219. * @return Nothing
  220. */
  221. STATIC INLINE void Chip_SYSCTL_SetUSARTFRGDivider(uint8_t div)
  222. {
  223. LPC_SYSCTL->UARTFRGDIV = (uint32_t) div;
  224. }
  225. /**
  226. * @brief Get The USART Fractional Generator Divider
  227. * @return Value of USART Fractional Generator Divider
  228. */
  229. STATIC INLINE uint32_t Chip_SYSCTL_GetUSARTFRGDivider(void)
  230. {
  231. return LPC_SYSCTL->UARTFRGDIV & ~SYSCTL_UARTFRGDIV_RESERVED;
  232. }
  233. /**
  234. * @brief Set The USART Fractional Generator Multiplier
  235. * @param mult : An 8-bit value (0-255) U_PCLK = UARTCLKDIV/(1 + MULT/256)
  236. * @return Nothing
  237. */
  238. STATIC INLINE void Chip_SYSCTL_SetUSARTFRGMultiplier(uint8_t mult)
  239. {
  240. LPC_SYSCTL->UARTFRGMULT = (uint32_t) mult;
  241. }
  242. /**
  243. * @brief Get The USART Fractional Generator Multiplier
  244. * @return Value of USART Fractional Generator Multiplier
  245. */
  246. STATIC INLINE uint32_t Chip_SYSCTL_GetUSARTFRGMultiplier(void)
  247. {
  248. return LPC_SYSCTL->UARTFRGMULT & ~SYSCTL_UARTFRGMULT_RESERVED;
  249. }
  250. /**
  251. * @brief Set USART 0/1/2 UART base rate (up to main clock rate)
  252. * @param rate : Desired rate for fractional divider/multipler output
  253. * @param fEnable : true to use fractional clocking, false for integer clocking
  254. * @return Actual rate generated
  255. * @note USARTs 0 - 2 use the same base clock for their baud rate
  256. * basis. This function is used to generate that clock, while the
  257. * UART driver's SetBaud functions will attempt to get the closest
  258. * baud rate from this base clock without altering it. This needs
  259. * to be setup prior to individual UART setup.<br>
  260. * UARTs need a base clock 16x faster than the baud rate, so if you
  261. * need a 115.2Kbps baud rate, you will need a clock rate of at
  262. * least (115.2K * 16). The UART base clock is generated from the
  263. * main system clock, so fractional clocking may be the only
  264. * possible choice when using a low main system clock frequency.
  265. * Do not alter the FRGCLKDIV register after this call.
  266. */
  267. uint32_t Chip_Clock_SetUSARTNBaseClockRate(uint32_t rate, bool fEnable);
  268. /**
  269. * @brief Get USART 0/1/2 UART base rate
  270. * @return USART 0/1/2 UART base rate
  271. */
  272. uint32_t Chip_Clock_GetUSARTNBaseClockRate(void);
  273. /**
  274. * @brief Returns the main oscillator clock rate
  275. * @return main oscillator clock rate
  276. */
  277. STATIC INLINE uint32_t Chip_Clock_GetMainOscRate(void)
  278. {
  279. return OscRateIn;
  280. }
  281. /**
  282. * @brief Returns the internal oscillator (IRC) clock rate
  283. * @return internal oscillator (IRC) clock rate
  284. */
  285. STATIC INLINE uint32_t Chip_Clock_GetIntOscRate(void)
  286. {
  287. return SYSCTL_IRC_FREQ;
  288. }
  289. /**
  290. * @brief Returns the external clock input rate
  291. * @return External clock input rate
  292. */
  293. STATIC INLINE uint32_t Chip_Clock_GetExtClockInRate(void)
  294. {
  295. return ExtRateIn;
  296. }
  297. /**
  298. * @brief Set System PLL clock source
  299. * @param src : Clock source for system PLL
  300. * @return Nothing
  301. * @note This function will also toggle the clock source update register
  302. * to update the clock source
  303. */
  304. void Chip_Clock_SetSystemPLLSource(CHIP_SYSCTL_PLLCLKSRC_T src);
  305. /**
  306. * @brief Bypass System Oscillator and set oscillator frequency range
  307. * @param bypass : Flag to bypass oscillator
  308. * @param highfr : Flag to set oscillator range from 15-25 MHz
  309. * @return Nothing
  310. * @note Sets the PLL input to bypass the oscillator. This would be
  311. * used if an external clock that is not an oscillator is attached
  312. * to the XTALIN pin.
  313. */
  314. void Chip_Clock_SetPLLBypass(bool bypass, bool highfr);
  315. /**
  316. * @brief Set main system clock source
  317. * @param src : Clock source for main system
  318. * @return Nothing
  319. * @note This function will also toggle the clock source update register
  320. * to update the clock source
  321. */
  322. void Chip_Clock_SetMainClockSource(CHIP_SYSCTL_MAINCLKSRC_T src);
  323. /**
  324. * @brief Set CLKOUT clock source and divider
  325. * @param src : Clock source for CLKOUT
  326. * @param div : divider for CLKOUT clock
  327. * @return Nothing
  328. * @note Use 0 to disable, or a divider value of 1 to 255. The CLKOUT clock
  329. * rate is the clock source divided by the divider. This function will
  330. * also toggle the clock source update register to update the clock
  331. * source.
  332. */
  333. void Chip_Clock_SetCLKOUTSource(CHIP_SYSCTL_CLKOUTSRC_T src, uint32_t div);
  334. /**
  335. * @brief Return estimated watchdog oscillator rate
  336. * @return Estimated watchdog oscillator rate
  337. * @note This rate is accurate to plus or minus 40%.
  338. */
  339. uint32_t Chip_Clock_GetWDTOSCRate(void);
  340. /**
  341. * @brief Return System PLL input clock rate
  342. * @return System PLL input clock rate
  343. */
  344. uint32_t Chip_Clock_GetSystemPLLInClockRate(void);
  345. /**
  346. * @brief Return System PLL output clock rate
  347. * @return System PLL output clock rate
  348. */
  349. uint32_t Chip_Clock_GetSystemPLLOutClockRate(void);
  350. /**
  351. * @brief Return main clock rate
  352. * @return main clock rate
  353. */
  354. uint32_t Chip_Clock_GetMainClockRate(void);
  355. /**
  356. * @brief Return system clock rate
  357. * @return system clock rate
  358. */
  359. uint32_t Chip_Clock_GetSystemClockRate(void);
  360. /**
  361. * @brief Get IOCONCLKDIV clock rate
  362. * @param reg : Divider register to get
  363. * @return The clock rate going to the IOCON glitch filter
  364. * @note Use 0 to disable, or a divider value of 1 to 255.
  365. */
  366. uint32_t Chip_Clock_GetIOCONCLKDIVClockRate(CHIP_PIN_CLKDIV_T reg);
  367. /**
  368. * @brief Set IOCONCLKDIV divider
  369. * @param reg : divider register to set
  370. * @param div : divider value for IOCONCLKDIV[reg] clock
  371. * @return Nothing
  372. * @note Use 0 to disable, or a divider value of 1 to 255.
  373. */
  374. void Chip_Clock_SetIOCONCLKDIV(CHIP_PIN_CLKDIV_T reg, uint8_t div);
  375. /**
  376. * @}
  377. */
  378. /** @defgroup IRC_8XX CHIP: LPC8xx IRC Configuration
  379. * @ingroup CHIP_8XX_Drivers
  380. * @{
  381. */
  382. /**
  383. * @brief set main / system clock using IRC and PLL
  384. * @param main: main clock frequency (in MHz)
  385. * @param sys : system clock frequency (in MHz)
  386. * @return bool: Success = true / fail = false
  387. * @note This is a table based function. The table uses both the
  388. * main frequency and the system frequency to set the PLL.
  389. * All useful main / system clock combinations are in the table.
  390. * See irc_8xx.c for details.
  391. */
  392. bool Chip_IRC_SetFreq(uint32_t main, uint32_t sys);
  393. /**
  394. * @brief Set main / system clock using IRC and PLL
  395. * @param sys : system clock frequency (in MHz)
  396. * @return Nothing
  397. * @note This function uses the ROM set_pll() function.
  398. */
  399. void Chip_IRC_SetFreq_ROM(uint32_t sys);
  400. /**
  401. * @}
  402. */
  403. #ifdef __cplusplus
  404. }
  405. #endif
  406. #endif /* __CLOCK_8XX_H_ */