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