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