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