lib_rtc.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. ******************************************************************************
  3. * @file lib_rtc.h
  4. * @author Application Team
  5. * @version V4.5.0
  6. * @date 2019-05-14
  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. } RTC_TimeTypeDef;
  30. //INT
  31. #define RTC_INT_CEILLE RTC_INTEN_INTEN8
  32. #define RTC_INT_ACDONE RTC_INTEN_INTEN7
  33. #define RTC_INT_WKUCNT RTC_INTEN_INTEN6
  34. #define RTC_INT_MIDNIGHT RTC_INTEN_INTEN5
  35. #define RTC_INT_WKUHOUR RTC_INTEN_INTEN4
  36. #define RTC_INT_WKUMIN RTC_INTEN_INTEN3
  37. #define RTC_INT_WKUSEC RTC_INTEN_INTEN2
  38. #define RTC_INT_TIMEILLE RTC_INTEN_INTEN1
  39. #define RTC_INT_Msk (0x1FEUL)
  40. //INTSTS
  41. #define RTC_INTSTS_CEILLE RTC_INTSTS_INTSTS8
  42. #define RTC_INTSTS_ACDONE RTC_INTSTS_INTSTS7
  43. #define RTC_INTSTS_WKUCNT RTC_INTSTS_INTSTS6
  44. #define RTC_INTSTS_MIDNIGHT RTC_INTSTS_INTSTS5
  45. #define RTC_INTSTS_WKUHOUR RTC_INTSTS_INTSTS4
  46. #define RTC_INTSTS_WKUMIN RTC_INTSTS_INTSTS3
  47. #define RTC_INTSTS_WKUSEC RTC_INTSTS_INTSTS2
  48. #define RTC_INTSTS_TIMEILLE RTC_INTSTS_INTSTS1
  49. #define RTC_INTSTS_Msk (0x1FEUL)
  50. /* RTC AutoCal struct */
  51. typedef struct
  52. {
  53. uint32_t Period;
  54. uint32_t ATDelay;
  55. uint32_t ATClockSource;
  56. uint32_t ADCSource;
  57. } RTC_AutCalType;
  58. //ATDelay
  59. #define RTC_ATDELAY_15MS RTC_ACCTRL_ACDEL_0
  60. #define RTC_ATDELAY_31MS RTC_ACCTRL_ACDEL_1
  61. #define RTC_ATDELAY_62MS RTC_ACCTRL_ACDEL_2
  62. #define RTC_ATDELAY_125MS RTC_ACCTRL_ACDEL_3
  63. //ATClockSource
  64. #define RTC_ATCS_DISABLE RTC_ACCTRL_ACCLK_0
  65. #define RTC_ATCS_SEC RTC_ACCTRL_ACCLK_1
  66. #define RTC_ATCS_MIN RTC_ACCTRL_ACCLK_2
  67. #define RTC_ATCS_HOUR RTC_ACCTRL_ACCLK_3
  68. //ADCSource
  69. #define RTC_ADCS_DATA (0)
  70. #define RTC_ADCS_PORT RTC_ACCTRL_ADCSEL
  71. //CNTCLK
  72. #define RTC_WKUCNT_RTCCLK RTC_WKUCNT_CNTSEL_0
  73. #define RTC_WKUCNT_2048 RTC_WKUCNT_CNTSEL_1
  74. #define RTC_WKUCNT_512 RTC_WKUCNT_CNTSEL_2
  75. #define RTC_WKUCNT_128 RTC_WKUCNT_CNTSEL_3
  76. //Prescaler
  77. #define RTC_CLKDIV_1 RTC_PSCA_PSCA_0
  78. #define RTC_CLKDIV_4 RTC_PSCA_PSCA_1
  79. /* Private macros ------------------------------------------------------------*/
  80. #define IS_RTC_REGOP_STARTADDR(__STARTADDR__) (((__STARTADDR__) & 0x3U) == 0U)
  81. /* Year 0 ~ 99 */
  82. #define IS_RTC_TIME_YEAR(__YEAR__) ((__YEAR__) < 0x9AU)
  83. /* Month 1 ~ 12 */
  84. #define IS_RTC_TIME_MONTH(__MONTH__) (((__MONTH__) > 0x0U) && ((__MONTH__) < 0x13U))
  85. /* Date 1 ~ 31 */
  86. #define IS_RTC_TIME_DATE(__DATE__) (((__DATE__) > 0x0U) && ((__DATE__) < 0x32))
  87. /* Weekday 0 ~ 6 */
  88. #define IS_RTC_TIME_WEEKDAY(__WEEKDAY__) ((__WEEKDAY__) < 0x7U)
  89. /* Hours 0 ~ 23 */
  90. #define IS_RTC_TIME_HOURS(__HOURS__) ((__HOURS__) < 0x24)
  91. /* Minutes 0 ~ 59 */
  92. #define IS_RTC_TIME_MINS(__MINS__) ((__MINS__) < 0x5A)
  93. /* Seconds 0 ~ 59 */
  94. #define IS_RTC_TIME_SECS(__SECS__) ((__SECS__) < 0x5A)
  95. #define IS_RTC_INT(__INT__) ((((__INT__) & RTC_INT_Msk) != 0U) &&\
  96. (((__INT__) & ~RTC_INT_Msk) == 0U))
  97. #define IS_RTC_INTFLAGR(__INTFLAGR_) (((__INTFLAGR_) == RTC_INTSTS_CEILLE) ||\
  98. ((__INTFLAGR_) == RTC_INTSTS_ACDONE) ||\
  99. ((__INTFLAGR_) == RTC_INTSTS_WKUCNT) ||\
  100. ((__INTFLAGR_) == RTC_INTSTS_MIDNIGHT) ||\
  101. ((__INTFLAGR_) == RTC_INTSTS_WKUHOUR) ||\
  102. ((__INTFLAGR_) == RTC_INTSTS_WKUMIN) ||\
  103. ((__INTFLAGR_) == RTC_INTSTS_WKUSEC) ||\
  104. ((__INTFLAGR_) == RTC_INTSTS_TIMEILLE))
  105. #define IS_RTC_INTFLAGC(__INTFLAGC__) ((((__INTFLAGC__) & RTC_INTSTS_Msk) != 0U) &&\
  106. (((__INTFLAGC__) & ~RTC_INTSTS_Msk) == 0U))
  107. #define IS_RTC_AUTOCAL_RELOAD(__RELOAD__) (((__RELOAD__) == RTC_AUTORELOAD_DISABLE) ||\
  108. ((__RELOAD__) == RTC_AUTORELOAD_ENABLE))
  109. #define IS_RTC_AUTOCAL_ATDLY(__ATDLY__) (((__ATDLY__) == RTC_ATDELAY_15MS) ||\
  110. ((__ATDLY__) == RTC_ATDELAY_31MS) ||\
  111. ((__ATDLY__) == RTC_ATDELAY_62MS) ||\
  112. ((__ATDLY__) == RTC_ATDELAY_125MS))
  113. #define IS_RTC_AUTOCAL_ATCS(__ATCS__) (((__ATCS__) == RTC_ATCS_DISABLE) ||\
  114. ((__ATCS__) == RTC_ATCS_SEC) ||\
  115. ((__ATCS__) == RTC_ATCS_MIN) ||\
  116. ((__ATCS__) == RTC_ATCS_HOUR))
  117. #define IS_RTC_AUTOCAL_ADCSRC(__ADCSRC__) (((__ADCSRC__) == RTC_ADCS_DATA) ||\
  118. ((__ADCSRC__) == RTC_ADCS_PORT))
  119. #define IS_RTC_AUTOCAL_PERIOD(__PERIOD__) ((__PERIOD__) < 64U)
  120. #define IS_RTC_WKUSEC_PERIOD(__PERIOD__) (((__PERIOD__) < 0x41U) && ((__PERIOD__) > 0U))
  121. #define IS_RTC_WKUMIN_PERIOD(__PERIOD__) (((__PERIOD__) < 0x41U) && ((__PERIOD__) > 0U))
  122. #define IS_RTC_WKUHOUR_PERIOD(__PERIOD__) (((__PERIOD__) < 0x21U) && ((__PERIOD__) > 0U))
  123. #define IS_RTC_WKUCNT_PERIOD(__PERIOD__) (((__PERIOD__) < 0x1000001U) && ((__PERIOD__) > 0U))
  124. #define IS_RTC_WKUCNT_CNTSEL(__CNTSEL__) (((__CNTSEL__) == RTC_WKUCNT_RTCCLK) ||\
  125. ((__CNTSEL__) == RTC_WKUCNT_2048) ||\
  126. ((__CNTSEL__) == RTC_WKUCNT_512) ||\
  127. ((__CNTSEL__) == RTC_WKUCNT_128))
  128. #define IS_RTC_CLKDIV(__CLKDIV__) (((__CLKDIV__) == RTC_CLKDIV_1) ||\
  129. ((__CLKDIV__) == RTC_CLKDIV_4))
  130. /* Exported Functions ------------------------------------------------------- */
  131. /* RTC Exported Functions Group1:
  132. Time functions -----------------------------*/
  133. void RTC_SetTime(RTC_TimeTypeDef *sTime);
  134. void RTC_GetTime(RTC_TimeTypeDef *gTime);
  135. /* RTC Exported Functions Group2:
  136. Registers operation functions --------------*/
  137. void RTC_WriteProtection(uint32_t NewState);
  138. void RTC_WaitForSynchro(void);
  139. void RTC_WriteRegisters(uint32_t StartAddr, const uint32_t *wBuffer, uint8_t Len);
  140. void RTC_ReadRegisters(uint32_t StartAddr, uint32_t *rBuffer, uint8_t Len);
  141. /* RTC Exported Functions Group3:
  142. Interrupt functions ------------------------*/
  143. void RTC_INTConfig(uint32_t INTMask, uint32_t NewState);
  144. uint8_t RTC_GetINTStatus(uint32_t FlagMask);
  145. void RTC_ClearINTStatus(uint32_t FlagMask);
  146. /* RTC Exported Functions Group4:
  147. AutoCal functions --------------------------*/
  148. void RTC_AutoCalStructInit(RTC_AutCalType *RTCAC_InitStruct);
  149. void RTC_AutoCalInit(RTC_AutCalType *InitStruct);
  150. void RTC_TrigSourceConfig(uint32_t TrigSource, uint32_t Period);
  151. uint32_t RTC_AutoCalCmd(uint32_t NewState);
  152. void RTC_StartAutoCalManual(void);
  153. void RTC_WaitForAutoCalManual(void);
  154. uint8_t RTC_GetACBusyFlag(void);
  155. /* RTC Exported Functions Group5:
  156. Wake-up functions --------------------------*/
  157. void RTC_WKUSecondsConfig(uint8_t nPeriod);
  158. void RTC_WKUMinutesConfig(uint8_t nPeriod);
  159. void RTC_WKUHoursConfig(uint8_t nPeriod);
  160. void RTC_WKUCounterConfig(uint32_t nClock,uint32_t CNTCLK);
  161. uint32_t RTC_GetWKUCounterValue(void);
  162. /* RTC Exported Functions Group6:
  163. MISC functions -----------------------------*/
  164. void RTC_PrescalerConfig(uint32_t Prescaler);
  165. void RTC_PLLDIVConfig(uint32_t nfrequency);
  166. void RTC_PLLDIVOutputCmd(uint8_t NewState);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif /* __LIB_RTC_H */
  171. /*********************************** END OF FILE ******************************/