1 /**
2 ******************************************************************************
3 * @file stm32f0xx_ll_rtc.c
4 * @author MCD Application Team
5 * @brief RTC LL module driver.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. Neither the name of STMicroelectronics nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 ******************************************************************************
34 */
35 #if defined(USE_FULL_LL_DRIVER)
36
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f0xx_ll_rtc.h"
39 #include "stm32f0xx_ll_cortex.h"
40 #ifdef USE_FULL_ASSERT
41 #include "stm32_assert.h"
42 #else
43 #define assert_param(expr) ((void)0U)
44 #endif
45
46 /** @addtogroup STM32F0xx_LL_Driver
47 * @{
48 */
49
50 #if defined(RTC)
51
52 /** @addtogroup RTC_LL
53 * @{
54 */
55
56 /* Private types -------------------------------------------------------------*/
57 /* Private variables ---------------------------------------------------------*/
58 /* Private constants ---------------------------------------------------------*/
59 /** @addtogroup RTC_LL_Private_Constants
60 * @{
61 */
62 /* Default values used for prescaler */
63 #define RTC_ASYNCH_PRESC_DEFAULT 0x0000007FU
64 #define RTC_SYNCH_PRESC_DEFAULT 0x000000FFU
65
66 /* Values used for timeout */
67 #define RTC_INITMODE_TIMEOUT 1000U /* 1s when tick set to 1ms */
68 #define RTC_SYNCHRO_TIMEOUT 1000U /* 1s when tick set to 1ms */
69 /**
70 * @}
71 */
72
73 /* Private macros ------------------------------------------------------------*/
74 /** @addtogroup RTC_LL_Private_Macros
75 * @{
76 */
77
78 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
79 || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
80
81 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FU)
82
83 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FFFU)
84
85 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
86 || ((__VALUE__) == LL_RTC_FORMAT_BCD))
87
88 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
89 || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
90
91 #define IS_LL_RTC_HOUR12(__HOUR__) (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
92 #define IS_LL_RTC_HOUR24(__HOUR__) ((__HOUR__) <= 23U)
93 #define IS_LL_RTC_MINUTES(__MINUTES__) ((__MINUTES__) <= 59U)
94 #define IS_LL_RTC_SECONDS(__SECONDS__) ((__SECONDS__) <= 59U)
95
96 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
97 || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
98 || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
99 || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
100 || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
101 || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
102 || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
103
104 #define IS_LL_RTC_DAY(__DAY__) (((__DAY__) >= 1U) && ((__DAY__) <= 31U))
105
106 #define IS_LL_RTC_MONTH(__VALUE__) (((__VALUE__) == LL_RTC_MONTH_JANUARY) \
107 || ((__VALUE__) == LL_RTC_MONTH_FEBRUARY) \
108 || ((__VALUE__) == LL_RTC_MONTH_MARCH) \
109 || ((__VALUE__) == LL_RTC_MONTH_APRIL) \
110 || ((__VALUE__) == LL_RTC_MONTH_MAY) \
111 || ((__VALUE__) == LL_RTC_MONTH_JUNE) \
112 || ((__VALUE__) == LL_RTC_MONTH_JULY) \
113 || ((__VALUE__) == LL_RTC_MONTH_AUGUST) \
114 || ((__VALUE__) == LL_RTC_MONTH_SEPTEMBER) \
115 || ((__VALUE__) == LL_RTC_MONTH_OCTOBER) \
116 || ((__VALUE__) == LL_RTC_MONTH_NOVEMBER) \
117 || ((__VALUE__) == LL_RTC_MONTH_DECEMBER))
118
119 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
120
121 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
122 || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
123 || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
124 || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
125 || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
126 || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
127
128
129 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
130 ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
131
132
133 /**
134 * @}
135 */
136 /* Private function prototypes -----------------------------------------------*/
137 /* Exported functions --------------------------------------------------------*/
138 /** @addtogroup RTC_LL_Exported_Functions
139 * @{
140 */
141
142 /** @addtogroup RTC_LL_EF_Init
143 * @{
144 */
145
146 /**
147 * @brief De-Initializes the RTC registers to their default reset values.
148 * @note This function doesn't reset the RTC Clock source and RTC Backup Data
149 * registers.
150 * @param RTCx RTC Instance
151 * @retval An ErrorStatus enumeration value:
152 * - SUCCESS: RTC registers are de-initialized
153 * - ERROR: RTC registers are not de-initialized
154 */
LL_RTC_DeInit(RTC_TypeDef * RTCx)155 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
156 {
157 ErrorStatus status = ERROR;
158
159 /* Check the parameter */
160 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
161
162 /* Disable the write protection for RTC registers */
163 LL_RTC_DisableWriteProtection(RTCx);
164
165 /* Set Initialization mode */
166 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
167 {
168 /* Reset TR, DR and CR registers */
169 LL_RTC_WriteReg(RTCx, TR, 0x00000000U);
170 #if defined(RTC_WAKEUP_SUPPORT)
171 LL_RTC_WriteReg(RTCx, WUTR, RTC_WUTR_WUT);
172 #endif /* RTC_WAKEUP_SUPPORT */
173 LL_RTC_WriteReg(RTCx, DR , (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
174 /* Reset All CR bits except CR[2:0] */
175 #if defined(RTC_WAKEUP_SUPPORT)
176 LL_RTC_WriteReg(RTCx, CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL));
177 #else
178 LL_RTC_WriteReg(RTCx, CR, 0x00000000U);
179 #endif /* RTC_WAKEUP_SUPPORT */
180 LL_RTC_WriteReg(RTCx, PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
181 LL_RTC_WriteReg(RTCx, ALRMAR, 0x00000000U);
182 LL_RTC_WriteReg(RTCx, SHIFTR, 0x00000000U);
183 LL_RTC_WriteReg(RTCx, CALR, 0x00000000U);
184 LL_RTC_WriteReg(RTCx, ALRMASSR, 0x00000000U);
185
186 /* Reset ISR register and exit initialization mode */
187 LL_RTC_WriteReg(RTCx, ISR, 0x00000000U);
188
189 /* Reset Tamper and alternate functions configuration register */
190 LL_RTC_WriteReg(RTCx, TAFCR, 0x00000000U);
191
192 /* Wait till the RTC RSF flag is set */
193 status = LL_RTC_WaitForSynchro(RTCx);
194 }
195
196 /* Enable the write protection for RTC registers */
197 LL_RTC_EnableWriteProtection(RTCx);
198
199 return status;
200 }
201
202 /**
203 * @brief Initializes the RTC registers according to the specified parameters
204 * in RTC_InitStruct.
205 * @param RTCx RTC Instance
206 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
207 * the configuration information for the RTC peripheral.
208 * @note The RTC Prescaler register is write protected and can be written in
209 * initialization mode only.
210 * @retval An ErrorStatus enumeration value:
211 * - SUCCESS: RTC registers are initialized
212 * - ERROR: RTC registers are not initialized
213 */
LL_RTC_Init(RTC_TypeDef * RTCx,LL_RTC_InitTypeDef * RTC_InitStruct)214 ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct)
215 {
216 ErrorStatus status = ERROR;
217
218 /* Check the parameters */
219 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
220 assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat));
221 assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler));
222 assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler));
223
224 /* Disable the write protection for RTC registers */
225 LL_RTC_DisableWriteProtection(RTCx);
226
227 /* Set Initialization mode */
228 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
229 {
230 /* Set Hour Format */
231 LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat);
232
233 /* Configure Synchronous and Asynchronous prescaler factor */
234 LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler);
235 LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler);
236
237 /* Exit Initialization mode */
238 LL_RTC_DisableInitMode(RTCx);
239
240 status = SUCCESS;
241 }
242 /* Enable the write protection for RTC registers */
243 LL_RTC_EnableWriteProtection(RTCx);
244
245 return status;
246 }
247
248 /**
249 * @brief Set each @ref LL_RTC_InitTypeDef field to default value.
250 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
251 * @retval None
252 */
LL_RTC_StructInit(LL_RTC_InitTypeDef * RTC_InitStruct)253 void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct)
254 {
255 /* Set RTC_InitStruct fields to default values */
256 RTC_InitStruct->HourFormat = LL_RTC_HOURFORMAT_24HOUR;
257 RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT;
258 RTC_InitStruct->SynchPrescaler = RTC_SYNCH_PRESC_DEFAULT;
259 }
260
261 /**
262 * @brief Set the RTC current time.
263 * @param RTCx RTC Instance
264 * @param RTC_Format This parameter can be one of the following values:
265 * @arg @ref LL_RTC_FORMAT_BIN
266 * @arg @ref LL_RTC_FORMAT_BCD
267 * @param RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
268 * the time configuration information for the RTC.
269 * @retval An ErrorStatus enumeration value:
270 * - SUCCESS: RTC Time register is configured
271 * - ERROR: RTC Time register is not configured
272 */
LL_RTC_TIME_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_TimeTypeDef * RTC_TimeStruct)273 ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct)
274 {
275 ErrorStatus status = ERROR;
276
277 /* Check the parameters */
278 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
279 assert_param(IS_LL_RTC_FORMAT(RTC_Format));
280
281 if (RTC_Format == LL_RTC_FORMAT_BIN)
282 {
283 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
284 {
285 assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours));
286 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
287 }
288 else
289 {
290 RTC_TimeStruct->TimeFormat = 0x00U;
291 assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours));
292 }
293 assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes));
294 assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds));
295 }
296 else
297 {
298 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
299 {
300 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
301 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
302 }
303 else
304 {
305 RTC_TimeStruct->TimeFormat = 0x00U;
306 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
307 }
308 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes)));
309 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds)));
310 }
311
312 /* Disable the write protection for RTC registers */
313 LL_RTC_DisableWriteProtection(RTCx);
314
315 /* Set Initialization mode */
316 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
317 {
318 /* Check the input parameters format */
319 if (RTC_Format != LL_RTC_FORMAT_BIN)
320 {
321 LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours,
322 RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds);
323 }
324 else
325 {
326 LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours),
327 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes),
328 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds));
329 }
330
331 /* Exit Initialization mode */
332 LL_RTC_DisableInitMode(RTC);
333
334 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
335 if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
336 {
337 status = LL_RTC_WaitForSynchro(RTCx);
338 }
339 else
340 {
341 status = SUCCESS;
342 }
343 }
344 /* Enable the write protection for RTC registers */
345 LL_RTC_EnableWriteProtection(RTCx);
346
347 return status;
348 }
349
350 /**
351 * @brief Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
352 * @param RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
353 * @retval None
354 */
LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef * RTC_TimeStruct)355 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
356 {
357 /* Time = 00h:00min:00sec */
358 RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
359 RTC_TimeStruct->Hours = 0U;
360 RTC_TimeStruct->Minutes = 0U;
361 RTC_TimeStruct->Seconds = 0U;
362 }
363
364 /**
365 * @brief Set the RTC current date.
366 * @param RTCx RTC Instance
367 * @param RTC_Format This parameter can be one of the following values:
368 * @arg @ref LL_RTC_FORMAT_BIN
369 * @arg @ref LL_RTC_FORMAT_BCD
370 * @param RTC_DateStruct pointer to a RTC_DateTypeDef structure that contains
371 * the date configuration information for the RTC.
372 * @retval An ErrorStatus enumeration value:
373 * - SUCCESS: RTC Day register is configured
374 * - ERROR: RTC Day register is not configured
375 */
LL_RTC_DATE_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_DateTypeDef * RTC_DateStruct)376 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
377 {
378 ErrorStatus status = ERROR;
379
380 /* Check the parameters */
381 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
382 assert_param(IS_LL_RTC_FORMAT(RTC_Format));
383
384 if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
385 {
386 RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint32_t)~(0x10U)) + 0x0AU;
387 }
388 if (RTC_Format == LL_RTC_FORMAT_BIN)
389 {
390 assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
391 assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
392 assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
393 }
394 else
395 {
396 assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
397 assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
398 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
399 }
400 assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
401
402 /* Disable the write protection for RTC registers */
403 LL_RTC_DisableWriteProtection(RTCx);
404
405 /* Set Initialization mode */
406 if (LL_RTC_EnterInitMode(RTCx) != ERROR)
407 {
408 /* Check the input parameters format */
409 if (RTC_Format != LL_RTC_FORMAT_BIN)
410 {
411 LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year);
412 }
413 else
414 {
415 LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
416 __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
417 }
418
419 /* Exit Initialization mode */
420 LL_RTC_DisableInitMode(RTC);
421
422 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
423 if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
424 {
425 status = LL_RTC_WaitForSynchro(RTCx);
426 }
427 else
428 {
429 status = SUCCESS;
430 }
431 }
432 /* Enable the write protection for RTC registers */
433 LL_RTC_EnableWriteProtection(RTCx);
434
435 return status;
436 }
437
438 /**
439 * @brief Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
440 * @param RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
441 * @retval None
442 */
LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef * RTC_DateStruct)443 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
444 {
445 /* Monday, January 01 xx00 */
446 RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
447 RTC_DateStruct->Day = 1U;
448 RTC_DateStruct->Month = LL_RTC_MONTH_JANUARY;
449 RTC_DateStruct->Year = 0U;
450 }
451
452 /**
453 * @brief Set the RTC Alarm A.
454 * @note The Alarm register can only be written when the corresponding Alarm
455 * is disabled (Use @ref LL_RTC_ALMA_Disable function).
456 * @param RTCx RTC Instance
457 * @param RTC_Format This parameter can be one of the following values:
458 * @arg @ref LL_RTC_FORMAT_BIN
459 * @arg @ref LL_RTC_FORMAT_BCD
460 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
461 * contains the alarm configuration parameters.
462 * @retval An ErrorStatus enumeration value:
463 * - SUCCESS: ALARMA registers are configured
464 * - ERROR: ALARMA registers are not configured
465 */
LL_RTC_ALMA_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_AlarmTypeDef * RTC_AlarmStruct)466 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
467 {
468 /* Check the parameters */
469 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
470 assert_param(IS_LL_RTC_FORMAT(RTC_Format));
471 assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
472 assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
473
474 if (RTC_Format == LL_RTC_FORMAT_BIN)
475 {
476 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
477 {
478 assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
479 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
480 }
481 else
482 {
483 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
484 assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
485 }
486 assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
487 assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
488
489 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
490 {
491 assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
492 }
493 else
494 {
495 assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
496 }
497 }
498 else
499 {
500 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
501 {
502 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
503 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
504 }
505 else
506 {
507 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
508 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
509 }
510
511 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
512 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
513
514 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
515 {
516 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
517 }
518 else
519 {
520 assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
521 }
522 }
523
524 /* Disable the write protection for RTC registers */
525 LL_RTC_DisableWriteProtection(RTCx);
526
527 /* Select weekday selection */
528 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
529 {
530 /* Set the date for ALARM */
531 LL_RTC_ALMA_DisableWeekday(RTCx);
532 if (RTC_Format != LL_RTC_FORMAT_BIN)
533 {
534 LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
535 }
536 else
537 {
538 LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
539 }
540 }
541 else
542 {
543 /* Set the week day for ALARM */
544 LL_RTC_ALMA_EnableWeekday(RTCx);
545 LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
546 }
547
548 /* Configure the Alarm register */
549 if (RTC_Format != LL_RTC_FORMAT_BIN)
550 {
551 LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
552 RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
553 }
554 else
555 {
556 LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
557 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
558 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
559 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
560 }
561 /* Set ALARM mask */
562 LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
563
564 /* Enable the write protection for RTC registers */
565 LL_RTC_EnableWriteProtection(RTCx);
566
567 return SUCCESS;
568 }
569
570 /**
571 * @brief Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
572 * Day = 1st day of the month/Mask = all fields are masked).
573 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
574 * @retval None
575 */
LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef * RTC_AlarmStruct)576 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
577 {
578 /* Alarm Time Settings : Time = 00h:00mn:00sec */
579 RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
580 RTC_AlarmStruct->AlarmTime.Hours = 0U;
581 RTC_AlarmStruct->AlarmTime.Minutes = 0U;
582 RTC_AlarmStruct->AlarmTime.Seconds = 0U;
583
584 /* Alarm Day Settings : Day = 1st day of the month */
585 RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
586 RTC_AlarmStruct->AlarmDateWeekDay = 1U;
587
588 /* Alarm Masks Settings : Mask = all fields are not masked */
589 RTC_AlarmStruct->AlarmMask = LL_RTC_ALMA_MASK_NONE;
590 }
591
592 /**
593 * @brief Enters the RTC Initialization mode.
594 * @note The RTC Initialization mode is write protected, use the
595 * @ref LL_RTC_DisableWriteProtection before calling this function.
596 * @param RTCx RTC Instance
597 * @retval An ErrorStatus enumeration value:
598 * - SUCCESS: RTC is in Init mode
599 * - ERROR: RTC is not in Init mode
600 */
LL_RTC_EnterInitMode(RTC_TypeDef * RTCx)601 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
602 {
603 __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
604 ErrorStatus status = SUCCESS;
605 uint32_t tmp = 0U;
606
607 /* Check the parameter */
608 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
609
610 /* Check if the Initialization mode is set */
611 if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
612 {
613 /* Set the Initialization mode */
614 LL_RTC_EnableInitMode(RTCx);
615
616 /* Wait till RTC is in INIT state and if Time out is reached exit */
617 tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
618 while ((timeout != 0U) && (tmp != 1U))
619 {
620 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
621 {
622 timeout --;
623 }
624 tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
625 if (timeout == 0U)
626 {
627 status = ERROR;
628 }
629 }
630 }
631 return status;
632 }
633
634 /**
635 * @brief Exit the RTC Initialization mode.
636 * @note When the initialization sequence is complete, the calendar restarts
637 * counting after 4 RTCCLK cycles.
638 * @note The RTC Initialization mode is write protected, use the
639 * @ref LL_RTC_DisableWriteProtection before calling this function.
640 * @param RTCx RTC Instance
641 * @retval An ErrorStatus enumeration value:
642 * - SUCCESS: RTC exited from in Init mode
643 * - ERROR: Not applicable
644 */
LL_RTC_ExitInitMode(RTC_TypeDef * RTCx)645 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
646 {
647 /* Check the parameter */
648 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
649
650 /* Disable initialization mode */
651 LL_RTC_DisableInitMode(RTCx);
652
653 return SUCCESS;
654 }
655
656 /**
657 * @brief Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
658 * synchronized with RTC APB clock.
659 * @note The RTC Resynchronization mode is write protected, use the
660 * @ref LL_RTC_DisableWriteProtection before calling this function.
661 * @note To read the calendar through the shadow registers after Calendar
662 * initialization, calendar update or after wakeup from low power modes
663 * the software must first clear the RSF flag.
664 * The software must then wait until it is set again before reading
665 * the calendar, which means that the calendar registers have been
666 * correctly copied into the RTC_TR and RTC_DR shadow registers.
667 * @param RTCx RTC Instance
668 * @retval An ErrorStatus enumeration value:
669 * - SUCCESS: RTC registers are synchronised
670 * - ERROR: RTC registers are not synchronised
671 */
LL_RTC_WaitForSynchro(RTC_TypeDef * RTCx)672 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
673 {
674 __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
675 ErrorStatus status = SUCCESS;
676 uint32_t tmp = 0U;
677
678 /* Check the parameter */
679 assert_param(IS_RTC_ALL_INSTANCE(RTCx));
680
681 /* Clear RSF flag */
682 LL_RTC_ClearFlag_RS(RTCx);
683
684 /* Wait the registers to be synchronised */
685 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
686 while ((timeout != 0U) && (tmp != 0U))
687 {
688 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
689 {
690 timeout--;
691 }
692 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
693 if (timeout == 0U)
694 {
695 status = ERROR;
696 }
697 }
698
699 if (status != ERROR)
700 {
701 timeout = RTC_SYNCHRO_TIMEOUT;
702 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
703 while ((timeout != 0U) && (tmp != 1U))
704 {
705 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
706 {
707 timeout--;
708 }
709 tmp = LL_RTC_IsActiveFlag_RS(RTCx);
710 if (timeout == 0U)
711 {
712 status = ERROR;
713 }
714 }
715 }
716
717 return (status);
718 }
719
720 /**
721 * @}
722 */
723
724 /**
725 * @}
726 */
727
728 /**
729 * @}
730 */
731
732 #endif /* defined(RTC) */
733
734 /**
735 * @}
736 */
737
738 #endif /* USE_FULL_LL_DRIVER */
739
740 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
741