source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_rtc.c

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