timer_5410x.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * @brief LPC5410X 32-bit Timer/PWM 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 licensor 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 __TIMER_5410X_H_
  32. #define __TIMER_5410X_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup TIMER_5410X CHIP: LPC5410X 32-bit Timer driver
  37. * @ingroup CHIP_5410X_DRIVERS
  38. * @{
  39. */
  40. /**
  41. * @brief 32-bit Standard timer register block structure
  42. */
  43. typedef struct { /*!< TIMERn Structure */
  44. __IO uint32_t IR; /*!< Interrupt Register. The IR can be written to clear interrupts. The IR can be read to identify which of eight possible interrupt sources are pending. */
  45. __IO uint32_t TCR; /*!< Timer Control Register. The TCR is used to control the Timer Counter functions. The Timer Counter can be disabled or reset through the TCR. */
  46. __IO uint32_t TC; /*!< Timer Counter. The 32 bit TC is incremented every PR+1 cycles of PCLK. The TC is controlled through the TCR. */
  47. __IO uint32_t PR; /*!< Prescale Register. The Prescale Counter (below) is equal to this value, the next clock increments the TC and clears the PC. */
  48. __IO uint32_t PC; /*!< Prescale Counter. The 32 bit PC is a counter which is incremented to the value stored in PR. When the value in PR is reached, the TC is incremented and the PC is cleared. The PC is observable and controllable through the bus interface. */
  49. __IO uint32_t MCR; /*!< Match Control Register. The MCR is used to control if an interrupt is generated and if the TC is reset when a Match occurs. */
  50. __IO uint32_t MR[4]; /*!< Match Register. MR can be enabled through the MCR to reset the TC, stop both the TC and PC, and/or generate an interrupt every time MR matches the TC. */
  51. __IO uint32_t CCR; /*!< Capture Control Register. The CCR controls which edges of the capture inputs are used to load the Capture Registers and whether or not an interrupt is generated when a capture takes place. */
  52. __IO uint32_t CR[4]; /*!< Capture Register. CR is loaded with the value of TC when there is an event on the CAPn.0 input. */
  53. __IO uint32_t EMR; /*!< External Match Register. The EMR controls the external match pins MATn.0-3 (MAT0.0-3 and MAT1.0-3 respectively). */
  54. __I uint32_t RESERVED0[12];
  55. __IO uint32_t CTCR; /*!< Count Control Register. The CTCR selects between Timer and Counter mode, and in Counter mode selects the signal and edge(s) for counting. */
  56. __IO uint32_t PWMC;
  57. } LPC_TIMER_T;
  58. /** Macro to clear interrupt pending */
  59. #define TIMER_IR_CLR(n) _BIT(n)
  60. /** Macro for getting a timer match interrupt bit */
  61. #define TIMER_MATCH_INT(n) (_BIT((n) & 0x0F))
  62. /** Macro for getting a capture event interrupt bit */
  63. #define TIMER_CAP_INT(n) (_BIT((((n) & 0x0F) + 4)))
  64. /** Timer/counter enable bit */
  65. #define TIMER_ENABLE ((uint32_t) (1 << 0))
  66. /** Timer/counter reset bit */
  67. #define TIMER_RESET ((uint32_t) (1 << 1))
  68. /** Timer Control register Mask */
  69. #define TIMER_CTRL_MASK ((uint32_t) 0x03)
  70. /** Bit location for interrupt on MRx match, n = 0 to 3 */
  71. #define TIMER_INT_ON_MATCH(n) (_BIT(((n) * 3)))
  72. /** Bit location for reset on MRx match, n = 0 to 3 */
  73. #define TIMER_RESET_ON_MATCH(n) (_BIT((((n) * 3) + 1)))
  74. /** Bit location for stop on MRx match, n = 0 to 3 */
  75. #define TIMER_STOP_ON_MATCH(n) (_BIT((((n) * 3) + 2)))
  76. /** Match Control register Mask */
  77. #define TIMER_MCR_MASK ((uint32_t) 0x0FFF)
  78. /** Bit location for CAP.n on CRx rising edge, n = 0 to 3 */
  79. #define TIMER_CAP_RISING(n) (_BIT(((n) * 3)))
  80. /** Bit location for CAP.n on CRx falling edge, n = 0 to 3 */
  81. #define TIMER_CAP_FALLING(n) (_BIT((((n) * 3) + 1)))
  82. /** Bit location for CAP.n on CRx interrupt enable, n = 0 to 3 */
  83. #define TIMER_INT_ON_CAP(n) (_BIT((((n) * 3) + 2)))
  84. /** Capture Control register Mask */
  85. #define TIMER_CCR_MASK ((uint32_t) 0x0FFF)
  86. /** External Match register Mask */
  87. #define TIMER_EMR_MASK ((uint32_t) 0x0FFF)
  88. /** Counter Control register Mask */
  89. #define TIMER_CTCR_MASK ((uint32_t) 0x0F)
  90. /**
  91. * @brief Initialize a timer
  92. * @param pTMR : Pointer to timer IP register address
  93. * @return Nothing
  94. */
  95. void Chip_TIMER_Init(LPC_TIMER_T *pTMR);
  96. /**
  97. * @brief Shutdown a timer
  98. * @param pTMR : Pointer to timer IP register address
  99. * @return Nothing
  100. */
  101. void Chip_TIMER_DeInit(LPC_TIMER_T *pTMR);
  102. /**
  103. * @brief Determine if a match interrupt is pending
  104. * @param pTMR : Pointer to timer IP register address
  105. * @param matchnum : Match interrupt number to check
  106. * @return false if the interrupt is not pending, otherwise true
  107. * @note Determine if the match interrupt for the passed timer and match
  108. * counter is pending.
  109. */
  110. STATIC INLINE bool Chip_TIMER_MatchPending(LPC_TIMER_T *pTMR, int8_t matchnum)
  111. {
  112. return (bool) ((pTMR->IR & TIMER_MATCH_INT(matchnum)) != 0);
  113. }
  114. /**
  115. * @brief Determine if a capture interrupt is pending
  116. * @param pTMR : Pointer to timer IP register address
  117. * @param capnum : Capture interrupt number to check
  118. * @return false if the interrupt is not pending, otherwise true
  119. * @note Determine if the capture interrupt for the passed capture pin is
  120. * pending.
  121. */
  122. STATIC INLINE bool Chip_TIMER_CapturePending(LPC_TIMER_T *pTMR, int8_t capnum)
  123. {
  124. return (bool) ((pTMR->IR & TIMER_CAP_INT(capnum)) != 0);
  125. }
  126. /**
  127. * @brief Clears a (pending) match interrupt
  128. * @param pTMR : Pointer to timer IP register address
  129. * @param matchnum : Match interrupt number to clear
  130. * @return Nothing
  131. * @note Clears a pending timer match interrupt.
  132. */
  133. STATIC INLINE void Chip_TIMER_ClearMatch(LPC_TIMER_T *pTMR, int8_t matchnum)
  134. {
  135. pTMR->IR = TIMER_IR_CLR(matchnum);
  136. }
  137. /**
  138. * @brief Clears a (pending) capture interrupt
  139. * @param pTMR : Pointer to timer IP register address
  140. * @param capnum : Capture interrupt number to clear
  141. * @return Nothing
  142. * @note Clears a pending timer capture interrupt.
  143. */
  144. STATIC INLINE void Chip_TIMER_ClearCapture(LPC_TIMER_T *pTMR, int8_t capnum)
  145. {
  146. pTMR->IR = (0x10 << capnum);
  147. }
  148. /**
  149. * @brief Enables the timer (starts count)
  150. * @param pTMR : Pointer to timer IP register address
  151. * @return Nothing
  152. * @note Enables the timer to start counting.
  153. */
  154. STATIC INLINE void Chip_TIMER_Enable(LPC_TIMER_T *pTMR)
  155. {
  156. pTMR->TCR = (pTMR->TCR & TIMER_CTRL_MASK) | TIMER_ENABLE;
  157. }
  158. /**
  159. * @brief Disables the timer (stops count)
  160. * @param pTMR : Pointer to timer IP register address
  161. * @return Nothing
  162. * @note Disables the timer to stop counting.
  163. */
  164. STATIC INLINE void Chip_TIMER_Disable(LPC_TIMER_T *pTMR)
  165. {
  166. pTMR->TCR = (pTMR->TCR & TIMER_CTRL_MASK) & ~TIMER_ENABLE;
  167. }
  168. /**
  169. * @brief Returns the current timer count
  170. * @param pTMR : Pointer to timer IP register address
  171. * @return Current timer terminal count value
  172. * @note Returns the current timer terminal count.
  173. */
  174. STATIC INLINE uint32_t Chip_TIMER_ReadCount(LPC_TIMER_T *pTMR)
  175. {
  176. return pTMR->TC;
  177. }
  178. /**
  179. * @brief Returns the current prescale count
  180. * @param pTMR : Pointer to timer IP register address
  181. * @return Current timer prescale count value
  182. * @note Returns the current prescale count.
  183. */
  184. STATIC INLINE uint32_t Chip_TIMER_ReadPrescale(LPC_TIMER_T *pTMR)
  185. {
  186. return pTMR->PC;
  187. }
  188. /**
  189. * @brief Sets the prescaler value
  190. * @param pTMR : Pointer to timer IP register address
  191. * @param prescale : Prescale value to set the prescale register to
  192. * @return Nothing
  193. * @note Sets the prescale count value.
  194. */
  195. STATIC INLINE void Chip_TIMER_PrescaleSet(LPC_TIMER_T *pTMR, uint32_t prescale)
  196. {
  197. pTMR->PR = prescale;
  198. }
  199. /**
  200. * @brief Sets a timer match value
  201. * @param pTMR : Pointer to timer IP register address
  202. * @param matchnum : Match timer to set match count for
  203. * @param matchval : Match value for the selected match count
  204. * @return Nothing
  205. * @note Sets one of the timer match values.
  206. */
  207. STATIC INLINE void Chip_TIMER_SetMatch(LPC_TIMER_T *pTMR, int8_t matchnum, uint32_t matchval)
  208. {
  209. pTMR->MR[matchnum] = matchval;
  210. }
  211. /**
  212. * @brief Reads a capture register
  213. * @param pTMR : Pointer to timer IP register address
  214. * @param capnum : Capture register to read
  215. * @return The selected capture register value
  216. * @note Returns the selected capture register value.
  217. */
  218. STATIC INLINE uint32_t Chip_TIMER_ReadCapture(LPC_TIMER_T *pTMR, int8_t capnum)
  219. {
  220. return pTMR->CR[capnum];
  221. }
  222. /**
  223. * @brief Resets the timer terminal and prescale counts to 0
  224. * @param pTMR : Pointer to timer IP register address
  225. * @return Nothing
  226. */
  227. void Chip_TIMER_Reset(LPC_TIMER_T *pTMR);
  228. /**
  229. * @brief Enables a match interrupt that fires when the terminal count
  230. * matches the match counter value.
  231. * @param pTMR : Pointer to timer IP register address
  232. * @param matchnum : Match timer, 0 to 3
  233. * @return Nothing
  234. */
  235. STATIC INLINE void Chip_TIMER_MatchEnableInt(LPC_TIMER_T *pTMR, int8_t matchnum)
  236. {
  237. pTMR->MCR = (pTMR->MCR & TIMER_MCR_MASK) | TIMER_INT_ON_MATCH(matchnum);
  238. }
  239. /**
  240. * @brief Disables a match interrupt for a match counter.
  241. * @param pTMR : Pointer to timer IP register address
  242. * @param matchnum : Match timer, 0 to 3
  243. * @return Nothing
  244. */
  245. STATIC INLINE void Chip_TIMER_MatchDisableInt(LPC_TIMER_T *pTMR, int8_t matchnum)
  246. {
  247. pTMR->MCR = (pTMR->MCR & TIMER_MCR_MASK) & ~TIMER_INT_ON_MATCH(matchnum);
  248. }
  249. /**
  250. * @brief For the specific match counter, enables reset of the terminal count register when a match occurs
  251. * @param pTMR : Pointer to timer IP register address
  252. * @param matchnum : Match timer, 0 to 3
  253. * @return Nothing
  254. */
  255. STATIC INLINE void Chip_TIMER_ResetOnMatchEnable(LPC_TIMER_T *pTMR, int8_t matchnum)
  256. {
  257. pTMR->MCR = (pTMR->MCR & TIMER_MCR_MASK) | TIMER_RESET_ON_MATCH(matchnum);
  258. }
  259. /**
  260. * @brief For the specific match counter, disables reset of the terminal count register when a match occurs
  261. * @param pTMR : Pointer to timer IP register address
  262. * @param matchnum : Match timer, 0 to 3
  263. * @return Nothing
  264. */
  265. STATIC INLINE void Chip_TIMER_ResetOnMatchDisable(LPC_TIMER_T *pTMR, int8_t matchnum)
  266. {
  267. pTMR->MCR = (pTMR->MCR & TIMER_MCR_MASK) & ~TIMER_RESET_ON_MATCH(matchnum);
  268. }
  269. /**
  270. * @brief Enable a match timer to stop the terminal count when a
  271. * match count equals the terminal count.
  272. * @param pTMR : Pointer to timer IP register address
  273. * @param matchnum : Match timer, 0 to 3
  274. * @return Nothing
  275. */
  276. STATIC INLINE void Chip_TIMER_StopOnMatchEnable(LPC_TIMER_T *pTMR, int8_t matchnum)
  277. {
  278. pTMR->MCR = (pTMR->MCR & TIMER_MCR_MASK) | TIMER_STOP_ON_MATCH(matchnum);
  279. }
  280. /**
  281. * @brief Disable stop on match for a match timer. Disables a match timer
  282. * to stop the terminal count when a match count equals the terminal count.
  283. * @param pTMR : Pointer to timer IP register address
  284. * @param matchnum : Match timer, 0 to 3
  285. * @return Nothing
  286. */
  287. STATIC INLINE void Chip_TIMER_StopOnMatchDisable(LPC_TIMER_T *pTMR, int8_t matchnum)
  288. {
  289. pTMR->MCR = (pTMR->MCR & TIMER_MCR_MASK) & ~TIMER_STOP_ON_MATCH(matchnum);
  290. }
  291. /**
  292. * @brief Enables capture on on rising edge of selected CAP signal for the
  293. * selected capture register, enables the selected CAPn.capnum signal to load
  294. * the capture register with the terminal coount on a rising edge.
  295. * @param pTMR : Pointer to timer IP register address
  296. * @param capnum : Capture signal/register to use
  297. * @return Nothing
  298. */
  299. STATIC INLINE void Chip_TIMER_CaptureRisingEdgeEnable(LPC_TIMER_T *pTMR, int8_t capnum)
  300. {
  301. pTMR->CCR = (pTMR->CCR & TIMER_CCR_MASK) | TIMER_CAP_RISING(capnum);
  302. }
  303. /**
  304. * @brief Disables capture on on rising edge of selected CAP signal. For the
  305. * selected capture register, disables the selected CAPn.capnum signal to load
  306. * the capture register with the terminal coount on a rising edge.
  307. * @param pTMR : Pointer to timer IP register address
  308. * @param capnum : Capture signal/register to use
  309. * @return Nothing
  310. */
  311. STATIC INLINE void Chip_TIMER_CaptureRisingEdgeDisable(LPC_TIMER_T *pTMR, int8_t capnum)
  312. {
  313. pTMR->CCR = (pTMR->CCR & TIMER_CCR_MASK) & ~TIMER_CAP_RISING(capnum);
  314. }
  315. /**
  316. * @brief Enables capture on on falling edge of selected CAP signal. For the
  317. * selected capture register, enables the selected CAPn.capnum signal to load
  318. * the capture register with the terminal coount on a falling edge.
  319. * @param pTMR : Pointer to timer IP register address
  320. * @param capnum : Capture signal/register to use
  321. * @return Nothing
  322. */
  323. STATIC INLINE void Chip_TIMER_CaptureFallingEdgeEnable(LPC_TIMER_T *pTMR, int8_t capnum)
  324. {
  325. pTMR->CCR = (pTMR->CCR & TIMER_CCR_MASK) | TIMER_CAP_FALLING(capnum);
  326. }
  327. /**
  328. * @brief Disables capture on on falling edge of selected CAP signal. For the
  329. * selected capture register, disables the selected CAPn.capnum signal to load
  330. * the capture register with the terminal coount on a falling edge.
  331. * @param pTMR : Pointer to timer IP register address
  332. * @param capnum : Capture signal/register to use
  333. * @return Nothing
  334. */
  335. STATIC INLINE void Chip_TIMER_CaptureFallingEdgeDisable(LPC_TIMER_T *pTMR, int8_t capnum)
  336. {
  337. pTMR->CCR = (pTMR->CCR & TIMER_CCR_MASK) & ~TIMER_CAP_FALLING(capnum);
  338. }
  339. /**
  340. * @brief Enables interrupt on capture of selected CAP signal. For the
  341. * selected capture register, an interrupt will be generated when the enabled
  342. * rising or falling edge on CAPn.capnum is detected.
  343. * @param pTMR : Pointer to timer IP register address
  344. * @param capnum : Capture signal/register to use
  345. * @return Nothing
  346. */
  347. STATIC INLINE void Chip_TIMER_CaptureEnableInt(LPC_TIMER_T *pTMR, int8_t capnum)
  348. {
  349. pTMR->CCR = (pTMR->CCR & TIMER_CCR_MASK) | TIMER_INT_ON_CAP(capnum);
  350. }
  351. /**
  352. * @brief Disables interrupt on capture of selected CAP signal
  353. * @param pTMR : Pointer to timer IP register address
  354. * @param capnum : Capture signal/register to use
  355. * @return Nothing
  356. */
  357. STATIC INLINE void Chip_TIMER_CaptureDisableInt(LPC_TIMER_T *pTMR, int8_t capnum)
  358. {
  359. pTMR->CCR = (pTMR->CCR & TIMER_CCR_MASK) & ~TIMER_INT_ON_CAP(capnum);
  360. }
  361. /**
  362. * @brief Standard timer initial match pin state and change state
  363. */
  364. typedef enum IP_TIMER_PIN_MATCH_STATE {
  365. TIMER_EXTMATCH_DO_NOTHING = 0, /*!< Timer match state does nothing on match pin */
  366. TIMER_EXTMATCH_CLEAR = 1, /*!< Timer match state sets match pin low */
  367. TIMER_EXTMATCH_SET = 2, /*!< Timer match state sets match pin high */
  368. TIMER_EXTMATCH_TOGGLE = 3 /*!< Timer match state toggles match pin */
  369. } TIMER_PIN_MATCH_STATE_T;
  370. /**
  371. * @brief Sets external match control (MATn.matchnum) pin control. For the pin
  372. * selected with matchnum, sets the function of the pin that occurs on
  373. * a terminal count match for the match count.
  374. * @param pTMR : Pointer to timer IP register address
  375. * @param initial_state : Initial state of the pin, high(1) or low(0)
  376. * @param matchState : Selects the match state for the pin
  377. * @param matchnum : MATn.matchnum signal to use
  378. * @return Nothing
  379. * @note For the pin selected with matchnum, sets the function of the pin that occurs on
  380. * a terminal count match for the match count.
  381. */
  382. void Chip_TIMER_ExtMatchControlSet(LPC_TIMER_T *pTMR, int8_t initial_state,
  383. TIMER_PIN_MATCH_STATE_T matchState, int8_t matchnum);
  384. /**
  385. * @brief Standard timer clock and edge for count source
  386. */
  387. typedef enum IP_TIMER_CAP_SRC_STATE {
  388. TIMER_CAPSRC_RISING_PCLK = 0, /*!< Timer ticks on PCLK rising edge */
  389. TIMER_CAPSRC_RISING_CAPN = 1, /*!< Timer ticks on CAPn.x rising edge */
  390. TIMER_CAPSRC_FALLING_CAPN = 2, /*!< Timer ticks on CAPn.x falling edge */
  391. TIMER_CAPSRC_BOTH_CAPN = 3 /*!< Timer ticks on CAPn.x both edges */
  392. } TIMER_CAP_SRC_STATE_T;
  393. /**
  394. * @brief Sets timer count source and edge with the selected passed from CapSrc.
  395. * If CapSrc selected a CAPn pin, select the specific CAPn pin with the capnum value.
  396. * @param pTMR : Pointer to timer IP register address
  397. * @param capSrc : timer clock source and edge
  398. * @param capnum : CAPn.capnum pin to use (0 - 2)
  399. * @return Nothing
  400. * @note If CapSrc selected a CAPn pin, select the specific CAPn pin with the capnum value.
  401. */
  402. STATIC INLINE void Chip_TIMER_TIMER_SetCountClockSrc(LPC_TIMER_T *pTMR,
  403. TIMER_CAP_SRC_STATE_T capSrc,
  404. int8_t capnum)
  405. {
  406. pTMR->CTCR = (pTMR->CTCR & ~TIMER_CTCR_MASK) | ((uint32_t) capSrc | ((uint32_t) capnum) << 2);
  407. }
  408. /**
  409. * @}
  410. */
  411. #ifdef __cplusplus
  412. }
  413. #endif
  414. #endif /* __TIMER_5410X_H_ */