1 /**
2   ******************************************************************************
3   * @file    stm32c0xx_ll_rtc.c
4   * @author  MCD Application Team
5   * @brief   RTC LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2022 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32c0xx_ll_rtc.h"
22 #include "stm32c0xx_ll_cortex.h"
23 #ifdef  USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else
26 #define assert_param(expr) ((void)0U)
27 #endif /* USE_FULL_ASSERT */
28 
29 /** @addtogroup STM32C0xx_LL_Driver
30   * @{
31   */
32 
33 #if defined(RTC)
34 
35 /** @addtogroup RTC_LL
36   * @{
37   */
38 
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /** @addtogroup RTC_LL_Private_Constants
43   * @{
44   */
45 /* Default values used for prescaler */
46 #define RTC_ASYNCH_PRESC_DEFAULT     0x0000007FU
47 #define RTC_SYNCH_PRESC_DEFAULT      0x000000FFU
48 
49 /* Values used for timeout */
50 #define RTC_INITMODE_TIMEOUT         1000U /* 1s when tick set to 1ms */
51 #define RTC_SYNCHRO_TIMEOUT          1000U /* 1s when tick set to 1ms */
52 /**
53   * @}
54   */
55 
56 /* Private macros ------------------------------------------------------------*/
57 /** @addtogroup RTC_LL_Private_Macros
58   * @{
59   */
60 
61 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
62                                          || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
63 
64 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__)   ((__VALUE__) <= 0x7FU)
65 
66 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__)    ((__VALUE__) <= 0x7FFFU)
67 
68 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
69                                      || ((__VALUE__) == LL_RTC_FORMAT_BCD))
70 
71 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
72                                           || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
73 
74 #define IS_LL_RTC_HOUR12(__HOUR__)            (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
75 #define IS_LL_RTC_HOUR24(__HOUR__)            ((__HOUR__) <= 23U)
76 #define IS_LL_RTC_MINUTES(__MINUTES__)        ((__MINUTES__) <= 59U)
77 #define IS_LL_RTC_SECONDS(__SECONDS__)        ((__SECONDS__) <= 59U)
78 
79 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
80                                       || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
81                                       || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
82                                       || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
83                                       || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
84                                       || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
85                                       || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
86 
87 #define IS_LL_RTC_DAY(__DAY__)    (((__DAY__) >= 1U) && ((__DAY__) <= 31U))
88 
89 #define IS_LL_RTC_MONTH(__VALUE__) (((__VALUE__) == LL_RTC_MONTH_JANUARY) \
90                                     || ((__VALUE__) == LL_RTC_MONTH_FEBRUARY) \
91                                     || ((__VALUE__) == LL_RTC_MONTH_MARCH) \
92                                     || ((__VALUE__) == LL_RTC_MONTH_APRIL) \
93                                     || ((__VALUE__) == LL_RTC_MONTH_MAY) \
94                                     || ((__VALUE__) == LL_RTC_MONTH_JUNE) \
95                                     || ((__VALUE__) == LL_RTC_MONTH_JULY) \
96                                     || ((__VALUE__) == LL_RTC_MONTH_AUGUST) \
97                                     || ((__VALUE__) == LL_RTC_MONTH_SEPTEMBER) \
98                                     || ((__VALUE__) == LL_RTC_MONTH_OCTOBER) \
99                                     || ((__VALUE__) == LL_RTC_MONTH_NOVEMBER) \
100                                     || ((__VALUE__) == LL_RTC_MONTH_DECEMBER))
101 
102 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
103 
104 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
105                                         || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
106                                         || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
107                                         || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
108                                         || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
109                                         || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
110 
111 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
112                                                   ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
113 
114 /**
115   * @}
116   */
117 /* Private function prototypes -----------------------------------------------*/
118 /* Exported functions --------------------------------------------------------*/
119 /** @addtogroup RTC_LL_Exported_Functions
120   * @{
121   */
122 
123 /** @addtogroup RTC_LL_EF_Init
124   * @{
125   */
126 
127 /**
128   * @brief  De-Initializes the RTC registers to their default reset values.
129   * @note   This function does not reset the RTC Clock source and RTC Backup Data
130   *         registers.
131   * @param  RTCx RTC Instance
132   * @retval An ErrorStatus enumeration value:
133   *          - SUCCESS: RTC registers are de-initialized
134   *          - ERROR: RTC registers are not de-initialized
135   */
LL_RTC_DeInit(RTC_TypeDef * RTCx)136 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
137 {
138   ErrorStatus status = ERROR;
139 
140   /* Check the parameter */
141   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
142 
143   /* Disable the write protection for RTC registers */
144   LL_RTC_DisableWriteProtection(RTCx);
145 
146   /* Set Initialization mode */
147   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
148   {
149     /* Reset TR, DR and CR registers */
150     WRITE_REG(RTCx->TR,       0U);
151     WRITE_REG(RTCx->DR, (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
152     WRITE_REG(RTCx->PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
153     WRITE_REG(RTCx->ALRMAR,   0U);
154     WRITE_REG(RTCx->SHIFTR,   0U);
155     WRITE_REG(RTCx->CALR,     0U);
156     WRITE_REG(RTCx->ALRMASSR, 0U);
157     /* Exit Initialization mode */
158     LL_RTC_DisableInitMode(RTCx);
159 
160     /* Wait till the RTC RSF flag is set */
161     status = LL_RTC_WaitForSynchro(RTCx);
162   }
163 
164   /* Enable the write protection for RTC registers */
165   LL_RTC_EnableWriteProtection(RTCx);
166 
167   return status;
168 }
169 
170 /**
171   * @brief  Initializes the RTC registers according to the specified parameters
172   *         in RTC_InitStruct.
173   * @param  RTCx RTC Instance
174   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
175   *         the configuration information for the RTC peripheral.
176   * @note   The RTC Prescaler register is write protected and can be written in
177   *         initialization mode only.
178   * @retval An ErrorStatus enumeration value:
179   *          - SUCCESS: RTC registers are initialized
180   *          - ERROR: RTC registers are not initialized
181   */
LL_RTC_Init(RTC_TypeDef * RTCx,LL_RTC_InitTypeDef * RTC_InitStruct)182 ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct)
183 {
184   ErrorStatus status = ERROR;
185 
186   /* Check the parameters */
187   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
188   assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat));
189   assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler));
190   assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler));
191 
192   /* Disable the write protection for RTC registers */
193   LL_RTC_DisableWriteProtection(RTCx);
194 
195   /* Set Initialization mode */
196   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
197   {
198     /* Set Hour Format */
199     LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat);
200 
201     /* Configure Synchronous and Asynchronous prescaler factor */
202     LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler);
203     LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler);
204 
205     /* Exit Initialization mode */
206     LL_RTC_DisableInitMode(RTCx);
207 
208     status = SUCCESS;
209   }
210   /* Enable the write protection for RTC registers */
211   LL_RTC_EnableWriteProtection(RTCx);
212 
213   return status;
214 }
215 
216 /**
217   * @brief  Set each @ref LL_RTC_InitTypeDef field to default value.
218   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
219   * @retval None
220   */
LL_RTC_StructInit(LL_RTC_InitTypeDef * RTC_InitStruct)221 void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct)
222 {
223   /* Set RTC_InitStruct fields to default values */
224   RTC_InitStruct->HourFormat      = LL_RTC_HOURFORMAT_24HOUR;
225   RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT;
226   RTC_InitStruct->SynchPrescaler  = RTC_SYNCH_PRESC_DEFAULT;
227 }
228 
229 /**
230   * @brief  Set the RTC current time.
231   * @param  RTCx RTC Instance
232   * @param  RTC_Format This parameter can be one of the following values:
233   *         @arg @ref LL_RTC_FORMAT_BIN
234   *         @arg @ref LL_RTC_FORMAT_BCD
235   * @param  RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
236   *                        the time configuration information for the RTC.
237   * @retval An ErrorStatus enumeration value:
238   *          - SUCCESS: RTC Time register is configured
239   *          - ERROR: RTC Time register is not configured
240   */
LL_RTC_TIME_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_TimeTypeDef * RTC_TimeStruct)241 ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct)
242 {
243   ErrorStatus status = ERROR;
244 
245   /* Check the parameters */
246   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
247   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
248 
249   if (RTC_Format == LL_RTC_FORMAT_BIN)
250   {
251     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
252     {
253       assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours));
254       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
255     }
256     else
257     {
258       RTC_TimeStruct->TimeFormat = 0U;
259       assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours));
260     }
261     assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes));
262     assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds));
263   }
264   else
265   {
266     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
267     {
268       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
269       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
270     }
271     else
272     {
273       RTC_TimeStruct->TimeFormat = 0U;
274       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
275     }
276     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes)));
277     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds)));
278   }
279 
280   /* Disable the write protection for RTC registers */
281   LL_RTC_DisableWriteProtection(RTCx);
282 
283   /* Set Initialization mode */
284   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
285   {
286     /* Check the input parameters format */
287     if (RTC_Format != LL_RTC_FORMAT_BIN)
288     {
289       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours,
290                          RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds);
291     }
292     else
293     {
294       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours),
295                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes),
296                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds));
297     }
298 
299     /* Exit Initialization mode */
300     LL_RTC_DisableInitMode(RTC);
301 
302     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
303     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
304     {
305       status = LL_RTC_WaitForSynchro(RTCx);
306     }
307     else
308     {
309       status = SUCCESS;
310     }
311   }
312   /* Enable the write protection for RTC registers */
313   LL_RTC_EnableWriteProtection(RTCx);
314 
315   return status;
316 }
317 
318 /**
319   * @brief  Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
320   * @param  RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
321   * @retval None
322   */
LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef * RTC_TimeStruct)323 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
324 {
325   /* Time = 00h:00min:00sec */
326   RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
327   RTC_TimeStruct->Hours      = 0U;
328   RTC_TimeStruct->Minutes    = 0U;
329   RTC_TimeStruct->Seconds    = 0U;
330 }
331 
332 /**
333   * @brief  Set the RTC current date.
334   * @param  RTCx RTC Instance
335   * @param  RTC_Format This parameter can be one of the following values:
336   *         @arg @ref LL_RTC_FORMAT_BIN
337   *         @arg @ref LL_RTC_FORMAT_BCD
338   * @param  RTC_DateStruct pointer to a RTC_DateTypeDef structure that contains
339   *                         the date configuration information for the RTC.
340   * @retval An ErrorStatus enumeration value:
341   *          - SUCCESS: RTC Day register is configured
342   *          - ERROR: RTC Day register is not configured
343   */
LL_RTC_DATE_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_DateTypeDef * RTC_DateStruct)344 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
345 {
346   ErrorStatus status = ERROR;
347 
348   /* Check the parameters */
349   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
350   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
351 
352   if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
353   {
354     RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint8_t)~(0x10U)) + 0x0AU;
355   }
356   if (RTC_Format == LL_RTC_FORMAT_BIN)
357   {
358     assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
359     assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
360     assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
361   }
362   else
363   {
364     assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
365     assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
366     assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
367   }
368   assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
369 
370   /* Disable the write protection for RTC registers */
371   LL_RTC_DisableWriteProtection(RTCx);
372 
373   /* Set Initialization mode */
374   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
375   {
376     /* Check the input parameters format */
377     if (RTC_Format != LL_RTC_FORMAT_BIN)
378     {
379       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month,
380                          RTC_DateStruct->Year);
381     }
382     else
383     {
384       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
385                          __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month),
386                          __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
387     }
388 
389     /* Exit Initialization mode */
390     LL_RTC_DisableInitMode(RTC);
391 
392     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
393     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
394     {
395       status = LL_RTC_WaitForSynchro(RTCx);
396     }
397     else
398     {
399       status = SUCCESS;
400     }
401   }
402   /* Enable the write protection for RTC registers */
403   LL_RTC_EnableWriteProtection(RTCx);
404 
405   return status;
406 }
407 
408 /**
409   * @brief  Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
410   * @param  RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
411   * @retval None
412   */
LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef * RTC_DateStruct)413 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
414 {
415   /* Monday, January 01 xx00 */
416   RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
417   RTC_DateStruct->Day     = 1U;
418   RTC_DateStruct->Month   = LL_RTC_MONTH_JANUARY;
419   RTC_DateStruct->Year    = 0U;
420 }
421 
422 /**
423   * @brief  Set the RTC Alarm A.
424   * @note   The Alarm register can only be written when the corresponding Alarm
425   *         is disabled (Use @ref LL_RTC_ALMA_Disable function).
426   * @param  RTCx RTC Instance
427   * @param  RTC_Format This parameter can be one of the following values:
428   *         @arg @ref LL_RTC_FORMAT_BIN
429   *         @arg @ref LL_RTC_FORMAT_BCD
430   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
431   *                         contains the alarm configuration parameters.
432   * @retval An ErrorStatus enumeration value:
433   *          - SUCCESS: ALARMA registers are configured
434   *          - ERROR: ALARMA registers are not configured
435   */
LL_RTC_ALMA_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_AlarmTypeDef * RTC_AlarmStruct)436 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
437 {
438   /* Check the parameters */
439   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
440   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
441   assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
442   assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
443 
444   if (RTC_Format == LL_RTC_FORMAT_BIN)
445   {
446     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
447     {
448       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
449       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
450     }
451     else
452     {
453       RTC_AlarmStruct->AlarmTime.TimeFormat = 0U;
454       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
455     }
456     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
457     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
458 
459     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
460     {
461       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
462     }
463     else
464     {
465       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
466     }
467   }
468   else
469   {
470     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
471     {
472       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
473       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
474     }
475     else
476     {
477       RTC_AlarmStruct->AlarmTime.TimeFormat = 0U;
478       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
479     }
480 
481     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
482     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
483 
484     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
485     {
486       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
487     }
488     else
489     {
490       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
491     }
492   }
493 
494   /* Disable the write protection for RTC registers */
495   LL_RTC_DisableWriteProtection(RTCx);
496 
497   /* Select weekday selection */
498   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
499   {
500     /* Set the date for ALARM */
501     LL_RTC_ALMA_DisableWeekday(RTCx);
502     if (RTC_Format != LL_RTC_FORMAT_BIN)
503     {
504       LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
505     }
506     else
507     {
508       LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
509     }
510   }
511   else
512   {
513     /* Set the week day for ALARM */
514     LL_RTC_ALMA_EnableWeekday(RTCx);
515     LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
516   }
517 
518   /* Configure the Alarm register */
519   if (RTC_Format != LL_RTC_FORMAT_BIN)
520   {
521     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
522                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
523   }
524   else
525   {
526     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
527                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
528                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
529                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
530   }
531   /* Set ALARM mask */
532   LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
533 
534   /* Enable the write protection for RTC registers */
535   LL_RTC_EnableWriteProtection(RTCx);
536 
537   return SUCCESS;
538 }
539 
540 /**
541   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
542   *         Day = 1st day of the month/Mask = all fields are masked).
543   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
544   * @retval None
545   */
LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef * RTC_AlarmStruct)546 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
547 {
548   /* Alarm Time Settings : Time = 00h:00mn:00sec */
549   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
550   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
551   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
552   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
553 
554   /* Alarm Day Settings : Day = 1st day of the month */
555   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
556   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
557 
558   /* Alarm Masks Settings : Mask =  all fields are not masked */
559   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMA_MASK_NONE;
560 }
561 
562 /**
563   * @brief  Enters the RTC Initialization mode.
564   * @note   The RTC Initialization mode is write protected, use the
565   *         @ref LL_RTC_DisableWriteProtection before calling this function.
566   * @param  RTCx RTC Instance
567   * @retval An ErrorStatus enumeration value:
568   *          - SUCCESS: RTC is in Init mode
569   *          - ERROR: RTC is not in Init mode
570   */
LL_RTC_EnterInitMode(RTC_TypeDef * RTCx)571 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
572 {
573   __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
574   ErrorStatus status = SUCCESS;
575   uint32_t tmp;
576 
577   /* Check the parameter */
578   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
579 
580   /* Check if the Initialization mode is set */
581   if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
582   {
583     /* Set the Initialization mode */
584     LL_RTC_EnableInitMode(RTCx);
585 
586     /* Wait till RTC is in INIT state and if Time out is reached exit */
587     tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
588     while ((timeout != 0U) && (tmp != 1U))
589     {
590       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
591       {
592         timeout --;
593       }
594       tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
595       if (timeout == 0U)
596       {
597         status = ERROR;
598       }
599     }
600   }
601   return status;
602 }
603 
604 /**
605   * @brief  Exit the RTC Initialization mode.
606   * @note   When the initialization sequence is complete, the calendar restarts
607   *         counting after 4 RTCCLK cycles.
608   * @note   The RTC Initialization mode is write protected, use the
609   *         @ref LL_RTC_DisableWriteProtection before calling this function.
610   * @param  RTCx RTC Instance
611   * @retval An ErrorStatus enumeration value:
612   *          - SUCCESS: RTC exited from in Init mode
613   *          - ERROR: Not applicable
614   */
LL_RTC_ExitInitMode(RTC_TypeDef * RTCx)615 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
616 {
617   /* Check the parameter */
618   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
619 
620   /* Disable initialization mode */
621   LL_RTC_DisableInitMode(RTCx);
622 
623   return SUCCESS;
624 }
625 
626 /**
627   * @brief  Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
628   *         synchronized with RTC APB clock.
629   * @note   The RTC Resynchronization mode is write protected, use the
630   *         @ref LL_RTC_DisableWriteProtection before calling this function.
631   * @note   To read the calendar through the shadow registers after Calendar
632   *         initialization, calendar update or after wakeup from low power modes
633   *         the software must first clear the RSF flag.
634   *         The software must then wait until it is set again before reading
635   *         the calendar, which means that the calendar registers have been
636   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
637   * @param  RTCx RTC Instance
638   * @retval An ErrorStatus enumeration value:
639   *          - SUCCESS: RTC registers are synchronised
640   *          - ERROR: RTC registers are not synchronised
641   */
LL_RTC_WaitForSynchro(RTC_TypeDef * RTCx)642 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
643 {
644   __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
645   ErrorStatus status = SUCCESS;
646   uint32_t tmp;
647 
648   /* Check the parameter */
649   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
650 
651   /* Clear RSF flag */
652   LL_RTC_ClearFlag_RS(RTCx);
653 
654   /* Wait the registers to be synchronised */
655   tmp = LL_RTC_IsActiveFlag_RS(RTCx);
656   while ((timeout != 0U) && (tmp != 0U))
657   {
658     if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
659     {
660       timeout--;
661     }
662     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
663     if (timeout == 0U)
664     {
665       status = ERROR;
666     }
667   }
668 
669   if (status != ERROR)
670   {
671     timeout = RTC_SYNCHRO_TIMEOUT;
672     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
673     while ((timeout != 0U) && (tmp != 1U))
674     {
675       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
676       {
677         timeout--;
678       }
679       tmp = LL_RTC_IsActiveFlag_RS(RTCx);
680       if (timeout == 0U)
681       {
682         status = ERROR;
683       }
684     }
685   }
686 
687   return (status);
688 }
689 
690 /**
691   * @}
692   */
693 
694 /**
695   * @}
696   */
697 
698 /**
699   * @}
700   */
701 
702 #endif /* defined(RTC) */
703 
704 /**
705   * @}
706   */
707 
708 #endif /* USE_FULL_LL_DRIVER */
709