nu_rtc.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**************************************************************************//**
  2. * @file nu_rtc.h
  3. * @version V3.00
  4. * @brief Real Time Clock(RTC) driver header file
  5. *
  6. * @copyright SPDX-License-Identifier: Apache-2.0
  7. * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_RTC_H__
  10. #define __NU_RTC_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup RTC_Driver RTC Driver
  19. @{
  20. */
  21. /** @addtogroup RTC_EXPORTED_CONSTANTS RTC Exported Constants
  22. @{
  23. */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /* RTC Initial Keyword Constant Definitions */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. #define RTC_INIT_KEY 0xA5EB1357UL /*!< RTC Initiation Key to make RTC leaving reset state \hideinitializer */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* RTC Time Attribute Constant Definitions */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. #define RTC_CLOCK_12 0UL /*!< RTC as 12-hour time scale with AM and PM indication \hideinitializer */
  32. #define RTC_CLOCK_24 1UL /*!< RTC as 24-hour time scale \hideinitializer */
  33. #define RTC_AM 1UL /*!< RTC as AM indication \hideinitializer */
  34. #define RTC_PM 2UL /*!< RTC as PM indication \hideinitializer */
  35. /*---------------------------------------------------------------------------------------------------------*/
  36. /* RTC Tick Period Constant Definitions */
  37. /*---------------------------------------------------------------------------------------------------------*/
  38. #define RTC_TICK_1_SEC 0x0UL /*!< RTC time tick period is 1 second \hideinitializer */
  39. #define RTC_TICK_1_2_SEC 0x1UL /*!< RTC time tick period is 1/2 second \hideinitializer */
  40. #define RTC_TICK_1_4_SEC 0x2UL /*!< RTC time tick period is 1/4 second \hideinitializer */
  41. #define RTC_TICK_1_8_SEC 0x3UL /*!< RTC time tick period is 1/8 second \hideinitializer */
  42. #define RTC_TICK_1_16_SEC 0x4UL /*!< RTC time tick period is 1/16 second \hideinitializer */
  43. #define RTC_TICK_1_32_SEC 0x5UL /*!< RTC time tick period is 1/32 second \hideinitializer */
  44. #define RTC_TICK_1_64_SEC 0x6UL /*!< RTC time tick period is 1/64 second \hideinitializer */
  45. #define RTC_TICK_1_128_SEC 0x7UL /*!< RTC time tick period is 1/128 second \hideinitializer */
  46. /*---------------------------------------------------------------------------------------------------------*/
  47. /* RTC Day of Week Constant Definitions */
  48. /*---------------------------------------------------------------------------------------------------------*/
  49. #define RTC_SUNDAY 0x0UL /*!< Day of the Week is Sunday \hideinitializer */
  50. #define RTC_MONDAY 0x1UL /*!< Day of the Week is Monday \hideinitializer */
  51. #define RTC_TUESDAY 0x2UL /*!< Day of the Week is Tuesday \hideinitializer */
  52. #define RTC_WEDNESDAY 0x3UL /*!< Day of the Week is Wednesday \hideinitializer */
  53. #define RTC_THURSDAY 0x4UL /*!< Day of the Week is Thursday \hideinitializer */
  54. #define RTC_FRIDAY 0x5UL /*!< Day of the Week is Friday \hideinitializer */
  55. #define RTC_SATURDAY 0x6UL /*!< Day of the Week is Saturday \hideinitializer */
  56. /*---------------------------------------------------------------------------------------------------------*/
  57. /* RTC Miscellaneous Constant Definitions */
  58. /*---------------------------------------------------------------------------------------------------------*/
  59. #define RTC_YEAR2000 2000UL /*!< RTC Reference for compute year data \hideinitializer */
  60. #define RTC_FCR_REFERENCE 32752 /*!< RTC Reference for frequency compensation */
  61. /*---------------------------------------------------------------------------------------------------------*/
  62. /* RTC Tamper Constant Definitions */
  63. /*---------------------------------------------------------------------------------------------------------*/
  64. #define RTC_TAMPER0_SELECT (0x1UL << 0) /*!< Select Tamper 0 \hideinitializer */
  65. #define RTC_TAMPER1_SELECT (0x1UL << 1) /*!< Select Tamper 1 \hideinitializer */
  66. #define RTC_TAMPER2_SELECT (0x1UL << 2) /*!< Select Tamper 2 \hideinitializer */
  67. #define RTC_TAMPER3_SELECT (0x1UL << 3) /*!< Select Tamper 3 \hideinitializer */
  68. #define RTC_TAMPER4_SELECT (0x1UL << 4) /*!< Select Tamper 4 \hideinitializer */
  69. #define RTC_TAMPER5_SELECT (0x1UL << 5) /*!< Select Tamper 5 \hideinitializer */
  70. #define RTC_MAX_TAMPER_PIN_NUM 6UL /*!< Tamper Pin number \hideinitializer */
  71. #define RTC_TAMPER_LOW_LEVEL_DETECT 0UL /*!< Tamper pin detect voltage level is low \hideinitializer */
  72. #define RTC_TAMPER_HIGH_LEVEL_DETECT 1UL /*!< Tamper pin detect voltage level is high \hideinitializer */
  73. #define RTC_TAMPER_DEBOUNCE_DISABLE 0UL /*!< Disable RTC tamper pin de-bounce function \hideinitializer */
  74. #define RTC_TAMPER_DEBOUNCE_ENABLE 1UL /*!< Enable RTC tamper pin de-bounce function \hideinitializer */
  75. #define RTC_PAIR0_SELECT (0x1UL << 0) /*!< Select Pair 0 \hideinitializer */
  76. #define RTC_PAIR1_SELECT (0x1UL << 1) /*!< Select Pair 1 \hideinitializer */
  77. #define RTC_PAIR2_SELECT (0x1UL << 2) /*!< Select Pair 2 \hideinitializer */
  78. #define RTC_MAX_PAIR_NUM 3UL /*!< Pair number \hideinitializer */
  79. #define RTC_2POW10_CLK (0x0UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 RTC clock cycles \hideinitializer */
  80. #define RTC_2POW11_CLK (0x1UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 2 RTC clock cycles \hideinitializer */
  81. #define RTC_2POW12_CLK (0x2UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 4 RTC clock cycles \hideinitializer */
  82. #define RTC_2POW13_CLK (0x3UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 6 RTC clock cycles \hideinitializer */
  83. #define RTC_2POW14_CLK (0x4UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 8 RTC clock cycles \hideinitializer */
  84. #define RTC_2POW15_CLK (0x5UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 16 RTC clock cycles \hideinitializer */
  85. #define RTC_2POW16_CLK (0x6UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 32 RTC clock cycles \hideinitializer */
  86. #define RTC_2POW17_CLK (0x7UL << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 64 RTC clock cycles \hideinitializer */
  87. #define RTC_REF_RANDOM_PATTERN 0x0UL /*!< The new reference pattern is generated by random number generator when the reference pattern run out \hideinitializer */
  88. #define RTC_REF_SEED_VALUE 0x1UL /*!< The new reference pattern is repeated from SEED (RTC_TAMPSEED[31:0]) when the reference pattern run out \hideinitializer */
  89. /*---------------------------------------------------------------------------------------------------------*/
  90. /* RTC Clock Source Constant Definitions */
  91. /*---------------------------------------------------------------------------------------------------------*/
  92. #define RTC_CLOCK_SOURCE_LXT 0UL /*!< Set RTC clock source as external LXT \hideinitializer */
  93. #define RTC_CLOCK_SOURCE_LIRC 1UL /*!< Set RTC clock source as LIRC \hideinitializer */
  94. #define RTC_CLOCK_SOURCE_LIRC32K 2UL /*!< Set RTC clock source as LIRC32K \hideinitializer */
  95. /*---------------------------------------------------------------------------------------------------------*/
  96. /* RTC GPIO_MODE Constant Definitions */
  97. /*---------------------------------------------------------------------------------------------------------*/
  98. #define RTC_IO_MODE_INPUT 0x0UL /*!< Input Mode */
  99. #define RTC_IO_MODE_OUTPUT 0x1UL /*!< Output Mode */
  100. #define RTC_IO_MODE_OPEN_DRAIN 0x2UL /*!< Open-Drain Mode */
  101. #define RTC_IO_MODE_QUASI 0x3UL /*!< Quasi-bidirectional Mode */
  102. #define RTC_IO_DIGITAL_ENABLE 0UL /*!< I/O digital path is enabled */
  103. #define RTC_IO_DIGITAL_DISABLE 1UL /*!< I/O digital path is disabled */
  104. #define RTC_IO_PULL_UP_DOWN_DISABLE 0x0UL /*!< I/O pull-up and pull-down is disabled */
  105. #define RTC_IO_PULL_UP_ENABLE 0x1UL /*!< I/O pull-up is enabled */
  106. #define RTC_IO_PULL_DOWN_ENABLE 0x2UL /*!< I/O pull-down is enabled */
  107. /**@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
  108. /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
  109. @{
  110. */
  111. /**
  112. * @details RTC define Time Data Struct
  113. */
  114. typedef struct
  115. {
  116. uint32_t u32Year; /*!< Year value */
  117. uint32_t u32Month; /*!< Month value */
  118. uint32_t u32Day; /*!< Day value */
  119. uint32_t u32DayOfWeek; /*!< Day of week value */
  120. uint32_t u32Hour; /*!< Hour value */
  121. uint32_t u32Minute; /*!< Minute value */
  122. uint32_t u32Second; /*!< Second value */
  123. uint32_t u32TimeScale; /*!< 12-Hour, 24-Hour */
  124. uint32_t u32AmPm; /*!< Only Time Scale select 12-hr used */
  125. } S_RTC_TIME_DATA_T;
  126. /**@}*/ /* end of group RTC_EXPORTED_STRUCTS */
  127. /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
  128. @{
  129. */
  130. /**
  131. * @brief Indicate is Leap Year or not
  132. *
  133. * @param[in] rtc The pointer of RTC module.
  134. *
  135. * @retval 0 This year is not a leap year
  136. * @retval 1 This year is a leap year
  137. *
  138. * @details According to current date, return this year is leap year or not.
  139. * \hideinitializer
  140. */
  141. #define RTC_IS_LEAP_YEAR(rtc) (((rtc)->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk)? 1:0)
  142. /**
  143. * @brief Clear RTC Alarm Interrupt Flag
  144. *
  145. * @param[in] rtc The pointer of RTC module.
  146. *
  147. * @return None
  148. *
  149. * @details This macro is used to clear RTC alarm interrupt flag.
  150. * \hideinitializer
  151. */
  152. #define RTC_CLEAR_ALARM_INT_FLAG(rtc) ((rtc)->INTSTS = RTC_INTSTS_ALMIF_Msk)
  153. /**
  154. * @brief Clear RTC Tick Interrupt Flag
  155. *
  156. * @param[in] rtc The pointer of RTC module.
  157. *
  158. * @return None
  159. *
  160. * @details This macro is used to clear RTC tick interrupt flag.
  161. * \hideinitializer
  162. */
  163. #define RTC_CLEAR_TICK_INT_FLAG(rtc) ((rtc)->INTSTS = RTC_INTSTS_TICKIF_Msk)
  164. /**
  165. * @brief Clear RTC Tamper Interrupt Flag
  166. *
  167. * @param[in] rtc The pointer of RTC module.
  168. * @param[in] u32TamperFlag Tamper interrupt flag. It consists of: \n
  169. * - \ref RTC_INTSTS_TAMP0IF_Msk \n
  170. * - \ref RTC_INTSTS_TAMP1IF_Msk \n
  171. * - \ref RTC_INTSTS_TAMP2IF_Msk \n
  172. * - \ref RTC_INTSTS_TAMP3IF_Msk \n
  173. * - \ref RTC_INTSTS_TAMP4IF_Msk \n
  174. * - \ref RTC_INTSTS_TAMP5IF_Msk
  175. *
  176. * @return None
  177. *
  178. * @details This macro is used to clear RTC tamper pin interrupt flag.
  179. * \hideinitializer
  180. */
  181. #define RTC_CLEAR_TAMPER_INT_FLAG(rtc, u32TamperFlag) ((rtc)->INTSTS = (u32TamperFlag))
  182. /**
  183. * @brief Get RTC Alarm Interrupt Flag
  184. *
  185. * @param[in] rtc The pointer of RTC module.
  186. *
  187. * @retval 0 RTC alarm interrupt did not occur
  188. * @retval 1 RTC alarm interrupt occurred
  189. *
  190. * @details This macro indicates RTC alarm interrupt occurred or not.
  191. * \hideinitializer
  192. */
  193. #define RTC_GET_ALARM_INT_FLAG(rtc) (((rtc)->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
  194. /**
  195. * @brief Get RTC Time Tick Interrupt Flag
  196. *
  197. * @param[in] rtc The pointer of RTC module.
  198. *
  199. * @retval 0 RTC time tick interrupt did not occur
  200. * @retval 1 RTC time tick interrupt occurred
  201. *
  202. * @details This macro indicates RTC time tick interrupt occurred or not.
  203. * \hideinitializer
  204. */
  205. #define RTC_GET_TICK_INT_FLAG(rtc) (((rtc)->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
  206. /**
  207. * @brief Set I/O Control By GPIO
  208. *
  209. * @param[in] rtc The pointer of RTC module.
  210. *
  211. * @return None
  212. *
  213. * @details This macro sets the PF.4~11 pin I/O is controlled by GPIO module.
  214. * \hideinitializer
  215. */
  216. #define RTC_SET_IOCTL_BY_GPIO(rtc) ((rtc)->LXTCTL &= ~RTC_LXTCTL_IOCTLSEL_Msk)
  217. /**
  218. * @brief Set I/O Control By RTC
  219. *
  220. * @param[in] rtc The pointer of RTC module.
  221. *
  222. * @return None
  223. *
  224. * @details This macro sets the PF.4~11 pin I/O is controlled by RTC module.
  225. * \hideinitializer
  226. */
  227. #define RTC_SET_IOCTL_BY_RTC(rtc) ((rtc)->LXTCTL |= RTC_LXTCTL_IOCTLSEL_Msk)
  228. /**
  229. * @brief Get I/O Control Property
  230. *
  231. * @param[in] rtc The pointer of RTC module.
  232. *
  233. * @retval 0 PF.4~11 pin I/O is controlled by GPIO module
  234. * @retval 1 PF.4~11 pin I/O is controlled by RTC module
  235. *
  236. * @details This macro indicates the PF.4~11 pin I/O control property.
  237. * \hideinitializer
  238. */
  239. #define RTC_GET_IOCTL_PROPERTY(rtc) (((rtc)->LXTCTL & RTC_LXTCTL_IOCTLSEL_Msk)? 1:0)
  240. /**
  241. * @brief Get RTC Tamper Interrupt Flag
  242. *
  243. * @param[in] rtc The pointer of RTC module.
  244. *
  245. * @retval 0 RTC tamper event interrupt did not occur
  246. * @retval 1 RTC tamper event interrupt occurred
  247. *
  248. * @details This macro indicates RTC tamper event occurred or not.
  249. * \hideinitializer
  250. */
  251. #define RTC_GET_TAMPER_INT_FLAG(rtc) (((rtc)->INTSTS & (0x3F00))? 1:0)
  252. /**
  253. * @brief Get RTC Tamper Interrupt Status
  254. *
  255. * @param[in] rtc The pointer of RTC module.
  256. *
  257. * @retval RTC_INTSTS_TAMP0IF_Msk Tamper 0 interrupt flag is generated
  258. * @retval RTC_INTSTS_TAMP1IF_Msk Tamper 1 interrupt flag is generated
  259. * @retval RTC_INTSTS_TAMP2IF_Msk Tamper 2 interrupt flag is generated
  260. * @retval RTC_INTSTS_TAMP3IF_Msk Tamper 3 interrupt flag is generated
  261. * @retval RTC_INTSTS_TAMP4IF_Msk Tamper 4 interrupt flag is generated
  262. * @retval RTC_INTSTS_TAMP5IF_Msk Tamper 5 interrupt flag is generated
  263. *
  264. * @details This macro indicates RTC tamper interrupt status.
  265. * \hideinitializer
  266. */
  267. #define RTC_GET_TAMPER_INT_STATUS(rtc) (((rtc)->INTSTS & (0x3F00)))
  268. /**
  269. * @brief Enable RTC Tick Wake-up Function
  270. *
  271. * @param[in] rtc The pointer of RTC module.
  272. *
  273. * @return None
  274. *
  275. * @details This macro is used to enable RTC tick interrupt wake-up function.
  276. * \hideinitializer
  277. */
  278. #define RTC_ENABLE_TICK_WAKEUP(rtc) ((rtc)->INTEN |= RTC_INTEN_TICKIEN_Msk);
  279. /**
  280. * @brief Disable RTC Tick Wake-up Function
  281. *
  282. * @param[in] rtc The pointer of RTC module.
  283. *
  284. * @return None
  285. *
  286. * @details This macro is used to disable RTC tick interrupt wake-up function.
  287. * \hideinitializer
  288. */
  289. #define RTC_DISABLE_TICK_WAKEUP(rtc) ((rtc)->INTEN &= ~RTC_INTEN_TICKIEN_Msk);
  290. /**
  291. * @brief Read Spare Register
  292. *
  293. * @param[in] rtc The pointer of RTC module.
  294. * @param[in] u32RegNum The spare register number, 0~19.
  295. *
  296. * @return Spare register content
  297. *
  298. * @details Read the specify spare register content.
  299. * \hideinitializer
  300. */
  301. #define RTC_READ_SPARE_REGISTER(rtc, u32RegNum) ((rtc)->SPR[(u32RegNum)])
  302. /**
  303. * @brief Write Spare Register
  304. *
  305. * @param[in] rtc The pointer of RTC module.
  306. * @param[in] u32RegNum The spare register number, 0~19.
  307. * @param[in] u32RegValue The spare register value.
  308. *
  309. * @return None
  310. *
  311. * @details Write specify data to spare register.
  312. * \hideinitializer
  313. */
  314. #define RTC_WRITE_SPARE_REGISTER(rtc, u32RegNum, u32RegValue) ((rtc)->SPR[(u32RegNum)] = (u32RegValue))
  315. void RTC_Open(S_RTC_TIME_DATA_T *sPt);
  316. void RTC_Close(void);
  317. void RTC_32KCalibration(int32_t i32FrequencyX10000);
  318. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  319. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  320. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  321. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  322. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
  323. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  324. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
  325. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  326. void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
  327. void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
  328. uint32_t RTC_GetDayOfWeek(void);
  329. void RTC_SetTickPeriod(uint32_t u32TickSelection);
  330. void RTC_EnableInt(uint32_t u32IntFlagMask);
  331. void RTC_DisableInt(uint32_t u32IntFlagMask);
  332. void RTC_EnableSpareAccess(void);
  333. void RTC_DisableSpareRegister(void);
  334. void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn);
  335. void RTC_StaticTamperDisable(uint32_t u32TamperSelect);
  336. void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source);
  337. void RTC_DynamicTamperDisable(uint32_t u32PairSel);
  338. void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed);
  339. uint32_t RTC_SetClockSource(uint32_t u32ClkSrc);
  340. void RTC_SetGPIOMode(uint32_t u32PFPin, uint32_t u32Mode, uint32_t u32DigitalCtl, uint32_t u32PullCtl, uint32_t u32OutputLevel);
  341. void RTC_SetGPIOLevel(uint32_t u32PFPin, uint32_t u32OutputLevel);
  342. /**@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  343. /**@}*/ /* end of group RTC_Driver */
  344. /**@}*/ /* end of group Standard_Driver */
  345. #ifdef __cplusplus
  346. }
  347. #endif
  348. #endif /* __NU_RTC_H__ */