nu_rtc.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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) 2021 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. /*---------------------------------------------------------------------------------------------------------*/
  95. /* RTC GPIO_MODE Constant Definitions */
  96. /*---------------------------------------------------------------------------------------------------------*/
  97. #define RTC_IO_MODE_INPUT 0x0UL /*!< Input Mode */
  98. #define RTC_IO_MODE_OUTPUT 0x1UL /*!< Output Mode */
  99. #define RTC_IO_MODE_OPEN_DRAIN 0x2UL /*!< Open-Drain Mode */
  100. #define RTC_IO_MODE_QUASI 0x3UL /*!< Quasi-bidirectional Mode */
  101. #define RTC_IO_DIGITAL_ENABLE 0UL /*!< I/O digital path is enabled */
  102. #define RTC_IO_DIGITAL_DISABLE 1UL /*!< I/O digital path is disabled */
  103. #define RTC_IO_PULL_UP_DOWN_DISABLE 0x0UL /*!< I/O pull-up and pull-down is disabled */
  104. #define RTC_IO_PULL_UP_ENABLE 0x1UL /*!< I/O pull-up is enabled */
  105. #define RTC_IO_PULL_DOWN_ENABLE 0x2UL /*!< I/O pull-down is enabled */
  106. /*---------------------------------------------------------------------------------------------------------*/
  107. /* RTC Time-out Handler Constant Definitions */
  108. /*---------------------------------------------------------------------------------------------------------*/
  109. #define RTC_TIMEOUT_ERR (-1L) /*!< RTC operation abort due to timeout error \hideinitializer */
  110. /**@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
  111. /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
  112. @{
  113. */
  114. /**
  115. * @details RTC define Time Data Struct
  116. */
  117. typedef struct
  118. {
  119. uint32_t u32Year; /*!< Year value */
  120. uint32_t u32Month; /*!< Month value */
  121. uint32_t u32Day; /*!< Day value */
  122. uint32_t u32DayOfWeek; /*!< Day of week value */
  123. uint32_t u32Hour; /*!< Hour value */
  124. uint32_t u32Minute; /*!< Minute value */
  125. uint32_t u32Second; /*!< Second value */
  126. uint32_t u32TimeScale; /*!< 12-Hour, 24-Hour */
  127. uint32_t u32AmPm; /*!< Only Time Scale select 12-hr used */
  128. } S_RTC_TIME_DATA_T;
  129. /**@}*/ /* end of group RTC_EXPORTED_STRUCTS */
  130. extern int32_t g_RTC_i32ErrCode;
  131. /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
  132. @{
  133. */
  134. /**
  135. * @brief Indicate is Leap Year or not
  136. *
  137. * @param None
  138. *
  139. * @retval 0 This year is not a leap year
  140. * @retval 1 This year is a leap year
  141. *
  142. * @details According to current date, return this year is leap year or not.
  143. * \hideinitializer
  144. */
  145. #define RTC_IS_LEAP_YEAR() ((RTC->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk)? 1:0)
  146. /**
  147. * @brief Clear RTC Alarm Interrupt Flag
  148. *
  149. * @param None
  150. *
  151. * @return None
  152. *
  153. * @details This macro is used to clear RTC alarm interrupt flag.
  154. * \hideinitializer
  155. */
  156. #define RTC_CLEAR_ALARM_INT_FLAG() (RTC->INTSTS = RTC_INTSTS_ALMIF_Msk)
  157. /**
  158. * @brief Clear RTC Tick Interrupt Flag
  159. *
  160. * @param None
  161. *
  162. * @return None
  163. *
  164. * @details This macro is used to clear RTC tick interrupt flag.
  165. * \hideinitializer
  166. */
  167. #define RTC_CLEAR_TICK_INT_FLAG() (RTC->INTSTS = RTC_INTSTS_TICKIF_Msk)
  168. /**
  169. * @brief Clear RTC Tamper Interrupt Flag
  170. *
  171. * @param u32TamperFlag Tamper interrupt flag. It consists of: \n
  172. * - \ref RTC_INTSTS_TAMP0IF_Msk \n
  173. * - \ref RTC_INTSTS_TAMP1IF_Msk \n
  174. * - \ref RTC_INTSTS_TAMP2IF_Msk \n
  175. * - \ref RTC_INTSTS_TAMP3IF_Msk \n
  176. * - \ref RTC_INTSTS_TAMP4IF_Msk \n
  177. * - \ref RTC_INTSTS_TAMP5IF_Msk
  178. *
  179. * @return None
  180. *
  181. * @details This macro is used to clear RTC snooper pin interrupt flag.
  182. * \hideinitializer
  183. */
  184. #define RTC_CLEAR_TAMPER_INT_FLAG(u32TamperFlag) (RTC->INTSTS = (u32TamperFlag))
  185. /**
  186. * @brief Get RTC Alarm Interrupt Flag
  187. *
  188. * @param None
  189. *
  190. * @retval 0 RTC alarm interrupt did not occur
  191. * @retval 1 RTC alarm interrupt occurred
  192. *
  193. * @details This macro indicates RTC alarm interrupt occurred or not.
  194. * \hideinitializer
  195. */
  196. #define RTC_GET_ALARM_INT_FLAG() ((RTC->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
  197. /**
  198. * @brief Get RTC Time Tick Interrupt Flag
  199. *
  200. * @param None
  201. *
  202. * @retval 0 RTC time tick interrupt did not occur
  203. * @retval 1 RTC time tick interrupt occurred
  204. *
  205. * @details This macro indicates RTC time tick interrupt occurred or not.
  206. * \hideinitializer
  207. */
  208. #define RTC_GET_TICK_INT_FLAG() ((RTC->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
  209. /**
  210. * @brief Set I/O Control By GPIO Module
  211. *
  212. * @param None
  213. *
  214. * @return None
  215. *
  216. * @details This macro sets the PF.4~11 pin I/O is controlled by GPIO module.
  217. * \hideinitializer
  218. */
  219. #define RTC_SET_IOCTL_BY_GPIO() (RTC->LXTCTL &= ~RTC_LXTCTL_IOCTLSEL_Msk)
  220. /**
  221. * @brief Set I/O Control By RTC Module
  222. *
  223. * @param None
  224. *
  225. * @return None
  226. *
  227. * @details This macro sets the PF.4~11 pin I/O is controlled by RTC module.
  228. * \hideinitializer
  229. */
  230. #define RTC_SET_IOCTL_BY_RTC() (RTC->LXTCTL |= RTC_LXTCTL_IOCTLSEL_Msk)
  231. /**
  232. * @brief Get I/O Control Property
  233. *
  234. * @param None
  235. *
  236. * @retval 0 PF.4~11 pin I/O is controlled by GPIO module
  237. * @retval 1 PF.4~11 pin I/O is controlled by RTC module
  238. *
  239. * @details This macro indicates the PF.4~11 pin I/O control property.
  240. * \hideinitializer
  241. */
  242. #define RTC_GET_IOCTL_PROPERTY() ((RTC->LXTCTL & RTC_LXTCTL_IOCTLSEL_Msk)? 1:0)
  243. /**
  244. * @brief Get RTC Tamper Interrupt Flag
  245. *
  246. * @param None
  247. *
  248. * @retval 0 RTC tamper event interrupt did not occur
  249. * @retval 1 RTC tamper event interrupt occurred
  250. *
  251. * @details This macro indicates RTC tamper event occurred or not.
  252. * \hideinitializer
  253. */
  254. #define RTC_GET_TAMPER_INT_FLAG() ((RTC->INTSTS & (0x3F00))? 1:0)
  255. /**
  256. * @brief Get RTC Tamper Interrupt Status
  257. *
  258. * @param None
  259. *
  260. * @retval RTC_INTSTS_TAMP0IF_Msk Tamper 0 interrupt flag is generated
  261. * @retval RTC_INTSTS_TAMP1IF_Msk Tamper 1 interrupt flag is generated
  262. * @retval RTC_INTSTS_TAMP2IF_Msk Tamper 2 interrupt flag is generated
  263. * @retval RTC_INTSTS_TAMP3IF_Msk Tamper 3 interrupt flag is generated
  264. * @retval RTC_INTSTS_TAMP4IF_Msk Tamper 4 interrupt flag is generated
  265. * @retval RTC_INTSTS_TAMP5IF_Msk Tamper 5 interrupt flag is generated
  266. *
  267. * @details This macro indicates RTC tamper interrupt status.
  268. * \hideinitializer
  269. */
  270. #define RTC_GET_TAMPER_INT_STATUS() ((RTC->INTSTS & (0x3F00)))
  271. /**
  272. * @brief Enable RTC Tick Wake-up Function
  273. *
  274. * @param None
  275. *
  276. * @return None
  277. *
  278. * @details This macro is used to enable RTC tick interrupt wake-up function.
  279. * \hideinitializer
  280. */
  281. #define RTC_ENABLE_TICK_WAKEUP() (RTC->INTEN |= RTC_INTEN_TICKIEN_Msk);
  282. /**
  283. * @brief Disable RTC Tick Wake-up Function
  284. *
  285. * @param[in] rtc The pointer of RTC module.
  286. *
  287. * @return None
  288. *
  289. * @details This macro is used to disable RTC tick interrupt wake-up function.
  290. * \hideinitializer
  291. */
  292. #define RTC_DISABLE_TICK_WAKEUP(rtc) ((rtc)->INTEN &= ~RTC_INTEN_TICKIEN_Msk);
  293. /**
  294. * @brief Read Spare Register
  295. *
  296. * @param[in] rtc The pointer of RTC module.
  297. * @param[in] u32RegNum The spare register number, 0~19.
  298. *
  299. * @return Spare register content
  300. *
  301. * @details Read the specify spare register content.
  302. * \hideinitializer
  303. */
  304. #define RTC_READ_SPARE_REGISTER(u32RegNum) (RTC->SPR[(u32RegNum)])
  305. /**
  306. * @brief Write Spare Register
  307. *
  308. * @param[in] u32RegNum The spare register number, 0~19.
  309. * @param[in] u32RegValue The spare register value.
  310. *
  311. * @return None
  312. *
  313. * @details Write specify data to spare register.
  314. * \hideinitializer
  315. */
  316. #define RTC_WRITE_SPARE_REGISTER(u32RegNum, u32RegValue) (RTC->SPR[(u32RegNum)] = (u32RegValue))
  317. int32_t RTC_Open(S_RTC_TIME_DATA_T *sPt);
  318. void RTC_Close(void);
  319. void RTC_32KCalibration(int32_t i32FrequencyX10000);
  320. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  321. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  322. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  323. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  324. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
  325. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  326. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
  327. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  328. void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
  329. void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
  330. uint32_t RTC_GetDayOfWeek(void);
  331. void RTC_SetTickPeriod(uint32_t u32TickSelection);
  332. void RTC_EnableInt(uint32_t u32IntFlagMask);
  333. void RTC_DisableInt(uint32_t u32IntFlagMask);
  334. void RTC_EnableSpareAccess(void);
  335. void RTC_DisableSpareRegister(void);
  336. void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn);
  337. void RTC_StaticTamperDisable(uint32_t u32TamperSelect);
  338. void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source);
  339. void RTC_DynamicTamperDisable(uint32_t u32PairSel);
  340. void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed);
  341. uint32_t RTC_SetClockSource(uint32_t u32ClkSrc);
  342. void RTC_SetGPIOMode(uint32_t u32PFPin, uint32_t u32Mode, uint32_t u32DigitalCtl, uint32_t u32PullCtl, uint32_t u32OutputLevel);
  343. void RTC_SetGPIOLevel(uint32_t u32PFPin, uint32_t u32OutputLevel);
  344. /**@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  345. /**@}*/ /* end of group RTC_Driver */
  346. /**@}*/ /* end of group Standard_Driver */
  347. #ifdef __cplusplus
  348. }
  349. #endif
  350. #endif /* __NU_RTC_H__ */