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