nu_rtc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**************************************************************************//**
  2. * @file nu_rtc.h
  3. * @version V3.00
  4. * @brief M480 series RTC driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-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. #define RTC_WRITE_KEY 0x0000A965UL /*!< RTC Register Access Enable Key to enable RTC read/write accessible and kept 1024 RTC clock \hideinitializer */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. /* RTC Time Attribute Constant Definitions */
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. #define RTC_CLOCK_12 0UL /*!< RTC as 12-hour time scale with AM and PM indication \hideinitializer */
  33. #define RTC_CLOCK_24 1UL /*!< RTC as 24-hour time scale \hideinitializer */
  34. #define RTC_AM 1UL /*!< RTC as AM indication \hideinitializer */
  35. #define RTC_PM 2UL /*!< RTC as PM indication \hideinitializer */
  36. /*---------------------------------------------------------------------------------------------------------*/
  37. /* RTC Tick Period Constant Definitions */
  38. /*---------------------------------------------------------------------------------------------------------*/
  39. #define RTC_TICK_1_SEC 0x0UL /*!< RTC time tick period is 1 second \hideinitializer */
  40. #define RTC_TICK_1_2_SEC 0x1UL /*!< RTC time tick period is 1/2 second \hideinitializer */
  41. #define RTC_TICK_1_4_SEC 0x2UL /*!< RTC time tick period is 1/4 second \hideinitializer */
  42. #define RTC_TICK_1_8_SEC 0x3UL /*!< RTC time tick period is 1/8 second \hideinitializer */
  43. #define RTC_TICK_1_16_SEC 0x4UL /*!< RTC time tick period is 1/16 second \hideinitializer */
  44. #define RTC_TICK_1_32_SEC 0x5UL /*!< RTC time tick period is 1/32 second \hideinitializer */
  45. #define RTC_TICK_1_64_SEC 0x6UL /*!< RTC time tick period is 1/64 second \hideinitializer */
  46. #define RTC_TICK_1_128_SEC 0x7UL /*!< RTC time tick period is 1/128 second \hideinitializer */
  47. /*---------------------------------------------------------------------------------------------------------*/
  48. /* RTC Day of Week Constant Definitions */
  49. /*---------------------------------------------------------------------------------------------------------*/
  50. #define RTC_SUNDAY 0x0UL /*!< Day of the Week is Sunday \hideinitializer */
  51. #define RTC_MONDAY 0x1UL /*!< Day of the Week is Monday \hideinitializer */
  52. #define RTC_TUESDAY 0x2UL /*!< Day of the Week is Tuesday \hideinitializer */
  53. #define RTC_WEDNESDAY 0x3UL /*!< Day of the Week is Wednesday \hideinitializer */
  54. #define RTC_THURSDAY 0x4UL /*!< Day of the Week is Thursday \hideinitializer */
  55. #define RTC_FRIDAY 0x5UL /*!< Day of the Week is Friday \hideinitializer */
  56. #define RTC_SATURDAY 0x6UL /*!< Day of the Week is Saturday \hideinitializer */
  57. /*---------------------------------------------------------------------------------------------------------*/
  58. /* RTC Miscellaneous Constant Definitions */
  59. /*---------------------------------------------------------------------------------------------------------*/
  60. #define RTC_WAIT_COUNT 0xFFFFFFFFUL /*!< Initial Time-out Value \hideinitializer */
  61. #define RTC_YEAR2000 2000UL /*!< RTC Reference for compute year data \hideinitializer */
  62. #define RTC_FCR_REFERENCE 32761UL /*!< RTC Reference for frequency compensation \hideinitializer */
  63. #define RTC_TAMPER0_SELECT (0x1ul << 0) /*!< Select Tamper 0 \hideinitializer */
  64. #define RTC_TAMPER1_SELECT (0x1ul << 1) /*!< Select Tamper 1 \hideinitializer */
  65. #define RTC_TAMPER2_SELECT (0x1ul << 2) /*!< Select Tamper 2 \hideinitializer */
  66. #define RTC_TAMPER3_SELECT (0x1ul << 3) /*!< Select Tamper 3 \hideinitializer */
  67. #define RTC_TAMPER4_SELECT (0x1ul << 4) /*!< Select Tamper 4 \hideinitializer */
  68. #define RTC_TAMPER5_SELECT (0x1ul << 5) /*!< Select Tamper 5 \hideinitializer */
  69. #define MAX_TAMPER_PIN_NUM 6ul /*!< Tamper Pin number \hideinitializer */
  70. #define RTC_TAMPER_HIGH_LEVEL_DETECT 1ul /*!< Tamper pin detect voltage level is high \hideinitializer */
  71. #define RTC_TAMPER_LOW_LEVEL_DETECT 0ul /*!< Tamper pin detect voltage level is low \hideinitializer */
  72. #define RTC_TAMPER_DEBOUNCE_ENABLE 1ul /*!< Enable RTC tamper pin de-bounce function \hideinitializer */
  73. #define RTC_TAMPER_DEBOUNCE_DISABLE 0ul /*!< Disable RTC tamper pin de-bounce function \hideinitializer */
  74. #define RTC_PAIR0_SELECT (0x1ul << 0) /*!< Select Pair 0 \hideinitializer */
  75. #define RTC_PAIR1_SELECT (0x1ul << 1) /*!< Select Pair 1 \hideinitializer */
  76. #define RTC_PAIR2_SELECT (0x1ul << 2) /*!< Select Pair 2 \hideinitializer */
  77. #define MAX_PAIR_NUM 3ul /*!< Pair number \hideinitializer */
  78. #define RTC_2POW10_CLK (0x0 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 RTC clock cycles \hideinitializer */
  79. #define RTC_2POW11_CLK (0x1 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 2 RTC clock cycles \hideinitializer */
  80. #define RTC_2POW12_CLK (0x2 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 4 RTC clock cycles \hideinitializer */
  81. #define RTC_2POW13_CLK (0x3 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 6 RTC clock cycles \hideinitializer */
  82. #define RTC_2POW14_CLK (0x4 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 8 RTC clock cycles \hideinitializer */
  83. #define RTC_2POW15_CLK (0x5 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 16 RTC clock cycles \hideinitializer */
  84. #define RTC_2POW16_CLK (0x6 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 32 RTC clock cycles \hideinitializer */
  85. #define RTC_2POW17_CLK (0x7 << RTC_TAMPCTL_DYNRATE_Pos) /*!< 1024 x 64 RTC clock cycles \hideinitializer */
  86. #define REF_RANDOM_PATTERN 0x0 /*!< The new reference pattern is generated by random number generator when the reference pattern run out \hideinitializer */
  87. #define REF_PREVIOUS_PATTERN 0x1 /*!< The new reference pattern is repeated previous random value when the reference pattern run out \hideinitializer */
  88. #define REF_SEED 0x3 /*!< The new reference pattern is repeated from SEED (RTC_TAMPSEED[31:0]) when the reference pattern run out \hideinitializer */
  89. /*@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
  90. /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
  91. @{
  92. */
  93. /**
  94. * @details RTC define Time Data Struct
  95. */
  96. typedef struct
  97. {
  98. uint32_t u32Year; /*!< Year value */
  99. uint32_t u32Month; /*!< Month value */
  100. uint32_t u32Day; /*!< Day value */
  101. uint32_t u32DayOfWeek; /*!< Day of week value */
  102. uint32_t u32Hour; /*!< Hour value */
  103. uint32_t u32Minute; /*!< Minute value */
  104. uint32_t u32Second; /*!< Second value */
  105. uint32_t u32TimeScale; /*!< 12-Hour, 24-Hour */
  106. uint32_t u32AmPm; /*!< Only Time Scale select 12-hr used */
  107. } S_RTC_TIME_DATA_T;
  108. /*@}*/ /* end of group RTC_EXPORTED_STRUCTS */
  109. /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
  110. @{
  111. */
  112. /**
  113. * @brief Indicate is Leap Year or not
  114. *
  115. * @param None
  116. *
  117. * @retval 0 This year is not a leap year
  118. * @retval 1 This year is a leap year
  119. *
  120. * @details According to current date, return this year is leap year or not.
  121. * \hideinitializer
  122. */
  123. #define RTC_IS_LEAP_YEAR() (RTC->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk ? 1:0)
  124. /**
  125. * @brief Clear RTC Alarm Interrupt Flag
  126. *
  127. * @param None
  128. *
  129. * @return None
  130. *
  131. * @details This macro is used to clear RTC alarm interrupt flag.
  132. * \hideinitializer
  133. */
  134. #define RTC_CLEAR_ALARM_INT_FLAG() (RTC->INTSTS = RTC_INTSTS_ALMIF_Msk)
  135. /**
  136. * @brief Clear RTC Tick Interrupt Flag
  137. *
  138. * @param None
  139. *
  140. * @return None
  141. *
  142. * @details This macro is used to clear RTC tick interrupt flag.
  143. * \hideinitializer
  144. */
  145. #define RTC_CLEAR_TICK_INT_FLAG() (RTC->INTSTS = RTC_INTSTS_TICKIF_Msk)
  146. /**
  147. * @brief Clear RTC Tamper Interrupt Flag
  148. *
  149. * @param u32TamperFlag Tamper interrupt flag. It consists of: \n
  150. * - \ref RTC_INTSTS_TAMP0IF_Msk \n
  151. * - \ref RTC_INTSTS_TAMP1IF_Msk \n
  152. * - \ref RTC_INTSTS_TAMP2IF_Msk \n
  153. * - \ref RTC_INTSTS_TAMP3IF_Msk \n
  154. * - \ref RTC_INTSTS_TAMP4IF_Msk \n
  155. * - \ref RTC_INTSTS_TAMP5IF_Msk
  156. *
  157. * @return None
  158. *
  159. * @details This macro is used to clear RTC snooper pin interrupt flag.
  160. * \hideinitializer
  161. */
  162. #define RTC_CLEAR_TAMPER_INT_FLAG(u32TamperFlag) (RTC->INTSTS = (u32TamperFlag))
  163. /**
  164. * @brief Get RTC Alarm Interrupt Flag
  165. *
  166. * @param None
  167. *
  168. * @retval 0 RTC alarm interrupt did not occur
  169. * @retval 1 RTC alarm interrupt occurred
  170. *
  171. * @details This macro indicates RTC alarm interrupt occurred or not.
  172. * \hideinitializer
  173. */
  174. #define RTC_GET_ALARM_INT_FLAG() ((RTC->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
  175. /**
  176. * @brief Get RTC Time Tick Interrupt Flag
  177. *
  178. * @param None
  179. *
  180. * @retval 0 RTC time tick interrupt did not occur
  181. * @retval 1 RTC time tick interrupt occurred
  182. *
  183. * @details This macro indicates RTC time tick interrupt occurred or not.
  184. * \hideinitializer
  185. */
  186. #define RTC_GET_TICK_INT_FLAG() ((RTC->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
  187. /**
  188. * @brief Get RTC Tamper Interrupt Flag
  189. *
  190. * @param None
  191. *
  192. * @retval 0 RTC snooper pin interrupt did not occur
  193. * @retval 1 RTC snooper pin interrupt occurred
  194. *
  195. * @details This macro indicates RTC snooper pin interrupt occurred or not.
  196. * \hideinitializer
  197. */
  198. #define RTC_GET_TAMPER_INT_FLAG() ((RTC->INTSTS & (0x3F00))? 1:0)
  199. /**
  200. * @brief Get RTC TAMPER Interrupt Status
  201. *
  202. * @param None
  203. *
  204. * @retval RTC_INTSTS_TAMP0IF_Msk Tamper 0 interrupt flag is generated
  205. * @retval RTC_INTSTS_TAMP1IF_Msk Tamper 1 interrupt flag is generated
  206. * @retval RTC_INTSTS_TAMP2IF_Msk Tamper 2 interrupt flag is generated
  207. * @retval RTC_INTSTS_TAMP3IF_Msk Tamper 3 interrupt flag is generated
  208. * @retval RTC_INTSTS_TAMP4IF_Msk Tamper 4 interrupt flag is generated
  209. * @retval RTC_INTSTS_TAMP5IF_Msk Tamper 5 interrupt flag is generated
  210. *
  211. * @details This macro indicates RTC snooper pin interrupt occurred or not.
  212. * \hideinitializer
  213. */
  214. #define RTC_GET_TAMPER_INT_STATUS() ((RTC->INTSTS & (0x3F00)))
  215. /**
  216. * @brief Read Spare Register
  217. *
  218. * @param[in] u32RegNum The spare register number, 0~19.
  219. *
  220. * @return Spare register content
  221. *
  222. * @details Read the specify spare register content.
  223. * @note The returned value is valid only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
  224. * And its controlled by RTC Access Enable Register.
  225. * \hideinitializer
  226. */
  227. #define RTC_READ_SPARE_REGISTER(u32RegNum) (RTC->SPR[(u32RegNum)])
  228. /**
  229. * @brief Write Spare Register
  230. *
  231. * @param[in] u32RegNum The spare register number, 0~19.
  232. * @param[in] u32RegValue The spare register value.
  233. *
  234. * @return None
  235. *
  236. * @details Write specify data to spare register.
  237. * @note This macro is effect only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
  238. * And its controlled by RTC Access Enable Register(RTC_RWEN).
  239. * \hideinitializer
  240. */
  241. #define RTC_WRITE_SPARE_REGISTER(u32RegNum, u32RegValue) (RTC->SPR[(u32RegNum)] = (u32RegValue))
  242. /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
  243. __STATIC_INLINE void RTC_WaitAccessEnable(void);
  244. /**
  245. * @brief Wait RTC Access Enable
  246. *
  247. * @param None
  248. *
  249. * @return None
  250. *
  251. * @details This function is used to enable the maximum RTC read/write accessible time.
  252. */
  253. __STATIC_INLINE void RTC_WaitAccessEnable(void)
  254. {
  255. while((RTC->RWEN & RTC_RWEN_RTCBUSY_Msk) == RTC_RWEN_RTCBUSY_Msk)
  256. {
  257. }
  258. if(!(SYS->CSERVER & 0x1))
  259. {
  260. /* To wait RWENF bit is cleared and enable RWENF bit (Access Enable bit) again */
  261. RTC->RWEN = RTC_WRITE_KEY;
  262. }
  263. /* To wait RWENF bit is set and user can access the protected-register of RTC from now on */
  264. while((RTC->RWEN & RTC_RWEN_RWENF_Msk) == (uint32_t)0x0)
  265. {
  266. }
  267. }
  268. void RTC_Open(S_RTC_TIME_DATA_T *sPt);
  269. void RTC_Close(void);
  270. void RTC_32KCalibration(int32_t i32FrequencyX10000);
  271. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  272. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  273. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  274. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  275. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
  276. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  277. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
  278. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  279. void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
  280. void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
  281. uint32_t RTC_GetDayOfWeek(void);
  282. void RTC_SetTickPeriod(uint32_t u32TickSelection);
  283. void RTC_EnableInt(uint32_t u32IntFlagMask);
  284. void RTC_DisableInt(uint32_t u32IntFlagMask);
  285. void RTC_EnableSpareAccess(void);
  286. void RTC_DisableSpareRegister(void);
  287. void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn);
  288. void RTC_StaticTamperDisable(uint32_t u32TamperSelect);
  289. void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source);
  290. void RTC_DynamicTamperDisable(uint32_t u32PairSel);
  291. void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed);
  292. /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  293. /*@}*/ /* end of group RTC_Driver */
  294. /*@}*/ /* end of group Standard_Driver */
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. #endif /* __NU_RTC_H__ */
  299. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/