mrt_8xx.h 9.8 KB

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