rtc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**************************************************************************//**
  2. * @file rtc.h
  3. * @version V3.00
  4. * $Revision: 10 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 series RTC driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __RTC_H__
  12. #define __RTC_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup Standard_Driver Standard Driver
  18. @{
  19. */
  20. /** @addtogroup RTC_Driver RTC Driver
  21. @{
  22. */
  23. /** @addtogroup RTC_EXPORTED_CONSTANTS RTC Exported Constants
  24. @{
  25. */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. /* RTC Initial Keyword Constant Definitions */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. #define RTC_INIT_KEY 0xA5EB1357UL /*!< RTC Initiation Key to make RTC leaving reset state */
  30. #define RTC_WRITE_KEY 0x0000A965UL /*!< RTC Register Access Enable Key to enable RTC read/write accessible and kept 1024 RTC clock */
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. /* RTC Time Attribute Constant Definitions */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. #define RTC_CLOCK_12 0 /*!< RTC as 12-hour time scale with AM and PM indication */
  35. #define RTC_CLOCK_24 1 /*!< RTC as 24-hour time scale */
  36. #define RTC_AM 1 /*!< RTC as AM indication */
  37. #define RTC_PM 2 /*!< RTC as PM indication */
  38. /*---------------------------------------------------------------------------------------------------------*/
  39. /* RTC Tick Period Constant Definitions */
  40. /*---------------------------------------------------------------------------------------------------------*/
  41. #define RTC_TICK_1_SEC 0x0UL /*!< RTC time tick period is 1 second */
  42. #define RTC_TICK_1_2_SEC 0x1UL /*!< RTC time tick period is 1/2 second */
  43. #define RTC_TICK_1_4_SEC 0x2UL /*!< RTC time tick period is 1/4 second */
  44. #define RTC_TICK_1_8_SEC 0x3UL /*!< RTC time tick period is 1/8 second */
  45. #define RTC_TICK_1_16_SEC 0x4UL /*!< RTC time tick period is 1/16 second */
  46. #define RTC_TICK_1_32_SEC 0x5UL /*!< RTC time tick period is 1/32 second */
  47. #define RTC_TICK_1_64_SEC 0x6UL /*!< RTC time tick period is 1/64 second */
  48. #define RTC_TICK_1_128_SEC 0x7UL /*!< RTC time tick period is 1/128 second */
  49. /*---------------------------------------------------------------------------------------------------------*/
  50. /* RTC Day of Week Constant Definitions */
  51. /*---------------------------------------------------------------------------------------------------------*/
  52. #define RTC_SUNDAY 0x0UL /*!< Day of the Week is Sunday */
  53. #define RTC_MONDAY 0x1UL /*!< Day of the Week is Monday */
  54. #define RTC_TUESDAY 0x2UL /*!< Day of the Week is Tuesday */
  55. #define RTC_WEDNESDAY 0x3UL /*!< Day of the Week is Wednesday */
  56. #define RTC_THURSDAY 0x4UL /*!< Day of the Week is Thursday */
  57. #define RTC_FRIDAY 0x5UL /*!< Day of the Week is Friday */
  58. #define RTC_SATURDAY 0x6UL /*!< Day of the Week is Saturday */
  59. /*---------------------------------------------------------------------------------------------------------*/
  60. /* RTC Snooper Detection Mode Constant Definitions */
  61. /*---------------------------------------------------------------------------------------------------------*/
  62. #define RTC_SNOOPER_LOW_LEVEL 0x0UL /*!< Snooper pin detected is low-level trigger */
  63. #define RTC_SNOOPER_HIGH_LEVEL 0x2UL /*!< Snooper pin detected is high-level trigger */
  64. #define RTC_SNOOPER_FALLING_EDGE 0x8UL /*!< Snooper pin detected is falling-edge trigger */
  65. #define RTC_SNOOPER_RISING_EDGE 0xAUL /*!< Snooper pin detected is rising-edge trigger */
  66. #define RTC_SNOOPER_DETECT_Msk 0xAUL /*!< Snooper pin detected mask bits */
  67. /*---------------------------------------------------------------------------------------------------------*/
  68. /* RTC Miscellaneous Constant Definitions */
  69. /*---------------------------------------------------------------------------------------------------------*/
  70. #define RTC_WAIT_COUNT 0xFFFFFFFF /*!< Initial Time-out Value */
  71. #define RTC_YEAR2000 2000 /*!< RTC Reference for compute year data */
  72. #define RTC_FCR_REFERENCE 32761 /*!< RTC Reference for frequency compensation */
  73. /*@}*/ /* end of group RTC_EXPORTED_CONSTANTS */
  74. /** @addtogroup RTC_EXPORTED_STRUCTS RTC Exported Structs
  75. @{
  76. */
  77. /**
  78. * @details RTC define Time Data Struct
  79. */
  80. typedef struct
  81. {
  82. uint32_t u32Year; /*!< Year value */
  83. uint32_t u32Month; /*!< Month value */
  84. uint32_t u32Day; /*!< Day value */
  85. uint32_t u32DayOfWeek; /*!< Day of week value */
  86. uint32_t u32Hour; /*!< Hour value */
  87. uint32_t u32Minute; /*!< Minute value */
  88. uint32_t u32Second; /*!< Second value */
  89. uint32_t u32TimeScale; /*!< 12-Hour, 24-Hour */
  90. uint32_t u32AmPm; /*!< Only Time Scale select 12-hr used */
  91. } S_RTC_TIME_DATA_T;
  92. /*@}*/ /* end of group RTC_EXPORTED_STRUCTS */
  93. /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
  94. @{
  95. */
  96. /**
  97. * @brief Indicate is Leap Year or not
  98. *
  99. * @param None
  100. *
  101. * @retval 0 This year is not a leap year
  102. * @retval 1 This year is a leap year
  103. *
  104. * @details According to current date, return this year is leap year or not.
  105. */
  106. #define RTC_IS_LEAP_YEAR() (RTC->LEAPYEAR & RTC_LEAPYEAR_LEAPYEAR_Msk ? 1:0)
  107. /**
  108. * @brief Clear RTC Alarm Interrupt Flag
  109. *
  110. * @param None
  111. *
  112. * @return None
  113. *
  114. * @details This macro is used to clear RTC alarm interrupt flag.
  115. */
  116. #define RTC_CLEAR_ALARM_INT_FLAG() (RTC->INTSTS = (RTC->INTSTS & ~(RTC_INTSTS_TICKIF_Msk | RTC_INTSTS_SNPDIF_Msk)) | RTC_INTSTS_ALMIF_Msk)
  117. /**
  118. * @brief Clear RTC Tick Interrupt Flag
  119. *
  120. * @param None
  121. *
  122. * @return None
  123. *
  124. * @details This macro is used to clear RTC tick interrupt flag.
  125. */
  126. #define RTC_CLEAR_TICK_INT_FLAG() (RTC->INTSTS = (RTC->INTSTS & ~(RTC_INTSTS_ALMIF_Msk | RTC_INTSTS_SNPDIF_Msk)) | RTC_INTSTS_TICKIF_Msk)
  127. /**
  128. * @brief Clear RTC Snooper Interrupt Flag
  129. *
  130. * @param None
  131. *
  132. * @return None
  133. *
  134. * @details This macro is used to clear RTC snooper pin interrupt flag.
  135. */
  136. #define RTC_CLEAR_SNOOPER_INT_FLAG() (RTC->INTSTS = (RTC->INTSTS & ~(RTC_INTSTS_ALMIF_Msk | RTC_INTSTS_TICKIF_Msk)) | RTC_INTSTS_SNPDIF_Msk)
  137. /**
  138. * @brief Get RTC Alarm Interrupt Flag
  139. *
  140. * @param None
  141. *
  142. * @retval 0 RTC alarm interrupt did not occur
  143. * @retval 1 RTC alarm interrupt occurred
  144. *
  145. * @details This macro indicates RTC alarm interrupt occurred or not.
  146. */
  147. #define RTC_GET_ALARM_INT_FLAG() ((RTC->INTSTS & RTC_INTSTS_ALMIF_Msk)? 1:0)
  148. /**
  149. * @brief Get RTC Time Tick Interrupt Flag
  150. *
  151. * @param None
  152. *
  153. * @retval 0 RTC time tick interrupt did not occur
  154. * @retval 1 RTC time tick interrupt occurred
  155. *
  156. * @details This macro indicates RTC time tick interrupt occurred or not.
  157. */
  158. #define RTC_GET_TICK_INT_FLAG() ((RTC->INTSTS & RTC_INTSTS_TICKIF_Msk)? 1:0)
  159. /**
  160. * @brief Get RTC Snooper Interrupt Flag
  161. *
  162. * @param None
  163. *
  164. * @retval 0 RTC snooper pin interrupt did not occur
  165. * @retval 1 RTC snooper pin interrupt occurred
  166. *
  167. * @details This macro indicates RTC snooper pin interrupt occurred or not.
  168. */
  169. #define RTC_GET_SNPPOER_INT_FLAG() ((RTC->INTSTS & RTC_INTSTS_SNPDIF_Msk)? 1:0)
  170. /**
  171. * @brief Read Spare Register
  172. *
  173. * @param[in] u32RegNum The spare register number, 0~19.
  174. *
  175. * @return Spare register content
  176. *
  177. * @details Read the specify spare register content.
  178. * @note The returned value is valid only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
  179. * And its controlled by RTC Access Enable Register.
  180. */
  181. #define RTC_READ_SPARE_REGISTER(u32RegNum) (RTC->SPR[(u32RegNum)])
  182. /**
  183. * @brief Write Spare Register
  184. *
  185. * @param[in] u32RegNum The spare register number, 0~19.
  186. * @param[in] u32RegValue The spare register value.
  187. *
  188. * @return None
  189. *
  190. * @details Write specify data to spare register.
  191. * @note This macro is effect only when SPRRDY(SPRCTL[7] SPR Register Ready) bit is set. \n
  192. * And its controlled by RTC Access Enable Register(RTC_RWEN).
  193. */
  194. #define RTC_WRITE_SPARE_REGISTER(u32RegNum, u32RegValue) (RTC->SPR[(u32RegNum)] = (u32RegValue))
  195. /**
  196. * @brief Wait RTC Access Enable
  197. *
  198. * @param None
  199. *
  200. * @return None
  201. *
  202. * @details This function is used to enable the maximum RTC read/write accessible time.
  203. */
  204. static __INLINE void RTC_WaitAccessEnable(void)
  205. {
  206. /* To wait RWENF bit is cleared and enable RWENF bit (Access Enable bit) again */
  207. while((RTC->RWEN & RTC_RWEN_RWENF_Msk) == RTC_RWEN_RWENF_Msk);
  208. RTC->RWEN = RTC_WRITE_KEY;
  209. /* To wait RWENF bit is set and user can access the protected-register of RTC from now on */
  210. while((RTC->RWEN & RTC_RWEN_RWENF_Msk) == 0x0);
  211. }
  212. void RTC_Open(S_RTC_TIME_DATA_T *sPt);
  213. void RTC_Close(void);
  214. void RTC_32KCalibration(int32_t i32FrequencyX100);
  215. void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  216. void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  217. void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt);
  218. void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt);
  219. void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek);
  220. void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  221. void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day);
  222. void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm);
  223. uint32_t RTC_GetDayOfWeek(void);
  224. void RTC_SetTickPeriod(uint32_t u32TickSelection);
  225. void RTC_EnableInt(uint32_t u32IntFlagMask);
  226. void RTC_DisableInt(uint32_t u32IntFlagMask);
  227. void RTC_EnableSpareAccess(void);
  228. void RTC_DisableSpareRegister(void);
  229. void RTC_EnableSnooperDetection(uint32_t u32PinCondition);
  230. void RTC_DisableSnooperDetection(void);
  231. /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
  232. /*@}*/ /* end of group RTC_Driver */
  233. /*@}*/ /* end of group Standard_Driver */
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif //__RTC_H__
  238. /*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/