1 /*!
2  * @file        apm32f4xx_rtc.c
3  *
4  * @brief       This file provides all the RTC firmware functions
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-06-23
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be usefull and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 #include "apm32f4xx.h"
27 #include "apm32f4xx_rtc.h"
28 #include "apm32f4xx_rcm.h"
29 
30 /** @addtogroup APM32F4xx_StdPeriphDriver
31   @{
32 */
33 
34 /** @defgroup RTC_Driver
35   * @brief RTC driver modules
36   @{
37 */
38 
39 /** @defgroup RTC_Macros Macros
40   @{
41 */
42 
43 /* RTC timeout definition */
44 #define RTC_TIMEOUT_WAKE_UP        ((uint32_t)0x00010000)
45 #define RTC_TIMEOUT_INIT           ((uint32_t)0x00010000)
46 #define RTC_TIMEOUT_ALARM          ((uint32_t)0x00010000)
47 #define RTC_TIMEOUT_CALIB          ((uint32_t)0x00020000)
48 #define RTC_TIMEOUT_SYNCHRO        ((uint32_t)0x00020000)
49 #define RTC_TIMEOUT_SHIFT          ((uint32_t)0x00001000)
50 
51 /**@} end of group RTC_Macros*/
52 
53 /** @defgroup RTC_Functions
54   @{
55 */
56 
57 static uint8_t RTC_ByteConBcd2(uint8_t val);
58 static uint8_t RTC_Bcd2ConByte(uint8_t val);
59 
60 /*!
61  * @brief     Reset the RTC registers to their default values.
62  *
63  * @param     None
64  *
65  * @retval    SUCCESS or ERROR
66  */
RTC_Reset(void)67 uint8_t RTC_Reset(void)
68 {
69     __IO uint32_t timeout = 0x00;
70 
71     RTC_DisableWriteProtection();
72 
73     if (RTC_EnableInit() == ERROR)
74     {
75         RTC_EnableWriteProtection();
76 
77         return ERROR;
78     }
79     else
80     {
81         /* Reset register */
82         RTC->TIME  = (uint32_t)0x00000000;
83         RTC->DATE  = (uint32_t)0x00002101;
84         RTC->CTRL &= (uint32_t)0x00000007;
85 
86         /* Wait for wakeup timer write flag */
87         while ((RTC->STS_B.WUTWFLG != BIT_SET) && (timeout != RTC_TIMEOUT_WAKE_UP))
88         {
89             timeout++;
90         }
91 
92         if (RTC->STS_B.WUTWFLG == BIT_RESET)
93         {
94             RTC_EnableWriteProtection();
95 
96             return ERROR;
97         }
98         else
99         {
100             /* Reset register */
101             RTC->CTRL = (uint32_t)0x00000000;
102             RTC->AUTORLD = (uint32_t)0x0000FFFF;
103             RTC->PSC = (uint32_t)0x007F00FF;
104             RTC->DCAL = (uint32_t)0x00000000;
105             RTC->ALRMA = (uint32_t)0x00000000;
106             RTC->ALRMB = (uint32_t)0x00000000;
107             RTC->SHIFT = (uint32_t)0x00000000;
108             RTC->CAL = (uint32_t)0x00000000;
109             RTC->ALRMASS = (uint32_t)0x00000000;
110             RTC->ALRMBSS = (uint32_t)0x00000000;
111 
112             RTC->STS = (uint32_t)0x00000000;
113 
114             RTC->TACFG = (uint32_t)0x00000000;
115 
116             if (RTC_WaitForSynchro() == ERROR)
117             {
118                 RTC_EnableWriteProtection();
119 
120                 return ERROR;
121             }
122             else
123             {
124                 RTC_EnableWriteProtection();
125 
126                 return SUCCESS;
127             }
128         }
129     }
130 }
131 
132 /*!
133  * @brief     Deinitializes the RTC registers to their default reset values
134  *
135  * @param     rtcConfig : pointer to a RTC_Config_T structure which will be initialized
136  *
137  * @retval    SUCCESS or ERROR
138  */
RTC_Config(RTC_Config_T * rtcConfig)139 uint8_t RTC_Config(RTC_Config_T *rtcConfig)
140 {
141     RTC_DisableWriteProtection();
142 
143     if (RTC_EnableInit() == ERROR)
144     {
145         RTC_EnableWriteProtection();
146 
147         return ERROR;
148     }
149     else
150     {
151         RTC->CTRL_B.TIMEFCFG  = (rtcConfig->format);
152         RTC->PSC_B.SPSC = (rtcConfig->synchPrediv);
153         RTC->PSC_B.APSC = (rtcConfig->asynchPrediv);
154 
155         RTC_DisableInit();
156         RTC_EnableWriteProtection();
157 
158         return SUCCESS;
159     }
160 }
161 
162 /*!
163  * @brief     Fills each RTC_ConfigStruct member with its default value
164  *
165  * @param     rtcConfig : pointer to a RTC_Config_T structure which will be initialized
166  *
167  * @retval    None
168  */
RTC_ConfigStructInit(RTC_Config_T * rtcConfig)169 void RTC_ConfigStructInit(RTC_Config_T *rtcConfig)
170 {
171     rtcConfig->format = RTC_HOURFORMAT_24;
172     rtcConfig->asynchPrediv = (uint32_t)0x7F;
173     rtcConfig->synchPrediv = (uint32_t)0xFF;
174 }
175 
176 /*!
177  * @brief     Enable the write protection for RTC registers
178  *
179  * @param     None
180  *
181  * @retval    None
182  */
RTC_EnableWriteProtection(void)183 void RTC_EnableWriteProtection(void)
184 {
185     RTC->WRPROT = 0xFF;
186 }
187 
188 /*!
189  * @brief     Disable the write protection for RTC registers
190  *
191  * @param     None
192  *
193  * @retval    None
194  */
RTC_DisableWriteProtection(void)195 void RTC_DisableWriteProtection(void)
196 {
197     RTC->WRPROT = 0xCA;
198     RTC->WRPROT = 0x53;
199 }
200 
201 /*!
202  * @brief     Enable the RTC Initialization mode.
203  *
204  * @param     None
205  *
206  * @retval    SUCCESS or ERROR
207  */
RTC_EnableInit(void)208 uint8_t RTC_EnableInit(void)
209 {
210     __IO uint32_t timeout = 0x00;
211 
212     if (RTC->STS_B.RINITFLG == BIT_RESET)
213     {
214         /* Enable initialization mode */
215         RTC->STS_B.INITEN = BIT_SET;
216 
217         /* Wait for initialization flag */
218         while ((RTC->STS_B.RINITFLG != BIT_SET) && (timeout != RTC_TIMEOUT_INIT))
219         {
220             timeout++;
221         }
222 
223         if (RTC->STS_B.RINITFLG != BIT_SET)
224         {
225             return ERROR;
226         }
227     }
228 
229     return SUCCESS;
230 }
231 
232 /*!
233  * @brief     Disable the RTC Initialization mode.
234  *
235  * @param     None
236  *
237  * @retval    None
238  */
RTC_DisableInit(void)239 void RTC_DisableInit(void)
240 {
241     RTC->STS_B.INITEN = BIT_RESET;
242 }
243 
244 /*!
245  * @brief     Waits until the RTC Time and Date registers are synchronized
246  *            with RTC APB clock
247  *
248  * @param     None
249  *
250  * @retval    SUCCESS or ERROR
251  */
RTC_WaitForSynchro(void)252 uint8_t RTC_WaitForSynchro(void)
253 {
254     __IO uint32_t timeout = 0;
255 
256     RTC_DisableWriteProtection();
257 
258     RTC->STS &= (uint32_t)0xFFFFFF5F;
259 
260     /* Wait for synchrocnt flag */
261     while ((RTC->STS_B.RSFLG != BIT_SET) && (timeout != RTC_TIMEOUT_SYNCHRO))
262     {
263         timeout++;
264     }
265 
266     if (RTC->STS_B.RSFLG != BIT_RESET)
267     {
268         RTC_EnableWriteProtection();
269 
270         return SUCCESS;
271     }
272     else
273     {
274         RTC_EnableWriteProtection();
275 
276         return ERROR;
277     }
278 }
279 
280 /*!
281  * @brief     Enables the RTC reference clock detection.
282  *
283  * @param     None
284  *
285  * @retval    SUCCESS or ERROR
286  */
RTC_EnableRefClock(void)287 uint8_t RTC_EnableRefClock(void)
288 {
289     RTC_DisableWriteProtection();
290 
291     if (RTC_EnableInit() == ERROR)
292     {
293         RTC_EnableWriteProtection();
294 
295         return ERROR;
296     }
297     else
298     {
299         RTC->CTRL_B.RCLKDEN = BIT_SET;
300         RTC_DisableInit();
301         RTC_EnableWriteProtection();
302 
303         return SUCCESS;
304     }
305 }
306 
307 /*!
308  * @brief     Disable the RTC reference clock detection
309  *
310  * @param     None
311  *
312  * @retval    SUCCESS or ERROR
313  */
RTC_DisableRefClock(void)314 uint8_t RTC_DisableRefClock(void)
315 {
316     RTC_DisableWriteProtection();
317 
318     if (RTC_EnableInit() == ERROR)
319     {
320         RTC_EnableWriteProtection();
321 
322         return ERROR;
323     }
324     else
325     {
326         RTC->CTRL_B.RCLKDEN = BIT_RESET;
327         RTC_DisableInit();
328         RTC_EnableWriteProtection();
329 
330         return SUCCESS;
331     }
332 }
333 
334 /*!
335  * @brief     Enable the Bypass Shadow feature.
336  *
337  * @param     None
338  *
339  * @retval    None
340  */
RTC_EnableBypassShadow(void)341 void RTC_EnableBypassShadow(void)
342 {
343     RTC_DisableWriteProtection();
344 
345     RTC->CTRL_B.RCMCFG = BIT_SET;
346 
347     RTC_EnableWriteProtection();
348 }
349 
350 /*!
351  * @brief     Disable the Bypass Shadow feature.
352  *
353  * @param     None
354  *
355  * @retval    None
356  */
RTC_DisableBypassShadow(void)357 void RTC_DisableBypassShadow(void)
358 {
359     RTC_DisableWriteProtection();
360 
361     RTC->CTRL_B.RCMCFG = BIT_RESET;
362 
363     RTC_EnableWriteProtection();
364 }
365 
366 /*!
367  * @brief     Config the RTC current time
368  *
369  * @param     format: specifies the format to write
370  *                    This parameter can be one of the following values:
371  *                    @arg RTC_FORMAT_BIN : Format in Bin
372  *                    @arg RTC_FORMAT_BCD : Format in BCD
373  *
374  * @param     timeConfig: Pointer to a RTC_TimeConfig_T structure that
375  *                        contains the configuration information for the RTC peripheral
376  *
377  * @retval    SUCCESS or ERROR
378  */
RTC_ConfigTime(RTC_FORMAT_T format,RTC_TimeConfig_T * timeConfig)379 uint8_t RTC_ConfigTime(RTC_FORMAT_T format, RTC_TimeConfig_T *timeConfig)
380 {
381     uint8_t state = ERROR;
382     uint32_t temp = 0;
383 
384     if (RTC->CTRL_B.TIMEFCFG == BIT_RESET)
385     {
386         timeConfig->h12 = RTC_H12_AM;
387     }
388 
389     /* Combine parameters of time */
390     if (format != RTC_FORMAT_BIN)
391     {
392         temp = (((uint32_t)(timeConfig->hours) << 16) | \
393                 ((uint32_t)(timeConfig->minutes) << 8) | \
394                 ((uint32_t)(timeConfig->seconds)) | \
395                 ((uint32_t)(timeConfig->h12) << 22));
396     }
397     else
398     {
399         temp = (uint32_t)(((uint32_t)RTC_ByteConBcd2(timeConfig->hours) << 16) | \
400                           ((uint32_t)RTC_ByteConBcd2(timeConfig->minutes) << 8) | \
401                           ((uint32_t)RTC_ByteConBcd2(timeConfig->seconds)) | \
402                           (((uint32_t)(timeConfig->h12) << 22)));
403     }
404 
405     RTC_DisableWriteProtection();
406 
407     if (RTC_EnableInit() == ERROR)
408     {
409         state = ERROR;
410     }
411     else
412     {
413         RTC->TIME = (uint32_t)(temp & 0x007F7F7F);
414 
415         RTC_DisableInit();
416 
417         if (RTC->CTRL_B.RCMCFG == RESET)
418         {
419             if (RTC_WaitForSynchro() == ERROR)
420             {
421                 state = ERROR;
422             }
423             else
424             {
425                 state = SUCCESS;
426             }
427         }
428         else
429         {
430             state = SUCCESS;
431         }
432     }
433 
434     RTC_EnableWriteProtection();
435 
436     return state;
437 }
438 
439 /*!
440  * @brief     Fills each timeConfig member with its default value
441  *
442  * @param     timeConfig:  Pointer to a RTC_TimeConfig_T structure that
443  *                         contains the configuration information for the RTC peripheral
444  *
445  * @retval    None
446  */
RTC_ConfigTimeStructInit(RTC_TimeConfig_T * timeConfig)447 void RTC_ConfigTimeStructInit(RTC_TimeConfig_T *timeConfig)
448 {
449     timeConfig->hours = 0;
450     timeConfig->minutes = 0;
451     timeConfig->seconds = 0;
452     timeConfig->h12 = RTC_H12_AM;
453 }
454 
455 /*!
456  * @brief     Read the RTC current Time
457  *
458  * @param     format: specifies the format to write
459  *                    This parameter can be one of the following values:
460  *                    @arg RTC_FORMAT_BIN : format in Bin
461  *                    @arg RTC_FORMAT_BCD : format in BCD
462  *
463  * @param     time:  Pointer to a RTC_TimeConfig_T structure that
464  *                   contains the configuration information for the RTC peripheral
465  *
466  * @retval    None
467  */
RTC_ReadTime(RTC_FORMAT_T format,RTC_TimeConfig_T * time)468 void RTC_ReadTime(RTC_FORMAT_T format, RTC_TimeConfig_T *time)
469 {
470     uint32_t temp = 0;
471 
472     temp = (uint32_t)((RTC->TIME) & 0x007F7F7F);
473 
474     time->h12 = (RTC_H12_T)((temp & 0x00400000) >> 22);
475     time->hours   = (uint8_t)((temp & 0x003F0000) >> 16);
476     time->minutes = (uint8_t)((temp & 0x00007F00) >> 8);
477     time->seconds = (uint8_t)(temp & 0x0000007F);
478 
479     if (format == RTC_FORMAT_BIN)
480     {
481         time->hours   = (uint8_t)RTC_Bcd2ConByte(time->hours);
482         time->minutes = (uint8_t)RTC_Bcd2ConByte(time->minutes);
483         time->seconds = (uint8_t)RTC_Bcd2ConByte(time->seconds);
484     }
485 }
486 
487 /*!
488  * @brief     Read the RTC current Calendar Subseconds value
489  *
490  * @param     None
491  *
492  * @retval    RTC current Calendar Subseconds value
493  */
RTC_ReadSubSecond(void)494 uint32_t RTC_ReadSubSecond(void)
495 {
496     uint32_t temp = 0;
497 
498     temp = (uint32_t)(RTC->SUBSEC);
499     (void)(RTC->DATE);
500 
501     return temp;
502 }
503 
504 /*!
505  * @brief     Config the RTC current time
506  *
507  * @param     format: specifies the format to write
508  *                    This parameter can be one of the following values:
509  *                    @arg RTC_FORMAT_BIN : format in Bin
510  *                    @arg RTC_FORMAT_BCD : format in BCD
511  *
512  * @param     dateConfig:  Pointer to a RTC_DateConfig_T structure that
513  *                         contains the configuration DATE information for the RTC peripheral
514  *
515  * @retval    None
516  */
RTC_ConfigDate(RTC_FORMAT_T format,RTC_DateConfig_T * dateConfig)517 uint8_t RTC_ConfigDate(RTC_FORMAT_T format, RTC_DateConfig_T *dateConfig)
518 {
519     uint8_t state = ERROR;
520     uint32_t temp = 0;
521 
522     if ((format == RTC_FORMAT_BIN) && ((dateConfig->month & 0x10) == 0x10))
523     {
524         dateConfig->month = (RTC_MONTH_T)((dateConfig->month & (uint32_t)~(0x10)) + 0x0A);
525     }
526 
527     if (format != RTC_FORMAT_BIN)
528     {
529         temp = (((uint32_t)(dateConfig->year) << 16) | \
530                 ((uint32_t)(dateConfig->month) << 8) | \
531                 ((uint32_t)(dateConfig->date)) | \
532                 ((uint32_t)(dateConfig->weekday) << 13));
533     }
534     else
535     {
536         temp = (((uint32_t)RTC_ByteConBcd2(dateConfig->year) << 16) | \
537                 ((uint32_t)RTC_ByteConBcd2(dateConfig->month) << 8) | \
538                 ((uint32_t)RTC_ByteConBcd2(dateConfig->date)) | \
539                 ((uint32_t)(dateConfig->weekday) << 13));
540     }
541 
542     RTC_DisableWriteProtection();
543 
544     if (RTC_EnableInit() == ERROR)
545     {
546         state = ERROR;
547     }
548     else
549     {
550         RTC->DATE = (uint32_t)(temp & 0x00FFFF3F);
551         RTC_DisableInit();
552 
553         if (RTC->CTRL_B.RCMCFG == RESET)
554         {
555             if (RTC_WaitForSynchro() == ERROR)
556             {
557                 state = ERROR;
558             }
559             else
560             {
561                 state = SUCCESS;
562             }
563         }
564         else
565         {
566             state = SUCCESS;
567         }
568     }
569 
570     RTC_EnableWriteProtection();
571     return state;
572 }
573 
574 /*!
575  * @brief     Fills each dateConfig member with its default value
576  *
577  * @param     dateConfig: Pointer to a RTC_DateConfig_T structure that contains
578  *                        the configuration DATE information for the RTC peripheral
579  *
580  * @retval    None
581  */
RTC_ConfigDateStructInit(RTC_DateConfig_T * dateConfig)582 void RTC_ConfigDateStructInit(RTC_DateConfig_T *dateConfig)
583 {
584     dateConfig->weekday = RTC_WEEKDAY_MONDAY;
585     dateConfig->month = RTC_MONTH_JANUARY;
586     dateConfig->date = 0x01;
587     dateConfig->year = 0x00;
588 }
589 
590 /*!
591  * @brief     Read the RTC current date
592  *
593  * @param     format: specifies the format to write
594  *                    This parameter can be one of the following values:
595  *                    @arg RTC_FORMAT_BIN : format in Bin
596  *                    @arg RTC_FORMAT_BCD : format in BCD
597  *
598  * @param     date: Pointer to a RTC_DateConfig_T structure that contains the
599  *                  configuration DATE information for the RTC peripheral.
600  *
601  * @retval    None
602  */
RTC_ReadDate(RTC_FORMAT_T format,RTC_DateConfig_T * date)603 void RTC_ReadDate(RTC_FORMAT_T format, RTC_DateConfig_T *date)
604 {
605     uint32_t temp = 0;
606     temp = (uint32_t)((RTC->DATE) & 0x00FFFF3F);
607 
608     date->year  = (uint8_t)((temp & 0x00FF0000) >> 16);
609     date->month = (RTC_MONTH_T)((temp & 0x00001F00) >> 8);
610     date->date  = (uint8_t)(temp &  0x0000003F);
611     date->weekday = (RTC_WEEKDAY_T)((temp & 0x0000E000) >> 13);
612 
613     if (format == RTC_FORMAT_BIN)
614     {
615         date->year  = (uint8_t)RTC_Bcd2ConByte(date->year);
616         date->month = (RTC_MONTH_T)RTC_Bcd2ConByte(date->month);
617         date->date  = (uint8_t)RTC_Bcd2ConByte(date->date);
618         date->weekday = (RTC_WEEKDAY_T)(date->weekday);
619     }
620 }
621 
622 /*!
623  * @brief    Config the specified RTC alarm
624  *
625  * @param    format: specifies the format to write
626  *                   This parameter can be one of the following values:
627  *                   @arg RTC_FORMAT_BIN : format in Bin
628  *                   @arg RTC_FORMAT_BCD : format in BCD
629  *
630  * @param    alarm: specifies the format to write
631  *                  This parameter can be one of the following values:
632  *                  @arg RTC_ALARM_A : to select Alarm A
633  *                  @arg RTC_ALARM_B : to select Alarm B
634  *
635  * @param    alarmConfig: Pointer to a RTC_AlarmConfig_T structure that
636  *                        contains the configuration ALRMA information for the RTC peripheral
637  *
638  * @retva    None
639  */
RTC_ConfigAlarm(RTC_FORMAT_T format,RTC_ALARM_T alarm,RTC_AlarmConfig_T * alarmConfig)640 void RTC_ConfigAlarm(RTC_FORMAT_T format, RTC_ALARM_T alarm, RTC_AlarmConfig_T *alarmConfig)
641 {
642     uint32_t temp = 0;
643 
644     if (RTC->CTRL_B.TIMEFCFG == BIT_RESET)
645     {
646         alarmConfig->time.h12 = RTC_H12_AM;
647     }
648 
649     if (format == RTC_FORMAT_BCD)
650     {
651         temp = (((uint32_t)(alarmConfig->time.hours) << 16) | \
652                 ((uint32_t)(alarmConfig->time.minutes) << 8) | \
653                 ((uint32_t)alarmConfig->time.seconds) | \
654                 ((uint32_t)(alarmConfig->time.h12) << 22) | \
655                 ((uint32_t)(alarmConfig->alarmDateWeekDay) << 24) | \
656                 ((uint32_t)alarmConfig->alarmDateWeekDaySel << 30) | \
657                 ((uint32_t)alarmConfig->alarmMask));
658     }
659     else
660     {
661         temp = (((uint32_t)RTC_ByteConBcd2(alarmConfig->time.hours) << 16) | \
662                 ((uint32_t)RTC_ByteConBcd2(alarmConfig->time.minutes) << 8) | \
663                 ((uint32_t)RTC_ByteConBcd2(alarmConfig->time.seconds)) | \
664                 ((uint32_t)(alarmConfig->time.h12) << 22) | \
665                 ((uint32_t)RTC_ByteConBcd2(alarmConfig->alarmDateWeekDay) << 24) | \
666                 ((uint32_t)alarmConfig->alarmDateWeekDaySel << 30) | \
667                 ((uint32_t)alarmConfig->alarmMask));
668     }
669 
670     RTC_DisableWriteProtection();
671 
672     if (alarm == RTC_ALARM_A)
673     {
674         RTC->ALRMA = temp;
675     }
676     else
677     {
678         RTC->ALRMB = temp;
679     }
680 
681     RTC_EnableWriteProtection();
682 }
683 
684 /*!
685  * @brief     Fills each alarmConfig member with its default value.
686  *
687  * @param     alarmConfig: Pointer to a RTC_AlarmConfig_T structure that
688  *                         contains the configuration ALRMA information for the RTC peripheral
689  *
690  * @retval    None
691  */
RTC_ConfigAlarmStructInit(RTC_AlarmConfig_T * alarmConfig)692 void RTC_ConfigAlarmStructInit(RTC_AlarmConfig_T *alarmConfig)
693 {
694     alarmConfig->time.hours = 0;
695     alarmConfig->time.minutes = 0;
696     alarmConfig->time.seconds = 0;
697     alarmConfig->time.h12 = RTC_H12_AM;
698     alarmConfig->alarmDateWeekDay = 1;
699     alarmConfig->alarmDateWeekDaySel = RTC_WEEKDAY_SEL_DATE;
700     alarmConfig->alarmMask = RTC_MASK_NONE;
701 }
702 
703 /*!
704  * @brief     Read the RTC alarm value.
705  *
706  * @param     format: specifies the format to write
707  *                    This parameter can be one of the following values:
708  *                    @arg RTC_FORMAT_BIN : format in Bin
709  *                    @arg RTC_FORMAT_BCD : format in BCD
710  *
711  * @param     alarm: specifies the format to write
712  *                   This parameter can be one of the following values:
713  *                   @arg RTC_ALARM_A : to select Alarm A
714  *                   @arg RTC_ALARM_B : to select Alarm B
715  *
716  * @param     alarmConfig: Pointer to a RTC_AlarmConfig_T structure that contains
717  *                         the configuration ALRMA information for the RTC peripheral
718  *
719  * @retval    None
720  */
RTC_ReadAlarm(RTC_FORMAT_T format,RTC_ALARM_T alarm,RTC_AlarmConfig_T * alarmConfig)721 void RTC_ReadAlarm(RTC_FORMAT_T format, RTC_ALARM_T alarm, RTC_AlarmConfig_T *alarmConfig)
722 {
723     uint8_t day_d, day_u, hours_d, hours_u, minutes_d, minutes_u, seconds_d, seconds_u;
724     uint32_t day_mask, hours_mask, minutes_mask, seconds_mask;
725 
726     if (alarm == RTC_ALARM_A)
727     {
728         day_d = RTC->ALRMA_B.DAYT << 0x04;
729         day_u = RTC->ALRMA_B.DAYU;
730         hours_d = RTC->ALRMA_B.HRT << 0x04;
731         hours_u = RTC->ALRMA_B.HRU;
732         minutes_d = RTC->ALRMA_B.MINT << 0x04;
733         minutes_u = RTC->ALRMA_B.MINU;
734         seconds_d = RTC->ALRMA_B.SECT << 0x04;
735         seconds_u = RTC->ALRMA_B.SECU;
736 
737         day_mask = RTC->ALRMA_B.DATEMEN << 8;
738         hours_mask = RTC->ALRMA_B.HRMEN << 8;
739         minutes_mask = RTC->ALRMA_B.MINMEN << 8;
740         seconds_mask = RTC->ALRMA_B.SECMEN << 7;
741 
742         alarmConfig->time.hours   = (uint8_t)(hours_d | hours_u);
743         alarmConfig->time.minutes = (uint8_t)(minutes_d | minutes_u);
744         alarmConfig->time.seconds = (uint8_t)(seconds_d | seconds_u);
745         alarmConfig->time.h12     = (RTC_H12_T)(RTC->ALRMA_B.TIMEFCFG);
746         alarmConfig->alarmDateWeekDay = (uint8_t)(day_d | day_u);
747         alarmConfig->alarmDateWeekDaySel = (RTC_WEEKDAY_SEL_T)(RTC->ALRMA_B.WEEKSEL);
748         alarmConfig->alarmMask = (uint32_t)(day_mask | hours_mask | minutes_mask | seconds_mask);
749     }
750     else
751     {
752         day_d = RTC->ALRMB_B.DAYT << 0x04;
753         day_u = RTC->ALRMB_B.DAYU;
754         hours_d = RTC->ALRMB_B.HRT << 0x04;
755         hours_u = RTC->ALRMB_B.HRU;
756         minutes_d = RTC->ALRMB_B.MINT << 0x04;
757         minutes_u = RTC->ALRMB_B.MINU;
758         seconds_d = RTC->ALRMB_B.SECT << 0x04;
759         seconds_u = RTC->ALRMB_B.SECU;
760 
761         day_mask = RTC->ALRMB_B.DATEMEN << 8;
762         hours_mask = RTC->ALRMB_B.HRMEN << 8;
763         minutes_mask = RTC->ALRMB_B.MINMEN << 8;
764         seconds_mask = RTC->ALRMB_B.SECMEN << 7;
765 
766         alarmConfig->time.hours   = (uint8_t)(hours_d | hours_u);
767         alarmConfig->time.minutes = (uint8_t)(minutes_d | minutes_u);
768         alarmConfig->time.seconds = (uint8_t)(seconds_d | seconds_u);
769         alarmConfig->time.h12     = (RTC_H12_T)(RTC->ALRMB_B.TIMEFCFG);
770         alarmConfig->alarmDateWeekDay = (uint8_t)(day_d | day_u);
771         alarmConfig->alarmDateWeekDaySel = (RTC_WEEKDAY_SEL_T)(RTC->ALRMB_B.WEEKSEL);
772         alarmConfig->alarmMask = (uint32_t)(day_mask | hours_mask | minutes_mask | seconds_mask);
773     }
774 
775     if (format == RTC_FORMAT_BIN)
776     {
777         alarmConfig->time.hours = (uint8_t)RTC_Bcd2ConByte(alarmConfig->time.hours);
778         alarmConfig->time.minutes = (uint8_t)RTC_Bcd2ConByte(alarmConfig->time.minutes);
779         alarmConfig->time.seconds = (uint8_t)RTC_Bcd2ConByte(alarmConfig->time.seconds);
780         alarmConfig->alarmDateWeekDay = (uint8_t)RTC_Bcd2ConByte(alarmConfig->alarmDateWeekDay);
781     }
782 }
783 
784 /*!
785  * @brief     Enable the RTC Alarm A.
786  *
787  * @param     None
788  *
789  * @retval    None
790  */
RTC_EnableAlarmA(void)791 void RTC_EnableAlarmA(void)
792 {
793     RTC_DisableWriteProtection();
794 
795     RTC->CTRL_B.ALRAEN = BIT_SET;
796 
797     RTC_EnableWriteProtection();
798 }
799 
800 /*!
801  * @brief     Disable the the RTC Alarm A.
802  *
803  * @param     None
804  *
805  * @retval    None
806  */
RTC_DisableAlarmA(void)807 uint8_t RTC_DisableAlarmA(void)
808 {
809     __IO uint32_t timeout = 0x00;
810 
811     RTC_DisableWriteProtection();
812     RTC->CTRL_B.ALRAEN = BIT_RESET;
813 
814     /* wait for Alarm A write flag */
815     while (((RTC->STS_B.ALRAWFLG) != BIT_SET) && (timeout != RTC_TIMEOUT_ALARM))
816     {
817         timeout++;
818     }
819 
820     if ((RTC->STS_B.ALRAWFLG) == BIT_RESET)
821     {
822         RTC_EnableWriteProtection();
823         return ERROR;
824     }
825     else
826     {
827         RTC_EnableWriteProtection();
828         return SUCCESS;
829     }
830 }
831 
832 /*!
833  * @brief     Enable the RTC Alarm B.
834  *
835  * @param     None
836  *
837  * @retval    None
838  */
RTC_EnableAlarmB(void)839 void RTC_EnableAlarmB(void)
840 {
841     RTC_DisableWriteProtection();
842 
843     RTC->CTRL_B.ALRBEN = BIT_SET;
844 
845     RTC_EnableWriteProtection();
846 }
847 
848 /*!
849  * @brief     Disable the the RTC Alarm B.
850  *
851  * @param     None
852  *
853  * @retval    None
854  */
RTC_DisableAlarmB(void)855 uint8_t RTC_DisableAlarmB(void)
856 {
857     __IO uint32_t timeout = 0x00;
858 
859     RTC_DisableWriteProtection();
860     RTC->CTRL_B.ALRBEN = BIT_RESET;
861 
862     /* wait for Alarm B write flag */
863     while (((RTC->STS_B.ALRBWFLG) != BIT_SET) && (timeout != RTC_TIMEOUT_ALARM))
864     {
865         timeout++;
866     }
867 
868     if ((RTC->STS_B.ALRBWFLG) == BIT_RESET)
869     {
870         RTC_EnableWriteProtection();
871         return ERROR;
872     }
873     else
874     {
875         RTC_EnableWriteProtection();
876         return SUCCESS;
877     }
878 }
879 
880 /*!
881  * @brief     Read the RTC ALRMA Subseconds value
882  *
883  * @param     val: specifies the value for ALRMA Sub Second
884  *                 this value must less than 0x00007FFF
885  *
886  * @param     format: specifies the format to write
887  *                    This parameter can be one of the following values:
888  *                    @arg RTC_ALARM_A : to select Alarm A
889  *                    @arg RTC_ALARM_B : to select Alarm B
890  *
891  * @param     mask: specifies the mask for ALRMA Sub Second.
892  *                  This parameter can be one of the following values:
893  *                  @arg RTC_ALARM_SUBSEC_MASK_ALL   : All Alarm SUBSEC fields are masked.
894  *                  @arg RTC_ALARM_SUBSEC_MASK_14_1  : Mask SUBSEC[14:1]
895  *                  @arg RTC_ALARM_SUBSEC_MASK_14_2  : Mask SUBSEC[14:2]
896  *                  @arg RTC_ALARM_SUBSEC_MASK_14_3  : Mask SUBSEC[14:3]
897  *                  @arg RTC_ALARM_SUBSEC_MASK_14_4  : Mask SUBSEC[14:4]
898  *                  @arg RTC_ALARM_SUBSEC_MASK_14_5  : Mask SUBSEC[14:5]
899  *                  @arg RTC_ALARM_SUBSEC_MASK_14_6  : Mask SUBSEC[14:6]
900  *                  @arg RTC_ALARM_SUBSEC_MASK_14_7  : Mask SUBSEC[14:7]
901  *                  @arg RTC_ALARM_SUBSEC_MASK_14_8  : Mask SUBSEC[14:8]
902  *                  @arg RTC_ALARM_SUBSEC_MASK_14_9  : Mask SUBSEC[14:9]
903  *                  @arg RTC_ALARM_SUBSEC_MASK_14_10 : Mask SUBSEC[14:10]
904  *                  @arg RTC_ALARM_SUBSEC_MASK_14_11 : Mask SUBSEC[14:11]
905  *                  @arg RTC_ALARM_SUBSEC_MASK_14_12 : Mask SUBSEC[14:12]
906  *                  @arg RTC_ALARM_SUBSEC_MASK_14_13 : Mask SUBSEC[14:13]
907  *                  @arg RTC_ALARM_SUBSEC_MASK_14    : Mask SUBSEC[14]
908  *                  @arg RTC_ALARM_SUBSEC_MASK_NONE  : Alarm comparison is all the SUBSEC bit.
909  *
910  *                  this value must less than 0x0f
911  *
912  * @retval    None
913  */
RTC_ConfigAlarmSubSecond(RTC_ALARM_T alarm,uint32_t val,RTC_ALARM_SUBSEC_MASK_T mask)914 void RTC_ConfigAlarmSubSecond(RTC_ALARM_T alarm, uint32_t val, RTC_ALARM_SUBSEC_MASK_T mask)
915 {
916     RTC_DisableWriteProtection();
917 
918     if (alarm == RTC_ALARM_A)
919     {
920         RTC->ALRMASS_B.SUBSEC  = val;
921         RTC->ALRMASS_B.MASKSEL = mask;
922     }
923     else
924     {
925         RTC->ALRMBSS_B.SUBSEC  = val;
926         RTC->ALRMBSS_B.MASKSEL = mask;
927     }
928 
929     RTC_EnableWriteProtection();
930 }
931 
932 /*!
933  * @brief     Read the RTC Alarm Subseconds value
934  *
935  * @param     format: specifies the format to write
936  *                    This parameter can be one of the following values:
937  *                    @arg RTC_ALARM_A : select Alarm A
938  *                    @arg RTC_ALARM_B : select Alarm B
939  *
940  * @retval    RTC ALRM Subseconds value
941  */
RTC_ReadAlarmSubSecond(RTC_ALARM_T alarm)942 uint16_t RTC_ReadAlarmSubSecond(RTC_ALARM_T alarm)
943 {
944     if (alarm == RTC_ALARM_A)
945     {
946         return (uint16_t)(RTC->ALRMASS_B.SUBSEC);
947     }
948     else
949     {
950         return (uint16_t)(RTC->ALRMBSS_B.SUBSEC);
951     }
952 }
953 
954 /*!
955  * @brief     Configure the RTC Wakeup clock source.
956  *
957  * @param     wakeUpClock: specifies the Wakeup Clock source.
958  *                         This parameter can be one of the following values:
959  *                         @arg RTC_WAKEUP_CLOCK_RTC_DIV16   : Wakeup Clock Select to RTC/16
960  *                         @arg RTC_WAKEUP_CLOCK_RTC_DIV8    : Wakeup Clock Select to RTC/8
961  *                         @arg RTC_WAKEUP_CLOCK_RTC_DIV4    : Wakeup Clock Select to RTC/4
962  *                         @arg RTC_WAKEUP_CLOCK_RTC_DIV2    : Wakeup Clock Select to RTC/2
963  *                         @arg RTC_WAKEUP_CLOCK_CK_SPRE_16B : Wakeup Clock Select to clk_spre
964  *                         @arg RTC_WAKEUP_CLOCK_CK_SPRE_17B : Wakeup Clock Select to clk_spre
965  *
966  * @retval    None
967  */
RTC_ConfigWakeUpClock(RTC_WAKEUP_CLOCK_T wakeUpClock)968 void RTC_ConfigWakeUpClock(RTC_WAKEUP_CLOCK_T wakeUpClock)
969 {
970     RTC_DisableWriteProtection();
971 
972     RTC->CTRL_B.WUCLKSEL = wakeUpClock;
973 
974     RTC_EnableWriteProtection();
975 }
976 
977 /*!
978  * @brief     Configure the RTC Wakeup counter value.
979  *
980  * @param     wakeUpValue: a 16-bit Wakeup auto-reload value.
981  *
982  * @retval    None
983  */
RTC_ConfigWakeUpValue(uint16_t wakeUpValue)984 void RTC_ConfigWakeUpValue(uint16_t wakeUpValue)
985 {
986     RTC_DisableWriteProtection();
987 
988     RTC->AUTORLD = (uint32_t)wakeUpValue;
989 
990     RTC_EnableWriteProtection();
991 }
992 
993 /*!
994  * @brief     Read the RTC Wakeup auto-reload value.
995  *
996  * @param     None
997  *
998  * @retval    The RTC WakeUp auto-reload value.
999  */
RTC_ReadWakeUpValue(void)1000 uint16_t RTC_ReadWakeUpValue(void)
1001 {
1002     return (uint16_t)RTC->AUTORLD;
1003 }
1004 
1005 /*!
1006  * @brief     Enable the RTC WakeUp timer.
1007  *
1008  * @param     None
1009  *
1010  * @retval    None
1011  */
RTC_EnableWakeUp(void)1012 void RTC_EnableWakeUp(void)
1013 {
1014     RTC_DisableWriteProtection();
1015 
1016     RTC->CTRL_B.WUTEN = BIT_SET;
1017 
1018     RTC_EnableWriteProtection();
1019 }
1020 
1021 /*!
1022  * @brief     Disable the RTC WakeUp timer.
1023  *
1024  * @param     None
1025  *
1026  * @retval    None
1027  */
RTC_DisableWakeUp(void)1028 uint8_t RTC_DisableWakeUp(void)
1029 {
1030     __IO uint32_t timeout = 0x00;
1031     RTC_DisableWriteProtection();
1032 
1033     RTC->CTRL_B.WUTEN = BIT_RESET;
1034 
1035     /* wait for wakeup timer write flag */
1036     while (((RTC->STS_B.WUTWFLG) == BIT_RESET) && (timeout != RTC_TIMEOUT_WAKE_UP))
1037     {
1038         timeout++;
1039     }
1040 
1041     if ((RTC->STS_B.WUTWFLG) == BIT_RESET)
1042     {
1043         RTC_EnableWriteProtection();
1044         return ERROR;
1045     }
1046     else
1047     {
1048         RTC_EnableWriteProtection();
1049         return SUCCESS;
1050     }
1051 }
1052 
1053 /*!
1054  * @brief     Adds or substract one hour from the current time
1055  *
1056  * @param     saving: specifies the DayLight Saving.
1057  *                 This parameter can be one of the following values:
1058  *                 @arg RTC_DLS_SUB_1H : Winter time change
1059  *                 @arg RTC_DLS_ADD_1H : Summer time change
1060  *
1061  * @param     bit: set the Backup Value.
1062  *                 This parameter can be one of the following values:
1063  *                 @arg RTC_BACKUP_RESET : Reset backup value
1064  *                 @arg RTC_BACKUP_SET   : Set backup value
1065  *
1066  * @retval    None
1067  */
RTC_ConfigDayLightSaving(RTC_DLS_T saving,RTC_BACKUP_T bit)1068 void RTC_ConfigDayLightSaving(RTC_DLS_T saving, RTC_BACKUP_T bit)
1069 {
1070     RTC_DisableWriteProtection();
1071 
1072     if (saving == RTC_DLS_ADD_1H)
1073     {
1074         RTC->CTRL_B.STCCFG = BIT_SET;
1075     }
1076     else
1077     {
1078         RTC->CTRL_B.WTCCFG = BIT_SET;
1079     }
1080 
1081     /* Backup Value Setup */
1082     RTC->CTRL_B.BAKP = bit;
1083 
1084     RTC_EnableWriteProtection();
1085 }
1086 
1087 /*!
1088  * @brief     Returns the RTC Day Light Saving stored operation
1089  *
1090  * @param     None
1091  *
1092  * @retval    RTC Day Light Saving backup stored operation
1093  */
RTC_ReadStoreOperation(void)1094 uint8_t RTC_ReadStoreOperation(void)
1095 {
1096     return (uint8_t)RTC->CTRL_B.BAKP;
1097 }
1098 
1099 /*!
1100  * @brief     Config the RTC alarm Output Way and output polarity.
1101  *
1102  * @param     outputSel: specifies RTC alarm Output Way.
1103  *                       This parameter can be one of the following values:
1104  *                       @arg RTC_OUT_SEL_DISABLE : Disable RTC output
1105  *                       @arg RTC_OUT_SEL_ALARM_A : Select alarm A as RTC output
1106  *                       @arg RTC_OUT_SEL_ALARM_B : Select alarm B as RTC output
1107  *                       @arg RTC_OUT_SEL_WAKEUP: : Select wake up as RTC output
1108  *
1109  * @param     polarity: specified the output polarity.
1110  *                      This parameter can be one of the following values:
1111  *                      @arg RTC_OUT_POLARITY_HIGH : The output polarity is high
1112  *                      @arg RTC_OUT_POLARITY_LOW  : The output polarity is low
1113  *
1114  * @retval    None
1115  */
RTC_ConfigOutput(RTC_OUT_SEL_T outputSel,RTC_OUT_POLARITY_T polarity)1116 void RTC_ConfigOutput(RTC_OUT_SEL_T outputSel, RTC_OUT_POLARITY_T polarity)
1117 {
1118     RTC_DisableWriteProtection();
1119 
1120     RTC->CTRL_B.OUTSEL = outputSel;
1121     RTC->CTRL_B.POLCFG = polarity;
1122 
1123     RTC_EnableWriteProtection();
1124 }
1125 
1126 /*!
1127  * @brief     Config RTC Coarse calibration.
1128  *
1129  * @param     calibSign: specifies the sign of the coarse calibration value.
1130  *                       This parameter can be  one of the following values:
1131  *                       @arg RTC_CALIB_SIGN_POSITIVE : The Calibration sign is positive
1132  *                       @arg RTC_CALIB_SIGN_NEGATIVE : The Calibration sign is negative
1133  *
1134  * @param     value: a 5-bit value of coarse calibration expressed in ppm.
1135  *
1136  * @note      For positive sign, the Calibration value could be between 0 and 126
1137  *            with a 4-ppm step.
1138  *
1139  * @note      For negative sign, the Calibration value could be between 0 and 63
1140  *            with a 2-ppm step.
1141  *
1142  * @retval    SUCCESS or ERROR
1143  */
RTC_ConfigCoarseCalib(RTC_CALIB_SIGN_T calibSign,uint32_t value)1144 uint8_t RTC_ConfigCoarseCalib(RTC_CALIB_SIGN_T calibSign, uint32_t value)
1145 {
1146     RTC_DisableWriteProtection();
1147 
1148     if (RTC_EnableInit() == ERROR)
1149     {
1150         RTC_EnableWriteProtection();
1151         return ERROR;
1152     }
1153     else
1154     {
1155         RTC->DCAL_B.DCALCFG = calibSign;
1156         RTC->DCAL_B.DCAL    = value;
1157 
1158         RTC_DisableInit();
1159         RTC_EnableWriteProtection();
1160 
1161         return SUCCESS;
1162     }
1163 }
1164 
1165 /*!
1166  * @brief     Enables the Coarse calibration.
1167  *
1168  * @param     None
1169  *
1170  * @retval    SUCCESS or ERROR
1171  */
RTC_EnableCoarseCalib(void)1172 uint8_t RTC_EnableCoarseCalib(void)
1173 {
1174     RTC_DisableWriteProtection();
1175 
1176     if (RTC_EnableInit() == ERROR)
1177     {
1178         RTC_EnableWriteProtection();
1179 
1180         return ERROR;
1181     }
1182     else
1183     {
1184         RTC->CTRL_B.DCALEN = BIT_SET;
1185 
1186         RTC_DisableInit();
1187         RTC_EnableWriteProtection();
1188 
1189         return SUCCESS;
1190     }
1191 }
1192 
1193 /*!
1194  * @brief     Disables the Coarse calibration.
1195  *
1196  * @param     None
1197  *
1198  * @retval    SUCCESS or ERROR
1199  */
RTC_DisableCoarseCalib(void)1200 uint8_t RTC_DisableCoarseCalib(void)
1201 {
1202     RTC_DisableWriteProtection();
1203 
1204     if (RTC_EnableInit() == ERROR)
1205     {
1206         RTC_EnableWriteProtection();
1207 
1208         return ERROR;
1209     }
1210     else
1211     {
1212         RTC->CTRL_B.DCALEN = BIT_RESET;
1213 
1214         RTC_DisableInit();
1215         RTC_EnableWriteProtection();
1216 
1217         return SUCCESS;
1218     }
1219 }
1220 
1221 /*!
1222  * @brief     Enable the RTC clock Calibration Output.
1223  *
1224  * @param     None
1225  *
1226  * @retval    None
1227  */
RTC_EnableCalibOutput(void)1228 void RTC_EnableCalibOutput(void)
1229 {
1230     RTC_DisableWriteProtection();
1231 
1232     RTC->CTRL_B.CALOEN = BIT_SET;
1233 
1234     RTC_EnableWriteProtection();
1235 }
1236 
1237 /*!
1238  * @brief     Disable the RTC clock Calibration Output.
1239  *
1240  * @param     None
1241  *
1242  * @retval    None
1243  */
RTC_DisableCalibOutput(void)1244 void RTC_DisableCalibOutput(void)
1245 {
1246     RTC_DisableWriteProtection();
1247 
1248     RTC->CTRL_B.CALOEN = BIT_RESET;
1249 
1250     RTC_EnableWriteProtection();
1251 }
1252 
1253 /*!
1254  * @brief     Config the Calibration output.
1255  *
1256  * @param     calib: Select the Calibration output.
1257  *                   This parameter can be one of the following values:
1258  *                   @arg RTC_CALIB_OUTPUT_512HZ : Calibration output is 512 Hz
1259  *                   @arg RTC_CALIB_OUTPUT_1HZ   : Calibration output is 1 Hz
1260  *
1261  * @retval    None
1262  */
RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_T calib)1263 void RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_T calib)
1264 {
1265     RTC_DisableWriteProtection();
1266 
1267     RTC->CTRL_B.CALOSEL = calib;
1268 
1269     RTC_EnableWriteProtection();
1270 }
1271 
1272 /*!
1273  * @brief     Config the Synchronization Shift Control Settings.
1274  *
1275  * @param     period: Select the Smooth Calibration period.
1276  *                    This parameter can be can be one of the following values:
1277  *                    @arg RTC_SCP_32SEC : The smooth calibration periode is 32s.
1278  *                    @arg RTC_SCP_16SEC : The smooth calibration periode is 16s.
1279  *                    @arg RTC_SCP_8SEC  : The smooth calibartion periode is 8s.
1280  *
1281  * @param     calibPulse: Select to Set or reset the CALP bit.
1282  *                        This parameter can be one of the following values:
1283  *                        @arg RTC_SCPP_RESET : Add one RTCCLK puls every 2**11 pulses.
1284  *                        @arg RTC_SCPP_SET   : No RTCCLK pulses are added.
1285  *
1286  * @param     value: a 9-bits value to Config RECALF[8:0].
1287  *
1288  * @retval    SUCCESS or ERROR
1289  */
RTC_ConfigSmoothCalib(RTC_SCP_T period,RTC_SCPP_T calibPulse,uint16_t value)1290 uint8_t RTC_ConfigSmoothCalib(RTC_SCP_T period, RTC_SCPP_T calibPulse, uint16_t value)
1291 {
1292     uint8_t state = ERROR;
1293     uint32_t timeout = 0;
1294 
1295     RTC_DisableWriteProtection();
1296 
1297     /* wait for Recalibration flag to be cleared */
1298     while ((RTC->STS_B.RCALPFLG != BIT_RESET) && (timeout != RTC_TIMEOUT_CALIB))
1299     {
1300         timeout++;
1301     }
1302 
1303     if (RTC->STS_B.RCALPFLG != BIT_SET)
1304     {
1305         RTC->CAL = ((uint32_t)calibPulse << 15) | ((uint32_t)period << 13) |
1306                    (uint32_t)(value & 0x01FF);
1307 
1308         state = SUCCESS;
1309     }
1310     else
1311     {
1312         state = ERROR;
1313     }
1314 
1315     RTC_EnableWriteProtection();
1316 
1317     return state;
1318 }
1319 
1320 /*!
1321  * @brief     Enables the RTC TimeStamp functionality and config the Timestamp event
1322  *            active edge.
1323  *
1324  * @param     edge: Specifies the Timestamp event active edge.
1325  *                  This paramete can be one of the following:
1326  *                  @arg RTC_TIMESTAMP_EDGE_RISING  : Rising edge generates a timestamp event.
1327  *                  @arg RTC_TIMESTAMP_EDGE_FALLING : Falling edge generates a timestamp event
1328  *
1329  * @retval    None
1330  */
RTC_EnableTimeStamp(RTC_TIMESTAMP_EDGE_T edge)1331 void RTC_EnableTimeStamp(RTC_TIMESTAMP_EDGE_T edge)
1332 {
1333     RTC_DisableWriteProtection();
1334 
1335     /* Disable Timestamp when change the edge */
1336     RTC->CTRL_B.TSEN = BIT_RESET;
1337 
1338     RTC->CTRL_B.TSETECFG = edge;
1339     RTC->CTRL_B.TSEN = BIT_SET;
1340 
1341     RTC_EnableWriteProtection();
1342 }
1343 
1344 /*!
1345  * @brief     Disables the RTC TimeStamp functionality.
1346  *
1347  * @param     None
1348  *
1349  * @retval    None
1350  */
RTC_DisableTimeStamp(void)1351 void RTC_DisableTimeStamp(void)
1352 {
1353     RTC_DisableWriteProtection();
1354 
1355     RTC->CTRL_B.TSEN = BIT_RESET;
1356 
1357     RTC_EnableWriteProtection();
1358 }
1359 
1360 /*!
1361  * @brief     Read the RTC TimeStamp value and masks
1362  *
1363  * @param     format: specifies the format of the output parameters.
1364  *                    This parameter can be one of the following values:
1365  *                    @arg RTC_FORMAT_BIN : data in Binary format
1366  *                    @arg RTC_FORMAT_BCD : data in BCD format
1367  *
1368  * @param     time: pointer to a RTC_TimeConfig_T structure that will
1369  *                  contains the TimeStamp time values.
1370  *
1371  * @param     date: pointer to a RTC_DateConfig_T structure that will
1372  *                  contains the TimeStamp date values.
1373  *
1374  * @retval    None
1375  */
RTC_ReadTimeDate(RTC_FORMAT_T format,RTC_TimeConfig_T * time,RTC_DateConfig_T * date)1376 void RTC_ReadTimeDate(RTC_FORMAT_T format, RTC_TimeConfig_T *time, RTC_DateConfig_T *date)
1377 {
1378     uint32_t temptime = 0, tempdate = 0;
1379     temptime = (uint32_t)((RTC->TSTIME) & 0x007F7F7F);
1380     tempdate = (uint32_t)((RTC->TSDATE) & 0x00FFFF3F);
1381 
1382     /* Read the time in BCD format */
1383     time->hours   = (uint8_t)((temptime & 0x003F0000) >> 16);
1384     time->minutes = (uint8_t)((temptime & 0x00007F00) >> 8);
1385     time->seconds = (uint8_t)(temptime &  0x0000007F);
1386     time->h12 = (RTC_H12_T)((temptime & 0x00400000) >> 22);
1387 
1388     /* Read the date in BCD format */
1389     date->year  =  0;
1390     date->month = (RTC_MONTH_T)((tempdate & 0x00001F00) >> 8);
1391     date->date  = (uint8_t)(tempdate &  0x0000003F);
1392     date->weekday = (RTC_WEEKDAY_T)((tempdate & 0x0000E000) >> 13);
1393 
1394     /* Binary format */
1395     if (format == RTC_FORMAT_BIN)
1396     {
1397         time->hours   = (uint8_t)RTC_Bcd2ConByte(time->hours);
1398         time->minutes = (uint8_t)RTC_Bcd2ConByte(time->minutes);
1399         time->seconds = (uint8_t)RTC_Bcd2ConByte(time->seconds);
1400 
1401         date->month = (RTC_MONTH_T)RTC_Bcd2ConByte(date->month);
1402         date->date  = (uint8_t)RTC_Bcd2ConByte(date->date);
1403         date->weekday = (RTC_WEEKDAY_T)(date->weekday);
1404     }
1405 }
1406 
1407 /*!
1408  * @brief     Get the RTC timestamp Subseconds value
1409  *
1410  * @param     None
1411  *
1412  * @retval    RTC current timestamp Subseconds value
1413  */
RTC_ReadTimeStampSubSecond(void)1414 uint16_t RTC_ReadTimeStampSubSecond(void)
1415 {
1416     return RTC->TSSUBSEC_B.SUBSEC;
1417 }
1418 
1419 /*!
1420  * @brief     Config the select Tamper pin edge
1421  *
1422  * @param     tamper:  Selected tamper pin.
1423  *                     This parameter can be one of the following values:
1424  *                     @arg RTC_TAMPER_1 : Select Tamper 1.
1425  *                     @arg RTC_TAMPER_2 : Select Tamper 2.
1426  *
1427  * @param     trigger: Specifies the trigger on the tamper pin that stimulates tamper event.
1428  *                     This parameter can be one of the following values:
1429  *                     @arg RTC_TAMPER_TRIGGER_EDGE_RISING  : Rising Edge of the tamper pin causes tamper event.
1430  *                     @arg RTC_TAMPER_TRIGGER_EDGE_FALLING : Falling Edge of the tamper pin causes tamper event.
1431  *                     @arg RTC_TAMPER_TRIGGER_LEVEL_LOW    : Low Level of the tamper pin causes tamper event.
1432  *                     @arg RTC_TAMPER_TRIGGER_LEVEL_HIGH   : High Level of the tamper pin causes tamper event.
1433  *
1434  * @retval    None
1435  */
RTC_ConfigTamperTrigger(RTC_TAMPER_T tamper,RTC_TAMPER_TRIGGER_T trigger)1436 void RTC_ConfigTamperTrigger(RTC_TAMPER_T tamper, RTC_TAMPER_TRIGGER_T trigger)
1437 {
1438     if (tamper == RTC_TAMPER_1)
1439     {
1440         RTC->TACFG_B.TP1ALCFG = trigger;
1441     }
1442     else
1443     {
1444         RTC->TACFG_B.TP2ALCFG = trigger;
1445     }
1446 }
1447 
1448 /*!
1449  * @brief     Enables the Tamper detection
1450  *
1451  * @param     tamper: Selected tamper pin.
1452  *                    This parameter can be one of the following values:
1453  *                    @arg RTC_TAMPER_1 : Select Tamper 1.
1454  *                    @arg RTC_TAMPER_2 : Select Tamper 2.
1455  *
1456  * @retval    None
1457  */
RTC_EnableTamper(RTC_TAMPER_T tamper)1458 void RTC_EnableTamper(RTC_TAMPER_T tamper)
1459 {
1460     if (tamper == RTC_TAMPER_1)
1461     {
1462         RTC->TACFG_B.TP1EN = BIT_SET;
1463     }
1464     else
1465     {
1466         RTC->TACFG_B.TP2EN = BIT_SET;
1467     }
1468 }
1469 
1470 /*!
1471  * @brief     Disables the Tamper detection
1472  *
1473  * @param     tamper: Selected tamper pin.
1474  *                    This parameter can be any combination of the following values:
1475  *                    @arg RTC_TAMPER_1 : Select Tamper 1.
1476  *                    @arg RTC_TAMPER_2 : Select Tamper 2.
1477  *
1478  * @retval    None
1479  */
RTC_DisableTamper(RTC_TAMPER_T tamper)1480 void RTC_DisableTamper(RTC_TAMPER_T tamper)
1481 {
1482     if (tamper == RTC_TAMPER_1)
1483     {
1484         RTC->TACFG_B.TP1EN = BIT_RESET;
1485     }
1486     else
1487     {
1488         RTC->TACFG_B.TP2EN = BIT_RESET;
1489     }
1490 }
1491 
1492 /*!
1493  * @brief     Config the Tampers Filter
1494  *
1495  * @param    filter: Specifies the tampers filter.
1496  *                   This parameter can be one of the following values:
1497  *                   @arg RTC_TAMPER_FILTER_DISABLE : Tamper filter is disabled.
1498  *                   @arg RTC_TAMPER_FILTER_2SAMPLE : Tamper is activated after 2 consecutive samples at the active level
1499  *                   @arg RTC_TAMPER_FILTER_4SAMPLE : Tamper is activated after 4 consecutive samples at the active level
1500  *                   @arg RTC_TAMPER_FILTER_8SAMPLE : Tamper is activated after 8 consecutive samples at the active level
1501  *
1502  * @retval    None
1503  */
RTC_ConfigFilter(RTC_TAMPER_FILTER_T filter)1504 void RTC_ConfigFilter(RTC_TAMPER_FILTER_T filter)
1505 {
1506     RTC->TACFG_B.TPFCSEL = filter;
1507 
1508 }
1509 
1510 /*!
1511  * @brief     Config the Tampers Sampling Frequency
1512  *
1513  * @param     freq: Specifies the tampers Sampling Frequency.
1514  *                  This parameter can be one of the following values:
1515  *                  @arg RTC_TSF_DIV_32768 : Tampers Sampling Frequency = RTC_CLK/32768
1516  *                  @arg RTC_TSF_DIV_16384 : Tampers Sampling Frequency = RTC_CLK/16384
1517  *                  @arg RTC_TSF_DIV_8192  : Tampers Sampling Frequency = RTC_CLK/8192
1518  *                  @arg RTC_TSF_DIV_4096  : Tampers Sampling Frequency = RTC_CLK/4096
1519  *                  @arg RTC_TSF_DIV_2048  : Tampers Sampling Frequency = RTC_CLK/2048
1520  *                  @arg RTC_TSF_DIV_1024  : Tampers Sampling Frequency = RTC_CLK/1024
1521  *                  @arg RTC_TSF_DIV_512   : Tampers Sampling Frequency = RTC_CLK/512
1522  *                  @arg RTC_TSF_DIV_256   : Tampers Sampling Frequency = RTC_CLK/256
1523  *
1524  * @retval    None
1525  */
RTC_ConfigSamplingFreq(RTC_TSF_DIV_T freq)1526 void RTC_ConfigSamplingFreq(RTC_TSF_DIV_T freq)
1527 {
1528     RTC->TACFG_B.TPSFSEL = freq;
1529 }
1530 
1531 /*!
1532  * @brief     Config the Precharge Duration.
1533  *
1534  * @param     duration: Specifies the Tampers Pins input  Precharge Duration.
1535  *                  This parameter can be one of the following values:
1536  *                  @arg RTC_TPD_RTCCLK_1 : Tamper pins are pre-charged before sampling during 1 RTCCLK cycle.
1537  *                  @arg RTC_TPD_RTCCLK_2 : Tamper pins are pre-charged before sampling during 2 RTCCLK cycle.
1538  *                  @arg RTC_TPD_RTCCLK_4 : Tamper pins are pre-charged before sampling during 4 RTCCLK cycle.
1539  *                  @arg RTC_TPD_RTCCLK_8 : Tamper pins are pre-charged before sampling during 8 RTCCLK cycle.
1540  *
1541  * @retval    None
1542  */
RTC_PinsPrechargeDuration(RTC_TPD_RTCCLK_T duration)1543 void RTC_PinsPrechargeDuration(RTC_TPD_RTCCLK_T duration)
1544 {
1545     RTC->TACFG_B.TPPRDUSEL = duration;
1546 }
1547 
1548 /*!
1549  * @brief     Enables the TimeStamp on Tamper Detection Event
1550  *
1551  * @param     None
1552  *
1553  * @retval    None
1554  */
RTC_EnableTDE(void)1555 void RTC_EnableTDE(void)
1556 {
1557     RTC->TACFG_B.TPTSEN = BIT_SET;
1558 }
1559 
1560 /*!
1561  * @brief     Disable the TimeStamp on Tamper Detection Event
1562  *
1563  * @param     None
1564  *
1565  * @retval    None
1566  */
RTC_DisableTDE(void)1567 void RTC_DisableTDE(void)
1568 {
1569     RTC->TACFG_B.TPTSEN = BIT_RESET;
1570 }
1571 
1572 /*!
1573  * @brief     Enable pull-up resistors to precharge of the selected Tamper pin.
1574  *
1575  * @param     None
1576  *
1577  * @retval    None
1578  */
RTC_EnablePullUp(void)1579 void RTC_EnablePullUp(void)
1580 {
1581     /* Enable precharge */
1582     RTC->TACFG_B.TPPUDIS = BIT_RESET;
1583 }
1584 
1585 /*!
1586  * @brief     Disable pull-up resistors to precharge of the selected Tamper pin.
1587  *
1588  * @param     None
1589  *
1590  * @retval    None
1591  */
RTC_DisablePullUp(void)1592 void RTC_DisablePullUp(void)
1593 {
1594     /* Disable precharge */
1595     RTC->TACFG_B.TPPUDIS = BIT_SET;
1596 }
1597 
1598 /*!
1599  * @brief     Writes a data in RTC Backup data.
1600  *
1601  * @param     backup: RTC Backup data Register number.
1602  *                    This parameter can be RTC_BAKP_REG_x where x can be from 0 to 19.
1603  *
1604  * @param     data: Data to be written in the specified RTC Backup data register.
1605  *
1606  * @retval    None
1607  */
RTC_WriteBackup(RTC_BAKP_REG_T backupReg,uint32_t data)1608 void RTC_WriteBackup(RTC_BAKP_REG_T backupReg, uint32_t data)
1609 {
1610     RTC->BAKP[backupReg] = (uint32_t)data;
1611 }
1612 
1613 /*!
1614  * @brief     Reads a data in RTC Backup data.
1615  *
1616  * @param     backup: RTC Backup data Register number.
1617  *                    This parameter can be RTC_BAKP_REG_x where x can be from 0 to 19.
1618  *
1619  * @retval    None
1620  */
RTC_ReadBackup(RTC_BAKP_REG_T backupReg)1621 uint32_t RTC_ReadBackup(RTC_BAKP_REG_T backupReg)
1622 {
1623     return RTC->BAKP[backupReg];
1624 }
1625 
1626 /*!
1627  * @brief     Config Tamper Pin.
1628  *
1629  * @param     tamperPin: specifies the RTC Tamper Pin.
1630  *                       This parameter can be one of the following values:
1631  *                       @arg RTC_TAMPER_PIN_AF1 : RTC_AF1(PC13) is used as RTC Tamper Pin.
1632  *                       @arg RTC_TAMPER_PIN_AF2 : RTC_AF2(PI8) is used as RTC Tamper Pin.
1633  *
1634  * @retval    None
1635  */
RTC_ConfigTamperPin(RTC_TAMPER_PIN_T tamperPin)1636 void RTC_ConfigTamperPin(RTC_TAMPER_PIN_T tamperPin)
1637 {
1638     RTC->TACFG_B.TP1MSEL = tamperPin;
1639 }
1640 
1641 /*!
1642  * @brief     Config the RTC TimeStamp Pin.
1643  *
1644  * @param     timeStampPin: specifies the RTC TimeStamp Pin.
1645  *                          This parameter can be one of the following values:
1646  *                          @arg RTC_TIMESTAMP_PIN_AF1 : RTC_AF1(PC13) is used as RTC TimeStamp Pin.
1647  *                          @arg RTC_TIMESTAMP_PIN_AF2 : RTC_AF2(PI8) is used as RTC TimeStamp Pin.
1648  *
1649  * @retval    None
1650  */
RTC_ConfigTimeStampPin(RTC_TIMESTAMP_PIN_T timeStampPin)1651 void RTC_ConfigTimeStampPin(RTC_TIMESTAMP_PIN_T timeStampPin)
1652 {
1653     RTC->TACFG_B.TSMSEL = timeStampPin;
1654 }
1655 
1656 /*!
1657  * @brief     Config the RTC Output Pin mode
1658  *
1659  * @param     outputType: specifies the RTC Output (PC13) pin mode.
1660  *                        This parameter can be one of the following values:
1661  *                        @arg RTC_OUTPUT_OD : RTC_ALARM(PC13) output Open-drain
1662  *                        @arg RTC_OUTPUT_PP : RTC_ALARM(PC13) output Push-pull
1663  *
1664  * @retval    None
1665  */
RTC_ConfigOutputType(RTC_OUTPUT_T outputType)1666 void RTC_ConfigOutputType(RTC_OUTPUT_T outputType)
1667 {
1668     RTC->TACFG_B.ALRMOT = outputType;
1669 }
1670 
1671 /*!
1672  * @brief     Config the Synchronization Shift Control Settings.
1673  *
1674  * @param     add1S:  Select to add or not 1 second to the time Calendar.
1675  *                    This parameter can be one of the following values :
1676  *                    @arg RTC_SHIFT_ADD1S_RESET: No effect.
1677  *                    @arg RTC_SHIFT_ADD1S_SET  : Add one second to the clock calendar.
1678  *
1679  * @param     subFS:  Select the number of Second Fractions to Substitute.
1680  *                    This parameter can be one any value from 0 to 0x7FFF.
1681  *
1682  * @retval    SUCCESS or ERROR
1683  */
RTC_ConfigSynchroShift(RTC_SHIFT_ADD1S_T add1S,uint16_t subFS)1684 uint8_t RTC_ConfigSynchroShift(RTC_SHIFT_ADD1S_T add1S, uint16_t subFS)
1685 {
1686     uint32_t timeout = 0;
1687 
1688     RTC_DisableWriteProtection();
1689 
1690     /* wait for Shift operation flag to be cleared */
1691     while ((RTC->STS_B.SOPFLG != BIT_RESET) && (timeout != RTC_TIMEOUT_SHIFT))
1692     {
1693         timeout++;
1694     }
1695 
1696     /* check if Shift Operation flag is cleared and Reference Clock Detection is disabled */
1697     if ((RTC->STS_B.SOPFLG != BIT_SET) && (RTC->CTRL_B.RCLKDEN != BIT_SET))
1698     {
1699         RTC->SHIFT = ((uint32_t)add1S << 31) | (uint32_t)subFS;
1700 
1701         if (RTC_WaitForSynchro() != ERROR)
1702         {
1703             RTC_EnableWriteProtection();
1704 
1705             return SUCCESS;
1706         }
1707     }
1708 
1709     RTC_EnableWriteProtection();
1710 
1711     return ERROR;
1712 }
1713 
1714 /*!
1715  * @brief     Enable RTC interrupts.
1716  *
1717  * @param     interrupt: specifies the RTC interrupt sources to be enabled
1718  *                     This parameter can be any combination of the following values:
1719  *                      @arg RTC_INT_ALRA : Enable ALRMA A interrupt
1720  *                      @arg RTC_INT_ALRB : Enable ALRMA B interrupt
1721  *                      @arg RTC_INT_WT   : Enable WakeUp Timer interrupt
1722  *                      @arg RTC_INT_TS   : Enable Time Stamp interrupt
1723  *                      @arg RTC_INT_TAMP : Enable Tamper event interrupt
1724  *
1725  * @retval    None
1726  */
RTC_EnableInterrupt(uint32_t interrupt)1727 void RTC_EnableInterrupt(uint32_t interrupt)
1728 {
1729     RTC_DisableWriteProtection();
1730 
1731     RTC->CTRL |= (uint32_t)(interrupt & ~RTC_INT_TAMP);
1732     RTC->TACFG |= (uint32_t)(interrupt & RTC_INT_TAMP);
1733 
1734     RTC_EnableWriteProtection();
1735 }
1736 
1737 /*!
1738  * @brief     Disable RTC interrupts.
1739  *
1740  * @param     interrupt: specifies the RTC interrupt sources to be disable
1741  *                       This parameter can be any combination of the following values:
1742  *                        @arg RTC_INT_ALRA : Disable ALRMA A interrupt
1743  *                        @arg RTC_INT_ALRB : Disable ALRMA B interrupt
1744  *                        @arg RTC_INT_WT   : Disable WakeUp Timer interrupt
1745  *                        @arg RTC_INT_TS   : Disable Time Stamp interrupt
1746  *                        @arg RTC_INT_TAMP : Disable Tamper event interrupt
1747  *
1748  * @retval    None
1749  */
RTC_DisableInterrupt(uint32_t interrupt)1750 void RTC_DisableInterrupt(uint32_t interrupt)
1751 {
1752     RTC_DisableWriteProtection();
1753 
1754     RTC->CTRL &= (uint32_t)~(interrupt & ~RTC_INT_TAMP);
1755     RTC->TACFG &= (uint32_t)~(interrupt & RTC_INT_TAMP);
1756 
1757     RTC_EnableWriteProtection();
1758 }
1759 
1760 /*!
1761  * @brief     Read the specified RTC flag.
1762  *
1763  * @param     flag: specifies the flag to check.
1764  *                  This parameter can be one of the following values:
1765  *                  @arg RTC_FLAG_AAWF : Alarm A Write Flag
1766  *                  @arg RTC_FLAG_ABWF : Alarm B Write Flag
1767  *                  @arg RTC_FLAG_WTWF : Wakeup Timer Write Flag
1768  *                  @arg RTC_FLAG_SOPF : Shift Operation Pending Flag
1769  *                  @arg RTC_FLAG_ISF  : Initialization State Flag
1770  *                  @arg RTC_FLAG_RSF  : Registers Synchronization Flag
1771  *                  @arg RTC_FLAG_INTF : Register Initialization Flag
1772  *                  @arg RTC_FLAG_ALRAF: Alarm A Match Flag
1773  *                  @arg RTC_FLAG_ALRBF: Alarm B Match Flag
1774  *                  @arg RTC_FLAG_WTF  : Wakeup Timer Flag
1775  *                  @arg RTC_FLAG_TSF  : Time Stamp Flag
1776  *                  @arg RTC_FLAG_TSOF : Time Stamp Overflow Flag
1777  *                  @arg RTC_FLAG_TP1F : Tamper 1 event Detection Flag
1778  *                  @arg RTC_FLAG_TP2F : Tamper 2 event Detection Flag
1779  *                  @arg RTC_FLAG_RPF  : Recalibration Pending Flag
1780  *
1781  * @retval    SET or RESET.
1782  */
RTC_ReadStatusFlag(RTC_FLAG_T flag)1783 uint8_t RTC_ReadStatusFlag(RTC_FLAG_T flag)
1784 {
1785     return (RTC->STS & flag) ? SET : RESET;
1786 }
1787 
1788 /*!
1789  * @brief     Clears the RTC's status flags.
1790  * @param     flag: specifies the RTC flag to clear.
1791  *                  This parameter can be any combination of the following values:
1792  *                  @arg RTC_FLAG_RSF   : Registers Synchronization Flag
1793  *                  @arg RTC_FLAG_ALRAF : Alarm A Match Flag
1794  *                  @arg RTC_FLAG_ALRBF : Alarm B Match Flag
1795  *                  @arg RTC_FLAG_WTF   : Wakeup Timer Flag
1796  *                  @arg RTC_FLAG_TSF   : Time Stamp Flag
1797  *                  @arg RTC_FLAG_TSOF  : Time Stamp Overflow Flag
1798  *                  @arg RTC_FLAG_TP1F  : Tamper 1 event Detection Flag
1799  *                  @arg RTC_FLAG_TP2F  : Tamper 2 event Detection Flag
1800  */
RTC_ClearStatusFlag(uint32_t flag)1801 void RTC_ClearStatusFlag(uint32_t flag)
1802 {
1803     RTC->STS &= (uint32_t)~flag;
1804 }
1805 
1806 /*!
1807  * @brief     Read interrupt flag bit is set
1808  *
1809  * @param     flag: specifies the flag to read.
1810  *                  This parameter can be one of the following values:
1811  *                  @arg RTC_INT_FLAG_ALRA  : Alarm A interrupt
1812  *                  @arg RTC_INT_FLAG_ALRB  : Alarm B interrupt
1813  *                  @arg RTC_INT_FLAG_WT    : WakeUp Timer interrupt
1814  *                  @arg RTC_INT_FLAG_TS    : Time Stamp interrupt
1815  *                  @arg RTC_INT_FLAG_TAMP1 : Tamper1 event interrupt
1816  *                  @arg RTC_INT_FLAG_TAMP2 : Tamper2 event interrupt
1817  *
1818  * @retval    The new state of flag (SET or RESET).
1819  */
RTC_ReadIntFlag(RTC_INT_FLAG_T flag)1820 uint8_t RTC_ReadIntFlag(RTC_INT_FLAG_T flag)
1821 {
1822     uint8_t intEnable;
1823     uint32_t intStatus;
1824 
1825     if (flag & 0x01)
1826     {
1827         intEnable = RTC->TACFG_B.TPIEN;
1828         intStatus = (uint32_t)(RTC->STS & (flag >> 4));
1829     }
1830     else
1831     {
1832         intEnable = (RTC->CTRL & flag) ? SET : RESET;
1833         intStatus = (uint32_t)(RTC->STS & (flag >> 4));
1834     }
1835 
1836     if (intEnable && intStatus)
1837     {
1838         return SET;
1839     }
1840 
1841     return RESET;
1842 }
1843 
1844 /*!
1845  * @brief     Clear RTC interrupt flag bit
1846  *
1847  * @param     flag: specifies the flag to clear.
1848  *                  This parameter can be any combination the following values:
1849  *                  @arg RTC_INT_FLAG_ALRA  : ALRMA interrupt
1850  *                  @arg RTC_INT_FLAG_ALRB  : ALRMB interrupt
1851  *                  @arg RTC_INT_FLAG_TS    : Time Stamp interrupt
1852  *                  @arg RTC_INT_FLAG_WT    : WakeUp Timer interrupt
1853  *                  @arg RTC_INT_FLAG_TAMP1 : Tamper1 event interrupt
1854  *                  @arg RTC_INT_FLAG_TAMP2 : Tamper2 event interrupt
1855  *
1856  * @retval    SET or RESET
1857  */
RTC_ClearIntFlag(uint32_t flag)1858 void RTC_ClearIntFlag(uint32_t flag)
1859 {
1860     RTC->STS &= (uint32_t) ~(flag >> 4);
1861 }
1862 
1863 /*!
1864  * @brief     Converts a 2 digit decimal to BCD format
1865  *
1866  * @param     val: Byte to be converted
1867  *
1868  * @retval    Converted byte
1869  */
RTC_ByteConBcd2(uint8_t val)1870 static uint8_t RTC_ByteConBcd2(uint8_t val)
1871 {
1872     uint8_t bcdhigh = 0;
1873 
1874     while (val >= 10)
1875     {
1876         bcdhigh++;
1877         val -= 10;
1878     }
1879 
1880     return ((uint8_t)(bcdhigh << 4) | val);
1881 }
1882 
1883 /*!
1884  * @brief     Convert from 2 digit BCD to Binary
1885  *
1886  * @param     val: BCD value to be converted
1887  *
1888  * @retval    Converted word
1889  */
RTC_Bcd2ConByte(uint8_t val)1890 static uint8_t RTC_Bcd2ConByte(uint8_t val)
1891 {
1892     uint8_t tmp = 0;
1893     tmp = ((uint8_t)(val & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1894     return (tmp + (val & (uint8_t)0x0F));
1895 }
1896 
1897 /**@} end of group RTC_Functions */
1898 /**@} end of group RTC_Driver */
1899 /**@} end of group APM32F4xx_StdPeriphDriver */
1900