rtc_5410x.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * @brief LPC5410X RTC chip 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 __RTC_5410X_H_
  32. #define __RTC_5410X_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup RTC_5410X CHIP: LPC5410X Real Time clock
  37. * @ingroup CHIP_5410X_DRIVERS
  38. * @{
  39. */
  40. /**
  41. * @brief LPC5410X Real Time clock register block structure
  42. */
  43. typedef struct { /*!< RTC */
  44. __IO uint32_t CTRL; /*!< RTC control register */
  45. __IO uint32_t MATCH; /*!< PRTC match (alarm) register */
  46. __IO uint32_t COUNT; /*!< RTC counter register */
  47. __IO uint32_t WAKE; /*!< RTC high-resolution/wake-up timer control register */
  48. } LPC_RTC_T;
  49. /* CTRL register defniitions */
  50. #define RTC_CTRL_SWRESET (1 << 0) /*!< Apply reset to RTC */
  51. #define RTC_CTRL_OFD (1 << 1) /*!< Oscillator fail detect status (failed bit) */
  52. #define RTC_CTRL_ALARM1HZ (1 << 2) /*!< RTC 1 Hz timer alarm flag status (match) bit */
  53. #define RTC_CTRL_WAKE1KHZ (1 << 3) /*!< RTC 1 kHz timer wake-up flag status (timeout) bit */
  54. #define RTC_CTRL_ALARMDPD_EN (1 << 4) /*!< RTC 1 Hz timer alarm for Deep power-down enable bit */
  55. #define RTC_CTRL_WAKEDPD_EN (1 << 5) /*!< RTC 1 kHz timer wake-up for Deep power-down enable bit */
  56. #define RTC_CTRL_RTC1KHZ_EN (1 << 6) /*!< RTC 1 kHz clock enable bit */
  57. #define RTC_CTRL_RTC_EN (1 << 7) /*!< RTC enable bit */
  58. #define RTC_CTRL_MASK ((uint32_t) 0xF1) /*!< RTC Control register Mask for reserved and status bits */
  59. /**
  60. * @brief Initialize the RTC peripheral
  61. * @param pRTC : RTC peripheral selected
  62. * @return None
  63. */
  64. STATIC INLINE void Chip_RTC_Init(LPC_RTC_T *pRTC)
  65. {
  66. Chip_Clock_EnablePeriphClock(SYSCON_CLOCK_RTC);
  67. Chip_SYSCON_PeriphReset(RESET_RTC);
  68. }
  69. /**
  70. * @brief De-initialize the RTC peripheral
  71. * @param pRTC : RTC peripheral selected
  72. * @return None
  73. */
  74. STATIC INLINE void Chip_RTC_DeInit(LPC_RTC_T *pRTC)
  75. {
  76. Chip_Clock_DisablePeriphClock(SYSCON_CLOCK_RTC);
  77. }
  78. /**
  79. * @brief Enable RTC options
  80. * @param pRTC : The base address of RTC block
  81. * @param flags : And OR'ed value of RTC_CTRL_* definitions to enable
  82. * @return Nothing
  83. * @note You can enable multiple RTC options at once using this function
  84. * by OR'ing them together. It is recommended to only use the
  85. * RTC_CTRL_ALARMDPD_EN, RTC_CTRL_WAKEDPD_EN, RTC_CTRL_RTC1KHZ_EN, and
  86. * RTC_CTRL_RTC_EN flags with this function.
  87. */
  88. STATIC INLINE void Chip_RTC_EnableOptions(LPC_RTC_T *pRTC, uint32_t flags)
  89. {
  90. pRTC->CTRL = (pRTC->CTRL & RTC_CTRL_MASK) | flags;
  91. }
  92. /**
  93. * @brief Disable RTC options
  94. * @param pRTC : The base address of RTC block
  95. * @param flags : And OR'ed value of RTC_CTRL_* definitions to disable
  96. * @return Nothing
  97. * @note You can enable multiple RTC options at once using this function
  98. * by OR'ing them together. It is recommended to only use the
  99. * RTC_CTRL_ALARMDPD_EN, RTC_CTRL_WAKEDPD_EN, RTC_CTRL_RTC1KHZ_EN, and
  100. * RTC_CTRL_RTC_EN flags with this function.
  101. */
  102. STATIC INLINE void Chip_RTC_DisableOptions(LPC_RTC_T *pRTC, uint32_t flags)
  103. {
  104. pRTC->CTRL = (pRTC->CTRL & RTC_CTRL_MASK) & ~flags;
  105. }
  106. /**
  107. * @brief Reset RTC
  108. * @param pRTC : The base address of RTC block
  109. * @return Nothing
  110. * @note The RTC state will be returned to it's default.
  111. */
  112. STATIC INLINE void Chip_RTC_Reset(LPC_RTC_T *pRTC)
  113. {
  114. Chip_RTC_EnableOptions(pRTC, RTC_CTRL_SWRESET);
  115. Chip_RTC_DisableOptions(pRTC, RTC_CTRL_SWRESET);
  116. }
  117. /**
  118. * @brief Enables the RTC
  119. * @param pRTC : The base address of RTC block
  120. * @return Nothing
  121. * @note You can also use Chip_RTC_EnableOptions() with the
  122. * RTC_CTRL_RTC_EN flag to enable the RTC.
  123. */
  124. STATIC INLINE void Chip_RTC_Enable(LPC_RTC_T *pRTC)
  125. {
  126. Chip_RTC_EnableOptions(pRTC, RTC_CTRL_RTC_EN);
  127. }
  128. /**
  129. * @brief Disables the RTC
  130. * @param pRTC : The base address of RTC block
  131. * @return Nothing
  132. * @note You can also use Chip_RTC_DisableOptions() with the
  133. * RTC_CTRL_RTC_EN flag to enable the RTC.
  134. */
  135. STATIC INLINE void Chip_RTC_Disable(LPC_RTC_T *pRTC)
  136. {
  137. Chip_RTC_DisableOptions(pRTC, RTC_CTRL_RTC_EN);
  138. }
  139. /**
  140. * @brief Enables the RTC 1KHz high resolution timer
  141. * @param pRTC : The base address of RTC block
  142. * @return Nothing
  143. * @note You can also use Chip_RTC_EnableOptions() with the
  144. * RTC_CTRL_RTC1KHZ_EN flag to enable the high resolution
  145. * timer.
  146. */
  147. STATIC INLINE void Chip_RTC_Enable1KHZ(LPC_RTC_T *pRTC)
  148. {
  149. Chip_RTC_EnableOptions(pRTC, RTC_CTRL_RTC1KHZ_EN);
  150. }
  151. /**
  152. * @brief Disables the RTC 1KHz high resolution timer
  153. * @param pRTC : The base address of RTC block
  154. * @return Nothing
  155. * @note You can also use Chip_RTC_DisableOptions() with the
  156. * RTC_CTRL_RTC1KHZ_EN flag to disable the high resolution
  157. * timer.
  158. */
  159. STATIC INLINE void Chip_RTC_Disable1KHZ(LPC_RTC_T *pRTC)
  160. {
  161. Chip_RTC_DisableOptions(pRTC, RTC_CTRL_RTC1KHZ_EN);
  162. }
  163. /**
  164. * @brief Enables selected RTC wakeup events
  165. * @param pRTC : The base address of RTC block
  166. * @param ints : Wakeup events to enable
  167. * @return Nothing
  168. * @note Select either one or both (OR'ed) RTC_CTRL_ALARMDPD_EN
  169. * and RTC_CTRL_WAKEDPD_EN values to enabled. You can also
  170. * use Chip_RTC_EnableOptions() with the flags to enable
  171. * the events.
  172. */
  173. STATIC INLINE void Chip_RTC_EnableWakeup(LPC_RTC_T *pRTC, uint32_t ints)
  174. {
  175. Chip_RTC_EnableOptions(pRTC, ints);
  176. }
  177. /**
  178. * @brief Disables selected RTC wakeup events
  179. * @param pRTC : The base address of RTC block
  180. * @param ints : Wakeup events to disable
  181. * @return Nothing
  182. * @note Select either one or both (OR'ed) RTC_CTRL_ALARMDPD_EN
  183. * and RTC_CTRL_WAKEDPD_EN values to disabled. You can also
  184. * use Chip_RTC_DisableOptions() with the flags to disable
  185. * the events.
  186. */
  187. STATIC INLINE void Chip_RTC_DisableWakeup(LPC_RTC_T *pRTC, uint32_t ints)
  188. {
  189. Chip_RTC_DisableOptions(pRTC, ints);
  190. }
  191. /**
  192. * @brief Clears latched RTC statuses
  193. * @param pRTC : The base address of RTC block
  194. * @param stsMask : OR'ed status bits to clear
  195. * @return Nothing
  196. * @note Use and OR'ed stsMask value of RTC_CTRL_OFD, RTC_CTRL_ALARM1HZ,
  197. * and RTC_CTRL_WAKE1KHZ to clear specific RTC states.
  198. */
  199. STATIC INLINE void Chip_RTC_ClearStatus(LPC_RTC_T *pRTC, uint32_t stsMask)
  200. {
  201. pRTC->CTRL = (pRTC->CTRL & RTC_CTRL_MASK) | stsMask;
  202. }
  203. /**
  204. * @brief Return RTC control/status register
  205. * @param pRTC : The base address of RTC block
  206. * @return The current RTC control/status register
  207. * @note Mask the return value with a RTC_CTRL_* definitions to determine
  208. * which bits are set. For example, mask the return value with
  209. * RTC_CTRL_ALARM1HZ to determine if the alarm interrupt is pending.
  210. */
  211. STATIC INLINE uint32_t Chip_RTC_GetStatus(LPC_RTC_T *pRTC)
  212. {
  213. return pRTC->CTRL;
  214. }
  215. /**
  216. * @brief Set RTC match value for alarm status/interrupt
  217. * @param pRTC : The base address of RTC block
  218. * @param count : Alarm event time
  219. * @return Nothing
  220. */
  221. STATIC INLINE void Chip_RTC_SetAlarm(LPC_RTC_T *pRTC, uint32_t count)
  222. {
  223. pRTC->MATCH = count;
  224. }
  225. /**
  226. * @brief Return the RTC match value used for alarm status/interrupt
  227. * @param pRTC : The base address of RTC block
  228. * @return Alarm event time
  229. */
  230. STATIC INLINE uint32_t Chip_RTC_GetAlarm(LPC_RTC_T *pRTC)
  231. {
  232. return pRTC->MATCH;
  233. }
  234. /**
  235. * @brief Set RTC match count for 1 second timer count
  236. * @param pRTC : The base address of RTC block
  237. * @param count : Initial count to set
  238. * @return Nothing
  239. * @note Only write to this register when the RTC_CTRL_RTC_EN bit in
  240. * the CTRL Register is 0. The counter increments one second
  241. * after the RTC_CTRL_RTC_EN bit is set.
  242. */
  243. STATIC INLINE void Chip_RTC_SetCount(LPC_RTC_T *pRTC, uint32_t count)
  244. {
  245. pRTC->COUNT = count;
  246. }
  247. /**
  248. * @brief Get current RTC 1 second timer count
  249. * @param pRTC : The base address of RTC block
  250. * @return current RTC 1 second timer count
  251. */
  252. STATIC INLINE uint32_t Chip_RTC_GetCount(LPC_RTC_T *pRTC)
  253. {
  254. return pRTC->COUNT;
  255. }
  256. /**
  257. * @brief Set RTC wake count countdown value (in mS ticks)
  258. * @param pRTC : The base address of RTC block
  259. * @param count : wakeup time in milliSeconds
  260. * @return Nothing
  261. * @note A write pre-loads a start count value into the wake-up
  262. * timer and initializes a count-down sequence.
  263. */
  264. STATIC INLINE void Chip_RTC_SetWake(LPC_RTC_T *pRTC, uint16_t count)
  265. {
  266. pRTC->WAKE = count;
  267. }
  268. /**
  269. * @brief Get RTC wake count countdown value
  270. * @param pRTC : The base address of RTC block
  271. * @return current RTC wake count countdown value (in mS)
  272. */
  273. STATIC INLINE uint16_t Chip_RTC_GetWake(LPC_RTC_T *pRTC)
  274. {
  275. return pRTC->WAKE;
  276. }
  277. /**
  278. * @}
  279. */
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #endif /* __RTC_5410X_H_ */