mrt_5410x.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * @brief LPC5410X Multi-Rate Timer (MRT) registers and driver functions
  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 __MRT_5410X_H_
  32. #define __MRT_5410X_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup MRT_5410X CHIP: LPC5410X Multi-Rate Timer driver
  37. * @ingroup CHIP_5410X_DRIVERS
  38. * @{
  39. */
  40. /**
  41. * @brief LPC5410X MRT chip configuration
  42. */
  43. #define MRT_CHANNELS_NUM (4)
  44. #define MRT_NO_IDLE_CHANNEL (0x40)
  45. /**
  46. * @brief MRT register block structure
  47. */
  48. typedef struct {
  49. __IO uint32_t INTVAL; /*!< Timer interval register */
  50. __O uint32_t TIMER; /*!< Timer register */
  51. __IO uint32_t CTRL; /*!< Timer control register */
  52. __IO uint32_t STAT; /*!< Timer status register */
  53. } LPC_MRT_CH_T;
  54. /**
  55. * @brief MRT register block structure
  56. */
  57. typedef struct {
  58. LPC_MRT_CH_T CHANNEL[MRT_CHANNELS_NUM];
  59. uint32_t unused[44];
  60. __IO uint32_t MODCFG;
  61. __O uint32_t IDLE_CH;
  62. __IO uint32_t IRQ_FLAG;
  63. } LPC_MRT_T;
  64. /**
  65. * @brief MRT Interrupt Modes enum
  66. */
  67. typedef enum MRT_MODE {
  68. MRT_MODE_REPEAT = (0 << 1), /*!< MRT Repeat interrupt mode */
  69. MRT_MODE_ONESHOT = (1 << 1) /*!< MRT One-shot interrupt mode */
  70. } MRT_MODE_T;
  71. /**
  72. * @brief MRT register bit fields & masks
  73. */
  74. /* MRT Time interval register bit fields */
  75. #define MRT_INTVAL_IVALUE (0x7FFFFFFF) /* Maximum interval load value and mask */
  76. #define MRT_INTVAL_LOAD (0x80000000UL) /* Force immediate load of timer interval register bit */
  77. /* MRT Control register bit fields & masks */
  78. #define MRT_CTRL_INTEN_MASK (0x01)
  79. #define MRT_CTRL_MODE_MASK (0x06)
  80. /* MRT Status register bit fields & masks */
  81. #define MRT_STAT_INTFLAG (0x01)
  82. #define MRT_STAT_RUNNING (0x02)
  83. /* Pointer to individual MR register blocks */
  84. #define LPC_MRT_CH0 ((LPC_MRT_CH_T *) &LPC_MRT->CHANNEL[0])
  85. #define LPC_MRT_CH1 ((LPC_MRT_CH_T *) &LPC_MRT->CHANNEL[1])
  86. #define LPC_MRT_CH2 ((LPC_MRT_CH_T *) &LPC_MRT->CHANNEL[2])
  87. #define LPC_MRT_CH3 ((LPC_MRT_CH_T *) &LPC_MRT->CHANNEL[3])
  88. #define LPC_MRT_CH(ch) ((LPC_MRT_CH_T *) &LPC_MRT->CHANNEL[(ch)])
  89. /* Global interrupt flag register interrupt mask/clear values */
  90. #define MRT0_INTFLAG (1)
  91. #define MRT1_INTFLAG (2)
  92. #define MRT2_INTFLAG (4)
  93. #define MRT3_INTFLAG (8)
  94. #define MRTn_INTFLAG(ch) (1 << (ch))
  95. /**
  96. * @brief Initializes the MRT
  97. * @return Nothing
  98. */
  99. STATIC INLINE void Chip_MRT_Init(void)
  100. {
  101. /* Enable the clock to the register interface */
  102. Chip_Clock_EnablePeriphClock(SYSCON_CLOCK_MRT);
  103. /* Reset MRT */
  104. Chip_SYSCON_PeriphReset(RESET_MRT);
  105. }
  106. /**
  107. * @brief De-initializes the MRT Channel
  108. * @return Nothing
  109. */
  110. STATIC INLINE void Chip_MRT_DeInit(void)
  111. {
  112. /* Disable the clock to the MRT */
  113. Chip_Clock_DisablePeriphClock(SYSCON_CLOCK_MRT);
  114. }
  115. /**
  116. * @brief Returns a pointer to the register block for a MRT channel
  117. * @param ch : MRT channel tog et register block for (0..3)
  118. * @return Pointer to the MRT register block for the channel
  119. */
  120. STATIC INLINE LPC_MRT_CH_T *Chip_MRT_GetRegPtr(uint8_t ch)
  121. {
  122. return LPC_MRT_CH(ch);
  123. }
  124. /**
  125. * @brief Returns the timer time interval value
  126. * @param pMRT : Pointer to selected MRT Channel
  127. * @return Timer time interval value (IVALUE)
  128. */
  129. STATIC INLINE uint32_t Chip_MRT_GetInterval(LPC_MRT_CH_T *pMRT)
  130. {
  131. return pMRT->INTVAL;
  132. }
  133. /**
  134. * @brief Sets the timer time interval value
  135. * @param pMRT : Pointer to selected MRT Channel
  136. * @param interval : The interval timeout (31-bits)
  137. * @return Nothing
  138. * @note Setting bit 31 in timer time interval register causes the time interval value
  139. * to load immediately, otherwise the time interval value will be loaded in
  140. * next timer cycle.<br>
  141. * Example: Chip_MRT_SetInterval(pMRT, 0x500 | MRT_INTVAL_LOAD); // Will load timer interval immediately<br>
  142. * Example: Chip_MRT_SetInterval(pMRT, 0x500); // Will load timer interval after internal expires
  143. */
  144. STATIC INLINE void Chip_MRT_SetInterval(LPC_MRT_CH_T *pMRT, uint32_t interval)
  145. {
  146. pMRT->INTVAL = interval;
  147. }
  148. /**
  149. * @brief Returns the current timer value
  150. * @param pMRT : Pointer to selected MRT Channel
  151. * @return The current timer value
  152. */
  153. STATIC INLINE uint32_t Chip_MRT_GetTimer(LPC_MRT_CH_T *pMRT)
  154. {
  155. return pMRT->TIMER;
  156. }
  157. /**
  158. * @brief Returns true if the timer is enabled
  159. * @param pMRT : Pointer to selected MRT Channel
  160. * @return True if enabled, Flase if not enabled
  161. */
  162. STATIC INLINE bool Chip_MRT_GetEnabled(LPC_MRT_CH_T *pMRT)
  163. {
  164. return (bool) ((pMRT->CTRL & MRT_CTRL_INTEN_MASK) != 0);
  165. }
  166. /**
  167. * @brief Enables the timer
  168. * @param pMRT : Pointer to selected MRT Channel
  169. * @return Nothing
  170. */
  171. STATIC INLINE void Chip_MRT_SetEnabled(LPC_MRT_CH_T *pMRT)
  172. {
  173. pMRT->CTRL |= MRT_CTRL_INTEN_MASK;
  174. }
  175. /**
  176. * @brief Disables the timer
  177. * @param pMRT : Pointer to selected MRT Channel
  178. * @return Nothing
  179. */
  180. STATIC INLINE void Chip_MRT_SetDisabled(LPC_MRT_CH_T *pMRT)
  181. {
  182. pMRT->CTRL &= ~MRT_CTRL_INTEN_MASK;
  183. }
  184. /**
  185. * @brief Returns the timer mode (repeat or one-shot)
  186. * @param pMRT : Pointer to selected MRT Channel
  187. * @return The current timer mode
  188. */
  189. STATIC INLINE MRT_MODE_T Chip_MRT_GetMode(LPC_MRT_CH_T *pMRT)
  190. {
  191. return (MRT_MODE_T) (pMRT->CTRL & MRT_CTRL_MODE_MASK);
  192. }
  193. /**
  194. * @brief Sets the timer mode (repeat or one-shot)
  195. * @param pMRT : Pointer to selected MRT Channel
  196. * @param mode : Timer mode
  197. * @return Nothing
  198. */
  199. STATIC INLINE void Chip_MRT_SetMode(LPC_MRT_CH_T *pMRT, MRT_MODE_T mode)
  200. {
  201. uint32_t reg;
  202. reg = pMRT->CTRL & ~MRT_CTRL_MODE_MASK;
  203. pMRT->CTRL = reg | (uint32_t) mode;
  204. }
  205. /**
  206. * @brief Check if the timer is configured in repeat mode
  207. * @param pMRT : Pointer to selected MRT Channel
  208. * @return True if in repeat mode, False if in one-shot mode
  209. */
  210. STATIC INLINE bool Chip_MRT_IsRepeatMode(LPC_MRT_CH_T *pMRT)
  211. {
  212. return ((pMRT->CTRL & MRT_CTRL_MODE_MASK) != 0) ? false : true;
  213. }
  214. /**
  215. * @brief Check if the timer is configured in one-shot mode
  216. * @param pMRT : Pointer to selected MRT Channel
  217. * @return True if in one-shot mode, False if in repeat mode
  218. */
  219. STATIC INLINE bool Chip_MRT_IsOneShotMode(LPC_MRT_CH_T *pMRT)
  220. {
  221. return ((pMRT->CTRL & MRT_CTRL_MODE_MASK) != 0) ? true : false;
  222. }
  223. /**
  224. * @brief Check if the timer has an interrupt pending
  225. * @param pMRT : Pointer to selected MRT Channel
  226. * @return True if interrupt is pending, False if no interrupt is pending
  227. */
  228. STATIC INLINE bool Chip_MRT_IntPending(LPC_MRT_CH_T *pMRT)
  229. {
  230. return (bool) ((pMRT->STAT & MRT_STAT_INTFLAG) != 0);
  231. }
  232. /**
  233. * @brief Clears the pending interrupt (if any)
  234. * @param pMRT : Pointer to selected MRT Channel
  235. * @return Nothing
  236. */
  237. STATIC INLINE void Chip_MRT_IntClear(LPC_MRT_CH_T *pMRT)
  238. {
  239. pMRT->STAT |= MRT_STAT_INTFLAG;
  240. }
  241. /**
  242. * @brief Check if the timer is running
  243. * @param pMRT : Pointer to selected MRT Channel
  244. * @return True if running, False if stopped
  245. */
  246. STATIC INLINE bool Chip_MRT_Running(LPC_MRT_CH_T *pMRT)
  247. {
  248. return (bool) ((pMRT->STAT & MRT_STAT_RUNNING) != 0);
  249. }
  250. /**
  251. * @brief Returns the IDLE channel value
  252. * @return IDLE channel value (unshifted in bits 7..4)
  253. */
  254. STATIC INLINE uint8_t Chip_MRT_GetIdleChannel(void)
  255. {
  256. return (uint8_t) (LPC_MRT->IDLE_CH);
  257. }
  258. /**
  259. * @brief Returns the IDLE channel value
  260. * @return IDLE channel value (shifted in bits 3..0)
  261. */
  262. STATIC INLINE uint8_t Chip_MRT_GetIdleChannelShifted(void)
  263. {
  264. return (uint8_t) (Chip_MRT_GetIdleChannel() >> 4);
  265. }
  266. /**
  267. * @brief Returns the interrupt pending status for all MRT channels
  268. * @return IRQ pending channel bitfield(bit 0 = MRT0, bit 1 = MRT1, etc.)
  269. */
  270. STATIC INLINE uint32_t Chip_MRT_GetIntPending(void)
  271. {
  272. return LPC_MRT->IRQ_FLAG;
  273. }
  274. /**
  275. * @brief Returns the interrupt pending status for a singel MRT channel
  276. * @param ch : Channel to check pending interrupt status for
  277. * @return IRQ pending channel number
  278. */
  279. STATIC INLINE bool Chip_MRT_GetIntPendingByChannel(uint8_t ch)
  280. {
  281. return (bool) (((LPC_MRT->IRQ_FLAG >> ch) & 1) != 0);
  282. }
  283. /**
  284. * @brief Clears the interrupt pending status for one or more MRT channels
  285. * @param mask : Channels to clear (bit 0 = MRT0, bit 1 = MRT1, etc.)
  286. * @return Nothing
  287. * @note Use this function to clear multiple interrupt pending states in
  288. * a single call via the IRQ_FLAG register. Performs the same function for
  289. * all MRT channels in a single call as the Chip_MRT_IntClear() does for a
  290. * single channel.
  291. */
  292. STATIC INLINE void Chip_MRT_ClearIntPending(uint32_t mask)
  293. {
  294. LPC_MRT->IRQ_FLAG = mask;
  295. }
  296. /**
  297. * @}
  298. */
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif /* __MRT_5410X_H_ */