1 /*****************************************************************************
2 * Copyright (c) 2019, Nations Technologies Inc.
3 *
4 * All rights reserved.
5 * ****************************************************************************
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the disclaimer below.
12 *
13 * Nations' name may not be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * ****************************************************************************/
27
28 /**
29 * @file n32wb452_rtc.c
30 * @author Nations
31 * @version v1.0.1
32 *
33 * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
34 */
35 #include "n32wb452_rtc.h"
36 #include "n32wb452_rcc.h"
37
38 /** @addtogroup N32WB452_StdPeriph_Driver
39 * @{
40 */
41
42 /** @addtogroup RTC
43 * @brief RTC driver modules
44 * @{
45 */
46
47 /* Masks Definition */
48 #define RTC_TR_RESERVED_MASK ((uint32_t)0x007F7F7F)
49 #define RTC_DATE_RESERVED_MASK ((uint32_t)0x00FFFF3F)
50
51 #define RTC_RSF_MASK ((uint32_t)0xFFFFFFDF)
52 #define RTC_FLAGS_MASK \
53 ((uint32_t)(RTC_FLAG_TISOVF | RTC_FLAG_TISF | RTC_FLAG_WTF | RTC_FLAG_ALBF | RTC_FLAG_ALAF | RTC_FLAG_INITF \
54 | RTC_FLAG_RSYF | RTC_FLAG_INITSF | RTC_FLAG_WTWF | RTC_FLAG_ALBWF | RTC_FLAG_ALAWF | RTC_FLAG_RECPF \
55 | RTC_FLAG_SHOPF))
56
57 #define INITMODE_TIMEOUT ((uint32_t)0x00002000)
58 #define SYNCHRO_TIMEOUT ((uint32_t)0x00008000)
59 #define RECALPF_TIMEOUT ((uint32_t)0x00001000)
60 #define SHPF_TIMEOUT ((uint32_t)0x00002000)
61
62 static uint8_t RTC_ByteToBcd2(uint8_t Value);
63 static uint8_t RTC_Bcd2ToByte(uint8_t Value);
64
65 /** @addtogroup RTC_Private_Functions
66 * @{
67 */
68
69 /** @addtogroup RTC_Group1 Initialization and Configuration functions
70 * @brief Initialization and Configuration functions
71 *
72 @verbatim
73 ===============================================================================
74 ##### Initialization and Configuration functions #####
75 ===============================================================================
76 [..] This section provide functions allowing to initialize and configure the
77 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
78 RTC registers Write protection, enter and exit the RTC initialization mode,
79 RTC registers synchronization check and reference clock detection enable.
80 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
81 It is split into 2 programmable prescalers to minimize power consumption.
82 (++) A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
83 (++) When both prescalers are used, it is recommended to configure the
84 asynchronous prescaler to a high value to minimize consumption.
85 (#) All RTC registers are Write protected. Writing to the RTC registers
86 is enabled by writing a key into the Write Protection register, RTC_WRP.
87 (#) To Configure the RTC Calendar, user application should enter
88 initialization mode. In this mode, the calendar counter is stopped
89 and its value can be updated. When the initialization sequence is
90 complete, the calendar restarts counting after 4 RTCCLK cycles.
91 (#) To read the calendar through the shadow registers after Calendar
92 initialization, calendar update or after wakeup from low power modes
93 the software must first clear the RSYF flag. The software must then
94 wait until it is set again before reading the calendar, which means
95 that the calendar registers have been correctly copied into the
96 RTC_TSH and RTC_DATE shadow registers.The RTC_WaitForSynchro() function
97 implements the above software sequence (RSYF clear and RSYF check).
98
99 @endverbatim
100 * @{
101 */
102
103 /**
104 * @brief Deinitializes the RTC registers to their default reset values.
105 * @note This function doesn't reset the RTC Clock source
106 * @return An ErrorStatus enumeration value:
107 * - SUCCESS: RTC registers are deinitialized
108 * - ERROR: RTC registers are not deinitialized
109 */
RTC_DeInit(void)110 ErrorStatus RTC_DeInit(void)
111 {
112 __IO uint32_t wutcounter = 0x00;
113 uint32_t wutwfstatus = 0x00;
114 ErrorStatus status = ERROR;
115
116 /* Disable the write protection for RTC registers */
117 RTC->WRP = 0xCA;
118 RTC->WRP = 0x53;
119
120 /* Set Initialization mode */
121 if (RTC_EnterInitMode() == ERROR)
122 {
123 status = ERROR;
124 }
125 else
126 {
127 /* Reset TSH, DAT and CTRL registers */
128 RTC->TSH = (uint32_t)0x00000000;
129 RTC->DATE = (uint32_t)0x00002101;
130
131 /* Reset All CTRL bits except CTRL[2:0] */
132 RTC->CTRL &= (uint32_t)0x00000007;
133
134 /* Wait till RTC WTWF flag is set and if Time out is reached exit */
135 do
136 {
137 wutwfstatus = RTC->INITSTS & RTC_INITSTS_WTWF;
138 wutcounter++;
139 } while ((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
140
141 if ((RTC->INITSTS & RTC_INITSTS_WTWF) == RESET)
142 {
143 status = ERROR;
144 }
145 else
146 {
147 /* Reset all RTC CTRL register bits */
148 RTC->CTRL &= (uint32_t)0x00000000;
149 RTC->WKUPT = (uint32_t)0x0000FFFF;
150 RTC->PRE = (uint32_t)0x007F00FF;
151 RTC->ALARMA = (uint32_t)0x00000000;
152 RTC->ALARMB = (uint32_t)0x00000000;
153 RTC->SCTRL = (uint32_t)0x00000000;
154 RTC->CALIB = (uint32_t)0x00000000;
155 RTC->ALRMASS = (uint32_t)0x00000000;
156 RTC->ALRMBSS = (uint32_t)0x00000000;
157
158 /* Reset INTSTS register and exit initialization mode */
159 RTC->INITSTS = (uint32_t)0x00000000;
160
161 RTC->OPT = (uint32_t)0x00000000;
162 RTC->TSCWKUPCTRL = (uint32_t)0x00000008;
163 RTC->TSCWKUPCNT = (uint32_t)0x000002FE;
164
165 /* Wait till the RTC RSYF flag is set */
166 if (RTC_WaitForSynchro() == ERROR)
167 {
168 status = ERROR;
169 }
170 else
171 {
172 status = SUCCESS;
173 }
174 }
175 }
176
177 /* Enable the write protection for RTC registers */
178 RTC->WRP = 0xFF;
179
180 return status;
181 }
182
183 /**
184 * @brief Initializes the RTC registers according to the specified parameters
185 * in RTC_InitStruct.
186 * @param RTC_InitStruct pointer to a RTC_InitType structure that contains
187 * the configuration information for the RTC peripheral.
188 * @note The RTC Prescaler register is write protected and can be written in
189 * initialization mode only.
190 * @return An ErrorStatus enumeration value:
191 * - SUCCESS: RTC registers are initialized
192 * - ERROR: RTC registers are not initialized
193 */
RTC_Init(RTC_InitType * RTC_InitStruct)194 ErrorStatus RTC_Init(RTC_InitType* RTC_InitStruct)
195 {
196 ErrorStatus status = ERROR;
197 uint32_t i =0;
198 /* Check the parameters */
199 assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
200 assert_param(IS_RTC_PREDIV_ASYNCH(RTC_InitStruct->RTC_AsynchPrediv));
201 assert_param(IS_RTC_PREDIV_SYNCH(RTC_InitStruct->RTC_SynchPrediv));
202
203 /* Disable the write protection for RTC registers */
204 RTC->WRP = 0xCA;
205 RTC->WRP = 0x53;
206
207 /* Set Initialization mode */
208 if (RTC_EnterInitMode() == ERROR)
209 {
210 status = ERROR;
211 }
212 else
213 {
214 /* Clear RTC CTRL HFMT Bit */
215 RTC->CTRL &= ((uint32_t) ~(RTC_CTRL_HFMT));
216 /* Set RTC_CTRL register */
217 RTC->CTRL |= ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
218
219 /* Configure the RTC PRE */
220 RTC->PRE = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
221 RTC->PRE |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
222
223 /* Exit Initialization mode */
224 RTC_ExitInitMode();
225
226 status = SUCCESS;
227 }
228 /* Enable the write protection for RTC registers */
229 RTC->WRP = 0xFF;
230 /* Delay for the RTC prescale effect */
231 for(i=0;i<0x2FF;i++);
232 return status;
233 }
234
235 /**
236 * @brief Fills each RTC_InitStruct member with its default value.
237 * @param RTC_InitStruct pointer to a RTC_InitType structure which will be
238 * initialized.
239 */
RTC_StructInit(RTC_InitType * RTC_InitStruct)240 void RTC_StructInit(RTC_InitType* RTC_InitStruct)
241 {
242 /* Initialize the RTC_HourFormat member */
243 RTC_InitStruct->RTC_HourFormat = RTC_24HOUR_FORMAT;
244
245 /* Initialize the RTC_AsynchPrediv member */
246 RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
247
248 /* Initialize the RTC_SynchPrediv member */
249 RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF;
250 }
251
252 /**
253 * @brief Enables or disables the RTC registers write protection.
254 * @note All the RTC registers are write protected except for RTC_INITSTS[13:8].
255 * @note Writing a wrong key reactivates the write protection.
256 * @note The protection mechanism is not affected by system reset.
257 * @param Cmd new state of the write protection.
258 * This parameter can be: ENABLE or DISABLE.
259 */
RTC_EnableWriteProtection(FunctionalState Cmd)260 void RTC_EnableWriteProtection(FunctionalState Cmd)
261 {
262 /* Check the parameters */
263 assert_param(IS_FUNCTIONAL_STATE(Cmd));
264
265 if (Cmd != DISABLE)
266 {
267 /* Enable the write protection for RTC registers */
268 RTC->WRP = 0xFF;
269 }
270 else
271 {
272 /* Disable the write protection for RTC registers */
273 RTC->WRP = 0xCA;
274 RTC->WRP = 0x53;
275 }
276 }
277
278 /**
279 * @brief Enters the RTC Initialization mode.
280 * @note The RTC Initialization mode is write protected, use the
281 * RTC_EnableWriteProtection(DISABLE) before calling this function.
282 * @return An ErrorStatus enumeration value:
283 * - SUCCESS: RTC is in Init mode
284 * - ERROR: RTC is not in Init mode
285 */
RTC_EnterInitMode(void)286 ErrorStatus RTC_EnterInitMode(void)
287 {
288 __IO uint32_t initcounter = 0x00;
289 ErrorStatus status = ERROR;
290 uint32_t initstatus = 0x00;
291
292 /* Check if the Initialization mode is set */
293 if ((RTC->INITSTS & RTC_INITSTS_INITF) == (uint32_t)RESET)
294 {
295 /* Set the Initialization mode */
296 RTC->INITSTS = (uint32_t)RTC_INITSTS_INITM;
297
298 /* Wait till RTC is in INIT state and if Time out is reached exit */
299 do
300 {
301 initstatus = RTC->INITSTS & RTC_INITSTS_INITF;
302 initcounter++;
303 } while ((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
304
305 if ((RTC->INITSTS & RTC_INITSTS_INITF) != RESET)
306 {
307 status = SUCCESS;
308 }
309 else
310 {
311 status = ERROR;
312 }
313 }
314 else
315 {
316 status = SUCCESS;
317 }
318
319 return (status);
320 }
321
322 /**
323 * @brief Exits the RTC Initialization mode.
324 * @note When the initialization sequence is complete, the calendar restarts
325 * counting after 4 RTCCLK cycles.
326 * @note The RTC Initialization mode is write protected, use the
327 * RTC_EnableWriteProtection(DISABLE) before calling this function.
328 */
RTC_ExitInitMode(void)329 void RTC_ExitInitMode(void)
330 {
331 /* Exit Initialization mode */
332 RTC->INITSTS &= (uint32_t)~RTC_INITSTS_INITM;
333 }
334
335 /**
336 * @brief Waits until the RTC Time and Date registers (RTC_TSH and RTC_DATE) are
337 * synchronized with RTC APB clock.
338 * @note The RTC Resynchronization mode is write protected, use the
339 * RTC_EnableWriteProtection(DISABLE) before calling this function.
340 * @note To read the calendar through the shadow registers after Calendar
341 * initialization, calendar update or after wakeup from low power modes
342 * the software must first clear the RSYF flag.
343 * The software must then wait until it is set again before reading
344 * the calendar, which means that the calendar registers have been
345 * correctly copied into the RTC_TSH and RTC_DATE shadow registers.
346 * @return An ErrorStatus enumeration value:
347 * - SUCCESS: RTC registers are synchronised
348 * - ERROR: RTC registers are not synchronised
349 */
RTC_WaitForSynchro(void)350 ErrorStatus RTC_WaitForSynchro(void)
351 {
352 __IO uint32_t synchrocounter = 0;
353 ErrorStatus status = ERROR;
354 uint32_t synchrostatus = 0x00;
355
356 /* Disable the write protection for RTC registers */
357 RTC->WRP = 0xCA;
358 RTC->WRP = 0x53;
359
360 /* Clear RSYF flag */
361 RTC->INITSTS &= (uint32_t)RTC_RSF_MASK;
362
363 /* Wait the registers to be synchronised */
364 do
365 {
366 synchrostatus = RTC->INITSTS & RTC_INITSTS_RSYF;
367 synchrocounter++;
368 } while ((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
369
370 if ((RTC->INITSTS & RTC_INITSTS_RSYF) != RESET)
371 {
372 status = SUCCESS;
373 }
374 else
375 {
376 status = ERROR;
377 }
378
379 /* Enable the write protection for RTC registers */
380 RTC->WRP = 0xFF;
381
382 return (status);
383 }
384
385
386
387 /**
388 * @brief Enables or Disables the Bypass Shadow feature.
389 * @note When the Bypass Shadow is enabled the calendar value are taken
390 * directly from the Calendar counter.
391 * @param Cmd new state of the Bypass Shadow feature.
392 * This parameter can be: ENABLE or DISABLE.
393 */
RTC_EnableBypassShadow(FunctionalState Cmd)394 void RTC_EnableBypassShadow(FunctionalState Cmd)
395 {
396 /* Check the parameters */
397 assert_param(IS_FUNCTIONAL_STATE(Cmd));
398
399 /* Disable the write protection for RTC registers */
400 RTC->WRP = 0xCA;
401 RTC->WRP = 0x53;
402
403 if (Cmd != DISABLE)
404 {
405 /* Set the BYPS bit */
406 RTC->CTRL |= (uint8_t)RTC_CTRL_BYPS;
407 }
408 else
409 {
410 /* Reset the BYPS bit */
411 RTC->CTRL &= (uint8_t)~RTC_CTRL_BYPS;
412 }
413
414 /* Enable the write protection for RTC registers */
415 RTC->WRP = 0xFF;
416 }
417
418 /**
419 * @}
420 */
421
422 /** @addtogroup RTC_Group2 Time and Date configuration functions
423 * @brief Time and Date configuration functions
424 *
425 @verbatim
426 ===============================================================================
427 ##### Time and Date configuration functions #####
428 ===============================================================================
429 [..] This section provide functions allowing to program and read the RTC
430 Calendar (Time and Date).
431
432 @endverbatim
433 * @{
434 */
435
436 /**
437 * @brief Set the RTC current time.
438 * @param RTC_Format specifies the format of the entered parameters.
439 * This parameter can be one of the following values:
440 * @arg RTC_FORMAT_BIN Binary data format.
441 * @arg RTC_FORMAT_BCD BCD data format.
442 * @param RTC_TimeStruct pointer to a RTC_TimeType structure that contains
443 * the time configuration information for the RTC.
444 * @return An ErrorStatus enumeration value:
445 * - SUCCESS: RTC Time register is configured
446 * - ERROR: RTC Time register is not configured
447 */
RTC_ConfigTime(uint32_t RTC_Format,RTC_TimeType * RTC_TimeStruct)448 ErrorStatus RTC_ConfigTime(uint32_t RTC_Format, RTC_TimeType* RTC_TimeStruct)
449 {
450 uint32_t tmpregister = 0;
451 ErrorStatus status = ERROR;
452
453 /* Check the parameters */
454 assert_param(IS_RTC_FORMAT(RTC_Format));
455
456 if (RTC_Format == RTC_FORMAT_BIN)
457 {
458 if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
459 {
460 assert_param(IS_RTC_12HOUR(RTC_TimeStruct->Hours));
461 assert_param(IS_RTC_H12(RTC_TimeStruct->H12));
462 }
463 else
464 {
465 RTC_TimeStruct->H12 = 0x00;
466 assert_param(IS_RTC_24HOUR(RTC_TimeStruct->Hours));
467 }
468 assert_param(IS_RTC_MINUTES(RTC_TimeStruct->Minutes));
469 assert_param(IS_RTC_SECONDS(RTC_TimeStruct->Seconds));
470 }
471 else
472 {
473 if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
474 {
475 tmpregister = RTC_Bcd2ToByte(RTC_TimeStruct->Hours);
476 assert_param(IS_RTC_12HOUR(tmpregister));
477 assert_param(IS_RTC_H12(RTC_TimeStruct->H12));
478 }
479 else
480 {
481 RTC_TimeStruct->H12 = 0x00;
482 assert_param(IS_RTC_24HOUR(RTC_Bcd2ToByte(RTC_TimeStruct->Hours)));
483 }
484 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->Minutes)));
485 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->Seconds)));
486 }
487
488 /* Check the input parameters format */
489 if (RTC_Format != RTC_FORMAT_BIN)
490 {
491 tmpregister = (((uint32_t)(RTC_TimeStruct->Hours) << 16) | ((uint32_t)(RTC_TimeStruct->Minutes) << 8)
492 | ((uint32_t)RTC_TimeStruct->Seconds) | ((uint32_t)(RTC_TimeStruct->H12) << 16));
493 }
494 else
495 {
496 tmpregister =
497 (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->Hours) << 16)
498 | ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->Minutes) << 8)
499 | ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->Seconds)) | (((uint32_t)RTC_TimeStruct->H12) << 16));
500 }
501
502 /* Disable the write protection for RTC registers */
503 RTC->WRP = 0xCA;
504 RTC->WRP = 0x53;
505
506 /* Set Initialization mode */
507 if (RTC_EnterInitMode() == ERROR)
508 {
509 status = ERROR;
510 }
511 else
512 {
513 /* Set the RTC_TSH register */
514 RTC->TSH = (uint32_t)(tmpregister & RTC_TR_RESERVED_MASK);
515
516 /* Exit Initialization mode */
517 RTC_ExitInitMode();
518
519 /* If RTC_CTRL_BYPS bit = 0, wait for synchro else this check is not needed */
520 if ((RTC->CTRL & RTC_CTRL_BYPS) == RESET)
521 {
522 if (RTC_WaitForSynchro() == ERROR)
523 {
524 status = ERROR;
525 }
526 else
527 {
528 status = SUCCESS;
529 }
530 }
531 else
532 {
533 status = SUCCESS;
534 }
535 }
536 /* Enable the write protection for RTC registers */
537 RTC->WRP = 0xFF;
538 /* Waits until the RTC Time and Date registers
539 (RTC_TSH and RTC_DATE) are synchronized with RTC APB clock. */
540 status=RTC_WaitForSynchro();
541 return status;
542 }
543
544 /**
545 * @brief Fills each RTC_TimeStruct member with its default value
546 * (Time = 00h:00min:00sec).
547 * @param RTC_TimeStruct pointer to a RTC_TimeType structure which will be
548 * initialized.
549 */
RTC_TimeStructInit(RTC_TimeType * RTC_TimeStruct)550 void RTC_TimeStructInit(RTC_TimeType* RTC_TimeStruct)
551 {
552 /* Time = 00h:00min:00sec */
553 RTC_TimeStruct->H12 = RTC_AM_H12;
554 RTC_TimeStruct->Hours = 0;
555 RTC_TimeStruct->Minutes = 0;
556 RTC_TimeStruct->Seconds = 0;
557 }
558
559 /**
560 * @brief Get the RTC current Time.
561 * @param RTC_Format specifies the format of the returned parameters.
562 * This parameter can be one of the following values:
563 * @arg RTC_FORMAT_BIN Binary data format.
564 * @arg RTC_FORMAT_BCD BCD data format.
565 * @param RTC_TimeStruct pointer to a RTC_TimeType structure that will
566 * contain the returned current time configuration.
567 */
RTC_GetTime(uint32_t RTC_Format,RTC_TimeType * RTC_TimeStruct)568 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeType* RTC_TimeStruct)
569 {
570 uint32_t tmpregister = 0;
571
572 /* Check the parameters */
573 assert_param(IS_RTC_FORMAT(RTC_Format));
574
575 /* Get the RTC_TSH register */
576 tmpregister = (uint32_t)(RTC->TSH & RTC_TR_RESERVED_MASK);
577
578 /* Fill the structure fields with the read parameters */
579 RTC_TimeStruct->Hours = (uint8_t)((tmpregister & (RTC_TSH_HOT | RTC_TSH_HOU)) >> 16);
580 RTC_TimeStruct->Minutes = (uint8_t)((tmpregister & (RTC_TSH_MIT | RTC_TSH_MIU)) >> 8);
581 RTC_TimeStruct->Seconds = (uint8_t)(tmpregister & (RTC_TSH_SCT | RTC_TSH_SCU));
582 RTC_TimeStruct->H12 = (uint8_t)((tmpregister & (RTC_TSH_APM)) >> 16);
583
584 /* Check the input parameters format */
585 if (RTC_Format == RTC_FORMAT_BIN)
586 {
587 /* Convert the structure parameters to Binary format */
588 RTC_TimeStruct->Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->Hours);
589 RTC_TimeStruct->Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->Minutes);
590 RTC_TimeStruct->Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->Seconds);
591 }
592 }
593
594 /**
595 * @brief Gets the RTC current Calendar Subseconds value.
596 * @return RTC current Calendar Subseconds value.
597 */
RTC_GetSubSecond(void)598 uint32_t RTC_GetSubSecond(void)
599 {
600 uint32_t tmpregister = 0;
601
602 /* Get subseconds values from the correspondent registers*/
603 tmpregister = (uint32_t)(RTC->SUBS);
604
605 return (tmpregister);
606 }
607
608 /**
609 * @brief Set the RTC current date.
610 * @param RTC_Format specifies the format of the entered parameters.
611 * This parameter can be one of the following values:
612 * @arg RTC_FORMAT_BIN Binary data format.
613 * @arg RTC_FORMAT_BCD BCD data format.
614 * @param RTC_DateStruct pointer to a RTC_DateType structure that contains
615 * the date configuration information for the RTC.
616 * @return An ErrorStatus enumeration value:
617 * - SUCCESS: RTC Date register is configured
618 * - ERROR: RTC Date register is not configured
619 */
RTC_SetDate(uint32_t RTC_Format,RTC_DateType * RTC_DateStruct)620 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateType* RTC_DateStruct)
621 {
622 uint32_t tmpregister = 0;
623 ErrorStatus status = ERROR;
624
625 /* Check the parameters */
626 assert_param(IS_RTC_FORMAT(RTC_Format));
627
628 if ((RTC_Format == RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10) == 0x10))
629 {
630 RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint32_t) ~(0x10)) + 0x0A;
631 }
632 if (RTC_Format == RTC_FORMAT_BIN)
633 {
634 assert_param(IS_RTC_YEAR(RTC_DateStruct->Year));
635 assert_param(IS_RTC_MONTH(RTC_DateStruct->Month));
636 assert_param(IS_RTC_DATE(RTC_DateStruct->Date));
637 }
638 else
639 {
640 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->Year)));
641 tmpregister = RTC_Bcd2ToByte(RTC_DateStruct->Month);
642 assert_param(IS_RTC_MONTH(tmpregister));
643 tmpregister = RTC_Bcd2ToByte(RTC_DateStruct->Date);
644 assert_param(IS_RTC_DATE(tmpregister));
645 }
646 assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
647
648 /* Check the input parameters format */
649 if (RTC_Format != RTC_FORMAT_BIN)
650 {
651 tmpregister = ((((uint32_t)RTC_DateStruct->Year) << 16) | (((uint32_t)RTC_DateStruct->Month) << 8)
652 | ((uint32_t)RTC_DateStruct->Date) | (((uint32_t)RTC_DateStruct->WeekDay) << 13));
653 }
654 else
655 {
656 tmpregister = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->Year) << 16)
657 | ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->Month) << 8)
658 | ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->Date)) | ((uint32_t)RTC_DateStruct->WeekDay << 13));
659 }
660
661 /* Disable the write protection for RTC registers */
662 RTC->WRP = 0xCA;
663 RTC->WRP = 0x53;
664
665 /* Set Initialization mode */
666 if (RTC_EnterInitMode() == ERROR)
667 {
668 status = ERROR;
669 }
670 else
671 {
672 /* Set the RTC_DATE register */
673 RTC->DATE = (uint32_t)(tmpregister & RTC_DATE_RESERVED_MASK);
674
675 /* Exit Initialization mode */
676 RTC_ExitInitMode();
677
678 /* If RTC_CTRL_BYPS bit = 0, wait for synchro else this check is not needed */
679 if ((RTC->CTRL & RTC_CTRL_BYPS) == RESET)
680 {
681 if (RTC_WaitForSynchro() == ERROR)
682 {
683 status = ERROR;
684 }
685 else
686 {
687 status = SUCCESS;
688 }
689 }
690 else
691 {
692 status = SUCCESS;
693 }
694 }
695 /* Enable the write protection for RTC registers */
696 RTC->WRP = 0xFF;
697 /* Waits until the RTC Time and Date registers
698 (RTC_TSH and RTC_DATE) are synchronized with RTC APB clock. */
699 status=RTC_WaitForSynchro();
700 return status;
701 }
702
703 /**
704 * @brief Fills each RTC_DateStruct member with its default value
705 * (Monday, January 01 xx00).
706 * @param RTC_DateStruct pointer to a RTC_DateType structure which will be
707 * initialized.
708 */
RTC_DateStructInit(RTC_DateType * RTC_DateStruct)709 void RTC_DateStructInit(RTC_DateType* RTC_DateStruct)
710 {
711 /* Monday, January 01 xx00 */
712 RTC_DateStruct->WeekDay = RTC_WEEKDAY_MONDAY;
713 RTC_DateStruct->Date = 1;
714 RTC_DateStruct->Month = RTC_MONTH_JANUARY;
715 RTC_DateStruct->Year = 0;
716 }
717
718 /**
719 * @brief Get the RTC current date.
720 * @param RTC_Format specifies the format of the returned parameters.
721 * This parameter can be one of the following values:
722 * @arg RTC_FORMAT_BIN Binary data format.
723 * @arg RTC_FORMAT_BCD BCD data format.
724 * @param RTC_DateStruct pointer to a RTC_DateType structure that will
725 * contain the returned current date configuration.
726 */
RTC_GetDate(uint32_t RTC_Format,RTC_DateType * RTC_DateStruct)727 void RTC_GetDate(uint32_t RTC_Format, RTC_DateType* RTC_DateStruct)
728 {
729 uint32_t tmpregister = 0;
730
731 /* Check the parameters */
732 assert_param(IS_RTC_FORMAT(RTC_Format));
733
734 /* Get the RTC_TSH register */
735 tmpregister = (uint32_t)(RTC->DATE & RTC_DATE_RESERVED_MASK);
736
737 /* Fill the structure fields with the read parameters */
738 RTC_DateStruct->Year = (uint8_t)((tmpregister & (RTC_DATE_YRT | RTC_DATE_YRU)) >> 16);
739 RTC_DateStruct->Month = (uint8_t)((tmpregister & (RTC_DATE_MOT | RTC_DATE_MOU)) >> 8);
740 RTC_DateStruct->Date = (uint8_t)(tmpregister & (RTC_DATE_DAT | RTC_DATE_DAU));
741 RTC_DateStruct->WeekDay = (uint8_t)((tmpregister & (RTC_DATE_WDU)) >> 13);
742
743 /* Check the input parameters format */
744 if (RTC_Format == RTC_FORMAT_BIN)
745 {
746 /* Convert the structure parameters to Binary format */
747 RTC_DateStruct->Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->Year);
748 RTC_DateStruct->Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->Month);
749 RTC_DateStruct->Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->Date);
750 }
751 }
752
753 /**
754 * @}
755 */
756
757 /** @addtogroup RTC_Group3 Alarms configuration functions
758 * @brief Alarms (Alarm A and Alarm B) configuration functions
759 *
760 @verbatim
761 ===============================================================================
762 ##### Alarms (Alarm A and Alarm B) configuration functions #####
763 ===============================================================================
764 [..] This section provide functions allowing to program and read the RTC
765 Alarms.
766
767 @endverbatim
768 * @{
769 */
770
771 /**
772 * @brief Set the specified RTC Alarm.
773 * @note The Alarm register can only be written when the corresponding Alarm
774 * is disabled (Use the RTC_EnableAlarm(DISABLE)).
775 * @param RTC_Format specifies the format of the returned parameters.
776 * This parameter can be one of the following values:
777 * @arg RTC_FORMAT_BIN Binary data format.
778 * @arg RTC_FORMAT_BCD BCD data format.
779 * @param RTC_Alarm specifies the alarm to be configured.
780 * This parameter can be one of the following values:
781 * @arg RTC_A_ALARM to select Alarm A.
782 * @arg RTC_B_ALARM to select Alarm B.
783 * @param RTC_AlarmStruct pointer to a RTC_AlarmType structure that
784 * contains the alarm configuration parameters.
785 */
RTC_SetAlarm(uint32_t RTC_Format,uint32_t RTC_Alarm,RTC_AlarmType * RTC_AlarmStruct)786 void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmType* RTC_AlarmStruct)
787 {
788 uint32_t tmpregister = 0;
789
790 /* Check the parameters */
791 assert_param(IS_RTC_FORMAT(RTC_Format));
792 assert_param(IS_RTC_ALARM_SEL(RTC_Alarm));
793 assert_param(IS_ALARM_MASK(RTC_AlarmStruct->AlarmMask));
794 assert_param(IS_RTC_ALARM_WEEKDAY_SEL(RTC_AlarmStruct->DateWeekMode));
795
796 if (RTC_Format == RTC_FORMAT_BIN)
797 {
798 if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
799 {
800 assert_param(IS_RTC_12HOUR(RTC_AlarmStruct->AlarmTime.Hours));
801 assert_param(IS_RTC_H12(RTC_AlarmStruct->AlarmTime.H12));
802 }
803 else
804 {
805 RTC_AlarmStruct->AlarmTime.H12 = 0x00;
806 assert_param(IS_RTC_24HOUR(RTC_AlarmStruct->AlarmTime.Hours));
807 }
808 assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
809 assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
810
811 if (RTC_AlarmStruct->DateWeekMode == RTC_ALARM_SEL_WEEKDAY_DATE)
812 {
813 assert_param(IS_RTC_ALARM_WEEKDAY_DATE(RTC_AlarmStruct->DateWeekValue));
814 }
815 else
816 {
817 assert_param(IS_RTC_ALARM_WEEKDAY_WEEKDAY(RTC_AlarmStruct->DateWeekValue));
818 }
819 }
820 else
821 {
822 if ((RTC->CTRL & RTC_CTRL_HFMT) != (uint32_t)RESET)
823 {
824 tmpregister = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Hours);
825 assert_param(IS_RTC_12HOUR(tmpregister));
826 assert_param(IS_RTC_H12(RTC_AlarmStruct->AlarmTime.H12));
827 }
828 else
829 {
830 RTC_AlarmStruct->AlarmTime.H12 = 0x00;
831 assert_param(IS_RTC_24HOUR(RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Hours)));
832 }
833
834 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Minutes)));
835 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Seconds)));
836
837 if (RTC_AlarmStruct->DateWeekMode == RTC_ALARM_SEL_WEEKDAY_DATE)
838 {
839 tmpregister = RTC_Bcd2ToByte(RTC_AlarmStruct->DateWeekValue);
840 assert_param(IS_RTC_ALARM_WEEKDAY_DATE(tmpregister));
841 }
842 else
843 {
844 tmpregister = RTC_Bcd2ToByte(RTC_AlarmStruct->DateWeekValue);
845 assert_param(IS_RTC_ALARM_WEEKDAY_WEEKDAY(tmpregister));
846 }
847 }
848
849 /* Check the input parameters format */
850 if (RTC_Format != RTC_FORMAT_BIN)
851 {
852 tmpregister =
853 (((uint32_t)(RTC_AlarmStruct->AlarmTime.Hours) << 16)
854 | ((uint32_t)(RTC_AlarmStruct->AlarmTime.Minutes) << 8) | ((uint32_t)RTC_AlarmStruct->AlarmTime.Seconds)
855 | ((uint32_t)(RTC_AlarmStruct->AlarmTime.H12) << 16) | ((uint32_t)(RTC_AlarmStruct->DateWeekValue) << 24)
856 | ((uint32_t)RTC_AlarmStruct->DateWeekMode) | ((uint32_t)RTC_AlarmStruct->AlarmMask));
857 }
858 else
859 {
860 tmpregister = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->AlarmTime.Hours) << 16)
861 | ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->AlarmTime.Minutes) << 8)
862 | ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->AlarmTime.Seconds))
863 | ((uint32_t)(RTC_AlarmStruct->AlarmTime.H12) << 16)
864 | ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->DateWeekValue) << 24)
865 | ((uint32_t)RTC_AlarmStruct->DateWeekMode) | ((uint32_t)RTC_AlarmStruct->AlarmMask));
866 }
867
868 /* Disable the write protection for RTC registers */
869 RTC->WRP = 0xCA;
870 RTC->WRP = 0x53;
871
872 /* Configure the Alarm register */
873 if (RTC_Alarm == RTC_A_ALARM)
874 {
875 RTC->ALARMA = (uint32_t)tmpregister;
876 }
877 else
878 {
879 RTC->ALARMB = (uint32_t)tmpregister;
880 }
881
882 /* Enable the write protection for RTC registers */
883 RTC->WRP = 0xFF;
884 }
885
886 /**
887 * @brief Fills each RTC_AlarmStruct member with its default value
888 * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
889 * all fields are masked).
890 * @param RTC_AlarmStruct pointer to a @ref RTC_AlarmType structure which
891 * will be initialized.
892 */
RTC_AlarmStructInit(RTC_AlarmType * RTC_AlarmStruct)893 void RTC_AlarmStructInit(RTC_AlarmType* RTC_AlarmStruct)
894 {
895 /* Alarm Time Settings : Time = 00h:00mn:00sec */
896 RTC_AlarmStruct->AlarmTime.H12 = RTC_AM_H12;
897 RTC_AlarmStruct->AlarmTime.Hours = 0;
898 RTC_AlarmStruct->AlarmTime.Minutes = 0;
899 RTC_AlarmStruct->AlarmTime.Seconds = 0;
900
901 /* Alarm Date Settings : Date = 1st day of the month */
902 RTC_AlarmStruct->DateWeekMode = RTC_ALARM_SEL_WEEKDAY_DATE;
903 RTC_AlarmStruct->DateWeekValue = 1;
904
905 /* Alarm Masks Settings : Mask = all fields are not masked */
906 RTC_AlarmStruct->AlarmMask = RTC_ALARMMASK_NONE;
907 }
908
909 /**
910 * @brief Get the RTC Alarm value and masks.
911 * @param RTC_Format specifies the format of the output parameters.
912 * This parameter can be one of the following values:
913 * @arg RTC_FORMAT_BIN Binary data format.
914 * @arg RTC_FORMAT_BCD BCD data format.
915 * @param RTC_Alarm specifies the alarm to be read.
916 * This parameter can be one of the following values:
917 * @arg RTC_A_ALARM to select Alarm A.
918 * @arg RTC_B_ALARM to select Alarm B.
919 * @param RTC_AlarmStruct pointer to a RTC_AlarmType structure that will
920 * contains the output alarm configuration values.
921 */
RTC_GetAlarm(uint32_t RTC_Format,uint32_t RTC_Alarm,RTC_AlarmType * RTC_AlarmStruct)922 void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmType* RTC_AlarmStruct)
923 {
924 uint32_t tmpregister = 0;
925
926 /* Check the parameters */
927 assert_param(IS_RTC_FORMAT(RTC_Format));
928 assert_param(IS_RTC_ALARM_SEL(RTC_Alarm));
929
930 /* Get the RTC_ALARMx register */
931 if (RTC_Alarm == RTC_A_ALARM)
932 {
933 tmpregister = (uint32_t)(RTC->ALARMA);
934 }
935 else
936 {
937 tmpregister = (uint32_t)(RTC->ALARMB);
938 }
939
940 /* Fill the structure with the read parameters */
941 RTC_AlarmStruct->AlarmTime.Hours = (uint32_t)((tmpregister & (RTC_ALARMA_HOT | RTC_ALARMA_HOU)) >> 16);
942 RTC_AlarmStruct->AlarmTime.Minutes = (uint32_t)((tmpregister & (RTC_ALARMA_MIT | RTC_ALARMA_MIU)) >> 8);
943 RTC_AlarmStruct->AlarmTime.Seconds = (uint32_t)(tmpregister & (RTC_ALARMA_SET | RTC_ALARMA_SEU));
944 RTC_AlarmStruct->AlarmTime.H12 = (uint32_t)((tmpregister & RTC_ALARMA_APM) >> 16);
945 RTC_AlarmStruct->DateWeekValue = (uint32_t)((tmpregister & (RTC_ALARMA_DTT | RTC_ALARMA_DTU)) >> 24);
946 RTC_AlarmStruct->DateWeekMode = (uint32_t)(tmpregister & RTC_ALARMA_WKDSEL);
947 RTC_AlarmStruct->AlarmMask = (uint32_t)(tmpregister & RTC_ALARMMASK_ALL);
948
949 if (RTC_Format == RTC_FORMAT_BIN)
950 {
951 RTC_AlarmStruct->AlarmTime.Hours = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Hours);
952 RTC_AlarmStruct->AlarmTime.Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Minutes);
953 RTC_AlarmStruct->AlarmTime.Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct->AlarmTime.Seconds);
954 RTC_AlarmStruct->DateWeekValue = RTC_Bcd2ToByte(RTC_AlarmStruct->DateWeekValue);
955 }
956 }
957
958 /**
959 * @brief Enables or disables the specified RTC Alarm.
960 * @param RTC_Alarm specifies the alarm to be configured.
961 * This parameter can be any combination of the following values:
962 * @arg RTC_A_ALARM to select Alarm A.
963 * @arg RTC_B_ALARM to select Alarm B.
964 * @param Cmd new state of the specified alarm.
965 * This parameter can be: ENABLE or DISABLE.
966 * @return An ErrorStatus enumeration value:
967 * - SUCCESS: RTC Alarm is enabled/disabled
968 * - ERROR: RTC Alarm is not enabled/disabled
969 */
RTC_EnableAlarm(uint32_t RTC_Alarm,FunctionalState Cmd)970 ErrorStatus RTC_EnableAlarm(uint32_t RTC_Alarm, FunctionalState Cmd)
971 {
972 __IO uint32_t alarmcounter = 0x00;
973 uint32_t alarmstatus = 0x00;
974 ErrorStatus status = ERROR;
975
976 /* Check the parameters */
977 assert_param(IS_RTC_ALARM_ENABLE(RTC_Alarm));
978 assert_param(IS_FUNCTIONAL_STATE(Cmd));
979
980 /* Disable the write protection for RTC registers */
981 RTC->WRP = 0xCA;
982 RTC->WRP = 0x53;
983
984 /* Configure the Alarm state */
985 if (Cmd != DISABLE)
986 {
987 RTC->CTRL |= (uint32_t)RTC_Alarm;
988
989 status = SUCCESS;
990 }
991 else
992 {
993 /* Disable the Alarm in RTC_CTRL register */
994 RTC->CTRL &= (uint32_t)~RTC_Alarm;
995
996 /* Wait till RTC ALxWF flag is set and if Time out is reached exit */
997 do
998 {
999 alarmstatus = RTC->INITSTS & (RTC_Alarm >> 8);
1000 alarmcounter++;
1001 } while ((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
1002
1003 if ((RTC->INITSTS & (RTC_Alarm >> 8)) == RESET)
1004 {
1005 status = ERROR;
1006 }
1007 else
1008 {
1009 status = SUCCESS;
1010 }
1011 }
1012
1013 /* Enable the write protection for RTC registers */
1014 RTC->WRP = 0xFF;
1015
1016 return status;
1017 }
1018
1019 /**
1020 * @brief Configure the RTC AlarmA/B Subseconds value and mask.*
1021 * @note This function is performed only when the Alarm is disabled.
1022 * @param RTC_Alarm specifies the alarm to be configured.
1023 * This parameter can be one of the following values:
1024 * @arg RTC_A_ALARM to select Alarm A.
1025 * @arg RTC_B_ALARM to select Alarm B.
1026 * @param RTC_AlarmSubSecondValue specifies the Subseconds value.
1027 * This parameter can be a value from 0 to 0x00007FFF.
1028 * @param RTC_AlarmSubSecondMask specifies the Subseconds Mask.
1029 * This parameter can be any combination of the following values:
1030 * @arg RTC_SUBS_MASK_ALL All Alarm SS fields are masked.
1031 * There is no comparison on sub seconds for Alarm.
1032 * @arg RTC_SUBS_MASK_SS14_1 SS[14:1] are don't care in Alarm comparison.
1033 * Only SS[0] is compared
1034 * @arg RTC_SUBS_MASK_SS14_2 SS[14:2] are don't care in Alarm comparison.
1035 * Only SS[1:0] are compared
1036 * @arg RTC_SUBS_MASK_SS14_3 SS[14:3] are don't care in Alarm comparison.
1037 * Only SS[2:0] are compared
1038 * @arg RTC_SUBS_MASK_SS14_4 SS[14:4] are don't care in Alarm comparison.
1039 * Only SS[3:0] are compared
1040 * @arg RTC_SUBS_MASK_SS14_5 SS[14:5] are don't care in Alarm comparison.
1041 * Only SS[4:0] are compared.
1042 * @arg RTC_SUBS_MASK_SS14_6 SS[14:6] are don't care in Alarm comparison.
1043 * Only SS[5:0] are compared.
1044 * @arg RTC_SUBS_MASK_SS14_7 SS[14:7] are don't care in Alarm comparison.
1045 * Only SS[6:0] are compared.
1046 * @arg RTC_SUBS_MASK_SS14_8 SS[14:8] are don't care in Alarm comparison.
1047 * Only SS[7:0] are compared.
1048 * @arg RTC_SUBS_MASK_SS14_9 SS[14:9] are don't care in Alarm comparison.
1049 * Only SS[8:0] are compared.
1050 * @arg RTC_SUBS_MASK_SS14_10 SS[14:10] are don't care in Alarm comparison.
1051 * Only SS[9:0] are compared.
1052 * @arg RTC_SUBS_MASK_SS14_11 SS[14:11] are don't care in Alarm comparison.
1053 * Only SS[10:0] are compared.
1054 * @arg RTC_SUBS_MASK_SS14_12 SS[14:12] are don't care in Alarm comparison.
1055 * Only SS[11:0] are compared.
1056 * @arg RTC_SUBS_MASK_SS14_13 SS[14:13] are don't care in Alarm comparison.
1057 * Only SS[12:0] are compared.
1058 * @arg RTC_SUBS_MASK_SS14_14 SS[14] is don't care in Alarm comparison.
1059 * Only SS[13:0] are compared.
1060 * @arg RTC_SUBS_MASK_NONE SS[14:0] are compared and must match
1061 * to activate alarm.
1062 */
RTC_ConfigAlarmSubSecond(uint32_t RTC_Alarm,uint32_t RTC_AlarmSubSecondValue,uint32_t RTC_AlarmSubSecondMask)1063 void RTC_ConfigAlarmSubSecond(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)
1064 {
1065 uint32_t tmpregister = 0;
1066
1067 /* Check the parameters */
1068 assert_param(IS_RTC_ALARM_SEL(RTC_Alarm));
1069 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
1070 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK_MODE(RTC_AlarmSubSecondMask));
1071
1072 /* Disable the write protection for RTC registers */
1073 RTC->WRP = 0xCA;
1074 RTC->WRP = 0x53;
1075
1076 /* Configure the Alarm A or Alarm B SubSecond registers */
1077 tmpregister = (uint32_t)(uint32_t)(RTC_AlarmSubSecondValue) | (uint32_t)(RTC_AlarmSubSecondMask);
1078
1079 if (RTC_Alarm == RTC_A_ALARM)
1080 {
1081 /* Configure the AlarmA SubSecond register */
1082 RTC->ALRMASS = tmpregister;
1083 }
1084 else
1085 {
1086 /* Configure the Alarm B SubSecond register */
1087 RTC->ALRMBSS = tmpregister;
1088 }
1089
1090 /* Enable the write protection for RTC registers */
1091 RTC->WRP = 0xFF;
1092 }
1093
1094 /**
1095 * @brief Gets the RTC Alarm Subseconds value.
1096 * @param RTC_Alarm specifies the alarm to be read.
1097 * This parameter can be one of the following values:
1098 * @arg RTC_A_ALARM to select Alarm A.
1099 * @arg RTC_B_ALARM to select Alarm B.
1100 * @return RTC Alarm Subseconds value.
1101 */
RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)1102 uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
1103 {
1104 uint32_t tmpregister = 0;
1105
1106 /* Get the RTC_ALARMx register */
1107 if (RTC_Alarm == RTC_A_ALARM)
1108 {
1109 tmpregister = (uint32_t)((RTC->ALRMASS) & RTC_ALRMASS_SSV);
1110 }
1111 else
1112 {
1113 tmpregister = (uint32_t)((RTC->ALRMBSS) & RTC_ALRMBSS_SSV);
1114 }
1115
1116 return (tmpregister);
1117 }
1118
1119 /**
1120 * @}
1121 */
1122
1123 /** @addtogroup RTC_Group4 WakeUp Timer configuration functions
1124 * @brief WakeUp Timer configuration functions
1125 *
1126 @verbatim
1127 ===============================================================================
1128 ##### WakeUp Timer configuration functions #####
1129 ===============================================================================
1130 [..] This section provide functions allowing to program and read the RTC WakeUp.
1131
1132 @endverbatim
1133 * @{
1134 */
1135
1136 /**
1137 * @brief Configures the RTC Wakeup clock source.
1138 * @note The WakeUp Clock source can only be changed when the RTC WakeUp
1139 * is disabled (Use the RTC_EnableWakeUp(DISABLE)).
1140 * @param RTC_WakeUpClock Wakeup Clock source.
1141 * This parameter can be one of the following values:
1142 * @arg RTC_WKUPCLK_RTCCLK_DIV16 RTC Wakeup Counter Clock = RTCCLK/16.
1143 * @arg RTC_WKUPCLK_RTCCLK_DIV8 RTC Wakeup Counter Clock = RTCCLK/8.
1144 * @arg RTC_WKUPCLK_RTCCLK_DIV4 RTC Wakeup Counter Clock = RTCCLK/4.
1145 * @arg RTC_WKUPCLK_RTCCLK_DIV2 RTC Wakeup Counter Clock = RTCCLK/2.
1146 * @arg RTC_WKUPCLK_CK_SPRE_16BITS RTC Wakeup Counter Clock = CK_SPRE.
1147 */
RTC_ConfigWakeUpClock(uint32_t RTC_WakeUpClock)1148 void RTC_ConfigWakeUpClock(uint32_t RTC_WakeUpClock)
1149 {
1150 /* Check the parameters */
1151 assert_param(IS_RTC_WKUP_CLOCK(RTC_WakeUpClock));
1152
1153 /* Disable the write protection for RTC registers */
1154 RTC->WRP = 0xCA;
1155 RTC->WRP = 0x53;
1156
1157 /* Clear the Wakeup Timer clock source bits in CTRL register */
1158 RTC->CTRL &= (uint32_t)~RTC_CTRL_WKUPSEL;
1159
1160 /* Configure the clock source */
1161 RTC->CTRL |= (uint32_t)RTC_WakeUpClock;
1162
1163 /* Enable the write protection for RTC registers */
1164 RTC->WRP = 0xFF;
1165 }
1166
1167 /**
1168 * @brief Configures the RTC Wakeup counter.
1169 * @note The RTC WakeUp counter can only be written when the RTC WakeUp.
1170 * is disabled (Use the RTC_EnableWakeUp(DISABLE)).
1171 * @param RTC_WakeUpCounter specifies the WakeUp counter.
1172 * This parameter can be a value from 0x0000 to 0xFFFF.
1173 */
RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)1174 void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
1175 {
1176 /* Check the parameters */
1177 assert_param(IS_RTC_WKUP_COUNTER(RTC_WakeUpCounter));
1178
1179 /* Disable the write protection for RTC registers */
1180 RTC->WRP = 0xCA;
1181 RTC->WRP = 0x53;
1182
1183 /* Configure the Wakeup Timer counter */
1184 RTC->WKUPT = (uint32_t)RTC_WakeUpCounter;
1185
1186 /* Enable the write protection for RTC registers */
1187 RTC->WRP = 0xFF;
1188 }
1189
1190 /**
1191 * @brief Returns the RTC WakeUp timer counter value.
1192 * @return The RTC WakeUp Counter value.
1193 */
RTC_GetWakeUpCounter(void)1194 uint32_t RTC_GetWakeUpCounter(void)
1195 {
1196 /* Get the counter value */
1197 return ((uint32_t)(RTC->WKUPT & RTC_WKUPT_WKUPT));
1198 }
1199
1200 /**
1201 * @brief Enables or Disables the RTC WakeUp timer.
1202 * @param Cmd new state of the WakeUp timer.
1203 * This parameter can be: ENABLE or DISABLE.
1204 */
RTC_EnableWakeUp(FunctionalState Cmd)1205 ErrorStatus RTC_EnableWakeUp(FunctionalState Cmd)
1206 {
1207 __IO uint32_t wutcounter = 0x00;
1208 uint32_t wutwfstatus = 0x00;
1209 ErrorStatus status = ERROR;
1210
1211 /* Check the parameters */
1212 assert_param(IS_FUNCTIONAL_STATE(Cmd));
1213
1214 /* Disable the write protection for RTC registers */
1215 RTC->WRP = 0xCA;
1216 RTC->WRP = 0x53;
1217
1218 if (Cmd != DISABLE)
1219 {
1220 /* Enable the Wakeup Timer */
1221 RTC->CTRL |= (uint32_t)RTC_CTRL_WTEN;
1222 status = SUCCESS;
1223 }
1224 else
1225 {
1226 /* Disable the Wakeup Timer */
1227 RTC->CTRL &= (uint32_t)~RTC_CTRL_WTEN;
1228 /* Wait till RTC WTWF flag is set and if Time out is reached exit */
1229 do
1230 {
1231 wutwfstatus = RTC->INITSTS & RTC_INITSTS_WTWF;
1232 wutcounter++;
1233 } while ((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
1234
1235 if ((RTC->INITSTS & RTC_INITSTS_WTWF) == RESET)
1236 {
1237 status = ERROR;
1238 }
1239 else
1240 {
1241 status = SUCCESS;
1242 }
1243 }
1244
1245 /* Enable the write protection for RTC registers */
1246 RTC->WRP = 0xFF;
1247
1248 return status;
1249 }
1250
1251 /**
1252 * @}
1253 */
1254
1255 /** @addtogroup RTC_Group5 Daylight Saving configuration functions
1256 * @brief Daylight Saving configuration functions
1257 *
1258 @verbatim
1259 ===============================================================================
1260 ##### Daylight Saving configuration functions #####
1261 ===============================================================================
1262 [..] This section provide functions allowing to configure the RTC DayLight Saving.
1263
1264 @endverbatim
1265 * @{
1266 */
1267
1268 /**
1269 * @brief Adds or substract one hour from the current time.
1270 * @param RTC_DayLightSaving the value of hour adjustment.
1271 * This parameter can be one of the following values:
1272 * @arg RTC_DAYLIGHT_SAVING_SUB1H Substract one hour (winter time).
1273 * @arg RTC_DAYLIGHT_SAVING_ADD1H Add one hour (summer time).
1274 * @param RTC_StoreOperation Specifies the value to be written in the BCK bit
1275 * in CTRL register to store the operation.
1276 * This parameter can be one of the following values:
1277 * @arg RTC_STORE_OPERATION_RESET BCK Bit Reset.
1278 * @arg RTC_STORE_OPERATION_SET BCK Bit Set.
1279 */
RTC_ConfigDayLightSaving(uint32_t RTC_DayLightSaving,uint32_t RTC_StoreOperation)1280 void RTC_ConfigDayLightSaving(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
1281 {
1282 /* Check the parameters */
1283 assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
1284 assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
1285
1286 /* Disable the write protection for RTC registers */
1287 RTC->WRP = 0xCA;
1288 RTC->WRP = 0x53;
1289
1290 /* Clear the bits to be configured */
1291 RTC->CTRL &= (uint32_t) ~(RTC_CTRL_BAKP);
1292 /* Clear the SU1H and AD1H bits to be configured */
1293 RTC->CTRL &= (uint32_t) ~(RTC_CTRL_SU1H & RTC_CTRL_AD1H);
1294 /* Configure the RTC_CTRL register */
1295 RTC->CTRL |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
1296
1297 /* Enable the write protection for RTC registers */
1298 RTC->WRP = 0xFF;
1299 }
1300
1301 /**
1302 * @brief Returns the RTC Day Light Saving stored operation.
1303 * @return RTC Day Light Saving stored operation.
1304 * - RTC_STORE_OPERATION_RESET
1305 * - RTC_STORE_OPERATION_SET
1306 */
RTC_GetStoreOperation(void)1307 uint32_t RTC_GetStoreOperation(void)
1308 {
1309 return (RTC->CTRL & RTC_CTRL_BAKP);
1310 }
1311
1312 /**
1313 * @}
1314 */
1315
1316 /** @addtogroup RTC_Group6 Output pin Configuration function
1317 * @brief Output pin Configuration function
1318 *
1319 @verbatim
1320 ===============================================================================
1321 ##### Output pin Configuration function #####
1322 ===============================================================================
1323 [..] This section provide functions allowing to configure the RTC Output source.
1324
1325 @endverbatim
1326 * @{
1327 */
1328
1329
1330
1331 /**
1332 * @brief Configures the RTC output source (AFO_ALARM).
1333 * @param RTC_Output Specifies which signal will be routed to the RTC output.
1334 * This parameter can be one of the following values:
1335 * @arg RTC_OUTPUT_DIS No output selected
1336 * @arg RTC_OUTPUT_ALA signal of AlarmA mapped to output.
1337 * @arg RTC_OUTPUT_ALB signal of AlarmB mapped to output.
1338 * @arg RTC_OUTPUT_WKUP signal of WakeUp mapped to output.
1339 * @param RTC_OutputPolarity Specifies the polarity of the output signal.
1340 * This parameter can be one of the following:
1341 * @arg RTC_OUTPOL_HIGH The output pin is high when the
1342 * ALRAF/ALRBF/WUTF is high (depending on OSEL).
1343 * @arg RTC_OUTPOL_LOW The output pin is low when the
1344 * ALRAF/ALRBF/WUTF is high (depending on OSEL).
1345 */
RTC_ConfigOutput(uint32_t RTC_Output,uint32_t RTC_OutputPolarity)1346 void RTC_ConfigOutput(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
1347 {
1348 /* Check the parameters */
1349 assert_param(IS_RTC_OUTPUT_MODE(RTC_Output));
1350 assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
1351
1352 /* Disable the write protection for RTC registers */
1353 RTC->WRP = 0xCA;
1354 RTC->WRP = 0x53;
1355
1356 /* Clear the bits to be configured */
1357 RTC->CTRL &= (uint32_t) ~(RTC_CTRL_OUTSEL | RTC_CTRL_OPOL);
1358
1359 /* Configure the output selection and polarity */
1360 RTC->CTRL |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
1361
1362 /* Enable the write protection for RTC registers */
1363 RTC->WRP = 0xFF;
1364 }
1365
1366 /**
1367 * @}
1368 */
1369
1370 /** @addtogroup RTC_Group7 Coarse and Smooth Calibrations configuration functions
1371 * @brief Coarse and Smooth Calibrations configuration functions
1372 *
1373 @verbatim
1374 ===============================================================================
1375 ##### Coarse and Smooth Calibrations configuration functions #####
1376 ===============================================================================
1377
1378 @endverbatim
1379 * @{
1380 */
1381
1382 /**
1383 * @brief Enables or disables the RTC clock to be output through the relative
1384 * pin.
1385 * @param Cmd new state of the coarse calibration Output.
1386 * This parameter can be: ENABLE or DISABLE.
1387 */
RTC_EnableCalibOutput(FunctionalState Cmd)1388 void RTC_EnableCalibOutput(FunctionalState Cmd)
1389 {
1390 /* Check the parameters */
1391 assert_param(IS_FUNCTIONAL_STATE(Cmd));
1392
1393 /* Disable the write protection for RTC registers */
1394 RTC->WRP = 0xCA;
1395 RTC->WRP = 0x53;
1396
1397 if (Cmd != DISABLE)
1398 {
1399 /* Enable the RTC clock output */
1400 RTC->CTRL |= (uint32_t)RTC_CTRL_COEN;
1401 }
1402 else
1403 {
1404 /* Disable the RTC clock output */
1405 RTC->CTRL &= (uint32_t)~RTC_CTRL_COEN;
1406 }
1407
1408 /* Enable the write protection for RTC registers */
1409 RTC->WRP = 0xFF;
1410 }
1411
1412 /**
1413 * @brief Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1414 * @param RTC_CalibOutput Select the Calibration output Selection .
1415 * This parameter can be one of the following values:
1416 * @arg RTC_CALIB_OUTPUT_256HZ A signal has a regular waveform at 256Hz.
1417 * @arg RTC_CALIB_OUTPUT_1HZ A signal has a regular waveform at 1Hz.
1418 */
RTC_ConfigCalibOutput(uint32_t RTC_CalibOutput)1419 void RTC_ConfigCalibOutput(uint32_t RTC_CalibOutput)
1420 {
1421 /* Check the parameters */
1422 assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
1423
1424 /* Disable the write protection for RTC registers */
1425 RTC->WRP = 0xCA;
1426 RTC->WRP = 0x53;
1427
1428 /*clear flags before config*/
1429 RTC->CTRL &= (uint32_t) ~(RTC_CTRL_CALOSEL);
1430
1431 /* Configure the RTC_CTRL register */
1432 RTC->CTRL |= (uint32_t)RTC_CalibOutput;
1433
1434 /* Enable the write protection for RTC registers */
1435 RTC->WRP = 0xFF;
1436 }
1437
1438 /**
1439 * @brief Configures the Smooth Calibration Settings.
1440 * @param RTC_SmoothCalibPeriod Select the Smooth Calibration Period.
1441 * This parameter can be can be one of the following values:
1442 * @arg SMOOTH_CALIB_32SEC The smooth calibration periode is 32s.
1443 * @arg SMOOTH_CALIB_16SEC The smooth calibration periode is 16s.
1444 * @arg SMOOTH_CALIB_8SEC The smooth calibartion periode is 8s.
1445 * @param RTC_SmoothCalibPlusPulses Select to Set or reset the CALP bit.
1446 * This parameter can be one of the following values:
1447 * @arg RTC_SMOOTH_CALIB_PLUS_PULSES_SET Add one RTCCLK puls every 2**11 pulses.
1448 * @arg RTC_SMOOTH_CALIB_PLUS_PULSES__RESET No RTCCLK pulses are added.
1449 * @param RTC_SmouthCalibMinusPulsesValue Select the value of CALM[8:0] bits.
1450 * This parameter can be one any value from 0 to 0x000001FF.
1451 * @return An ErrorStatus enumeration value:
1452 * - SUCCESS: RTC Calib registers are configured
1453 * - ERROR: RTC Calib registers are not configured
1454 */
RTC_ConfigSmoothCalib(uint32_t RTC_SmoothCalibPeriod,uint32_t RTC_SmoothCalibPlusPulses,uint32_t RTC_SmouthCalibMinusPulsesValue)1455 ErrorStatus RTC_ConfigSmoothCalib(uint32_t RTC_SmoothCalibPeriod,
1456 uint32_t RTC_SmoothCalibPlusPulses,
1457 uint32_t RTC_SmouthCalibMinusPulsesValue)
1458 {
1459 ErrorStatus status = ERROR;
1460 uint32_t recalpfcount = 0;
1461
1462 /* Check the parameters */
1463 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD_SEL(RTC_SmoothCalibPeriod));
1464 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
1465 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
1466
1467 /* Disable the write protection for RTC registers */
1468 RTC->WRP = 0xCA;
1469 RTC->WRP = 0x53;
1470
1471 /* check if a calibration is pending*/
1472 if ((RTC->INITSTS & RTC_INITSTS_RECPF) != RESET)
1473 {
1474 /* wait until the Calibration is completed*/
1475 while (((RTC->INITSTS & RTC_INITSTS_RECPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
1476 {
1477 recalpfcount++;
1478 }
1479 }
1480
1481 /* check if the calibration pending is completed or if there is no calibration operation at all*/
1482 if ((RTC->INITSTS & RTC_INITSTS_RECPF) == RESET)
1483 {
1484 /* Configure the Smooth calibration settings */
1485 RTC->CALIB = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses
1486 | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
1487
1488 status = SUCCESS;
1489 }
1490 else
1491 {
1492 status = ERROR;
1493 }
1494
1495 /* Enable the write protection for RTC registers */
1496 RTC->WRP = 0xFF;
1497
1498 return (ErrorStatus)(status);
1499 }
1500
1501 /**
1502 * @}
1503 */
1504
1505 /** @addtogroup RTC_Group8 TimeStamp configuration functions
1506 * @brief TimeStamp configuration functions
1507 *
1508 @verbatim
1509 ===============================================================================
1510 ##### TimeStamp configuration functions #####
1511 ===============================================================================
1512
1513 @endverbatim
1514 * @{
1515 */
1516
1517 /**
1518 * @brief Enables or Disables the RTC TimeStamp functionality with the
1519 * specified time stamp pin stimulating edge.
1520 * @param RTC_TimeStampEdge Specifies the pin edge on which the TimeStamp is
1521 * activated.
1522 * This parameter can be one of the following:
1523 * @arg RTC_TIMESTAMP_EDGE_RISING the Time stamp event occurs on the rising
1524 * edge of the related pin.
1525 * @arg RTC_TIMESTAMP_EDGE_FALLING the Time stamp event occurs on the
1526 * falling edge of the related pin.
1527 * @param Cmd new state of the TimeStamp.
1528 * This parameter can be: ENABLE or DISABLE.
1529 */
RTC_EnableTimeStamp(uint32_t RTC_TimeStampEdge,FunctionalState Cmd)1530 void RTC_EnableTimeStamp(uint32_t RTC_TimeStampEdge, FunctionalState Cmd)
1531 {
1532 uint32_t tmpregister = 0;
1533
1534 /* Check the parameters */
1535 assert_param(IS_RTC_TIMESTAMP_EDGE_MODE(RTC_TimeStampEdge));
1536 assert_param(IS_FUNCTIONAL_STATE(Cmd));
1537
1538 /* Get the RTC_CTRL register and clear the bits to be configured */
1539 tmpregister = (uint32_t)(RTC->CTRL & (uint32_t) ~(RTC_CTRL_TSPOL | RTC_CTRL_TSEN));
1540
1541 /* Get the new configuration */
1542 if (Cmd != DISABLE)
1543 {
1544 tmpregister |= (uint32_t)(RTC_TimeStampEdge | RTC_CTRL_TSEN);
1545 }
1546 else
1547 {
1548 tmpregister |= (uint32_t)(RTC_TimeStampEdge);
1549 }
1550
1551 /* Disable the write protection for RTC registers */
1552 RTC->WRP = 0xCA;
1553 RTC->WRP = 0x53;
1554
1555 /* Configure the Time Stamp TSEDGE and Enable bits */
1556 RTC->CTRL = (uint32_t)tmpregister;
1557
1558 /* Enable the write protection for RTC registers */
1559 RTC->WRP = 0xFF;
1560 }
1561
1562 /**
1563 * @brief Get the RTC TimeStamp value and masks.
1564 * @param RTC_Format specifies the format of the output parameters.
1565 * This parameter can be one of the following values:
1566 * @arg RTC_FORMAT_BIN Binary data format
1567 * @arg RTC_FORMAT_BCD BCD data format
1568 * @param RTC_StampTimeStruct pointer to a RTC_TimeType structure that will
1569 * contains the TimeStamp time values.
1570 * @param RTC_StampDateStruct pointer to a RTC_DateType structure that will
1571 * contains the TimeStamp date values.
1572 */
RTC_GetTimeStamp(uint32_t RTC_Format,RTC_TimeType * RTC_StampTimeStruct,RTC_DateType * RTC_StampDateStruct)1573 void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeType* RTC_StampTimeStruct, RTC_DateType* RTC_StampDateStruct)
1574 {
1575 uint32_t tmptime = 0, tmpdate = 0;
1576
1577 /* Check the parameters */
1578 assert_param(IS_RTC_FORMAT(RTC_Format));
1579
1580 /* Get the TimeStamp time and date registers values */
1581 tmptime = (uint32_t)(RTC->TST & RTC_TR_RESERVED_MASK);
1582 tmpdate = (uint32_t)(RTC->TSD & RTC_DATE_RESERVED_MASK);
1583
1584 /* Fill the Time structure fields with the read parameters */
1585 RTC_StampTimeStruct->Hours = (uint8_t)((tmptime & (RTC_TSH_HOT | RTC_TSH_HOU)) >> 16);
1586 RTC_StampTimeStruct->Minutes = (uint8_t)((tmptime & (RTC_TSH_MIT | RTC_TSH_MIU)) >> 8);
1587 RTC_StampTimeStruct->Seconds = (uint8_t)(tmptime & (RTC_TSH_SCT | RTC_TSH_SCU));
1588 RTC_StampTimeStruct->H12 = (uint8_t)((tmptime & (RTC_TSH_APM)) >> 16);
1589
1590 /* Fill the Date structure fields with the read parameters */
1591 RTC_StampDateStruct->Year = (uint8_t)((tmpdate & (RTC_DATE_YRT | RTC_DATE_YRU)) >> 16);
1592 RTC_StampDateStruct->Month = (uint8_t)((tmpdate & (RTC_DATE_MOT | RTC_DATE_MOU)) >> 8);
1593 RTC_StampDateStruct->Date = (uint8_t)(tmpdate & (RTC_DATE_DAT | RTC_DATE_DAU));
1594 RTC_StampDateStruct->WeekDay = (uint8_t)((tmpdate & (RTC_DATE_WDU)) >> 13);
1595
1596 /* Check the input parameters format */
1597 if (RTC_Format == RTC_FORMAT_BIN)
1598 {
1599 /* Convert the Time structure parameters to Binary format */
1600 RTC_StampTimeStruct->Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->Hours);
1601 RTC_StampTimeStruct->Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->Minutes);
1602 RTC_StampTimeStruct->Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->Seconds);
1603
1604 /* Convert the Date structure parameters to Binary format */
1605 RTC_StampDateStruct->Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->Month);
1606 RTC_StampDateStruct->Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->Date);
1607 RTC_StampDateStruct->WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->WeekDay);
1608 }
1609 }
1610
1611 /**
1612 * @brief Get the RTC timestamp Subseconds value.
1613 * @return RTC current timestamp Subseconds value.
1614 */
RTC_GetTimeStampSubSecond(void)1615 uint32_t RTC_GetTimeStampSubSecond(void)
1616 {
1617 /* Get timestamp subseconds values from the correspondent registers */
1618 return (uint32_t)(RTC->TSSS);
1619 }
1620
1621 /**
1622 * @}
1623 */
1624
1625 /** @addtogroup RTC_Group11 Output Type Config configuration functions
1626 * @brief Output Type Config configuration functions
1627 *
1628 @verbatim
1629 ===============================================================================
1630 ##### Output Type Config configuration functions #####
1631 ===============================================================================
1632
1633 @endverbatim
1634 * @{
1635 */
1636
1637 /**
1638 * @brief Configures the RTC Output Pin mode.
1639 * @param RTC_OutputType specifies the RTC Output (PC13) pin mode.
1640 * This parameter can be one of the following values:
1641 * @arg RTC_OUTPUT_OPENDRAIN RTC Output (PC13) is configured in
1642 * Open Drain mode.
1643 * @arg RTC_OUTPUT_PUSHPULL RTC Output (PC13) is configured in
1644 * Push Pull mode.
1645 */
RTC_ConfigOutputType(uint32_t RTC_OutputType)1646 void RTC_ConfigOutputType(uint32_t RTC_OutputType)
1647 {
1648 /* Check the parameters */
1649 assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
1650
1651 RTC->OPT &= (uint32_t) ~(RTC_OPT_TYPE);
1652 RTC->OPT |= (uint32_t)(RTC_OutputType);
1653 }
1654
1655 /**
1656 * @}
1657 */
1658
1659 /** @addtogroup RTC_Group12 Shift control synchronisation functions
1660 * @brief Shift control synchronisation functions
1661 *
1662 @verbatim
1663 ===============================================================================
1664 ##### Shift control synchronisation functions #####
1665 ===============================================================================
1666
1667 @endverbatim
1668 * @{
1669 */
1670
1671 /**
1672 * @brief Configures the Synchronization Shift Control Settings.
1673 * @note When REFCKON is set, firmware must not write to Shift control register
1674 * @param RTC_ShiftAdd1S Select to add or not 1 second to the time Calendar.
1675 * This parameter can be one of the following values :
1676 * @arg RTC_SHIFT_SUB1S_DISABLE Add one second to the clock calendar.
1677 * @arg RTC_SHIFT_SUB1S_ENABLE No effect.
1678 * @param RTC_ShiftAddFS Select the number of Second Fractions to Substitute.
1679 * This parameter can be one any value from 0 to 0x7FFF.
1680 * @return An ErrorStatus enumeration value:
1681 * - SUCCESS: RTC Shift registers are configured
1682 * - ERROR: RTC Shift registers are not configured
1683 */
RTC_ConfigSynchroShift(uint32_t RTC_ShiftAddFS,uint32_t RTC_ShiftSub1s)1684 ErrorStatus RTC_ConfigSynchroShift(uint32_t RTC_ShiftAddFS, uint32_t RTC_ShiftSub1s)
1685 {
1686 ErrorStatus status = ERROR;
1687 uint32_t shpfcount = 0;
1688
1689 /* Check the parameters */
1690 assert_param(IS_RTC_SHIFT_ADFS(RTC_ShiftAddFS));
1691 assert_param(IS_RTC_SHIFT_SUB1S(RTC_ShiftSub1s));
1692
1693 /* Disable the write protection for RTC registers */
1694 RTC->WRP = 0xCA;
1695 RTC->WRP = 0x53;
1696
1697 /* Check if a Shift is pending*/
1698 if ((RTC->INITSTS & RTC_INITSTS_SHOPF) != RESET)
1699 {
1700 /* Wait until the shift is completed*/
1701 while (((RTC->INITSTS & RTC_INITSTS_SHOPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
1702 {
1703 shpfcount++;
1704 }
1705 }
1706
1707 /* Check if the Shift pending is completed or if there is no Shift operation at all*/
1708 if ((RTC->INITSTS & RTC_INITSTS_SHOPF) == RESET)
1709 {
1710
1711 {
1712 /* Configure the Shift settings */
1713 RTC->SCTRL = (uint32_t)(uint32_t)(RTC_ShiftAddFS) | (uint32_t)(RTC_ShiftSub1s);
1714
1715 if (RTC_WaitForSynchro() == ERROR)
1716 {
1717 status = ERROR;
1718 }
1719 else
1720 {
1721 status = SUCCESS;
1722 }
1723 }
1724
1725 }
1726 else
1727 {
1728 status = ERROR;
1729 }
1730
1731 /* Enable the write protection for RTC registers */
1732 RTC->WRP = 0xFF;
1733
1734 return (ErrorStatus)(status);
1735 }
1736
1737 /**
1738 * @}
1739 */
1740
1741 /** @addtogroup RTC_Group13 Interrupts and flags management functions
1742 * @brief Interrupts and flags management functions
1743 *
1744 @verbatim
1745 ===============================================================================
1746 ##### Interrupts and flags management functions #####
1747 ===============================================================================
1748 [..] All RTC interrupts are connected to the EXTI controller.
1749 (+) To enable the RTC Alarm interrupt, the following sequence is required:
1750 (+) Configure and enable the EXTI Line 17 in interrupt mode and select
1751 the rising edge sensitivity using the EXTI_InitPeripheral() function.
1752 (+) Configure and enable the RTC_Alarm IRQ channel in the NVIC using
1753 the NVIC_Init() function.
1754 (+) Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B)
1755 using the RTC_SetAlarm() and RTC_EnableAlarm() functions.
1756
1757 (+) To enable the RTC Wakeup interrupt, the following sequence is required:
1758 (+) Configure and enable the EXTI Line 20 in interrupt mode and select
1759 the rising edge sensitivity using the EXTI_InitPeripheral() function.
1760 (+) Configure and enable the RTC_WKUP IRQ channel in the NVIC using the
1761 NVIC_Init() function.
1762 (+) Configure the RTC to generate the RTC wakeup timer event using the
1763 RTC_ConfigWakeUpClock(), RTC_SetWakeUpCounter() and RTC_EnableWakeUp()
1764 functions.
1765
1766 (+) To enable the RTC Tamper interrupt, the following sequence is required:
1767 (+) Configure and enable the EXTI Line 19 in interrupt mode and select
1768 the rising edge sensitivity using the EXTI_InitPeripheral() function.
1769 (+) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
1770 the NVIC_Init() function.
1771 (+) Configure the RTC to detect the RTC tamper event using the
1772 RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
1773
1774 (+) To enable the RTC TimeStamp interrupt, the following sequence is
1775 required:
1776 (+) Configure and enable the EXTI Line 19 in interrupt mode and select
1777 the rising edge sensitivity using the EXTI_InitPeripheral() function.
1778 (+) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
1779 the NVIC_Init() function.
1780 (+) Configure the RTC to detect the RTC time-stamp event using the
1781 RTC_EnableTimeStamp() functions.
1782
1783 @endverbatim
1784 * @{
1785 */
1786
1787 /**
1788 * @brief Enables or disables the specified RTC interrupts.
1789 * @param RTC_INT specifies the RTC interrupt sources to be enabled or disabled.
1790 * This parameter can be any combination of the following values:
1791 * @arg RTC_INT_WUT WakeUp Timer interrupt mask.
1792 * @arg RTC_INT_ALRB Alarm B interrupt mask.
1793 * @arg RTC_INT_ALRA Alarm A interrupt mask.
1794 * @param Cmd new state of the specified RTC interrupts.
1795 * This parameter can be: ENABLE or DISABLE.
1796 */
RTC_ConfigInt(uint32_t RTC_INT,FunctionalState Cmd)1797 void RTC_ConfigInt(uint32_t RTC_INT, FunctionalState Cmd)
1798 {
1799 /* Check the parameters */
1800 assert_param(IS_RTC_CONFIG_INT(RTC_INT));
1801 assert_param(IS_FUNCTIONAL_STATE(Cmd));
1802
1803 /* Disable the write protection for RTC registers */
1804 RTC->WRP = 0xCA;
1805 RTC->WRP = 0x53;
1806
1807 if (Cmd != DISABLE)
1808 {
1809 /* Configure the Interrupts in the RTC_CTRL register */
1810 RTC->CTRL |= RTC_INT ;
1811 }
1812 else
1813 {
1814 /* Configure the Interrupts in the RTC_CTRL register */
1815 RTC->CTRL &= (uint32_t) ~(RTC_INT);
1816 }
1817 /* Enable the write protection for RTC registers */
1818 RTC->WRP = 0xFF;
1819 }
1820
1821 /**
1822 * @brief Checks whether the specified RTC flag is set or not.
1823 * @param RTC_FLAG specifies the flag to check.
1824 * This parameter can be one of the following values:
1825 * @arg RTC_FLAG_RECPF RECALPF event flag.
1826 * @arg RTC_FLAG_TISOVF Time Stamp OverFlow flag.
1827 * @arg RTC_FLAG_TISF Time Stamp event flag.
1828 * @arg RTC_FLAG_WTF WakeUp Timer flag.
1829 * @arg RTC_FLAG_ALBF Alarm B flag.
1830 * @arg RTC_FLAG_ALAF Alarm A flag.
1831 * @arg RTC_FLAG_INITF Initialization mode flag.
1832 * @arg RTC_FLAG_RSYF Registers Synchronized flag.
1833 * @arg RTC_FLAG_INITSF Registers Configured flag.
1834 * @arg RTC_FLAG_SHOPF Shift operation pending flag.
1835 * @arg RTC_FLAG_WTWF WakeUp Timer Write flag.
1836 * @arg RTC_FLAG_ALBWF Alarm B Write flag.
1837 * @arg RTC_FLAG_ALAWF Alarm A write flag.
1838 * @return The new state of RTC_FLAG (SET or RESET).
1839 */
RTC_GetFlagStatus(uint32_t RTC_FLAG)1840 FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
1841 {
1842 FlagStatus bitstatus = RESET;
1843 uint32_t tmpregister = 0;
1844
1845 /* Check the parameters */
1846 assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
1847
1848 /* Get all the flags */
1849 tmpregister = (uint32_t)(RTC->INITSTS & RTC_FLAGS_MASK);
1850
1851 /* Return the status of the flag */
1852 if ((tmpregister & RTC_FLAG) != (uint32_t)RESET)
1853 {
1854 bitstatus = SET;
1855 }
1856 else
1857 {
1858 bitstatus = RESET;
1859 }
1860 return bitstatus;
1861 }
1862
1863 /**
1864 * @brief Clears the RTC's pending flags.
1865 * @param RTC_FLAG specifies the RTC flag to clear.
1866 * This parameter can be any combination of the following values:.
1867 * @arg RTC_FLAG_TISOVF Time Stamp Overflow flag.
1868 * @arg RTC_FLAG_TISF Time Stamp event flag.
1869 * @arg RTC_FLAG_WTF WakeUp Timer flag.
1870 * @arg RTC_FLAG_ALBF Alarm B flag.
1871 * @arg RTC_FLAG_ALAF Alarm A flag.
1872 * @arg RTC_FLAG_RSYF Registers Synchronized flag.
1873 */
RTC_ClrFlag(uint32_t RTC_FLAG)1874 void RTC_ClrFlag(uint32_t RTC_FLAG)
1875 {
1876 /* Check the parameters */
1877 assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
1878
1879 /* Clear the Flags in the RTC_INITSTS register */
1880 RTC->INITSTS = (uint32_t)(
1881 (uint32_t)(~((RTC_FLAG | RTC_INITSTS_INITM) & 0x00011FFF) | (uint32_t)(RTC->INITSTS & RTC_INITSTS_INITM)));
1882 }
1883
1884 /**
1885 * @brief Checks whether the specified RTC interrupt has occurred or not.
1886 * @param RTC_INT specifies the RTC interrupt source to check.
1887 * This parameter can be one of the following values:
1888 * @arg RTC_INT_WUT WakeUp Timer interrupt.
1889 * @arg RTC_INT_ALRB Alarm B interrupt.
1890 * @arg RTC_INT_ALRA Alarm A interrupt.
1891 * @return The new state of RTC_INT (SET or RESET).
1892 */
RTC_GetITStatus(uint32_t RTC_INT)1893 INTStatus RTC_GetITStatus(uint32_t RTC_INT)
1894 {
1895 INTStatus bitstatus = RESET;
1896 uint32_t tmpregister = 0, enablestatus = 0;
1897
1898 /* Check the parameters */
1899 assert_param(IS_RTC_GET_INT(RTC_INT));
1900
1901 /* Get the Interrupt enable Status */
1902 enablestatus = (uint32_t)((RTC->CTRL & RTC_INT));
1903
1904 /* Get the Interrupt pending bit */
1905 tmpregister = (uint32_t)((RTC->INITSTS & (uint32_t)(RTC_INT >> 4)));
1906
1907 /* Get the status of the Interrupt */
1908 if ((enablestatus != (uint32_t)RESET) && ((tmpregister & 0x0000FFFF) != (uint32_t)RESET))
1909 {
1910 bitstatus = SET;
1911 }
1912 else
1913 {
1914 bitstatus = RESET;
1915 }
1916 return bitstatus;
1917 }
1918
1919 /**
1920 * @brief Clears the RTC's interrupt pending bits.
1921 * @param RTC_INT specifies the RTC interrupt pending bit to clear.
1922 * This parameter can be any combination of the following values:
1923 * @arg RTC_INT_WUT WakeUp Timer interrupt
1924 * @arg RTC_INT_ALRB Alarm B interrupt
1925 * @arg RTC_INT_ALRA Alarm A interrupt
1926 */
RTC_ClrIntPendingBit(uint32_t RTC_INT)1927 void RTC_ClrIntPendingBit(uint32_t RTC_INT)
1928 {
1929 uint32_t tmpregister = 0;
1930
1931 /* Check the parameters */
1932 assert_param(IS_RTC_CLEAR_INT(RTC_INT));
1933
1934 /* Get the RTC_INITSTS Interrupt pending bits mask */
1935 tmpregister = (uint32_t)(RTC_INT >> 4);
1936
1937 /* Clear the interrupt pending bits in the RTC_INITSTS register */
1938 RTC->INITSTS = (uint32_t)(
1939 (uint32_t)(~((tmpregister | RTC_INITSTS_INITM) & 0x0000FFFF) | (uint32_t)(RTC->INITSTS & RTC_INITSTS_INITM)));
1940 }
1941
1942 /**
1943 * @}
1944 */
1945
1946 /**
1947 * @brief Converts a 2 digit decimal to BCD format.
1948 * @param Value Byte to be converted.
1949 * @return Converted byte
1950 */
RTC_ByteToBcd2(uint8_t Value)1951 static uint8_t RTC_ByteToBcd2(uint8_t Value)
1952 {
1953 uint8_t bcdhigh = 0;
1954
1955 while (Value >= 10)
1956 {
1957 bcdhigh++;
1958 Value -= 10;
1959 }
1960
1961 return ((uint8_t)(bcdhigh << 4) | Value);
1962 }
1963
1964 /**
1965 * @brief Convert from 2 digit BCD to Binary.
1966 * @param Value BCD value to be converted.
1967 * @return Converted word
1968 */
RTC_Bcd2ToByte(uint8_t Value)1969 static uint8_t RTC_Bcd2ToByte(uint8_t Value)
1970 {
1971 uint8_t tmp = 0;
1972 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1973 return (tmp + (Value & (uint8_t)0x0F));
1974 }
1975 /**
1976 * @brief Enable wakeup tsc functionand wakeup by the set time
1977 * @param count wakeup time.
1978 */
RTC_EnableWakeUpTsc(uint32_t count)1979 void RTC_EnableWakeUpTsc(uint32_t count)
1980 {
1981 // Wait until bit RTC_TSCWKUPCTRL_WKUPOFF is 1
1982 while (!(RTC->TSCWKUPCTRL & RTC_TSCWKUPCTRL_WKUPOFF))
1983 {
1984 }
1985 // enter config wakeup cnt mode
1986 RTC->TSCWKUPCTRL = RTC_TSCWKUPCTRL_WKUPCNF;
1987 // config tsc wakeup cnt ,tsc wakeup module counting cycle = WAKUPCNT * LSE/LSI
1988 RTC->TSCWKUPCNT = count;
1989 // exit config wakeup cnt mode
1990 RTC->TSCWKUPCTRL &= ~(RTC_TSCWKUPCTRL_WKUPCNF);
1991 while (!(RTC->TSCWKUPCTRL & RTC_TSCWKUPCTRL_WKUPOFF))
1992 {
1993 }
1994 // TSC wakeup enable
1995 RTC->TSCWKUPCTRL = RTC_TSCWKUPCTRL_WKUPEN;
1996 }
1997 /**
1998 * @}
1999 */
2000
2001 /**
2002 * @}
2003 */
2004
2005 /**
2006 * @}
2007 */
2008