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