lpc_rtc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**********************************************************************
  2. * $Id$ lpc_rtc.h 2011-06-02
  3. *//**
  4. * @file lpc_rtc.h
  5. * @brief Contains all macro definitions and function prototypes
  6. * support for RTC firmware library on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @defgroup RTC RTC (Real Time Clock)
  34. * @ingroup LPC_CMSIS_FwLib_Drivers
  35. * @{
  36. */
  37. #ifndef __LPC_RTC_H_
  38. #define __LPC_RTC_H_
  39. /* Includes ------------------------------------------------------------------- */
  40. #include "LPC407x_8x_177x_8x.h"
  41. #include "lpc_types.h"
  42. #ifdef __cplusplus
  43. extern "C"
  44. {
  45. #endif
  46. /* Private Macros ------------------------------------------------------------- */
  47. /** @defgroup RTC_Private_Macros RTC Private Macros
  48. * @{
  49. */
  50. /* ----------------------- BIT DEFINITIONS ----------------------------------- */
  51. /* Miscellaneous register group --------------------------------------------- */
  52. /**********************************************************************
  53. * ILR register definitions
  54. **********************************************************************/
  55. /** ILR register mask */
  56. #define RTC_ILR_BITMASK ((0x00000003))
  57. /** Bit inform the source interrupt is counter increment*/
  58. #define RTC_IRL_RTCCIF ((1<<0))
  59. /** Bit inform the source interrupt is alarm match*/
  60. #define RTC_IRL_RTCALF ((1<<1))
  61. /**********************************************************************
  62. * CCR register definitions
  63. **********************************************************************/
  64. /** CCR register mask */
  65. #define RTC_CCR_BITMASK ((0x00000013))
  66. /** Clock enable */
  67. #define RTC_CCR_CLKEN ((1<<0))
  68. /** Clock reset */
  69. #define RTC_CCR_CTCRST ((1<<1))
  70. /** Calibration counter enable */
  71. #define RTC_CCR_CCALEN ((1<<4))
  72. /**********************************************************************
  73. * CIIR register definitions
  74. **********************************************************************/
  75. /** Counter Increment Interrupt bit for second */
  76. #define RTC_CIIR_IMSEC ((1<<0))
  77. /** Counter Increment Interrupt bit for minute */
  78. #define RTC_CIIR_IMMIN ((1<<1))
  79. /** Counter Increment Interrupt bit for hour */
  80. #define RTC_CIIR_IMHOUR ((1<<2))
  81. /** Counter Increment Interrupt bit for day of month */
  82. #define RTC_CIIR_IMDOM ((1<<3))
  83. /** Counter Increment Interrupt bit for day of week */
  84. #define RTC_CIIR_IMDOW ((1<<4))
  85. /** Counter Increment Interrupt bit for day of year */
  86. #define RTC_CIIR_IMDOY ((1<<5))
  87. /** Counter Increment Interrupt bit for month */
  88. #define RTC_CIIR_IMMON ((1<<6))
  89. /** Counter Increment Interrupt bit for year */
  90. #define RTC_CIIR_IMYEAR ((1<<7))
  91. /** CIIR bit mask */
  92. #define RTC_CIIR_BITMASK ((0xFF))
  93. /**********************************************************************
  94. * AMR register definitions
  95. **********************************************************************/
  96. /** Counter Increment Select Mask bit for second */
  97. #define RTC_AMR_AMRSEC ((1<<0))
  98. /** Counter Increment Select Mask bit for minute */
  99. #define RTC_AMR_AMRMIN ((1<<1))
  100. /** Counter Increment Select Mask bit for hour */
  101. #define RTC_AMR_AMRHOUR ((1<<2))
  102. /** Counter Increment Select Mask bit for day of month */
  103. #define RTC_AMR_AMRDOM ((1<<3))
  104. /** Counter Increment Select Mask bit for day of week */
  105. #define RTC_AMR_AMRDOW ((1<<4))
  106. /** Counter Increment Select Mask bit for day of year */
  107. #define RTC_AMR_AMRDOY ((1<<5))
  108. /** Counter Increment Select Mask bit for month */
  109. #define RTC_AMR_AMRMON ((1<<6))
  110. /** Counter Increment Select Mask bit for year */
  111. #define RTC_AMR_AMRYEAR ((1<<7))
  112. /** AMR bit mask */
  113. #define RTC_AMR_BITMASK ((0xFF))
  114. /**********************************************************************
  115. * RTC_AUX register definitions
  116. **********************************************************************/
  117. /** RTC Oscillator Fail detect flag */
  118. #define RTC_AUX_RTC_OSCF ((1<<4))
  119. /**********************************************************************
  120. * RTC_AUXEN register definitions
  121. **********************************************************************/
  122. /** Oscillator Fail Detect interrupt enable*/
  123. #define RTC_AUXEN_RTC_OSCFEN ((1<<4))
  124. /* Consolidated time register group ----------------------------------- */
  125. /**********************************************************************
  126. * Consolidated Time Register 0 definitions
  127. **********************************************************************/
  128. #define RTC_CTIME0_SECONDS_MASK ((0x3F))
  129. #define RTC_CTIME0_MINUTES_MASK ((0x3F00))
  130. #define RTC_CTIME0_HOURS_MASK ((0x1F0000))
  131. #define RTC_CTIME0_DOW_MASK ((0x7000000))
  132. /**********************************************************************
  133. * Consolidated Time Register 1 definitions
  134. **********************************************************************/
  135. #define RTC_CTIME1_DOM_MASK ((0x1F))
  136. #define RTC_CTIME1_MONTH_MASK ((0xF00))
  137. #define RTC_CTIME1_YEAR_MASK ((0xFFF0000))
  138. /**********************************************************************
  139. * Consolidated Time Register 2 definitions
  140. **********************************************************************/
  141. #define RTC_CTIME2_DOY_MASK ((0xFFF))
  142. /**********************************************************************
  143. * Time Counter Group and Alarm register group
  144. **********************************************************************/
  145. /** SEC register mask */
  146. #define RTC_SEC_MASK (0x0000003F)
  147. /** MIN register mask */
  148. #define RTC_MIN_MASK (0x0000003F)
  149. /** HOUR register mask */
  150. #define RTC_HOUR_MASK (0x0000001F)
  151. /** DOM register mask */
  152. #define RTC_DOM_MASK (0x0000001F)
  153. /** DOW register mask */
  154. #define RTC_DOW_MASK (0x00000007)
  155. /** DOY register mask */
  156. #define RTC_DOY_MASK (0x000001FF)
  157. /** MONTH register mask */
  158. #define RTC_MONTH_MASK (0x0000000F)
  159. /** YEAR register mask */
  160. #define RTC_YEAR_MASK (0x00000FFF)
  161. /** Maximum value of second */
  162. #define RTC_SECOND_MAX 59
  163. /** Maximum value of minute*/
  164. #define RTC_MINUTE_MAX 59
  165. /** Maximum value of hour*/
  166. #define RTC_HOUR_MAX 23
  167. /** Minimum value of month*/
  168. #define RTC_MONTH_MIN 1
  169. /** Maximum value of month*/
  170. #define RTC_MONTH_MAX 12
  171. /** Minimum value of day of month*/
  172. #define RTC_DAYOFMONTH_MIN 1
  173. /** Maximum value of day of month*/
  174. #define RTC_DAYOFMONTH_MAX 31
  175. /** Maximum value of day of week*/
  176. #define RTC_DAYOFWEEK_MAX 6
  177. /** Minimum value of day of year*/
  178. #define RTC_DAYOFYEAR_MIN 1
  179. /** Maximum value of day of year*/
  180. #define RTC_DAYOFYEAR_MAX 366
  181. /** Maximum value of year*/
  182. #define RTC_YEAR_MAX 4095
  183. /**********************************************************************
  184. * Calibration register
  185. **********************************************************************/
  186. /** Calibration value */
  187. #define RTC_CALIBRATION_CALVAL_MASK ((0x1FFFF))
  188. /** Calibration direction */
  189. #define RTC_CALIBRATION_LIBDIR ((1<<17))
  190. /** Calibration max value */
  191. #define RTC_CALIBRATION_MAX ((0x20000))
  192. /** Calibration definitions */
  193. #define RTC_CALIB_DIR_FORWARD ((uint8_t)(0))
  194. #define RTC_CALIB_DIR_BACKWARD ((uint8_t)(1))
  195. /**********************************************************************
  196. * Event Monitor/Recorder Control register
  197. **********************************************************************/
  198. #define RTC_ERCTRL_EV0_INTWAKE_ENABLE (1<<0)
  199. #define RTC_ERCTRL_EV0_GPCLEAR_ENABLE (1<<1)
  200. #define RTC_ERCTRL_EV0_POS_EDGE (1<<2)
  201. #define RTC_ERCTRL_EV0_NEG_EDGE (0<<2)
  202. #define RTC_ERCTRL_EV0_INPUT_ENABLE (1<<3)
  203. #define RTC_ERCTRL_EV1_INTWAKE_ENABLE (1<<10)
  204. #define RTC_ERCTRL_EV1_GPCLEAR_ENABLE (1<<11)
  205. #define RTC_ERCTRL_EV1_POS_EDGE (1<<12)
  206. #define RTC_ERCTRL_EV1_NEG_EDGE (0<<12)
  207. #define RTC_ERCTRL_EV1_INPUT_ENABLE (1<<13)
  208. #define RTC_ERCTRL_EV2_INTWAKE_ENABLE (1<<20)
  209. #define RTC_ERCTRL_EV2_GPCLEAR_ENABLE (1<<21)
  210. #define RTC_ERCTRL_EV2_POS_EDGE (1<<22)
  211. #define RTC_ERCTRL_EV2_NEG_EDGE (0<<22)
  212. #define RTC_ERCTRL_EV2_INPUT_ENABLE (1<<23)
  213. #define RTC_ERCTRL_MODE_MASK (((uint32_t)3)<<30)
  214. #define RTC_ERCTRL_MODE_CLK_DISABLE (((uint32_t)0)<<30)
  215. #define RTC_ERCTRL_MODE_16HZ (((uint32_t)1)<<30)
  216. #define RTC_ERCTRL_MODE_64HZ (((uint32_t)2)<<30)
  217. #define RTC_ERCTRL_MODE_1KHZ (((uint32_t)3)<<30)
  218. #define RTC_ER_INPUT_CHANNEL_NUM (3)
  219. /**********************************************************************
  220. * Event Monitor/Recorder Status register
  221. **********************************************************************/
  222. #define RTC_ER_STATUS_EV0_BIT (0)
  223. #define RTC_ER_STATUS_EV1_BIT (1)
  224. #define RTC_ER_STATUS_EV2_BIT (2)
  225. #define RTC_ER_STATUS_GPCLEARED_BIT (3)
  226. #define RTC_ER_STATUS_WAKEUP_BIT (31)
  227. #define RTC_ER_EVENTS_ON_EV0_FLG (1<<RTC_ER_STATUS_EV0_BIT)
  228. #define RTC_ER_EVENTS_ON_EV1_FLG (1<<RTC_ER_STATUS_EV1_BIT)
  229. #define RTC_ER_EVENTS_ON_EV2_FLG (1<<RTC_ER_STATUS_EV2_BIT)
  230. #define RTC_ER_STATUS_GP_CLEARED_FLG (1<<RTC_ER_STATUS_GPCLEARED_BIT)
  231. #define RTC_ER_STATUS_WAKEUP_REQ_PENDING (((uint32_t)1)<<RTC_ER_STATUS_WAKEUP_BIT)
  232. /**********************************************************************
  233. * Event Monitor/Recorder Counter register
  234. **********************************************************************/
  235. #define RTC_ER_EV0_COUNTER(n) (n&0x07)
  236. #define RTC_ER_EV1_COUNTER(n) ((n>>8)&0x07)
  237. #define RTC_ER_EV2_COUNTER(n) ((n>>16)&0x07)
  238. /**********************************************************************
  239. * Event Monitor/Recorder TimeStamp register
  240. **********************************************************************/
  241. #define RTC_ER_TIMESTAMP_SEC(n) (n&0x3F)
  242. #define RTC_ER_TIMESTAMP_MIN(n) ((n>>6)&0x3F)
  243. #define RTC_ER_TIMESTAMP_HOUR(n) ((n>>12)&0x1F)
  244. #define RTC_ER_TIMESTAMP_DOY(n) ((n>>17)&0x1FF)
  245. /**
  246. * @}
  247. */
  248. /* Public Types --------------------------------------------------------------- */
  249. /** @defgroup RTC_Public_Types RTC Public Types
  250. * @{
  251. */
  252. /** @brief Time structure definitions for easy manipulate the data */
  253. typedef struct
  254. {
  255. /** Seconds Register */
  256. uint32_t SEC;
  257. /** Minutes Register */
  258. uint32_t MIN;
  259. /** Hours Register */
  260. uint32_t HOUR;
  261. /** Day of Month Register */
  262. uint32_t DOM;
  263. /** Day of Week Register */
  264. uint32_t DOW;
  265. /** Day of Year Register */
  266. uint32_t DOY;
  267. /** Months Register */
  268. uint32_t MONTH;
  269. /** Years Register */
  270. uint32_t YEAR;
  271. } RTC_TIME_Type;
  272. /** @brief RTC interrupt source */
  273. typedef enum
  274. {
  275. /** Counter Increment Interrupt */
  276. RTC_INT_COUNTER_INCREASE = RTC_IRL_RTCCIF,
  277. /** The alarm interrupt */
  278. RTC_INT_ALARM = RTC_IRL_RTCALF,
  279. } RTC_INT_OPT;
  280. /** @brief RTC time type option */
  281. typedef enum
  282. {
  283. /** Second */
  284. RTC_TIMETYPE_SECOND = 0,
  285. /** Month */
  286. RTC_TIMETYPE_MINUTE = 1,
  287. /** Hour */
  288. RTC_TIMETYPE_HOUR = 2,
  289. /** Day of week */
  290. RTC_TIMETYPE_DAYOFWEEK = 3,
  291. /** Day of month */
  292. RTC_TIMETYPE_DAYOFMONTH = 4,
  293. /** Day of year */
  294. RTC_TIMETYPE_DAYOFYEAR = 5,
  295. /** Month */
  296. RTC_TIMETYPE_MONTH = 6,
  297. /** Year */
  298. RTC_TIMETYPE_YEAR = 7,
  299. } RTC_TIMETYPE_Num;
  300. /** @brief Event Monitor/Recording Input Channel configuration */
  301. typedef struct
  302. {
  303. Bool EventOnPosEdge; // Event occurs on positive edge on the channel
  304. Bool IntWake; // Create interrupt and wake-up request if there is an event
  305. Bool GPClear; // Clear GP registers of RTC if there is an event.
  306. } RTC_ER_CHANNEL_Init_Type;
  307. /** @brief Event Monitor/Recording configuration */
  308. typedef struct
  309. {
  310. RTC_ER_CHANNEL_Init_Type InputChannel[RTC_ER_INPUT_CHANNEL_NUM];
  311. uint32_t Clk; // Sample clock on input channel. (Hz)
  312. } RTC_ER_CONFIG_Type;
  313. /** @brief Event Monitor/Recording TimeStamp Type */
  314. typedef struct
  315. {
  316. /** Seconds Register */
  317. uint32_t SEC;
  318. /** Minutes Register */
  319. uint32_t MIN;
  320. /** Hours Register */
  321. uint32_t HOUR;
  322. /** Day of Year Register */
  323. uint32_t DOY;
  324. } RTC_ER_TIMESTAMP_Type;
  325. /**
  326. * @}
  327. */
  328. /* Public Functions ----------------------------------------------------------- */
  329. /** @defgroup RTC_Public_Functions RTC Public Functions
  330. * @{
  331. */
  332. void RTC_Init (LPC_RTC_TypeDef *RTCx);
  333. void RTC_DeInit(LPC_RTC_TypeDef *RTCx);
  334. void RTC_ResetClockTickCounter(LPC_RTC_TypeDef *RTCx);
  335. void RTC_Cmd (LPC_RTC_TypeDef *RTCx, FunctionalState NewState);
  336. void RTC_CntIncrIntConfig (LPC_RTC_TypeDef *RTCx, uint32_t CntIncrIntType, \
  337. FunctionalState NewState);
  338. void RTC_AlarmIntConfig (LPC_RTC_TypeDef *RTCx, uint32_t AlarmTimeType, \
  339. FunctionalState NewState);
  340. void RTC_SetTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype, uint32_t TimeValue);
  341. uint32_t RTC_GetTime(LPC_RTC_TypeDef *RTCx, uint32_t Timetype);
  342. void RTC_SetFullTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime);
  343. void RTC_GetFullTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime);
  344. void RTC_SetAlarmTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype, uint32_t ALValue);
  345. uint32_t RTC_GetAlarmTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype);
  346. void RTC_SetFullAlarmTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime);
  347. void RTC_GetFullAlarmTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime);
  348. IntStatus RTC_GetIntPending (LPC_RTC_TypeDef *RTCx, uint32_t IntType);
  349. void RTC_ClearIntPending (LPC_RTC_TypeDef *RTCx, uint32_t IntType);
  350. void RTC_CalibCounterCmd(LPC_RTC_TypeDef *RTCx, FunctionalState NewState);
  351. void RTC_CalibConfig(LPC_RTC_TypeDef *RTCx, uint32_t CalibValue, uint8_t CalibDir);
  352. void RTC_WriteGPREG (LPC_RTC_TypeDef *RTCx, uint8_t Channel, uint32_t Value);
  353. uint32_t RTC_ReadGPREG (LPC_RTC_TypeDef *RTCx, uint8_t Channel);
  354. void RTC_ER_InitConfigStruct(RTC_ER_CONFIG_Type* pConfig);
  355. Status RTC_ER_Init(RTC_ER_CONFIG_Type* pConfig);
  356. Status RTC_ER_Cmd(uint8_t channel, FunctionalState state);
  357. uint8_t RTC_ER_GetEventCount(uint8_t channel);
  358. uint32_t RTC_ER_GetStatus(void);
  359. Bool RTC_ER_WakupReqPending(void);
  360. Bool RTC_ER_GPCleared(void);
  361. Status RTC_ER_GetFirstTimeStamp(uint8_t channel, RTC_ER_TIMESTAMP_Type* pTimeStamp);
  362. Status RTC_ER_GetLastTimeStamp(uint8_t channel, RTC_ER_TIMESTAMP_Type* pTimeStamp);
  363. void RTC_ER_ClearStatus(uint32_t status);
  364. /**
  365. * @}
  366. */
  367. #ifdef __cplusplus
  368. }
  369. #endif
  370. #endif /* __LPC_RTC_H_ */
  371. /**
  372. * @}
  373. */
  374. /* --------------------------------- End Of File ------------------------------ */