rtc_001.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * @brief Real Time Clock registers and control 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 __RTC_001_H_
  32. #define __RTC_001_H_
  33. #include "sys_config.h"
  34. #include "cmsis.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /** @defgroup IP_RTC_001 IP: RTC register block and driver
  39. * @ingroup IP_Drivers
  40. * Real Time Clock
  41. * @{
  42. */
  43. /**
  44. * @brief RTC time type option
  45. */
  46. typedef enum {
  47. RTC_TIMETYPE_SECOND, /*!< Second */
  48. RTC_TIMETYPE_MINUTE, /*!< Month */
  49. RTC_TIMETYPE_HOUR, /*!< Hour */
  50. RTC_TIMETYPE_DAYOFMONTH, /*!< Day of month */
  51. RTC_TIMETYPE_DAYOFWEEK, /*!< Day of week */
  52. RTC_TIMETYPE_DAYOFYEAR, /*!< Day of year */
  53. RTC_TIMETYPE_MONTH, /*!< Month */
  54. RTC_TIMETYPE_YEAR, /*!< Year */
  55. RTC_TIMETYPE_LAST
  56. } IP_RTC_TIMEINDEX_T;
  57. /**
  58. * @brief Real Time Clock register block structure
  59. */
  60. typedef struct { /*!< RTC Structure */
  61. __O uint32_t ILR; /*!< Interrupt Location Register */
  62. __I uint32_t RESERVED0;
  63. __IO uint32_t CCR; /*!< Clock Control Register */
  64. __IO uint32_t CIIR; /*!< Counter Increment Interrupt Register */
  65. __IO uint32_t AMR; /*!< Alarm Mask Register */
  66. __I uint32_t CTIME[3]; /*!< Consolidated Time Register 0,1,2 */
  67. __IO uint32_t TIME[RTC_TIMETYPE_LAST]; /*!< Timer field registers */
  68. __IO uint32_t CALIBRATION; /*!< Calibration Value Register */
  69. #if defined(CHIP_LPC177X_8X)
  70. __IO uint32_t GPREG[5]; /*!< General Purpose Storage Registers */
  71. __IO uint32_t RTC_AUXEN; /*!< */
  72. __IO uint32_t RTC_AUX; /*!< */
  73. #else
  74. __I uint32_t RESERVED1[7];
  75. #endif
  76. __IO uint32_t ALRM[RTC_TIMETYPE_LAST]; /*!< Alarm field registers */
  77. #if defined(CHIP_LPC177X_8X)
  78. __IO uint32_t ERSTATUS;
  79. #endif
  80. } IP_RTC_001_T;
  81. /**
  82. * @brief ILR register definitions
  83. */
  84. /** ILR register mask */
  85. #define RTC_ILR_BITMASK ((0x00000003))
  86. /** Bit inform the source interrupt is counter increment*/
  87. #define RTC_IRL_RTCCIF ((1 << 0))
  88. /** Bit inform the source interrupt is alarm match*/
  89. #define RTC_IRL_RTCALF ((1 << 1))
  90. /**
  91. * @brief CCR register definitions
  92. */
  93. /** CCR register mask */
  94. #define RTC_CCR_BITMASK ((0x00000013))
  95. /** Clock enable */
  96. #define RTC_CCR_CLKEN ((1 << 0))
  97. /** Clock reset */
  98. #define RTC_CCR_CTCRST ((1 << 1))
  99. /** Calibration counter enable */
  100. #define RTC_CCR_CCALEN ((1 << 4))
  101. /**
  102. * @brief CIIR and AMR register definitions
  103. */
  104. /** Counter Increment Interrupt bit for second */
  105. #define RTC_AMR_CIIR_IMSEC ((1 << 0))
  106. /** Counter Increment Interrupt bit for minute */
  107. #define RTC_AMR_CIIR_IMMIN ((1 << 1))
  108. /** Counter Increment Interrupt bit for hour */
  109. #define RTC_AMR_CIIR_IMHOUR ((1 << 2))
  110. /** Counter Increment Interrupt bit for day of month */
  111. #define RTC_AMR_CIIR_IMDOM ((1 << 3))
  112. /** Counter Increment Interrupt bit for day of week */
  113. #define RTC_AMR_CIIR_IMDOW ((1 << 4))
  114. /** Counter Increment Interrupt bit for day of year */
  115. #define RTC_AMR_CIIR_IMDOY ((1 << 5))
  116. /** Counter Increment Interrupt bit for month */
  117. #define RTC_AMR_CIIR_IMMON ((1 << 6))
  118. /** Counter Increment Interrupt bit for year */
  119. #define RTC_AMR_CIIR_IMYEAR ((1 << 7))
  120. /** CIIR bit mask */
  121. #define RTC_AMR_CIIR_BITMASK ((0xFF))
  122. /**
  123. * @brief RTC_AUX register definitions
  124. */
  125. /** RTC Oscillator Fail detect flag */
  126. #define RTC_AUX_RTC_OSCF ((1 << 4))
  127. /**
  128. * @brief RTC_AUXEN register definitions
  129. */
  130. /** Oscillator Fail Detect interrupt enable*/
  131. #define RTC_AUXEN_RTC_OSCFEN ((1 << 4))
  132. /**
  133. * @brief Consolidated Time Register 0 definitions
  134. */
  135. #define RTC_CTIME0_SECONDS_MASK ((0x3F))
  136. #define RTC_CTIME0_MINUTES_MASK ((0x3F00))
  137. #define RTC_CTIME0_HOURS_MASK ((0x1F0000))
  138. #define RTC_CTIME0_DOW_MASK ((0x7000000))
  139. /**
  140. * @brief Consolidated Time Register 1 definitions
  141. */
  142. #define RTC_CTIME1_DOM_MASK ((0x1F))
  143. #define RTC_CTIME1_MONTH_MASK ((0xF00))
  144. #define RTC_CTIME1_YEAR_MASK ((0xFFF0000))
  145. /**
  146. * @brief Consolidated Time Register 2 definitions
  147. */
  148. #define RTC_CTIME2_DOY_MASK ((0xFFF))
  149. /**
  150. * @brief Time Counter Group and Alarm register group
  151. */
  152. /** SEC register mask */
  153. #define RTC_SEC_MASK (0x0000003F)
  154. /** MIN register mask */
  155. #define RTC_MIN_MASK (0x0000003F)
  156. /** HOUR register mask */
  157. #define RTC_HOUR_MASK (0x0000001F)
  158. /** DOM register mask */
  159. #define RTC_DOM_MASK (0x0000001F)
  160. /** DOW register mask */
  161. #define RTC_DOW_MASK (0x00000007)
  162. /** DOY register mask */
  163. #define RTC_DOY_MASK (0x000001FF)
  164. /** MONTH register mask */
  165. #define RTC_MONTH_MASK (0x0000000F)
  166. /** YEAR register mask */
  167. #define RTC_YEAR_MASK (0x00000FFF)
  168. #define RTC_SECOND_MAX 59 /*!< Maximum value of second */
  169. #define RTC_MINUTE_MAX 59 /*!< Maximum value of minute*/
  170. #define RTC_HOUR_MAX 23 /*!< Maximum value of hour*/
  171. #define RTC_MONTH_MIN 1 /*!< Minimum value of month*/
  172. #define RTC_MONTH_MAX 12 /*!< Maximum value of month*/
  173. #define RTC_DAYOFMONTH_MIN 1 /*!< Minimum value of day of month*/
  174. #define RTC_DAYOFMONTH_MAX 31 /*!< Maximum value of day of month*/
  175. #define RTC_DAYOFWEEK_MAX 6 /*!< Maximum value of day of week*/
  176. #define RTC_DAYOFYEAR_MIN 1 /*!< Minimum value of day of year*/
  177. #define RTC_DAYOFYEAR_MAX 366 /*!< Maximum value of day of year*/
  178. #define RTC_YEAR_MAX 4095/*!< Maximum value of year*/
  179. /**
  180. * @brief Calibration register
  181. */
  182. /** Calibration value */
  183. #define RTC_CALIBRATION_CALVAL_MASK ((0x1FFFF))
  184. /** Calibration direction */
  185. #define RTC_CALIBRATION_LIBDIR ((1 << 17))
  186. /** Calibration max value */
  187. #define RTC_CALIBRATION_MAX ((0x20000))
  188. /** Calibration definitions */
  189. #define RTC_CALIB_DIR_FORWARD ((uint8_t) (0))
  190. #define RTC_CALIB_DIR_BACKWARD ((uint8_t) (1))
  191. /* Check Parameter Definitions */
  192. /** Macro to determine if it is valid RTC peripheral */
  193. #define PARAM_pRTC(x) (((uint32_t *) x) == ((uint32_t *) LPC_RTC))
  194. /* Macro check RTC interrupt type */
  195. #define PARAM_RTC_INT(n) ((n == RTC_INT_COUNTER_INCREASE) || (n == RTC_INT_ALARM))
  196. /* Macro check RTC calibration type */
  197. #define PARAM_RTC_CALIB_DIR(n) ((n == RTC_CALIB_DIR_FORWARD) || (n == RTC_CALIB_DIR_BACKWARD))
  198. /**
  199. * @brief RTC enumeration
  200. */
  201. /** @brief RTC interrupt source */
  202. typedef enum {
  203. RTC_INT_COUNTER_INCREASE = RTC_IRL_RTCCIF, /*!< Counter Increment Interrupt */
  204. RTC_INT_ALARM = RTC_IRL_RTCALF /*!< The alarm interrupt */
  205. } IP_RTC_INT_OPT;
  206. typedef struct {
  207. uint32_t time[RTC_TIMETYPE_LAST];
  208. } IP_RTC_TIME_T;
  209. /**
  210. * @brief Initialize the RTC peripheral
  211. * @param pRTC : pointer to RTC peripheral block
  212. * @return None
  213. */
  214. void IP_RTC_Init(IP_RTC_001_T *pRTC);
  215. /**
  216. * @brief De-initialize the RTC peripheral
  217. * @param pRTC : pointer to RTC peripheral block
  218. * @return None
  219. */
  220. STATIC INLINE void IP_RTC_DeInit(IP_RTC_001_T *pRTC)
  221. {
  222. pRTC->CCR = 0x00;
  223. }
  224. /**
  225. * @brief Reset clock tick counter in the RTC peripheral
  226. * @param pRTC : pointer to RTC peripheral block
  227. * @return None
  228. */
  229. void IP_RTC_ResetClockTickCounter(IP_RTC_001_T *pRTC);
  230. /**
  231. * @brief Start/Stop RTC peripheral
  232. * @param pRTC : pointer to RTC peripheral block
  233. * @param NewState : ENABLE or DISABLE
  234. * @return None
  235. */
  236. void IP_RTC_Enable(IP_RTC_001_T *pRTC, FunctionalState NewState);
  237. /**
  238. * @brief Enable/Disable Counter increment interrupt for a time type in the RTC peripheral
  239. * @param pRTC : pointer to RTC peripheral block
  240. * @param cntrMask : Or'ed bit values for time types (RTC_AMR_CIIR_IM*)
  241. * @param NewState : ENABLE or DISABLE
  242. * @return None
  243. */
  244. void IP_RTC_CntIncrIntConfig(IP_RTC_001_T *pRTC, uint32_t cntrMask, FunctionalState NewState);
  245. /**
  246. * @brief Enable/Disable Alarm interrupt for a time type in the RTC peripheral
  247. * @param pRTC : pointer to RTC peripheral block
  248. * @param alarmMask : Or'ed bit values for ALARM types (RTC_AMR_CIIR_IM*)
  249. * @param NewState : ENABLE or DISABLE
  250. * @return None
  251. */
  252. void IP_RTC_AlarmIntConfig(IP_RTC_001_T *pRTC, uint32_t alarmMask, FunctionalState NewState);
  253. /**
  254. * @brief Set current time value for a time type in the RTC peripheral
  255. * @param pRTC : pointer to RTC peripheral block
  256. * @param Timetype : time field index type to set
  257. * @param TimeValue : Value to palce in time field
  258. * @return None
  259. */
  260. void IP_RTC_SetTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype, uint32_t TimeValue);
  261. /**
  262. * @brief Get current time value for a type time type
  263. * @param pRTC : pointer to RTC peripheral block
  264. * @param Timetype : Time field index type to get
  265. * @return Value of time field according to specified time type
  266. */
  267. uint32_t IP_RTC_GetTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype);
  268. /**
  269. * @brief Set full time in the RTC peripheral
  270. * @param pRTC : pointer to RTC peripheral block
  271. * @param pFullTime : Pointer to full time data
  272. * @return None
  273. */
  274. void IP_RTC_SetFullTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime);
  275. /**
  276. * @brief Get full time from the RTC peripheral
  277. * @param pRTC : pointer to RTC peripheral block
  278. * @param pFullTime : Pointer to full time record to fill
  279. * @return None
  280. */
  281. void IP_RTC_GetFullTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime);
  282. /**
  283. * @brief Set alarm time value for a time type
  284. * @param pRTC : pointer to RTC peripheral block
  285. * @param Timetype : Time index field to set
  286. * @param ALValue : Alarm time value to set
  287. * @return None
  288. */
  289. void IP_RTC_SetAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype, uint32_t ALValue);
  290. /**
  291. * @brief Get alarm time value for a time type
  292. * @param pRTC : pointer to RTC peripheral block
  293. * @param Timetype : Time index field to get
  294. * @return Value of Alarm time according to specified time type
  295. */
  296. uint32_t IP_RTC_GetAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype);
  297. /**
  298. * @brief Set full alarm time in the RTC peripheral
  299. * @param pRTC : pointer to RTC peripheral block
  300. * @param pFullTime : Pointer to full time record to set alarm
  301. * @return None
  302. */
  303. void IP_RTC_SetFullAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime);
  304. /**
  305. * @brief Get full alarm time in the RTC peripheral
  306. * @param pRTC : pointer to RTC peripheral block
  307. * @param pFullTime : Pointer to full time record to fill
  308. * @return None
  309. */
  310. void IP_RTC_GetFullAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime);
  311. /**
  312. * @brief Enable/Disable calibration counter in the RTC peripheral
  313. * @param pRTC : pointer to RTC peripheral block
  314. * @param NewState : New State of this function, should be:
  315. * - ENABLE :The calibration counter is enabled and counting
  316. * - DISABLE :The calibration counter is disabled and reset to zero
  317. * @return None
  318. */
  319. void IP_RTC_CalibCounterCmd(IP_RTC_001_T *pRTC, FunctionalState NewState);
  320. /**
  321. * @brief Configures Calibration in the RTC peripheral
  322. * @param pRTC : pointer to RTC peripheral block
  323. * @param CalibValue : Calibration value, should be in range from 0 to 131,072
  324. * @param CalibDir : Calibration Direction, should be:
  325. * - RTC_CALIB_DIR_FORWARD :Forward calibration
  326. * - RTC_CALIB_DIR_BACKWARD :Backward calibration
  327. * @return None
  328. */
  329. void IP_RTC_CalibConfig(IP_RTC_001_T *pRTC, uint32_t CalibValue, uint8_t CalibDir);
  330. /**
  331. * @brief Clear specified Location interrupt pending in the RTC peripheral
  332. * @param pRTC : pointer to RTC peripheral block
  333. * @param IntType : Interrupt location type, should be:
  334. * - RTC_INT_COUNTER_INCREASE :Clear Counter Increment Interrupt pending.
  335. * - RTC_INT_ALARM :Clear alarm interrupt pending
  336. * @return None
  337. */
  338. STATIC INLINE void IP_RTC_ClearIntPending(IP_RTC_001_T *pRTC, uint32_t IntType)
  339. {
  340. pRTC->ILR = IntType;
  341. }
  342. /**
  343. * @brief Check whether if specified location interrupt in the
  344. * RTC peripheral is set or not
  345. * @param pRTC : pointer to RTC peripheral block
  346. * @param IntType : Interrupt location type, should be:
  347. * - RTC_INT_COUNTER_INCREASE: Counter Increment Interrupt block generated an interrupt.
  348. * - RTC_INT_ALARM: Alarm generated an interrupt.
  349. * @return Current state of specified interrupt in RTC peripheral, SET or RESET
  350. */
  351. STATIC INLINE IntStatus IP_RTC_GetIntPending(IP_RTC_001_T *pRTC, uint32_t IntType)
  352. {
  353. return (pRTC->ILR & IntType) ? SET : RESET;
  354. }
  355. /**
  356. * @}
  357. */
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. #endif /* __RTC_001_H_ */