rtc.h 15 KB

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