Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Src / stm32f4xx_ll_rtc.c
blobd7693e4f3ed5b2ca89094ef688b027416bcfcc7c
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_ll_rtc.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
7 * @brief RTC LL module driver.
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 ******************************************************************************
37 #if defined(USE_FULL_LL_DRIVER)
39 /* Includes ------------------------------------------------------------------*/
40 #include "stm32f4xx_ll_rtc.h"
41 #include "stm32f4xx_ll_cortex.h"
42 #ifdef USE_FULL_ASSERT
43 #include "stm32_assert.h"
44 #else
45 #define assert_param(expr) ((void)0U)
46 #endif
48 /** @addtogroup STM32F4xx_LL_Driver
49 * @{
52 #if defined(RTC)
54 /** @addtogroup RTC_LL
55 * @{
58 /* Private types -------------------------------------------------------------*/
59 /* Private variables ---------------------------------------------------------*/
60 /* Private constants ---------------------------------------------------------*/
61 /** @addtogroup RTC_LL_Private_Constants
62 * @{
64 /* Default values used for prescaler */
65 #define RTC_ASYNCH_PRESC_DEFAULT 0x0000007FU
66 #define RTC_SYNCH_PRESC_DEFAULT 0x000000FFU
68 /* Values used for timeout */
69 #define RTC_INITMODE_TIMEOUT 1000U /* 1s when tick set to 1ms */
70 #define RTC_SYNCHRO_TIMEOUT 1000U /* 1s when tick set to 1ms */
71 /**
72 * @}
75 /* Private macros ------------------------------------------------------------*/
76 /** @addtogroup RTC_LL_Private_Macros
77 * @{
80 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
81 || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
83 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FU)
85 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FFFU)
87 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
88 || ((__VALUE__) == LL_RTC_FORMAT_BCD))
90 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
91 || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
93 #define IS_LL_RTC_HOUR12(__HOUR__) (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
94 #define IS_LL_RTC_HOUR24(__HOUR__) ((__HOUR__) <= 23U)
95 #define IS_LL_RTC_MINUTES(__MINUTES__) ((__MINUTES__) <= 59U)
96 #define IS_LL_RTC_SECONDS(__SECONDS__) ((__SECONDS__) <= 59U)
98 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
99 || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
100 || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
101 || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
102 || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
103 || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
104 || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
106 #define IS_LL_RTC_DAY(__DAY__) (((__DAY__) >= 1U) && ((__DAY__) <= 31U))
108 #define IS_LL_RTC_MONTH(__VALUE__) (((__VALUE__) == LL_RTC_MONTH_JANUARY) \
109 || ((__VALUE__) == LL_RTC_MONTH_FEBRUARY) \
110 || ((__VALUE__) == LL_RTC_MONTH_MARCH) \
111 || ((__VALUE__) == LL_RTC_MONTH_APRIL) \
112 || ((__VALUE__) == LL_RTC_MONTH_MAY) \
113 || ((__VALUE__) == LL_RTC_MONTH_JUNE) \
114 || ((__VALUE__) == LL_RTC_MONTH_JULY) \
115 || ((__VALUE__) == LL_RTC_MONTH_AUGUST) \
116 || ((__VALUE__) == LL_RTC_MONTH_SEPTEMBER) \
117 || ((__VALUE__) == LL_RTC_MONTH_OCTOBER) \
118 || ((__VALUE__) == LL_RTC_MONTH_NOVEMBER) \
119 || ((__VALUE__) == LL_RTC_MONTH_DECEMBER))
121 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
123 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
124 || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
125 || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
126 || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
127 || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
128 || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
130 #define IS_LL_RTC_ALMB_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMB_MASK_NONE) \
131 || ((__VALUE__) == LL_RTC_ALMB_MASK_DATEWEEKDAY) \
132 || ((__VALUE__) == LL_RTC_ALMB_MASK_HOURS) \
133 || ((__VALUE__) == LL_RTC_ALMB_MASK_MINUTES) \
134 || ((__VALUE__) == LL_RTC_ALMB_MASK_SECONDS) \
135 || ((__VALUE__) == LL_RTC_ALMB_MASK_ALL))
138 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
139 ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
141 #define IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) || \
142 ((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_WEEKDAY))
146 * @}
148 /* Private function prototypes -----------------------------------------------*/
149 /* Exported functions --------------------------------------------------------*/
150 /** @addtogroup RTC_LL_Exported_Functions
151 * @{
154 /** @addtogroup RTC_LL_EF_Init
155 * @{
159 * @brief De-Initializes the RTC registers to their default reset values.
160 * @note This function doesn't reset the RTC Clock source and RTC Backup Data
161 * registers.
162 * @param RTCx RTC Instance
163 * @retval An ErrorStatus enumeration value:
164 * - SUCCESS: RTC registers are de-initialized
165 * - ERROR: RTC registers are not de-initialized
167 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
169 ErrorStatus status = ERROR;
171 /* Check the parameter */
172 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
174 /* Disable the write protection for RTC registers */
175 LL_RTC_DisableWriteProtection(RTCx);
177 /* Set Initialization mode */
178 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
180 /* Reset TR, DR and CR registers */
181 LL_RTC_WriteReg(RTCx, TR, 0x00000000U);
182 #if defined(RTC_WAKEUP_SUPPORT)
183 LL_RTC_WriteReg(RTCx, WUTR, RTC_WUTR_WUT);
184 #endif /* RTC_WAKEUP_SUPPORT */
185 LL_RTC_WriteReg(RTCx, DR , (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
186 /* Reset All CR bits except CR[2:0] */
187 #if defined(RTC_WAKEUP_SUPPORT)
188 LL_RTC_WriteReg(RTCx, CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL));
189 #else
190 LL_RTC_WriteReg(RTCx, CR, 0x00000000U);
191 #endif /* RTC_WAKEUP_SUPPORT */
192 LL_RTC_WriteReg(RTCx, PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
193 LL_RTC_WriteReg(RTCx, ALRMAR, 0x00000000U);
194 LL_RTC_WriteReg(RTCx, ALRMBR, 0x00000000U);
195 LL_RTC_WriteReg(RTCx, SHIFTR, 0x00000000U);
196 LL_RTC_WriteReg(RTCx, CALR, 0x00000000U);
197 LL_RTC_WriteReg(RTCx, ALRMASSR, 0x00000000U);
198 LL_RTC_WriteReg(RTCx, ALRMBSSR, 0x00000000U);
200 /* Reset ISR register and exit initialization mode */
201 LL_RTC_WriteReg(RTCx, ISR, 0x00000000U);
203 /* Reset Tamper and alternate functions configuration register */
204 LL_RTC_WriteReg(RTCx, TAFCR, 0x00000000U);
206 /* Wait till the RTC RSF flag is set */
207 status = LL_RTC_WaitForSynchro(RTCx);
210 /* Enable the write protection for RTC registers */
211 LL_RTC_EnableWriteProtection(RTCx);
213 return status;
217 * @brief Initializes the RTC registers according to the specified parameters
218 * in RTC_InitStruct.
219 * @param RTCx RTC Instance
220 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
221 * the configuration information for the RTC peripheral.
222 * @note The RTC Prescaler register is write protected and can be written in
223 * initialization mode only.
224 * @retval An ErrorStatus enumeration value:
225 * - SUCCESS: RTC registers are initialized
226 * - ERROR: RTC registers are not initialized
228 ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct)
230 ErrorStatus status = ERROR;
232 /* Check the parameters */
233 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
234 assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat));
235 assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler));
236 assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler));
238 /* Disable the write protection for RTC registers */
239 LL_RTC_DisableWriteProtection(RTCx);
241 /* Set Initialization mode */
242 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
244 /* Set Hour Format */
245 LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat);
247 /* Configure Synchronous and Asynchronous prescaler factor */
248 LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler);
249 LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler);
251 /* Exit Initialization mode */
252 LL_RTC_DisableInitMode(RTCx);
254 status = SUCCESS;
256 /* Enable the write protection for RTC registers */
257 LL_RTC_EnableWriteProtection(RTCx);
259 return status;
263 * @brief Set each @ref LL_RTC_InitTypeDef field to default value.
264 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
265 * @retval None
267 void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct)
269 /* Set RTC_InitStruct fields to default values */
270 RTC_InitStruct->HourFormat = LL_RTC_HOURFORMAT_24HOUR;
271 RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT;
272 RTC_InitStruct->SynchPrescaler = RTC_SYNCH_PRESC_DEFAULT;
276 * @brief Set the RTC current time.
277 * @param RTCx RTC Instance
278 * @param RTC_Format This parameter can be one of the following values:
279 * @arg @ref LL_RTC_FORMAT_BIN
280 * @arg @ref LL_RTC_FORMAT_BCD
281 * @param RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
282 * the time configuration information for the RTC.
283 * @retval An ErrorStatus enumeration value:
284 * - SUCCESS: RTC Time register is configured
285 * - ERROR: RTC Time register is not configured
287 ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct)
289 ErrorStatus status = ERROR;
291 /* Check the parameters */
292 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
293 assert_param(IS_LL_RTC_FORMAT(RTC_Format));
295 if (RTC_Format == LL_RTC_FORMAT_BIN)
297 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
299 assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours));
300 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
302 else
304 RTC_TimeStruct->TimeFormat = 0x00U;
305 assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours));
307 assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes));
308 assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds));
310 else
312 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
314 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
315 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
317 else
319 RTC_TimeStruct->TimeFormat = 0x00U;
320 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
322 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes)));
323 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds)));
326 /* Disable the write protection for RTC registers */
327 LL_RTC_DisableWriteProtection(RTCx);
329 /* Set Initialization mode */
330 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
332 /* Check the input parameters format */
333 if (RTC_Format != LL_RTC_FORMAT_BIN)
335 LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours,
336 RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds);
338 else
340 LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours),
341 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes),
342 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds));
345 /* Exit Initialization mode */
346 LL_RTC_DisableInitMode(RTC);
348 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
349 if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
351 status = LL_RTC_WaitForSynchro(RTCx);
353 else
355 status = SUCCESS;
358 /* Enable the write protection for RTC registers */
359 LL_RTC_EnableWriteProtection(RTCx);
361 return status;
365 * @brief Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
366 * @param RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
367 * @retval None
369 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
371 /* Time = 00h:00min:00sec */
372 RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
373 RTC_TimeStruct->Hours = 0U;
374 RTC_TimeStruct->Minutes = 0U;
375 RTC_TimeStruct->Seconds = 0U;
379 * @brief Set the RTC current date.
380 * @param RTCx RTC Instance
381 * @param RTC_Format This parameter can be one of the following values:
382 * @arg @ref LL_RTC_FORMAT_BIN
383 * @arg @ref LL_RTC_FORMAT_BCD
384 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
385 * the date configuration information for the RTC.
386 * @retval An ErrorStatus enumeration value:
387 * - SUCCESS: RTC Day register is configured
388 * - ERROR: RTC Day register is not configured
390 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
392 ErrorStatus status = ERROR;
394 /* Check the parameters */
395 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
396 assert_param(IS_LL_RTC_FORMAT(RTC_Format));
398 if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
400 RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint32_t)~(0x10U)) + 0x0AU;
402 if (RTC_Format == LL_RTC_FORMAT_BIN)
404 assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
405 assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
406 assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
408 else
410 assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
411 assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
412 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
414 assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
416 /* Disable the write protection for RTC registers */
417 LL_RTC_DisableWriteProtection(RTCx);
419 /* Set Initialization mode */
420 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
422 /* Check the input parameters format */
423 if (RTC_Format != LL_RTC_FORMAT_BIN)
425 LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year);
427 else
429 LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
430 __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
433 /* Exit Initialization mode */
434 LL_RTC_DisableInitMode(RTC);
436 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
437 if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
439 status = LL_RTC_WaitForSynchro(RTCx);
441 else
443 status = SUCCESS;
446 /* Enable the write protection for RTC registers */
447 LL_RTC_EnableWriteProtection(RTCx);
449 return status;
453 * @brief Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
454 * @param RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
455 * @retval None
457 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
459 /* Monday, January 01 xx00 */
460 RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
461 RTC_DateStruct->Day = 1U;
462 RTC_DateStruct->Month = LL_RTC_MONTH_JANUARY;
463 RTC_DateStruct->Year = 0U;
467 * @brief Set the RTC Alarm A.
468 * @note The Alarm register can only be written when the corresponding Alarm
469 * is disabled (Use @ref LL_RTC_ALMA_Disable function).
470 * @param RTCx RTC Instance
471 * @param RTC_Format This parameter can be one of the following values:
472 * @arg @ref LL_RTC_FORMAT_BIN
473 * @arg @ref LL_RTC_FORMAT_BCD
474 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
475 * contains the alarm configuration parameters.
476 * @retval An ErrorStatus enumeration value:
477 * - SUCCESS: ALARMA registers are configured
478 * - ERROR: ALARMA registers are not configured
480 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
482 /* Check the parameters */
483 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
484 assert_param(IS_LL_RTC_FORMAT(RTC_Format));
485 assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
486 assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
488 if (RTC_Format == LL_RTC_FORMAT_BIN)
490 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
492 assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
493 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
495 else
497 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
498 assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
500 assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
501 assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
503 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
505 assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
507 else
509 assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
512 else
514 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
516 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
517 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
519 else
521 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
522 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
525 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
526 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
528 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
530 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
532 else
534 assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
538 /* Disable the write protection for RTC registers */
539 LL_RTC_DisableWriteProtection(RTCx);
541 /* Select weekday selection */
542 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
544 /* Set the date for ALARM */
545 LL_RTC_ALMA_DisableWeekday(RTCx);
546 if (RTC_Format != LL_RTC_FORMAT_BIN)
548 LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
550 else
552 LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
555 else
557 /* Set the week day for ALARM */
558 LL_RTC_ALMA_EnableWeekday(RTCx);
559 LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
562 /* Configure the Alarm register */
563 if (RTC_Format != LL_RTC_FORMAT_BIN)
565 LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
566 RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
568 else
570 LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
571 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
572 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
573 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
575 /* Set ALARM mask */
576 LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
578 /* Enable the write protection for RTC registers */
579 LL_RTC_EnableWriteProtection(RTCx);
581 return SUCCESS;
585 * @brief Set the RTC Alarm B.
586 * @note The Alarm register can only be written when the corresponding Alarm
587 * is disabled (@ref LL_RTC_ALMB_Disable function).
588 * @param RTCx RTC Instance
589 * @param RTC_Format This parameter can be one of the following values:
590 * @arg @ref LL_RTC_FORMAT_BIN
591 * @arg @ref LL_RTC_FORMAT_BCD
592 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
593 * contains the alarm configuration parameters.
594 * @retval An ErrorStatus enumeration value:
595 * - SUCCESS: ALARMB registers are configured
596 * - ERROR: ALARMB registers are not configured
598 ErrorStatus LL_RTC_ALMB_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
600 /* Check the parameters */
601 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
602 assert_param(IS_LL_RTC_FORMAT(RTC_Format));
603 assert_param(IS_LL_RTC_ALMB_MASK(RTC_AlarmStruct->AlarmMask));
604 assert_param(IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
606 if (RTC_Format == LL_RTC_FORMAT_BIN)
608 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
610 assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
611 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
613 else
615 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
616 assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
618 assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
619 assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
621 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
623 assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
625 else
627 assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
630 else
632 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
634 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
635 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
637 else
639 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
640 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
643 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
644 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
646 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
648 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
650 else
652 assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
656 /* Disable the write protection for RTC registers */
657 LL_RTC_DisableWriteProtection(RTCx);
659 /* Select weekday selection */
660 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
662 /* Set the date for ALARM */
663 LL_RTC_ALMB_DisableWeekday(RTCx);
664 if (RTC_Format != LL_RTC_FORMAT_BIN)
666 LL_RTC_ALMB_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
668 else
670 LL_RTC_ALMB_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
673 else
675 /* Set the week day for ALARM */
676 LL_RTC_ALMB_EnableWeekday(RTCx);
677 LL_RTC_ALMB_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
680 /* Configure the Alarm register */
681 if (RTC_Format != LL_RTC_FORMAT_BIN)
683 LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
684 RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
686 else
688 LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
689 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
690 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
691 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
693 /* Set ALARM mask */
694 LL_RTC_ALMB_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
696 /* Enable the write protection for RTC registers */
697 LL_RTC_EnableWriteProtection(RTCx);
699 return SUCCESS;
703 * @brief Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
704 * Day = 1st day of the month/Mask = all fields are masked).
705 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
706 * @retval None
708 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
710 /* Alarm Time Settings : Time = 00h:00mn:00sec */
711 RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
712 RTC_AlarmStruct->AlarmTime.Hours = 0U;
713 RTC_AlarmStruct->AlarmTime.Minutes = 0U;
714 RTC_AlarmStruct->AlarmTime.Seconds = 0U;
716 /* Alarm Day Settings : Day = 1st day of the month */
717 RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
718 RTC_AlarmStruct->AlarmDateWeekDay = 1U;
720 /* Alarm Masks Settings : Mask = all fields are not masked */
721 RTC_AlarmStruct->AlarmMask = LL_RTC_ALMA_MASK_NONE;
725 * @brief Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
726 * Day = 1st day of the month/Mask = all fields are masked).
727 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
728 * @retval None
730 void LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
732 /* Alarm Time Settings : Time = 00h:00mn:00sec */
733 RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMB_TIME_FORMAT_AM;
734 RTC_AlarmStruct->AlarmTime.Hours = 0U;
735 RTC_AlarmStruct->AlarmTime.Minutes = 0U;
736 RTC_AlarmStruct->AlarmTime.Seconds = 0U;
738 /* Alarm Day Settings : Day = 1st day of the month */
739 RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMB_DATEWEEKDAYSEL_DATE;
740 RTC_AlarmStruct->AlarmDateWeekDay = 1U;
742 /* Alarm Masks Settings : Mask = all fields are not masked */
743 RTC_AlarmStruct->AlarmMask = LL_RTC_ALMB_MASK_NONE;
747 * @brief Enters the RTC Initialization mode.
748 * @note The RTC Initialization mode is write protected, use the
749 * @ref LL_RTC_DisableWriteProtection before calling this function.
750 * @param RTCx RTC Instance
751 * @retval An ErrorStatus enumeration value:
752 * - SUCCESS: RTC is in Init mode
753 * - ERROR: RTC is not in Init mode
755 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
757 __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
758 ErrorStatus status = SUCCESS;
759 uint32_t tmp = 0U;
761 /* Check the parameter */
762 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
764 /* Check if the Initialization mode is set */
765 if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
767 /* Set the Initialization mode */
768 LL_RTC_EnableInitMode(RTCx);
770 /* Wait till RTC is in INIT state and if Time out is reached exit */
771 tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
772 while ((timeout != 0U) && (tmp != 1U))
774 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
776 timeout --;
778 tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
779 if (timeout == 0U)
781 status = ERROR;
785 return status;
789 * @brief Exit the RTC Initialization mode.
790 * @note When the initialization sequence is complete, the calendar restarts
791 * counting after 4 RTCCLK cycles.
792 * @note The RTC Initialization mode is write protected, use the
793 * @ref LL_RTC_DisableWriteProtection before calling this function.
794 * @param RTCx RTC Instance
795 * @retval An ErrorStatus enumeration value:
796 * - SUCCESS: RTC exited from in Init mode
797 * - ERROR: Not applicable
799 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
801 /* Check the parameter */
802 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
804 /* Disable initialization mode */
805 LL_RTC_DisableInitMode(RTCx);
807 return SUCCESS;
811 * @brief Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
812 * synchronized with RTC APB clock.
813 * @note The RTC Resynchronization mode is write protected, use the
814 * @ref LL_RTC_DisableWriteProtection before calling this function.
815 * @note To read the calendar through the shadow registers after Calendar
816 * initialization, calendar update or after wakeup from low power modes
817 * the software must first clear the RSF flag.
818 * The software must then wait until it is set again before reading
819 * the calendar, which means that the calendar registers have been
820 * correctly copied into the RTC_TR and RTC_DR shadow registers.
821 * @param RTCx RTC Instance
822 * @retval An ErrorStatus enumeration value:
823 * - SUCCESS: RTC registers are synchronised
824 * - ERROR: RTC registers are not synchronised
826 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
828 __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
829 ErrorStatus status = SUCCESS;
830 uint32_t tmp = 0U;
832 /* Check the parameter */
833 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
835 /* Clear RSF flag */
836 LL_RTC_ClearFlag_RS(RTCx);
838 /* Wait the registers to be synchronised */
839 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
840 while ((timeout != 0U) && (tmp != 0U))
842 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
844 timeout--;
846 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
847 if (timeout == 0U)
849 status = ERROR;
853 if (status != ERROR)
855 timeout = RTC_SYNCHRO_TIMEOUT;
856 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
857 while ((timeout != 0U) && (tmp != 1U))
859 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
861 timeout--;
863 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
864 if (timeout == 0U)
866 status = ERROR;
871 return (status);
875 * @}
879 * @}
883 * @}
886 #endif /* defined(RTC) */
889 * @}
892 #endif /* USE_FULL_LL_DRIVER */
894 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/