nu_rtc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /******************************************************************************
  2. * @file nu_rtc.h
  3. * @version V1.00
  4. * $Revision: 4 $
  5. * $Date: 18/06/07 2:32p $
  6. * @brief M031 series Real Time Clock(RTC) driver header file
  7. *
  8. * @note
  9. * SPDX-License-Identifier: Apache-2.0
  10. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  11. *****************************************************************************/
  12. #ifndef __NU_RTC_H__
  13. #define __NU_RTC_H__
  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif
  18. /** @addtogroup Standard_Driver Standard Driver
  19. @{
  20. */
  21. /** @addtogroup RTC_Driver RTC Driver
  22. @{
  23. */
  24. /** @addtogroup RTC_EXPORTED_CONSTANTS RTC Exported Constants
  25. @{
  26. */
  27. /*---------------------------------------------------------------------------------------------------------*/
  28. /* RTC Initial Keyword Constant Definitions */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. #define RTC_INIT_KEY 0xA5EB1357UL /*!< RTC Initiation Key to make RTC leaving reset state \hideinitializer */
  31. #define RTC_WRITE_KEY 0x0000A965UL /*!< RTC Register Access Enable Key to enable RTC read/write accessible and kept 1024 RTC clock \hideinitializer */
  32. /*---------------------------------------------------------------------------------------------------------*/
  33. /* RTC Frequency Compensation Definitions */
  34. /*---------------------------------------------------------------------------------------------------------*/
  35. #define RTC_INTEGER_32752 (0x0ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32752HZ \hideinitializer */
  36. #define RTC_INTEGER_32753 (0x1ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32753HZ \hideinitializer */
  37. #define RTC_INTEGER_32754 (0x2ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32754HZ \hideinitializer */
  38. #define RTC_INTEGER_32755 (0x3ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32755HZ \hideinitializer */
  39. #define RTC_INTEGER_32756 (0x4ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32756HZ \hideinitializer */
  40. #define RTC_INTEGER_32757 (0x5ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32757HZ \hideinitializer */
  41. #define RTC_INTEGER_32758 (0x6ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32758HZ \hideinitializer */
  42. #define RTC_INTEGER_32759 (0x7ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32759HZ \hideinitializer */
  43. #define RTC_INTEGER_32760 (0x8ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32760HZ \hideinitializer */
  44. #define RTC_INTEGER_32761 (0x9ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32761HZ \hideinitializer */
  45. #define RTC_INTEGER_32762 (0xaul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32762HZ \hideinitializer */
  46. #define RTC_INTEGER_32763 (0xbul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32763HZ \hideinitializer */
  47. #define RTC_INTEGER_32764 (0xcul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32764HZ \hideinitializer */
  48. #define RTC_INTEGER_32765 (0xdul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32765HZ \hideinitializer */
  49. #define RTC_INTEGER_32766 (0xeul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32766HZ \hideinitializer */
  50. #define RTC_INTEGER_32767 (0xful << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32767HZ \hideinitializer */
  51. #define RTC_INTEGER_32768 (0x10ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32768HZ \hideinitializer */
  52. #define RTC_INTEGER_32769 (0x11ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32769HZ \hideinitializer */
  53. #define RTC_INTEGER_32770 (0x12ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32770HZ \hideinitializer */
  54. #define RTC_INTEGER_32771 (0x13ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32771HZ \hideinitializer */
  55. #define RTC_INTEGER_32772 (0x14ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32772HZ \hideinitializer */
  56. #define RTC_INTEGER_32773 (0x15ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32773HZ \hideinitializer */
  57. #define RTC_INTEGER_32774 (0x16ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32774HZ \hideinitializer */
  58. #define RTC_INTEGER_32775 (0x17ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32775HZ \hideinitializer */
  59. #define RTC_INTEGER_32776 (0x18ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32776HZ \hideinitializer */
  60. #define RTC_INTEGER_32777 (0x19ul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32777HZ \hideinitializer */
  61. #define RTC_INTEGER_32778 (0x1aul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32778HZ \hideinitializer */
  62. #define RTC_INTEGER_32779 (0x1bul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32779HZ \hideinitializer */
  63. #define RTC_INTEGER_32780 (0x1cul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32780HZ \hideinitializer */
  64. #define RTC_INTEGER_32781 (0x1dul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32781HZ \hideinitializer */
  65. #define RTC_INTEGER_32782 (0x1eul << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32782HZ \hideinitializer */
  66. #define RTC_INTEGER_32783 (0x1ful << RTC_FREQADJ_INTEGER_Pos ) /*!< RTC Frequency is 32783HZ \hideinitializer */
  67. /*---------------------------------------------------------------------------------------------------------*/
  68. /* RTC Time Attribute Constant Definitions */
  69. /*---------------------------------------------------------------------------------------------------------*/
  70. #define RTC_CLOCK_12 0UL /*!< RTC as 12-hour time scale with AM and PM indication \hideinitializer */
  71. #define RTC_CLOCK_24 1UL /*!< RTC as 24-hour time scale \hideinitializer */
  72. #define RTC_AM 1UL /*!< RTC as AM indication \hideinitializer */
  73. #define RTC_PM 2UL /*!< RTC as PM indication \hideinitializer */
  74. /*---------------------------------------------------------------------------------------------------------*/
  75. /* RTC Tick Period Constant Definitions */
  76. /*---------------------------------------------------------------------------------------------------------*/
  77. #define RTC_TICK_1_SEC 0x0UL /*!< RTC time tick period is 1 second \hideinitializer */
  78. #define RTC_TICK_1_2_SEC 0x1UL /*!< RTC time tick period is 1/2 second \hideinitializer */
  79. #define RTC_TICK_1_4_SEC 0x2UL /*!< RTC time tick period is 1/4 second \hideinitializer */
  80. #define RTC_TICK_1_8_SEC 0x3UL /*!< RTC time tick period is 1/8 second \hideinitializer */
  81. #define RTC_TICK_1_16_SEC 0x4UL /*!< RTC time tick period is 1/16 second \hideinitializer */
  82. #define RTC_TICK_1_32_SEC 0x5UL /*!< RTC time tick period is 1/32 second \hideinitializer */
  83. #define RTC_TICK_1_64_SEC 0x6UL /*!< RTC time tick period is 1/64 second \hideinitializer */
  84. #define RTC_TICK_1_128_SEC 0x7UL /*!< RTC time tick period is 1/128 second \hideinitializer */
  85. /*---------------------------------------------------------------------------------------------------------*/
  86. /* RTC Day of Week Constant Definitions */
  87. /*---------------------------------------------------------------------------------------------------------*/
  88. #define RTC_SUNDAY 0x0UL /*!< Day of the Week is Sunday \hideinitializer */
  89. #define RTC_MONDAY 0x1UL /*!< Day of the Week is Monday \hideinitializer */
  90. #define RTC_TUESDAY 0x2UL /*!< Day of the Week is Tuesday \hideinitializer */
  91. #define RTC_WEDNESDAY 0x3UL /*!< Day of the Week is Wednesday \hideinitializer */
  92. #define RTC_THURSDAY 0x4UL /*!< Day of the Week is Thursday \hideinitializer */
  93. #define RTC_FRIDAY 0x5UL /*!< Day of the Week is Friday \hideinitializer */
  94. #define RTC_SATURDAY 0x6UL /*!< Day of the Week is Saturday \hideinitializer */
  95. /*---------------------------------------------------------------------------------------------------------*/
  96. /* RTC Miscellaneous Constant Definitions */
  97. /*---------------------------------------------------------------------------------------------------------*/
  98. #define RTC_YEAR2000 2000UL /*!< RTC Reference for compute year data \hideinitializer */
  99. #define RTC_FCR_REFERENCE 32761UL /*!< RTC Reference for frequency compensation \hideinitializer */
  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /* RTC Clock Source Constant Definitions */
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. #define RTC_CLKSRC_LXT 0x0UL /*!< Clock Source from LXT \hideinitializer */
  104. #define RTC_CLKSRC_LIRC 0x1UL /*!< Clock Source from LIRC \hideinitializer */
  105. /*@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
  106. /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
  107. @{
  108. */
  109. /**
  110. * @details RTC define Time Data Struct
  111. */
  112. typedef struct
  113. {
  114. uint32_t u32Year; /*!< Year value */
  115. uint32_t u32Month; /*!< Month value */
  116. uint32_t u32Day; /*!< Day value */
  117. uint32_t u32DayOfWeek; /*!< Day of week value */
  118. uint32_t u32Hour; /*!< Hour value */
  119. uint32_t u32Minute; /*!< Minute value */
  120. uint32_t u32Second; /*!< Second value */
  121. uint32_t u32TimeScale; /*!< 12-Hour, 24-Hour */
  122. uint32_t u32AmPm; /*!< Only Time Scale select 12-hr used */
  123. } S_RTC_TIME_DATA_T;
  124. /*@}*/ /* end of group RTC_EXPORTED_STRUCTS */
  125. /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
  126. @{
  127. */
  128. /**
  129. * @brief Indicate is Leap Year or not
  130. *
  131. * @param None
  132. *
  133. * @retval 0 This year is not a leap year
  134. * @retval 1 This year is a leap year
  135. *
  136. * @details According to current date, return this year is leap year or not.
  137. * \hideinitializer
  138. */
  139. #define RTC_IS_LEAP_YEAR() (RTC->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk ? 1:0)
  140. /**
  141. * @brief Clear RTC Alarm Interrupt Flag
  142. *
  143. * @param None
  144. *
  145. * @return None
  146. *
  147. * @details This macro is used to clear RTC alarm interrupt flag.
  148. * \hideinitializer
  149. */
  150. #define RTC_CLEAR_ALARM_INT_FLAG() (RTC->INTSTS = RTC_INTSTS_ALMIF_Msk)
  151. /**
  152. * @brief Clear RTC Tick Interrupt Flag
  153. *
  154. * @param None
  155. *
  156. * @return None
  157. *
  158. * @details This macro is used to clear RTC tick interrupt flag.
  159. * \hideinitializer
  160. */
  161. #define RTC_CLEAR_TICK_INT_FLAG() (RTC->INTSTS = RTC_INTSTS_TICKIF_Msk)
  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 Enable RTC Tick Wake-up Function
  188. *
  189. * @param None
  190. *
  191. * @return None
  192. *
  193. * @details This macro is used to enable RTC tick interrupt wake-up function.
  194. * \hideinitializer
  195. */
  196. #define RTC_ENABLE_TICK_WAKEUP() ((RTC->INTEN |= RTC_INTEN_TICKIEN_Msk))
  197. /**
  198. * @brief Disable RTC Tick Wake-up Function
  199. *
  200. * @param None
  201. *
  202. * @return None
  203. *
  204. * @details This macro is used to disable RTC tick interrupt wake-up function.
  205. * \hideinitializer
  206. */
  207. #define RTC_DISABLE_TICK_WAKEUP() ((RTC->INTEN &= ~RTC_INTEN_TICKIEN_Msk));
  208. /**
  209. * @brief Enable RTC Alarm Wake-up Function
  210. *
  211. * @param None
  212. *
  213. * @return None
  214. *
  215. * @details This macro is used to enable RTC Alarm interrupt wake-up function.
  216. * \hideinitializer
  217. */
  218. #define RTC_ENABLE_ALARM_WAKEUP() ((RTC->INTEN |= RTC_INTEN_ALMIEN_Msk))
  219. /**
  220. * @brief Disable RTC Alarm Wake-up Function
  221. *
  222. * @param None
  223. *
  224. * @return None
  225. *
  226. * @details This macro is used to disable RTC Alarm interrupt wake-up function.
  227. * \hideinitializer
  228. */
  229. #define RTC_DISABLE_ALARM_WAKEUP() ((RTC->INTEN &= ~RTC_INTEN_ALMIEN_Msk));
  230. /**
  231. * @brief Select RTC Clock Source
  232. *
  233. * @param[in] u32ClkSrc Specify the clock source. It consists of:
  234. * - \ref RTC_CLKSRC_LXT : Clock source from LXT
  235. * - \ref RTC_CLKSRC_LIRC : Clock source from LIRC
  236. * @return None
  237. *
  238. * @details This macro is used to select RTC clock source.
  239. * \hideinitializer
  240. */
  241. #define RTC_CLKSRCSEL(u32ClkSrc) ((RTC->LXTCTL &= ~RTC_LXTCTL_C32KS_Msk) | u32ClkSrc);
  242. void RTC_Open(S_RTC_TIME_DATA_T *psPt);
  243. void RTC_Close(void);
  244. void RTC_32KCalibration(int32_t i32FrequencyX10000);
  245. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *psPt);
  246. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *psPt);
  247. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *psPt);
  248. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *psPt);
  249. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
  250. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  251. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
  252. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  253. void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk);
  254. void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk);
  255. uint32_t RTC_GetDayOfWeek(void);
  256. void RTC_SetTickPeriod(uint32_t u32TickSelection);
  257. void RTC_EnableInt(uint32_t u32IntFlagMask);
  258. void RTC_DisableInt(uint32_t u32IntFlagMask);
  259. /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  260. /*@}*/ /* end of group RTC_Driver */
  261. /*@}*/ /* end of group Standard_Driver */
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265. #endif /* __NU_RTC_H__ */
  266. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/