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