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