1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_ll_rtc.c
4   * @author  MCD Application Team
5   * @brief   RTC LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2016 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 
20 #if defined(USE_FULL_LL_DRIVER)
21 
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f2xx_ll_rtc.h"
24 #include "stm32f2xx_ll_cortex.h"
25 #ifdef  USE_FULL_ASSERT
26 #include "stm32_assert.h"
27 #else
28 #define assert_param(expr) ((void)0U)
29 #endif
30 
31 /** @addtogroup STM32F2xx_LL_Driver
32   * @{
33   */
34 
35 #if defined(RTC)
36 
37 /** @addtogroup RTC_LL
38   * @{
39   */
40 
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
44 /** @addtogroup RTC_LL_Private_Constants
45   * @{
46   */
47 /* Default values used for prescaler */
48 #define RTC_ASYNCH_PRESC_DEFAULT     0x0000007FU
49 #define RTC_SYNCH_PRESC_DEFAULT      0x000000FFU
50 
51 /* Values used for timeout */
52 #define RTC_INITMODE_TIMEOUT         1000U /* 1s when tick set to 1ms */
53 #define RTC_SYNCHRO_TIMEOUT          1000U /* 1s when tick set to 1ms */
54 /**
55   * @}
56   */
57 
58 /* Private macros ------------------------------------------------------------*/
59 /** @addtogroup RTC_LL_Private_Macros
60   * @{
61   */
62 
63 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
64                                       || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
65 
66 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__)   ((__VALUE__) <= 0x7FU)
67 
68 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__)    ((__VALUE__) <= 0x1FFFU)
69 
70 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
71                                   || ((__VALUE__) == LL_RTC_FORMAT_BCD))
72 
73 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
74                                        || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
75 
76 #define IS_LL_RTC_HOUR12(__HOUR__)            (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
77 #define IS_LL_RTC_HOUR24(__HOUR__)            ((__HOUR__) <= 23U)
78 #define IS_LL_RTC_MINUTES(__MINUTES__)        ((__MINUTES__) <= 59U)
79 #define IS_LL_RTC_SECONDS(__SECONDS__)        ((__SECONDS__) <= 59U)
80 
81 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
82                                    || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
83                                    || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
84                                    || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
85                                    || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
86                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
87                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
88 
89 #define IS_LL_RTC_DAY(__DAY__)    (((__DAY__) >= 1U) && ((__DAY__) <= 31U))
90 
91 #define IS_LL_RTC_MONTH(__MONTH__) (((__MONTH__) >= 1U) && ((__MONTH__) <= 12U))
92 
93 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
94 
95 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
96                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
97                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
98                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
99                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
100                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
101 
102 #define IS_LL_RTC_ALMB_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMB_MASK_NONE) \
103                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_DATEWEEKDAY) \
104                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_HOURS) \
105                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_MINUTES) \
106                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_SECONDS) \
107                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_ALL))
108 
109 
110 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
111                                                   ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
112 
113 #define IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) || \
114                                                   ((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_WEEKDAY))
115 
116 
117 /**
118   * @}
119   */
120 /* Private function prototypes -----------------------------------------------*/
121 /* Exported functions --------------------------------------------------------*/
122 /** @addtogroup RTC_LL_Exported_Functions
123   * @{
124   */
125 
126 /** @addtogroup RTC_LL_EF_Init
127   * @{
128   */
129 
130 /**
131   * @brief  De-Initializes the RTC registers to their default reset values.
132   * @note   This function doesn't reset the RTC Clock source and RTC Backup Data
133   *         registers.
134   * @param  RTCx RTC Instance
135   * @retval An ErrorStatus enumeration value:
136   *          - SUCCESS: RTC registers are de-initialized
137   *          - ERROR: RTC registers are not de-initialized
138   */
LL_RTC_DeInit(RTC_TypeDef * RTCx)139 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
140 {
141   ErrorStatus status = ERROR;
142 
143   /* Check the parameter */
144   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
145 
146   /* Disable the write protection for RTC registers */
147   LL_RTC_DisableWriteProtection(RTCx);
148 
149   /* Set Initialization mode */
150   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
151   {
152     /* Reset TR, DR and CR registers */
153     LL_RTC_WriteReg(RTCx, TR,       0x00000000U);
154 #if defined(RTC_WAKEUP_SUPPORT)
155     LL_RTC_WriteReg(RTCx, WUTR,     RTC_WUTR_WUT);
156 #endif /* RTC_WAKEUP_SUPPORT */
157     LL_RTC_WriteReg(RTCx, DR, (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
158     /* Reset All CR bits except CR[2:0] */
159 #if defined(RTC_WAKEUP_SUPPORT)
160     LL_RTC_WriteReg(RTCx, CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL));
161 #else
162     LL_RTC_WriteReg(RTCx, CR, 0x00000000U);
163 #endif /* RTC_WAKEUP_SUPPORT */
164     LL_RTC_WriteReg(RTCx, PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
165     LL_RTC_WriteReg(RTCx, ALRMAR,   0x00000000U);
166     LL_RTC_WriteReg(RTCx, ALRMBR,   0x00000000U);
167 
168     /* Reset ISR register and exit initialization mode */
169     LL_RTC_WriteReg(RTCx, ISR,      0x00000000U);
170 
171     /* Reset Tamper and alternate functions configuration register */
172     LL_RTC_WriteReg(RTCx, TAFCR, 0x00000000U);
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     status = SUCCESS;
317   }
318   /* Enable the write protection for RTC registers */
319   LL_RTC_EnableWriteProtection(RTCx);
320 
321   return status;
322 }
323 
324 /**
325   * @brief  Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
326   * @param  RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
327   * @retval None
328   */
LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef * RTC_TimeStruct)329 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
330 {
331   /* Time = 00h:00min:00sec */
332   RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
333   RTC_TimeStruct->Hours      = 0U;
334   RTC_TimeStruct->Minutes    = 0U;
335   RTC_TimeStruct->Seconds    = 0U;
336 }
337 
338 /**
339   * @brief  Set the RTC current date.
340   * @param  RTCx RTC Instance
341   * @param  RTC_Format This parameter can be one of the following values:
342   *         @arg @ref LL_RTC_FORMAT_BIN
343   *         @arg @ref LL_RTC_FORMAT_BCD
344   * @param  RTC_DateStruct pointer to a RTC_DateTypeDef structure that contains
345   *                         the date configuration information for the RTC.
346   * @retval An ErrorStatus enumeration value:
347   *          - SUCCESS: RTC Day register is configured
348   *          - ERROR: RTC Day register is not configured
349   */
LL_RTC_DATE_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_DateTypeDef * RTC_DateStruct)350 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
351 {
352   ErrorStatus status = ERROR;
353 
354   /* Check the parameters */
355   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
356   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
357 
358   if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
359   {
360     RTC_DateStruct->Month = (uint8_t)(RTC_DateStruct->Month & (uint8_t)~(0x10U)) + 0x0AU;
361   }
362   if (RTC_Format == LL_RTC_FORMAT_BIN)
363   {
364     assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
365     assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
366     assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
367   }
368   else
369   {
370     assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
371     assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
372     assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
373   }
374   assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
375 
376   /* Disable the write protection for RTC registers */
377   LL_RTC_DisableWriteProtection(RTCx);
378 
379   /* Set Initialization mode */
380   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
381   {
382     /* Check the input parameters format */
383     if (RTC_Format != LL_RTC_FORMAT_BIN)
384     {
385       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year);
386     }
387     else
388     {
389       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
390                          __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
391     }
392 
393     /* Exit Initialization mode */
394     LL_RTC_DisableInitMode(RTCx);
395 
396     status = SUCCESS;
397   }
398   /* Enable the write protection for RTC registers */
399   LL_RTC_EnableWriteProtection(RTCx);
400 
401   return status;
402 }
403 
404 /**
405   * @brief  Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
406   * @param  RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
407   * @retval None
408   */
LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef * RTC_DateStruct)409 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
410 {
411   /* Monday, January 01 xx00 */
412   RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
413   RTC_DateStruct->Day     = 1U;
414   RTC_DateStruct->Month   = LL_RTC_MONTH_JANUARY;
415   RTC_DateStruct->Year    = 0U;
416 }
417 
418 /**
419   * @brief  Set the RTC Alarm A.
420   * @note   The Alarm register can only be written when the corresponding Alarm
421   *         is disabled (Use @ref LL_RTC_ALMA_Disable function).
422   * @param  RTCx RTC Instance
423   * @param  RTC_Format This parameter can be one of the following values:
424   *         @arg @ref LL_RTC_FORMAT_BIN
425   *         @arg @ref LL_RTC_FORMAT_BCD
426   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
427   *                         contains the alarm configuration parameters.
428   * @retval An ErrorStatus enumeration value:
429   *          - SUCCESS: ALARMA registers are configured
430   *          - ERROR: ALARMA registers are not configured
431   */
LL_RTC_ALMA_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_AlarmTypeDef * RTC_AlarmStruct)432 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
433 {
434   /* Check the parameters */
435   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
436   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
437   assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
438   assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
439 
440   if (RTC_Format == LL_RTC_FORMAT_BIN)
441   {
442     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
443     {
444       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
445       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
446     }
447     else
448     {
449       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
450       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
451     }
452     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
453     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
454 
455     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
456     {
457       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
458     }
459     else
460     {
461       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
462     }
463   }
464   else
465   {
466     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
467     {
468       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
469       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
470     }
471     else
472     {
473       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
474       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
475     }
476 
477     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
478     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
479 
480     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
481     {
482       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
483     }
484     else
485     {
486       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
487     }
488   }
489 
490   /* Disable the write protection for RTC registers */
491   LL_RTC_DisableWriteProtection(RTCx);
492 
493   /* Select weekday selection */
494   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
495   {
496     /* Set the date for ALARM */
497     LL_RTC_ALMA_DisableWeekday(RTCx);
498     if (RTC_Format != LL_RTC_FORMAT_BIN)
499     {
500       LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
501     }
502     else
503     {
504       LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
505     }
506   }
507   else
508   {
509     /* Set the week day for ALARM */
510     LL_RTC_ALMA_EnableWeekday(RTCx);
511     LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
512   }
513 
514   /* Configure the Alarm register */
515   if (RTC_Format != LL_RTC_FORMAT_BIN)
516   {
517     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
518                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
519   }
520   else
521   {
522     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
523                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
524                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
525                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
526   }
527   /* Set ALARM mask */
528   LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
529 
530   /* Enable the write protection for RTC registers */
531   LL_RTC_EnableWriteProtection(RTCx);
532 
533   return SUCCESS;
534 }
535 
536 /**
537   * @brief  Set the RTC Alarm B.
538   * @note   The Alarm register can only be written when the corresponding Alarm
539   *         is disabled (@ref LL_RTC_ALMB_Disable function).
540   * @param  RTCx RTC Instance
541   * @param  RTC_Format This parameter can be one of the following values:
542   *         @arg @ref LL_RTC_FORMAT_BIN
543   *         @arg @ref LL_RTC_FORMAT_BCD
544   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
545   *                         contains the alarm configuration parameters.
546   * @retval An ErrorStatus enumeration value:
547   *          - SUCCESS: ALARMB registers are configured
548   *          - ERROR: ALARMB registers are not configured
549   */
LL_RTC_ALMB_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_AlarmTypeDef * RTC_AlarmStruct)550 ErrorStatus LL_RTC_ALMB_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
551 {
552   /* Check the parameters */
553   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
554   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
555   assert_param(IS_LL_RTC_ALMB_MASK(RTC_AlarmStruct->AlarmMask));
556   assert_param(IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
557 
558   if (RTC_Format == LL_RTC_FORMAT_BIN)
559   {
560     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
561     {
562       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
563       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
564     }
565     else
566     {
567       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
568       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
569     }
570     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
571     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
572 
573     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
574     {
575       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
576     }
577     else
578     {
579       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
580     }
581   }
582   else
583   {
584     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
585     {
586       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
587       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
588     }
589     else
590     {
591       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
592       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
593     }
594 
595     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
596     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
597 
598     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
599     {
600       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
601     }
602     else
603     {
604       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
605     }
606   }
607 
608   /* Disable the write protection for RTC registers */
609   LL_RTC_DisableWriteProtection(RTCx);
610 
611   /* Select weekday selection */
612   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
613   {
614     /* Set the date for ALARM */
615     LL_RTC_ALMB_DisableWeekday(RTCx);
616     if (RTC_Format != LL_RTC_FORMAT_BIN)
617     {
618       LL_RTC_ALMB_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
619     }
620     else
621     {
622       LL_RTC_ALMB_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
623     }
624   }
625   else
626   {
627     /* Set the week day for ALARM */
628     LL_RTC_ALMB_EnableWeekday(RTCx);
629     LL_RTC_ALMB_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
630   }
631 
632   /* Configure the Alarm register */
633   if (RTC_Format != LL_RTC_FORMAT_BIN)
634   {
635     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
636                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
637   }
638   else
639   {
640     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
641                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
642                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
643                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
644   }
645   /* Set ALARM mask */
646   LL_RTC_ALMB_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
647 
648   /* Enable the write protection for RTC registers */
649   LL_RTC_EnableWriteProtection(RTCx);
650 
651   return SUCCESS;
652 }
653 
654 /**
655   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
656   *         Day = 1st day of the month/Mask = all fields are masked).
657   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
658   * @retval None
659   */
LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef * RTC_AlarmStruct)660 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
661 {
662   /* Alarm Time Settings : Time = 00h:00mn:00sec */
663   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
664   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
665   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
666   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
667 
668   /* Alarm Day Settings : Day = 1st day of the month */
669   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
670   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
671 
672   /* Alarm Masks Settings : Mask =  all fields are not masked */
673   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMA_MASK_NONE;
674 }
675 
676 /**
677   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
678   *         Day = 1st day of the month/Mask = all fields are masked).
679   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
680   * @retval None
681   */
LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef * RTC_AlarmStruct)682 void LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
683 {
684   /* Alarm Time Settings : Time = 00h:00mn:00sec */
685   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMB_TIME_FORMAT_AM;
686   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
687   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
688   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
689 
690   /* Alarm Day Settings : Day = 1st day of the month */
691   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMB_DATEWEEKDAYSEL_DATE;
692   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
693 
694   /* Alarm Masks Settings : Mask =  all fields are not masked */
695   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMB_MASK_NONE;
696 }
697 
698 /**
699   * @brief  Enters the RTC Initialization mode.
700   * @note   The RTC Initialization mode is write protected, use the
701   *         @ref LL_RTC_DisableWriteProtection before calling this function.
702   * @param  RTCx RTC Instance
703   * @retval An ErrorStatus enumeration value:
704   *          - SUCCESS: RTC is in Init mode
705   *          - ERROR: RTC is not in Init mode
706   */
LL_RTC_EnterInitMode(RTC_TypeDef * RTCx)707 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
708 {
709   __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
710   ErrorStatus status = SUCCESS;
711   uint32_t tmp = 0U;
712 
713   /* Check the parameter */
714   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
715 
716   /* Check if the Initialization mode is set */
717   if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
718   {
719     /* Set the Initialization mode */
720     LL_RTC_EnableInitMode(RTCx);
721 
722     /* Wait till RTC is in INIT state and if Time out is reached exit */
723     tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
724     while ((timeout != 0U) && (tmp != 1U))
725     {
726       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
727       {
728         timeout --;
729       }
730       tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
731       if (timeout == 0U)
732       {
733         status = ERROR;
734       }
735     }
736   }
737   return status;
738 }
739 
740 /**
741   * @brief  Exit the RTC Initialization mode.
742   * @note   When the initialization sequence is complete, the calendar restarts
743   *         counting after 4 RTCCLK cycles.
744   * @note   The RTC Initialization mode is write protected, use the
745   *         @ref LL_RTC_DisableWriteProtection before calling this function.
746   * @param  RTCx RTC Instance
747   * @retval An ErrorStatus enumeration value:
748   *          - SUCCESS: RTC exited from in Init mode
749   *          - ERROR: Not applicable
750   */
LL_RTC_ExitInitMode(RTC_TypeDef * RTCx)751 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
752 {
753   /* Check the parameter */
754   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
755 
756   /* Disable initialization mode */
757   LL_RTC_DisableInitMode(RTCx);
758 
759   return SUCCESS;
760 }
761 
762 /**
763   * @brief  Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
764   *         synchronized with RTC APB clock.
765   * @note   The RTC Resynchronization mode is write protected, use the
766   *         @ref LL_RTC_DisableWriteProtection before calling this function.
767   * @note   To read the calendar through the shadow registers after Calendar
768   *         initialization, calendar update or after wakeup from low power modes
769   *         the software must first clear the RSF flag.
770   *         The software must then wait until it is set again before reading
771   *         the calendar, which means that the calendar registers have been
772   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
773   * @param  RTCx RTC Instance
774   * @retval An ErrorStatus enumeration value:
775   *          - SUCCESS: RTC registers are synchronised
776   *          - ERROR: RTC registers are not synchronised
777   */
LL_RTC_WaitForSynchro(RTC_TypeDef * RTCx)778 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
779 {
780   __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
781   ErrorStatus status = SUCCESS;
782   uint32_t tmp = 0U;
783 
784   /* Check the parameter */
785   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
786 
787   /* Clear RSF flag */
788   LL_RTC_ClearFlag_RS(RTCx);
789 
790   /* Wait the registers to be synchronised */
791   tmp = LL_RTC_IsActiveFlag_RS(RTCx);
792   while ((timeout != 0U) && (tmp != 0U))
793   {
794     if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
795     {
796       timeout--;
797     }
798     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
799     if (timeout == 0U)
800     {
801       status = ERROR;
802     }
803   }
804 
805   if (status != ERROR)
806   {
807     timeout = RTC_SYNCHRO_TIMEOUT;
808     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
809     while ((timeout != 0U) && (tmp != 1U))
810     {
811       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
812       {
813         timeout--;
814       }
815       tmp = LL_RTC_IsActiveFlag_RS(RTCx);
816       if (timeout == 0U)
817       {
818         status = ERROR;
819       }
820     }
821   }
822 
823   return (status);
824 }
825 
826 /**
827   * @}
828   */
829 
830 /**
831   * @}
832   */
833 
834 /**
835   * @}
836   */
837 
838 #endif /* defined(RTC) */
839 
840 /**
841   * @}
842   */
843 
844 #endif /* USE_FULL_LL_DRIVER */
845 
846 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
847