lib_rtc.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. ******************************************************************************
  3. * @file lib_rtc.h
  4. * @author Application Team
  5. * @version V1.1.0
  6. * @date 2019-10-28
  7. * @brief RTC library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #ifndef __LIB_RTC_H
  14. #define __LIB_RTC_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "target.h"
  19. /* RTC Time struct */
  20. typedef struct
  21. {
  22. uint32_t Year;
  23. uint32_t Month;
  24. uint32_t Date;
  25. uint32_t WeekDay;
  26. uint32_t Hours;
  27. uint32_t Minutes;
  28. uint32_t Seconds;
  29. uint32_t SubSeconds;
  30. } RTC_TimeTypeDef;
  31. /* RTC Alarm Time struct */
  32. typedef struct
  33. {
  34. uint32_t AlarmHours;
  35. uint32_t AlarmMinutes;
  36. uint32_t AlarmSeconds;
  37. uint32_t AlarmSubSeconds;
  38. }RTC_AlarmTypeDef;
  39. #define RTC_ACCURATE 0
  40. #define RTC_INACCURATE 1
  41. #define IS_RTC_ACCURATESEL(__ACCURATESEL__) (((__ACCURATESEL__) == RTC_ACCURATE) ||\
  42. ((__ACCURATESEL__) == RTC_INACCURATE))
  43. /************** Bits definition for RTC_WKUCNT register ******************/
  44. #define RTC_WKUCNT_CNTSEL_0 (0x0U << RTC_WKUCNT_CNTSEL_Pos)
  45. #define RTC_WKUCNT_CNTSEL_1 (0x1U << RTC_WKUCNT_CNTSEL_Pos)
  46. #define RTC_WKUCNT_CNTSEL_2 (0x2U << RTC_WKUCNT_CNTSEL_Pos)
  47. #define RTC_WKUCNT_CNTSEL_3 (0x3U << RTC_WKUCNT_CNTSEL_Pos)
  48. /************** Bits definition for RTC_PSCA register ******************/
  49. #define RTC_PSCA_PSCA_0 (0x0U << RTC_PSCA_PSCA_Pos)
  50. #define RTC_PSCA_PSCA_1 (0x1U << RTC_PSCA_PSCA_Pos)
  51. //#define RTC_PSCA_PSCA_2 (0x2U << RTC_PSCA_PSCA_Pos)
  52. //#define RTC_PSCA_PSCA_3 (0x3U << RTC_PSCA_PSCA_Pos)
  53. /****************************** RTC Instances *********************************/
  54. #define IS_RTC_ALL_INSTANCE(INSTANCE) ((INSTANCE) == RTC)
  55. //INT
  56. #define RTC_INT_ALARM RTC_INTSTS_INTSTS10
  57. #define RTC_INT_CEILLE RTC_INTSTS_INTSTS8
  58. #define RTC_INT_WKUCNT RTC_INTSTS_INTSTS6
  59. #define RTC_INT_MIDNIGHT RTC_INTSTS_INTSTS5
  60. #define RTC_INT_WKUHOUR RTC_INTSTS_INTSTS4
  61. #define RTC_INT_WKUMIN RTC_INTSTS_INTSTS3
  62. #define RTC_INT_WKUSEC RTC_INTSTS_INTSTS2
  63. #define RTC_INT_TIMEILLE RTC_INTSTS_INTSTS1
  64. #define RTC_INT_ITVSITV RTC_INTSTS_INTSTS0
  65. #define RTC_INT_Msk (0x57FUL)
  66. //INTSTS
  67. #define RTC_INTSTS_ALARM RTC_INTSTS_INTSTS10
  68. #define RTC_INTSTS_CEILLE RTC_INTSTS_INTSTS8
  69. #define RTC_INTSTS_WKUCNT RTC_INTSTS_INTSTS6
  70. #define RTC_INTSTS_MIDNIGHT RTC_INTSTS_INTSTS5
  71. #define RTC_INTSTS_WKUHOUR RTC_INTSTS_INTSTS4
  72. #define RTC_INTSTS_WKUMIN RTC_INTSTS_INTSTS3
  73. #define RTC_INTSTS_WKUSEC RTC_INTSTS_INTSTS2
  74. #define RTC_INTSTS_TIMEILLE RTC_INTSTS_INTSTS1
  75. #define RTC_INTSTS_ITVSITV RTC_INTSTS_INTSTS0
  76. #define RTC_INTSTS_Msk (0x57FUL)
  77. //CNTCLK
  78. #define RTC_WKUCNT_RTCCLK RTC_WKUCNT_CNTSEL_0
  79. #define RTC_WKUCNT_2048 RTC_WKUCNT_CNTSEL_1
  80. #define RTC_WKUCNT_512 RTC_WKUCNT_CNTSEL_2
  81. #define RTC_WKUCNT_128 RTC_WKUCNT_CNTSEL_3
  82. //Prescaler
  83. #define RTC_CLKDIV_1 RTC_PSCA_PSCA_0
  84. #define RTC_CLKDIV_4 RTC_PSCA_PSCA_1
  85. //PLLDIVSOUCE
  86. #define RTC_PLLDIVSOURCE_PCLK 0
  87. #define RTC_PLLDIVSOURCE_PLLL (0x1U << RTC_CTL_RTCPLLCLKSEL_Pos)
  88. //RTC_ITV
  89. #define RTC_ITV_SEC (0x80)
  90. #define RTC_ITV_MIN (1 << RTC_ITV_ITV_Pos)
  91. #define RTC_ITV_HOUR (2 << RTC_ITV_ITV_Pos)
  92. #define RTC_ITV_DAY (3 << RTC_ITV_ITV_Pos)
  93. #define RTC_ITV_500MS (4 << RTC_ITV_ITV_Pos)
  94. #define RTC_ITV_250MS (5 << RTC_ITV_ITV_Pos)
  95. #define RTC_ITV_125MS (6 << RTC_ITV_ITV_Pos)
  96. #define RTC_ITV_62MS (7 << RTC_ITV_ITV_Pos)
  97. #define RTC_ITV_SITVSEC (7 << RTC_ITV_ITV_Pos)
  98. //RTC_SITV
  99. #define RTC_SITV_EN (1 << RTC_SITV_SITVEN_Pos) //Control Multi Second interval.1:enable; 0:disable.
  100. /* Private macros ------------------------------------------------------------*/
  101. #define IS_RTC_REGOP_STARTADDR(__STARTADDR__) (((__STARTADDR__) & 0x3U) == 0U)
  102. /* Year 0 ~ 99 */
  103. #define IS_RTC_TIME_YEAR(__YEAR__) ((__YEAR__) < 0x9AU)
  104. /* Month 1 ~ 12 */
  105. #define IS_RTC_TIME_MONTH(__MONTH__) (((__MONTH__) > 0x0U) && ((__MONTH__) < 0x13U))
  106. /* Date 1 ~ 31 */
  107. #define IS_RTC_TIME_DATE(__DATE__) (((__DATE__) > 0x0U) && ((__DATE__) < 0x32U))
  108. /* Weekday 0 ~ 6 */
  109. #define IS_RTC_TIME_WEEKDAY(__WEEKDAY__) ((__WEEKDAY__) < 0x7U)
  110. /* Hours 0 ~ 23 */
  111. #define IS_RTC_TIME_HOURS(__HOURS__) ((__HOURS__) < 0x24U)
  112. /* Minutes 0 ~ 59 */
  113. #define IS_RTC_TIME_MINS(__MINS__) ((__MINS__) < 0x5AU)
  114. /* Seconds 0 ~ 59 */
  115. #define IS_RTC_TIME_SECS(__SECS__) ((__SECS__) < 0x5AU)
  116. /* SubSeconds 0 ~ 0x999 */
  117. #define IS_RTC_TIME_SubSECS(__SubSECS__) ((__SubSECS__) < 0x1000U)
  118. /* Alarm time 0 ~ 0x999 */
  119. #define IS_RTC_ALARMTIME(__ALARMTIME__) ((__ALARMTIME__) < 0x1E0000U)
  120. #define IS_RTC_INT(__INT__) ((((__INT__) & RTC_INT_Msk) != 0U) &&\
  121. (((__INT__) & ~RTC_INT_Msk) == 0U))
  122. #define IS_RTC_INTFLAGR(__INTFLAGR_) (((__INTFLAGR_) == RTC_INTSTS_CEILLE) ||\
  123. ((__INTFLAGR_) == RTC_INTSTS_WKUCNT) ||\
  124. ((__INTFLAGR_) == RTC_INTSTS_MIDNIGHT) ||\
  125. ((__INTFLAGR_) == RTC_INTSTS_WKUHOUR) ||\
  126. ((__INTFLAGR_) == RTC_INTSTS_WKUMIN) ||\
  127. ((__INTFLAGR_) == RTC_INTSTS_WKUSEC) ||\
  128. ((__INTFLAGR_) == RTC_INTSTS_ALARM) ||\
  129. ((__INTFLAGR_) == RTC_INTSTS_TIMEILLE) ||\
  130. ((__INTFLAGR_) == RTC_INTSTS_ITVSITV))
  131. #define IS_RTC_INTFLAGC(__INTFLAGC__) ((((__INTFLAGC__) & RTC_INTSTS_Msk) != 0U) &&\
  132. (((__INTFLAGC__) & ~RTC_INTSTS_Msk) == 0U))
  133. #define IS_RTC_WKUSEC_PERIOD(__PERIOD__) ((__PERIOD__) < 0x41U)
  134. #define IS_RTC_WKUMIN_PERIOD(__PERIOD__) ((__PERIOD__) < 0x41U)
  135. #define IS_RTC_WKUHOUR_PERIOD(__PERIOD__) ((__PERIOD__) < 0x21U)
  136. #define IS_RTC_WKUCNT_PERIOD(__PERIOD__) ((__PERIOD__) < 0x1000001U)
  137. #define IS_RTC_WKUCNT_CNTSEL(__CNTSEL__) (((__CNTSEL__) == RTC_WKUCNT_RTCCLK) ||\
  138. ((__CNTSEL__) == RTC_WKUCNT_2048) ||\
  139. ((__CNTSEL__) == RTC_WKUCNT_512) ||\
  140. ((__CNTSEL__) == RTC_WKUCNT_128))
  141. #define IS_RTC_CLKDIV(__CLKDIV__) (((__CLKDIV__) == RTC_CLKDIV_1) ||\
  142. ((__CLKDIV__) == RTC_CLKDIV_4))
  143. #define IS_RTC_PLLDIVSOURCE(__PLLDIVSOURCE__) (((__PLLDIVSOURCE__) == RTC_PLLDIVSOURCE_PCLK) ||\
  144. ((__PLLDIVSOURCE__) == RTC_PLLDIVSOURCE_PLLL))
  145. #define IS_RTC_ITV(__ITV__) (((__ITV__) == RTC_ITV_SEC) ||\
  146. ((__ITV__) == RTC_ITV_MIN) ||\
  147. ((__ITV__) == RTC_ITV_HOUR) ||\
  148. ((__ITV__) == RTC_ITV_DAY) ||\
  149. ((__ITV__) == RTC_ITV_500MS) ||\
  150. ((__ITV__) == RTC_ITV_250MS) ||\
  151. ((__ITV__) == RTC_ITV_125MS) ||\
  152. ((__ITV__) == RTC_ITV_62MS) ||\
  153. ((__ITV__) == RTC_ITV_SITVSEC))
  154. #define IS_RTC_SITV(__SITV__) ((__SITV__) < 64U)
  155. /* Exported Functions ------------------------------------------------------- */
  156. /* RTC Exported Functions Group1:
  157. Time functions -----------------------------*/
  158. void RTC_SetTime(RTC_TimeTypeDef *sTime, uint32_t AccurateSel);
  159. void RTC_GetTime(RTC_TimeTypeDef *gTime, uint32_t AccurateSel);
  160. void RTC_SubSecondCmd(uint32_t NewState);
  161. /* RTC Exported Functions Group2:
  162. Alarms configuration functions -------------*/
  163. void RTC_SetAlarm(RTC_AlarmTypeDef *RTC_AlarmStruct, uint32_t AccurateSel);
  164. void RTC_GetAlarm(RTC_AlarmTypeDef *RTC_AlarmStruct, uint32_t AccurateSel);
  165. void RTC_AlarmCmd(uint32_t NewState);
  166. void RTC_AlarmAccurateCmd(uint32_t NewState);
  167. /* RTC Exported Functions Group3:
  168. Registers operation functions --------------*/
  169. void RTC_WriteProtection(uint32_t NewState);
  170. void RTC_WaitForSynchro(void);
  171. void RTC_WriteRegisters(uint32_t StartAddr, const uint32_t *wBuffer, uint8_t Len);
  172. void RTC_ReadRegisters(uint32_t StartAddr, uint32_t *rBuffer, uint8_t Len);
  173. /* RTC Exported Functions Group4:
  174. Interrupt functions ------------------------*/
  175. void RTC_INTConfig(uint32_t INTMask, uint32_t NewState);
  176. uint8_t RTC_GetINTStatus(uint32_t FlagMask);
  177. void RTC_ClearINTStatus(uint32_t FlagMask);
  178. /* RTC Exported Functions Group5:
  179. Wake-up functions --------------------------*/
  180. void RTC_WKUSecondsConfig(uint8_t nPeriod);
  181. void RTC_WKUMinutesConfig(uint8_t nPeriod);
  182. void RTC_WKUHoursConfig(uint8_t nPeriod);
  183. void RTC_WKUCounterConfig(uint32_t nClock,uint32_t CNTCLK);
  184. void RTC_WAKE_ITV(uint8_t nType);
  185. void RTC_WAKE_SITV(uint8_t nPeriod);
  186. uint32_t RTC_GetWKUCounterValue(void);
  187. /* RTC Exported Functions Group6:
  188. MISC functions -----------------------------*/
  189. void RTC_PrescalerConfig(uint32_t Prescaler);
  190. void RTC_PLLDIVConfig(uint32_t DIVSource,uint32_t nfrequency);
  191. void RTC_PLLDIVOutputCmd(uint8_t NewState);
  192. #ifdef __cplusplus
  193. }
  194. #endif
  195. #endif /* __LIB_RTC_H */
  196. /*********************************** END OF FILE ******************************/