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