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
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, RTC_DateStruct->Year);
380     }
381     else
382     {
383       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
384                          __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
385     }
386 
387     /* Exit Initialization mode */
388     LL_RTC_DisableInitMode(RTC);
389 
390     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
391     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
392     {
393       status = LL_RTC_WaitForSynchro(RTCx);
394     }
395     else
396     {
397       status = SUCCESS;
398     }
399   }
400   /* Enable the write protection for RTC registers */
401   LL_RTC_EnableWriteProtection(RTCx);
402 
403   return status;
404 }
405 
406 /**
407   * @brief  Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
408   * @param  RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
409   * @retval None
410   */
LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef * RTC_DateStruct)411 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
412 {
413   /* Monday, January 01 xx00 */
414   RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
415   RTC_DateStruct->Day     = 1U;
416   RTC_DateStruct->Month   = LL_RTC_MONTH_JANUARY;
417   RTC_DateStruct->Year    = 0U;
418 }
419 
420 /**
421   * @brief  Set the RTC Alarm A.
422   * @note   The Alarm register can only be written when the corresponding Alarm
423   *         is disabled (Use @ref LL_RTC_ALMA_Disable function).
424   * @param  RTCx RTC Instance
425   * @param  RTC_Format This parameter can be one of the following values:
426   *         @arg @ref LL_RTC_FORMAT_BIN
427   *         @arg @ref LL_RTC_FORMAT_BCD
428   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
429   *                         contains the alarm configuration parameters.
430   * @retval An ErrorStatus enumeration value:
431   *          - SUCCESS: ALARMA registers are configured
432   *          - ERROR: ALARMA registers are not configured
433   */
LL_RTC_ALMA_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_AlarmTypeDef * RTC_AlarmStruct)434 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
435 {
436   /* Check the parameters */
437   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
438   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
439   assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
440   assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
441 
442   if (RTC_Format == LL_RTC_FORMAT_BIN)
443   {
444     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
445     {
446       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
447       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
448     }
449     else
450     {
451       RTC_AlarmStruct->AlarmTime.TimeFormat = 0U;
452       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
453     }
454     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
455     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
456 
457     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
458     {
459       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
460     }
461     else
462     {
463       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
464     }
465   }
466   else
467   {
468     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
469     {
470       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
471       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
472     }
473     else
474     {
475       RTC_AlarmStruct->AlarmTime.TimeFormat = 0U;
476       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
477     }
478 
479     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
480     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
481 
482     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
483     {
484       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
485     }
486     else
487     {
488       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
489     }
490   }
491 
492   /* Disable the write protection for RTC registers */
493   LL_RTC_DisableWriteProtection(RTCx);
494 
495   /* Select weekday selection */
496   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
497   {
498     /* Set the date for ALARM */
499     LL_RTC_ALMA_DisableWeekday(RTCx);
500     if (RTC_Format != LL_RTC_FORMAT_BIN)
501     {
502       LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
503     }
504     else
505     {
506       LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
507     }
508   }
509   else
510   {
511     /* Set the week day for ALARM */
512     LL_RTC_ALMA_EnableWeekday(RTCx);
513     LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
514   }
515 
516   /* Configure the Alarm register */
517   if (RTC_Format != LL_RTC_FORMAT_BIN)
518   {
519     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
520                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
521   }
522   else
523   {
524     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
525                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
526                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
527                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
528   }
529   /* Set ALARM mask */
530   LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
531 
532   /* Enable the write protection for RTC registers */
533   LL_RTC_EnableWriteProtection(RTCx);
534 
535   return SUCCESS;
536 }
537 
538 /**
539   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
540   *         Day = 1st day of the month/Mask = all fields are masked).
541   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
542   * @retval None
543   */
LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef * RTC_AlarmStruct)544 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
545 {
546   /* Alarm Time Settings : Time = 00h:00mn:00sec */
547   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
548   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
549   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
550   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
551 
552   /* Alarm Day Settings : Day = 1st day of the month */
553   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
554   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
555 
556   /* Alarm Masks Settings : Mask =  all fields are not masked */
557   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMA_MASK_NONE;
558 }
559 
560 /**
561   * @brief  Enters the RTC Initialization mode.
562   * @note   The RTC Initialization mode is write protected, use the
563   *         @ref LL_RTC_DisableWriteProtection before calling this function.
564   * @param  RTCx RTC Instance
565   * @retval An ErrorStatus enumeration value:
566   *          - SUCCESS: RTC is in Init mode
567   *          - ERROR: RTC is not in Init mode
568   */
LL_RTC_EnterInitMode(RTC_TypeDef * RTCx)569 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
570 {
571   __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
572   ErrorStatus status = SUCCESS;
573   uint32_t tmp;
574 
575   /* Check the parameter */
576   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
577 
578   /* Check if the Initialization mode is set */
579   if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
580   {
581     /* Set the Initialization mode */
582     LL_RTC_EnableInitMode(RTCx);
583 
584     /* Wait till RTC is in INIT state and if Time out is reached exit */
585     tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
586     while ((timeout != 0U) && (tmp != 1U))
587     {
588       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
589       {
590         timeout --;
591       }
592       tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
593       if (timeout == 0U)
594       {
595         status = ERROR;
596       }
597     }
598   }
599   return status;
600 }
601 
602 /**
603   * @brief  Exit the RTC Initialization mode.
604   * @note   When the initialization sequence is complete, the calendar restarts
605   *         counting after 4 RTCCLK cycles.
606   * @note   The RTC Initialization mode is write protected, use the
607   *         @ref LL_RTC_DisableWriteProtection before calling this function.
608   * @param  RTCx RTC Instance
609   * @retval An ErrorStatus enumeration value:
610   *          - SUCCESS: RTC exited from in Init mode
611   *          - ERROR: Not applicable
612   */
LL_RTC_ExitInitMode(RTC_TypeDef * RTCx)613 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
614 {
615   /* Check the parameter */
616   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
617 
618   /* Disable initialization mode */
619   LL_RTC_DisableInitMode(RTCx);
620 
621   return SUCCESS;
622 }
623 
624 /**
625   * @brief  Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
626   *         synchronized with RTC APB clock.
627   * @note   The RTC Resynchronization mode is write protected, use the
628   *         @ref LL_RTC_DisableWriteProtection before calling this function.
629   * @note   To read the calendar through the shadow registers after Calendar
630   *         initialization, calendar update or after wakeup from low power modes
631   *         the software must first clear the RSF flag.
632   *         The software must then wait until it is set again before reading
633   *         the calendar, which means that the calendar registers have been
634   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
635   * @param  RTCx RTC Instance
636   * @retval An ErrorStatus enumeration value:
637   *          - SUCCESS: RTC registers are synchronised
638   *          - ERROR: RTC registers are not synchronised
639   */
LL_RTC_WaitForSynchro(RTC_TypeDef * RTCx)640 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
641 {
642   __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
643   ErrorStatus status = SUCCESS;
644   uint32_t tmp;
645 
646   /* Check the parameter */
647   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
648 
649   /* Clear RSF flag */
650   LL_RTC_ClearFlag_RS(RTCx);
651 
652   /* Wait the registers to be synchronised */
653   tmp = LL_RTC_IsActiveFlag_RS(RTCx);
654   while ((timeout != 0U) && (tmp != 0U))
655   {
656     if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
657     {
658       timeout--;
659     }
660     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
661     if (timeout == 0U)
662     {
663       status = ERROR;
664     }
665   }
666 
667   if (status != ERROR)
668   {
669     timeout = RTC_SYNCHRO_TIMEOUT;
670     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
671     while ((timeout != 0U) && (tmp != 1U))
672     {
673       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
674       {
675         timeout--;
676       }
677       tmp = LL_RTC_IsActiveFlag_RS(RTCx);
678       if (timeout == 0U)
679       {
680         status = ERROR;
681       }
682     }
683   }
684 
685   return (status);
686 }
687 
688 /**
689   * @}
690   */
691 
692 /**
693   * @}
694   */
695 
696 /**
697   * @}
698   */
699 
700 #endif /* defined(RTC) */
701 
702 /**
703   * @}
704   */
705 
706 #endif /* USE_FULL_LL_DRIVER */
707