]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3XX/stm32f30x_rtc.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3XX / stm32f30x_rtc.c
1 /**
2 ******************************************************************************
3 * @file stm32f30x_rtc.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 27-February-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Real-Time Clock (RTC) peripheral:
9 * + Initialization
10 * + Calendar (Time and Date) configuration
11 * + Alarms (Alarm A and Alarm B) configuration
12 * + WakeUp Timer configuration
13 * + Daylight Saving configuration
14 * + Output pin Configuration
15 * + Smooth digital Calibration configuration
16 * + TimeStamp configuration
17 * + Tampers configuration
18 * + Backup Data Registers configuration
19 * + Output Type Config configuration
20 * + Shift control synchronisation
21 * + Interrupts and flags management
22 *
23 @verbatim
24
25 ===============================================================================
26 ##### RTC Operating Condition #####
27 ===============================================================================
28 [..] The real-time clock (RTC) and the RTC backup registers can be powered
29 from the VBAT voltage when the main VDD supply is powered off.
30 To retain the content of the RTC backup registers and supply the RTC
31 when VDD is turned off, VBAT pin can be connected to an optional
32 standby voltage supplied by a battery or by another source.
33
34 [..] To allow the RTC to operate even when the main digital supply (VDD)
35 is turned off, the VBAT pin powers the following blocks:
36 (#) The RTC
37 (#) The LSE oscillator
38 (#) PC13 to PC15 I/Os (when available)
39
40 [..] When the backup domain is supplied by VDD (analog switch connected
41 to VDD), the following functions are available:
42 (#) PC14 and PC15 can be used as either GPIO or LSE pins
43 (#) PC13 can be used as a GPIO or as the RTC_AF pin
44
45 [..] When the backup domain is supplied by VBAT (analog switch connected
46 to VBAT because VDD is not present), the following functions are available:
47 (#) PC14 and PC15 can be used as LSE pins only
48 (#) PC13 can be used as the RTC_AF pin
49
50 ##### Backup Domain Reset #####
51 ===============================================================================
52 [..] The backup domain reset sets all RTC registers and the RCC_BDCR
53 register to their reset values.
54 A backup domain reset is generated when one of the following events
55 occurs:
56 (#) Software reset, triggered by setting the BDRST bit in the
57 RCC Backup domain control register (RCC_BDCR). You can use the
58 RCC_BackupResetCmd().
59 (#) VDD or VBAT power on, if both supplies have previously been
60 powered off.
61
62 ##### Backup Domain Access #####
63 ===============================================================================
64 [..] After reset, the backup domain (RTC registers and RTC backup data
65 registers) is protected against possible unwanted write accesses.
66 [..] To enable access to the Backup Domain and RTC registers, proceed as follows:
67 (#) Enable the Power Controller (PWR) APB1 interface clock using the
68 RCC_APB1PeriphClockCmd() function.
69 (#) Enable access to Backup domain using the PWR_BackupAccessCmd() function.
70 (#) Select the RTC clock source using the RCC_RTCCLKConfig() function.
71 (#) Enable RTC Clock using the RCC_RTCCLKCmd() function.
72
73 ##### How to use this driver #####
74 ===============================================================================
75 [..]
76 (+) Enable the backup domain access (see description in the section above)
77 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and
78 RTC hour format using the RTC_Init() function.
79
80 *** Time and Date configuration ***
81 ===================================
82 [..]
83 (+) To configure the RTC Calendar (Time and Date) use the RTC_SetTime()
84 and RTC_SetDate() functions.
85 (+) To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()
86 functions.
87 (+) To read the RTC subsecond, use the RTC_GetSubSecond() function.
88 (+) Use the RTC_DayLightSavingConfig() function to add or sub one
89 hour to the RTC Calendar.
90
91 *** Alarm configuration ***
92 ===========================
93 [..]
94 (+) To configure the RTC Alarm use the RTC_SetAlarm() function.
95 (+) Enable the selected RTC Alarm using the RTC_AlarmCmd() function.
96 (+) To read the RTC Alarm, use the RTC_GetAlarm() function.
97 (+) To read the RTC alarm SubSecond, use the RTC_GetAlarmSubSecond() function.
98
99 *** RTC Wakeup configuration ***
100 ================================
101 [..]
102 (+) Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
103 function.
104 (+) Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter()
105 function
106 (+) Enable the RTC WakeUp using the RTC_WakeUpCmd() function
107 (+) To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter()
108 function.
109
110 *** Outputs configuration ***
111 =============================
112 [..] The RTC has 2 different outputs:
113 (+) AFO_ALARM: this output is used to manage the RTC Alarm A, Alarm B
114 and WaKeUp signals.
115 To output the selected RTC signal on RTC_AF pin, use the
116 RTC_OutputConfig() function.
117 (+) AFO_CALIB: this output is 512Hz signal or 1Hz .
118 To output the RTC Clock on RTC_AF pin, use the RTC_CalibOutputCmd()
119 function.
120
121 *** Smooth digital Calibration configuration ***
122 ================================================
123 [..]
124 (+) Configure the RTC Original Digital Calibration Value and the corresponding
125 calibration cycle period (32s,16s and 8s) using the RTC_SmoothCalibConfig()
126 function.
127
128 *** TimeStamp configuration ***
129 ===============================
130 [..]
131 (+) Configure the RTC_AF trigger and enables the RTC TimeStamp
132 using the RTC_TimeStampCmd() function.
133 (+) To read the RTC TimeStamp Time and Date register, use the
134 RTC_GetTimeStamp() function.
135 (+) To read the RTC TimeStamp SubSecond register, use the
136 RTC_GetTimeStampSubSecond() function.
137
138 *** Tamper configuration ***
139 ============================
140 [..]
141 (+) Configure the Tamper filter count using RTC_TamperFilterConfig()
142 function.
143 (+) Configure the RTC Tamper trigger Edge or Level according to the Tamper
144 filter (if equal to 0 Edge else Level) value using the RTC_TamperConfig() function.
145 (+) Configure the Tamper sampling frequency using RTC_TamperSamplingFreqConfig()
146 function.
147 (+) Configure the Tamper precharge or discharge duration using
148 RTC_TamperPinsPrechargeDuration() function.
149 (+) Enable the Tamper Pull-UP using RTC_TamperPullUpDisableCmd() function.
150 (+) Enable the RTC Tamper using the RTC_TamperCmd() function.
151 (+) Enable the Time stamp on Tamper detection event using
152 RTC_TSOnTamperDetecCmd() function.
153
154 *** Backup Data Registers configuration ***
155 ===========================================
156 [..]
157 (+) To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()
158 function.
159 (+) To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()
160 function.
161
162 ##### RTC and low power modes #####
163 ===============================================================================
164 [..] The MCU can be woken up from a low power mode by an RTC alternate
165 function.
166 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
167 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
168 These RTC alternate functions can wake up the system from the Stop
169 and Standby lowpower modes.
170 The system can also wake up from low power modes without depending
171 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
172 or the RTC wakeup events.
173 [..] The RTC provides a programmable time base for waking up from the
174 Stop or Standby mode at regular intervals.
175 Wakeup from STOP and Standby modes is possible only when the RTC
176 clock source is LSE or LSI.
177
178 ##### Selection of RTC_AF alternate functions #####
179 ===============================================================================
180 [..] The RTC_AF pin (PC13) can be used for the following purposes:
181 (+) Wakeup pin 2 (WKUP2) using the PWR_WakeUpPinCmd() function.
182 (+) AFO_ALARM output
183 (+) AFO_CALIB output
184 (+) AFI_TAMPER
185 (+) AFI_TIMESTAMP
186
187 +------------------------------------------------------------------------------------------+
188 | Pin |RTC ALARM |RTC CALIB |RTC TAMPER |RTC TIMESTAMP |PC13MODE| PC13VALUE |
189 | configuration | OUTPUT | OUTPUT | INPUT | INPUT | bit | bit |
190 | and function | ENABLED | ENABLED | ENABLED | ENABLED | | |
191 |-----------------|----------|----------|-----------|--------------|--------|--------------|
192 | Alarm out | | | | | Don't | |
193 | output OD | 1 |Don't care|Don't care | Don't care | care | 0 |
194 |-----------------|----------|----------|-----------|--------------|--------|--------------|
195 | Alarm out | | | | | Don't | |
196 | output PP | 1 |Don't care|Don't care | Don't care | care | 1 |
197 |-----------------|----------|----------|-----------|--------------|--------|--------------|
198 | Calibration out | | | | | Don't | |
199 | output PP | 0 | 1 |Don't care | Don't care | care | Don't care |
200 |-----------------|----------|----------|-----------|--------------|--------|--------------|
201 | TAMPER input | | | | | Don't | |
202 | floating | 0 | 0 | 1 | 0 | care | Don't care |
203 |-----------------|----------|----------|-----------|--------------|--------|--------------|
204 | TIMESTAMP and | | | | | Don't | |
205 | TAMPER input | 0 | 0 | 1 | 1 | care | Don't care |
206 | floating | | | | | | |
207 |-----------------|----------|----------|-----------|--------------|--------|--------------|
208 | TIMESTAMP input | | | | | Don't | |
209 | floating | 0 | 0 | 0 | 1 | care | Don't care |
210 |-----------------|----------|----------|-----------|--------------|--------|--------------|
211 | Output PP | 0 | 0 | 0 | 0 | 1 | PC13 output |
212 | Forced | | | | | | |
213 |-----------------|----------|----------|-----------|--------------|--------|--------------|
214 | Wakeup Pin or | 0 | 0 | 0 | 0 | 0 | Don't care |
215 | Standard GPIO | | | | | | |
216 +------------------------------------------------------------------------------------------+
217
218 @endverbatim
219
220 ******************************************************************************
221 * @attention
222 *
223 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
224 *
225 * Redistribution and use in source and binary forms, with or without modification,
226 * are permitted provided that the following conditions are met:
227 * 1. Redistributions of source code must retain the above copyright notice,
228 * this list of conditions and the following disclaimer.
229 * 2. Redistributions in binary form must reproduce the above copyright notice,
230 * this list of conditions and the following disclaimer in the documentation
231 * and/or other materials provided with the distribution.
232 * 3. Neither the name of STMicroelectronics nor the names of its contributors
233 * may be used to endorse or promote products derived from this software
234 * without specific prior written permission.
235 *
236 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
237 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
239 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
240 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
242 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
243 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
244 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
245 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
246 *
247 ******************************************************************************
248 */
249
250 /* Includes ------------------------------------------------------------------*/
251 #include "stm32f30x_rtc.h"
252 #include "stm32f30x_rcc.h"
253
254 /** @addtogroup STM32F30x_StdPeriph_Driver
255 * @{
256 */
257
258 /** @defgroup RTC
259 * @brief RTC driver modules
260 * @{
261 */
262
263 /* Private typedef -----------------------------------------------------------*/
264 /* Private define ------------------------------------------------------------*/
265
266 /* Masks Definition */
267 #define RTC_TR_RESERVED_MASK ((uint32_t)0x007F7F7F)
268 #define RTC_DR_RESERVED_MASK ((uint32_t)0x00FFFF3F)
269 #define RTC_INIT_MASK ((uint32_t)0xFFFFFFFF)
270 #define RTC_RSF_MASK ((uint32_t)0xFFFFFF5F)
271 #define RTC_FLAGS_MASK ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_WUTF | \
272 RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_INITF | \
273 RTC_FLAG_RSF | RTC_FLAG_INITS | RTC_FLAG_WUTWF | \
274 RTC_FLAG_ALRBWF | RTC_FLAG_ALRAWF | RTC_FLAG_TAMP1F | \
275 RTC_FLAG_TAMP2F | RTC_FLAG_TAMP3F | RTC_FLAG_RECALPF | \
276 RTC_FLAG_SHPF))
277
278 #define INITMODE_TIMEOUT ((uint32_t) 0x00002000)
279 #define SYNCHRO_TIMEOUT ((uint32_t) 0x00008000)
280 #define RECALPF_TIMEOUT ((uint32_t) 0x00001000)
281 #define SHPF_TIMEOUT ((uint32_t) 0x00002000)
282
283 /* Private macro -------------------------------------------------------------*/
284 /* Private variables ---------------------------------------------------------*/
285 /* Private function prototypes -----------------------------------------------*/
286 static uint8_t RTC_ByteToBcd2(uint8_t Value);
287 static uint8_t RTC_Bcd2ToByte(uint8_t Value);
288
289 /* Private functions ---------------------------------------------------------*/
290
291 /** @defgroup RTC_Private_Functions
292 * @{
293 */
294
295 /** @defgroup RTC_Group1 Initialization and Configuration functions
296 * @brief Initialization and Configuration functions
297 *
298 @verbatim
299 ===============================================================================
300 ##### Initialization and Configuration functions #####
301 ===============================================================================
302 [..] This section provide functions allowing to initialize and configure the RTC
303 Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers
304 Write protection, enter and exit the RTC initialization mode, RTC registers
305 synchronization check and reference clock detection enable.
306 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is
307 split into 2 programmable prescalers to minimize power consumption.
308 (++) A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
309 (++) When both prescalers are used, it is recommended to configure the
310 asynchronous prescaler to a high value to minimize consumption.
311 (#) All RTC registers are Write protected. Writing to the RTC registers
312 is enabled by writing a key into the Write Protection register, RTC_WPR.
313 (#) To Configure the RTC Calendar, user application should enter initialization
314 mode. In this mode, the calendar counter is stopped and its value
315 can be updated. When the initialization sequence is complete, the
316 calendar restarts counting after 4 RTCCLK cycles.
317 (#) To read the calendar through the shadow registers after Calendar
318 initialization, calendar update or after wakeup from low power modes
319 the software must first clear the RSF flag. The software must then
320 wait until it is set again before reading the calendar, which means
321 that the calendar registers have been correctly copied into the RTC_TR
322 and RTC_DR shadow registers. The RTC_WaitForSynchro() function
323 implements the above software sequence (RSF clear and RSF check).
324
325 @endverbatim
326 * @{
327 */
328
329 /**
330 * @brief Deinitializes the RTC registers to their default reset values.
331 * @note This function doesn't reset the RTC Clock source and RTC Backup Data
332 * registers.
333 * @param None
334 * @retval An ErrorStatus enumeration value:
335 * - SUCCESS: RTC registers are deinitialized
336 * - ERROR: RTC registers are not deinitialized
337 */
338 ErrorStatus RTC_DeInit(void)
339 {
340 __IO uint32_t wutcounter = 0x00;
341 uint32_t wutwfstatus = 0x00;
342 ErrorStatus status = ERROR;
343
344 /* Disable the write protection for RTC registers */
345 RTC->WPR = 0xCA;
346 RTC->WPR = 0x53;
347
348 /* Set Initialization mode */
349 if (RTC_EnterInitMode() == ERROR)
350 {
351 status = ERROR;
352 }
353 else
354 {
355 /* Reset TR, DR and CR registers */
356 RTC->TR = (uint32_t)0x00000000;
357 RTC->DR = (uint32_t)0x00002101;
358
359 /* Reset All CR bits except CR[2:0] */
360 RTC->CR &= (uint32_t)0x00000007;
361
362 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
363 do
364 {
365 wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
366 wutcounter++;
367 } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
368
369 if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
370 {
371 status = ERROR;
372 }
373 else
374 {
375 /* Reset all RTC CR register bits */
376 RTC->CR &= (uint32_t)0x00000000;
377 RTC->WUTR = (uint32_t)0x0000FFFF;
378 RTC->PRER = (uint32_t)0x007F00FF;
379 RTC->ALRMAR = (uint32_t)0x00000000;
380 RTC->ALRMBR = (uint32_t)0x00000000;
381 RTC->SHIFTR = (uint32_t)0x00000000;
382 RTC->CALR = (uint32_t)0x00000000;
383 RTC->ALRMASSR = (uint32_t)0x00000000;
384 RTC->ALRMBSSR = (uint32_t)0x00000000;
385
386 /* Reset ISR register and exit initialization mode */
387 RTC->ISR = (uint32_t)0x00000000;
388
389 /* Reset Tamper and alternate functions configuration register */
390 RTC->TAFCR = 0x00000000;
391
392 /* Wait till the RTC RSF flag is set */
393 if (RTC_WaitForSynchro() == ERROR)
394 {
395 status = ERROR;
396 }
397 else
398 {
399 status = SUCCESS;
400 }
401 }
402 }
403
404 /* Enable the write protection for RTC registers */
405 RTC->WPR = 0xFF;
406
407 return status;
408 }
409
410 /**
411 * @brief Initializes the RTC registers according to the specified parameters
412 * in RTC_InitStruct.
413 * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains
414 * the configuration information for the RTC peripheral.
415 * @note The RTC Prescaler register is write protected and can be written in
416 * initialization mode only.
417 * @retval An ErrorStatus enumeration value:
418 * - SUCCESS: RTC registers are initialized
419 * - ERROR: RTC registers are not initialized
420 */
421 ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
422 {
423 ErrorStatus status = ERROR;
424
425 /* Check the parameters */
426 assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
427 assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
428 assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
429
430 /* Disable the write protection for RTC registers */
431 RTC->WPR = 0xCA;
432 RTC->WPR = 0x53;
433
434 /* Set Initialization mode */
435 if (RTC_EnterInitMode() == ERROR)
436 {
437 status = ERROR;
438 }
439 else
440 {
441 /* Clear RTC CR FMT Bit */
442 RTC->CR &= ((uint32_t)~(RTC_CR_FMT));
443 /* Set RTC_CR register */
444 RTC->CR |= ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
445
446 /* Configure the RTC PRER */
447 RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
448 RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
449
450 /* Exit Initialization mode */
451 RTC_ExitInitMode();
452
453 status = SUCCESS;
454 }
455 /* Enable the write protection for RTC registers */
456 RTC->WPR = 0xFF;
457
458 return status;
459 }
460
461 /**
462 * @brief Fills each RTC_InitStruct member with its default value.
463 * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be
464 * initialized.
465 * @retval None
466 */
467 void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
468 {
469 /* Initialize the RTC_HourFormat member */
470 RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
471
472 /* Initialize the RTC_AsynchPrediv member */
473 RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
474
475 /* Initialize the RTC_SynchPrediv member */
476 RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF;
477 }
478
479 /**
480 * @brief Enables or disables the RTC registers write protection.
481 * @note All the RTC registers are write protected except for RTC_ISR[13:8],
482 * RTC_TAFCR and RTC_BKPxR.
483 * @note Writing a wrong key reactivates the write protection.
484 * @note The protection mechanism is not affected by system reset.
485 * @param NewState: new state of the write protection.
486 * This parameter can be: ENABLE or DISABLE.
487 * @retval None
488 */
489 void RTC_WriteProtectionCmd(FunctionalState NewState)
490 {
491 /* Check the parameters */
492 assert_param(IS_FUNCTIONAL_STATE(NewState));
493
494 if (NewState != DISABLE)
495 {
496 /* Enable the write protection for RTC registers */
497 RTC->WPR = 0xFF;
498 }
499 else
500 {
501 /* Disable the write protection for RTC registers */
502 RTC->WPR = 0xCA;
503 RTC->WPR = 0x53;
504 }
505 }
506
507 /**
508 * @brief Enters the RTC Initialization mode.
509 * @note The RTC Initialization mode is write protected, use the
510 * RTC_WriteProtectionCmd(DISABLE) before calling this function.
511 * @param None
512 * @retval An ErrorStatus enumeration value:
513 * - SUCCESS: RTC is in Init mode
514 * - ERROR: RTC is not in Init mode
515 */
516 ErrorStatus RTC_EnterInitMode(void)
517 {
518 __IO uint32_t initcounter = 0x00;
519 ErrorStatus status = ERROR;
520 uint32_t initstatus = 0x00;
521
522 /* Check if the Initialization mode is set */
523 if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
524 {
525 /* Set the Initialization mode */
526 RTC->ISR = (uint32_t)RTC_INIT_MASK;
527
528 /* Wait till RTC is in INIT state and if Time out is reached exit */
529 do
530 {
531 initstatus = RTC->ISR & RTC_ISR_INITF;
532 initcounter++;
533 } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
534
535 if ((RTC->ISR & RTC_ISR_INITF) != RESET)
536 {
537 status = SUCCESS;
538 }
539 else
540 {
541 status = ERROR;
542 }
543 }
544 else
545 {
546 status = SUCCESS;
547 }
548
549 return (status);
550 }
551
552 /**
553 * @brief Exits the RTC Initialization mode.
554 * @note When the initialization sequence is complete, the calendar restarts
555 * counting after 4 RTCCLK cycles.
556 * @note The RTC Initialization mode is write protected, use the
557 * RTC_WriteProtectionCmd(DISABLE) before calling this function.
558 * @param None
559 * @retval None
560 */
561 void RTC_ExitInitMode(void)
562 {
563 /* Exit Initialization mode */
564 RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
565 }
566
567 /**
568 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
569 * synchronized with RTC APB clock.
570 * @note The RTC Resynchronization mode is write protected, use the
571 * RTC_WriteProtectionCmd(DISABLE) before calling this function.
572 * @note To read the calendar through the shadow registers after Calendar
573 * initialization, calendar update or after wakeup from low power modes
574 * the software must first clear the RSF flag.
575 * The software must then wait until it is set again before reading
576 * the calendar, which means that the calendar registers have been
577 * correctly copied into the RTC_TR and RTC_DR shadow registers.
578 * @param None
579 * @retval An ErrorStatus enumeration value:
580 * - SUCCESS: RTC registers are synchronised
581 * - ERROR: RTC registers are not synchronised
582 */
583 ErrorStatus RTC_WaitForSynchro(void)
584 {
585 __IO uint32_t synchrocounter = 0;
586 ErrorStatus status = ERROR;
587 uint32_t synchrostatus = 0x00;
588
589 if ((RTC->CR & RTC_CR_BYPSHAD) != RESET)
590 {
591 /* Bypass shadow mode */
592 status = SUCCESS;
593 }
594 else
595 {
596 /* Disable the write protection for RTC registers */
597 RTC->WPR = 0xCA;
598 RTC->WPR = 0x53;
599
600 /* Clear RSF flag */
601 RTC->ISR &= (uint32_t)RTC_RSF_MASK;
602
603 /* Wait the registers to be synchronised */
604 do
605 {
606 synchrostatus = RTC->ISR & RTC_ISR_RSF;
607 synchrocounter++;
608 } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
609
610 if ((RTC->ISR & RTC_ISR_RSF) != RESET)
611 {
612 status = SUCCESS;
613 }
614 else
615 {
616 status = ERROR;
617 }
618
619 /* Enable the write protection for RTC registers */
620 RTC->WPR = 0xFF;
621 }
622
623 return (status);
624 }
625
626 /**
627 * @brief Enables or disables the RTC reference clock detection.
628 * @param NewState: new state of the RTC reference clock.
629 * This parameter can be: ENABLE or DISABLE.
630 * @retval An ErrorStatus enumeration value:
631 * - SUCCESS: RTC reference clock detection is enabled
632 * - ERROR: RTC reference clock detection is disabled
633 */
634 ErrorStatus RTC_RefClockCmd(FunctionalState NewState)
635 {
636 ErrorStatus status = ERROR;
637
638 /* Check the parameters */
639 assert_param(IS_FUNCTIONAL_STATE(NewState));
640
641 /* Disable the write protection for RTC registers */
642 RTC->WPR = 0xCA;
643 RTC->WPR = 0x53;
644
645 /* Set Initialization mode */
646 if (RTC_EnterInitMode() == ERROR)
647 {
648 status = ERROR;
649 }
650 else
651 {
652 if (NewState != DISABLE)
653 {
654 /* Enable the RTC reference clock detection */
655 RTC->CR |= RTC_CR_REFCKON;
656 }
657 else
658 {
659 /* Disable the RTC reference clock detection */
660 RTC->CR &= ~RTC_CR_REFCKON;
661 }
662 /* Exit Initialization mode */
663 RTC_ExitInitMode();
664
665 status = SUCCESS;
666 }
667
668 /* Enable the write protection for RTC registers */
669 RTC->WPR = 0xFF;
670
671 return status;
672 }
673
674 /**
675 * @brief Enables or Disables the Bypass Shadow feature.
676 * @note When the Bypass Shadow is enabled the calendar value are taken
677 * directly from the Calendar counter.
678 * @param NewState: new state of the Bypass Shadow feature.
679 * This parameter can be: ENABLE or DISABLE.
680 * @retval None
681 */
682 void RTC_BypassShadowCmd(FunctionalState NewState)
683 {
684 /* Check the parameters */
685 assert_param(IS_FUNCTIONAL_STATE(NewState));
686
687 /* Disable the write protection for RTC registers */
688 RTC->WPR = 0xCA;
689 RTC->WPR = 0x53;
690
691 if (NewState != DISABLE)
692 {
693 /* Set the BYPSHAD bit */
694 RTC->CR |= (uint8_t)RTC_CR_BYPSHAD;
695 }
696 else
697 {
698 /* Reset the BYPSHAD bit */
699 RTC->CR &= (uint8_t)~RTC_CR_BYPSHAD;
700 }
701
702 /* Enable the write protection for RTC registers */
703 RTC->WPR = 0xFF;
704 }
705
706 /**
707 * @}
708 */
709
710 /** @defgroup RTC_Group2 Time and Date configuration functions
711 * @brief Time and Date configuration functions
712 *
713 @verbatim
714 ===============================================================================
715 ##### Time and Date configuration functions #####
716 ===============================================================================
717 [..] This section provide functions allowing to program and read the RTC Calendar
718 (Time and Date).
719
720 @endverbatim
721 * @{
722 */
723
724 /**
725 * @brief Set the RTC current time.
726 * @param RTC_Format: specifies the format of the entered parameters.
727 * This parameter can be one of the following values:
728 * @arg RTC_Format_BIN: Binary data format
729 * @arg RTC_Format_BCD: BCD data format
730 * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that contains
731 * the time configuration information for the RTC.
732 * @retval An ErrorStatus enumeration value:
733 * - SUCCESS: RTC Time register is configured
734 * - ERROR: RTC Time register is not configured
735 */
736 ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
737 {
738 uint32_t tmpreg = 0;
739 ErrorStatus status = ERROR;
740
741 /* Check the parameters */
742 assert_param(IS_RTC_FORMAT(RTC_Format));
743
744 if (RTC_Format == RTC_Format_BIN)
745 {
746 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
747 {
748 assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
749 assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
750 }
751 else
752 {
753 RTC_TimeStruct->RTC_H12 = 0x00;
754 assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
755 }
756 assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
757 assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
758 }
759 else
760 {
761 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
762 {
763 tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
764 assert_param(IS_RTC_HOUR12(tmpreg));
765 assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
766 }
767 else
768 {
769 RTC_TimeStruct->RTC_H12 = 0x00;
770 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
771 }
772 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
773 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
774 }
775
776 /* Check the input parameters format */
777 if (RTC_Format != RTC_Format_BIN)
778 {
779 tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \
780 ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \
781 ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \
782 ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16));
783 }
784 else
785 {
786 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \
787 ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \
788 ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \
789 (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));
790 }
791
792 /* Disable the write protection for RTC registers */
793 RTC->WPR = 0xCA;
794 RTC->WPR = 0x53;
795
796 /* Set Initialization mode */
797 if (RTC_EnterInitMode() == ERROR)
798 {
799 status = ERROR;
800 }
801 else
802 {
803 /* Set the RTC_TR register */
804 RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
805
806 /* Exit Initialization mode */
807 RTC_ExitInitMode();
808
809 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
810 if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
811 {
812 if (RTC_WaitForSynchro() == ERROR)
813 {
814 status = ERROR;
815 }
816 else
817 {
818 status = SUCCESS;
819 }
820 }
821 else
822 {
823 status = SUCCESS;
824 }
825
826 }
827 /* Enable the write protection for RTC registers */
828 RTC->WPR = 0xFF;
829
830 return status;
831 }
832
833 /**
834 * @brief Fills each RTC_TimeStruct member with its default value
835 * (Time = 00h:00min:00sec).
836 * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be
837 * initialized.
838 * @retval None
839 */
840 void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
841 {
842 /* Time = 00h:00min:00sec */
843 RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
844 RTC_TimeStruct->RTC_Hours = 0;
845 RTC_TimeStruct->RTC_Minutes = 0;
846 RTC_TimeStruct->RTC_Seconds = 0;
847 }
848
849 /**
850 * @brief Get the RTC current Time.
851 * @param RTC_Format: specifies the format of the returned parameters.
852 * This parameter can be one of the following values:
853 * @arg RTC_Format_BIN: Binary data format
854 * @arg RTC_Format_BCD: BCD data format
855 * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will
856 * contain the returned current time configuration.
857 * @retval None
858 */
859 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
860 {
861 uint32_t tmpreg = 0;
862
863 /* Check the parameters */
864 assert_param(IS_RTC_FORMAT(RTC_Format));
865
866 /* Get the RTC_TR register */
867 tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
868
869 /* Fill the structure fields with the read parameters */
870 RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
871 RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
872 RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
873 RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
874
875 /* Check the input parameters format */
876 if (RTC_Format == RTC_Format_BIN)
877 {
878 /* Convert the structure parameters to Binary format */
879 RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
880 RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
881 RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);
882 }
883 }
884
885 /**
886 * @brief Gets the RTC current Calendar Subseconds value.
887 * @note This function freeze the Time and Date registers after reading the
888 * SSR register.
889 * @param None
890 * @retval RTC current Calendar Subseconds value.
891 */
892 uint32_t RTC_GetSubSecond(void)
893 {
894 uint32_t tmpreg = 0;
895
896 /* Get subseconds values from the correspondent registers*/
897 tmpreg = (uint32_t)(RTC->SSR);
898
899 /* Read DR register to unfroze calendar registers */
900 (void) (RTC->DR);
901
902 return (tmpreg);
903 }
904
905 /**
906 * @brief Set the RTC current date.
907 * @param RTC_Format: specifies the format of the entered parameters.
908 * This parameter can be one of the following values:
909 * @arg RTC_Format_BIN: Binary data format
910 * @arg RTC_Format_BCD: BCD data format
911 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
912 * the date configuration information for the RTC.
913 * @retval An ErrorStatus enumeration value:
914 * - SUCCESS: RTC Date register is configured
915 * - ERROR: RTC Date register is not configured
916 */
917 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
918 {
919 uint32_t tmpreg = 0;
920 ErrorStatus status = ERROR;
921
922 /* Check the parameters */
923 assert_param(IS_RTC_FORMAT(RTC_Format));
924
925 if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
926 {
927 RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
928 }
929 if (RTC_Format == RTC_Format_BIN)
930 {
931 assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
932 assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
933 assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
934 }
935 else
936 {
937 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
938 tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
939 assert_param(IS_RTC_MONTH(tmpreg));
940 tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
941 assert_param(IS_RTC_DATE(tmpreg));
942 }
943 assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
944
945 /* Check the input parameters format */
946 if (RTC_Format != RTC_Format_BIN)
947 {
948 tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
949 (((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
950 ((uint32_t)RTC_DateStruct->RTC_Date) | \
951 (((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13));
952 }
953 else
954 {
955 tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
956 ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
957 ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
958 ((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
959 }
960
961 /* Disable the write protection for RTC registers */
962 RTC->WPR = 0xCA;
963 RTC->WPR = 0x53;
964
965 /* Set Initialization mode */
966 if (RTC_EnterInitMode() == ERROR)
967 {
968 status = ERROR;
969 }
970 else
971 {
972 /* Set the RTC_DR register */
973 RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
974
975 /* Exit Initialization mode */
976 RTC_ExitInitMode();
977
978 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
979 if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
980 {
981 if (RTC_WaitForSynchro() == ERROR)
982 {
983 status = ERROR;
984 }
985 else
986 {
987 status = SUCCESS;
988 }
989 }
990 else
991 {
992 status = SUCCESS;
993 }
994 }
995 /* Enable the write protection for RTC registers */
996 RTC->WPR = 0xFF;
997
998 return status;
999 }
1000
1001 /**
1002 * @brief Fills each RTC_DateStruct member with its default value
1003 * (Monday, January 01 xx00).
1004 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure which will be
1005 * initialized.
1006 * @retval None
1007 */
1008 void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
1009 {
1010 /* Monday, January 01 xx00 */
1011 RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;
1012 RTC_DateStruct->RTC_Date = 1;
1013 RTC_DateStruct->RTC_Month = RTC_Month_January;
1014 RTC_DateStruct->RTC_Year = 0;
1015 }
1016
1017 /**
1018 * @brief Get the RTC current date.
1019 * @param RTC_Format: specifies the format of the returned parameters.
1020 * This parameter can be one of the following values:
1021 * @arg RTC_Format_BIN: Binary data format
1022 * @arg RTC_Format_BCD: BCD data format
1023 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that will
1024 * contain the returned current date configuration.
1025 * @retval None
1026 */
1027 void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
1028 {
1029 uint32_t tmpreg = 0;
1030
1031 /* Check the parameters */
1032 assert_param(IS_RTC_FORMAT(RTC_Format));
1033
1034 /* Get the RTC_TR register */
1035 tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK);
1036
1037 /* Fill the structure fields with the read parameters */
1038 RTC_DateStruct->RTC_Year = (uint8_t)((tmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
1039 RTC_DateStruct->RTC_Month = (uint8_t)((tmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1040 RTC_DateStruct->RTC_Date = (uint8_t)(tmpreg & (RTC_DR_DT | RTC_DR_DU));
1041 RTC_DateStruct->RTC_WeekDay = (uint8_t)((tmpreg & (RTC_DR_WDU)) >> 13);
1042
1043 /* Check the input parameters format */
1044 if (RTC_Format == RTC_Format_BIN)
1045 {
1046 /* Convert the structure parameters to Binary format */
1047 RTC_DateStruct->RTC_Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year);
1048 RTC_DateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
1049 RTC_DateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
1050 RTC_DateStruct->RTC_WeekDay = (uint8_t)(RTC_DateStruct->RTC_WeekDay);
1051 }
1052 }
1053
1054 /**
1055 * @}
1056 */
1057
1058 /** @defgroup RTC_Group3 Alarms configuration functions
1059 * @brief Alarms (Alarm A and Alarm B) configuration functions
1060 *
1061 @verbatim
1062 ===============================================================================
1063 ##### Alarms (Alarm A and Alarm B) configuration functions #####
1064 ===============================================================================
1065 [..] This section provides functions allowing to program and read the RTC Alarms.
1066
1067 @endverbatim
1068 * @{
1069 */
1070
1071 /**
1072 * @brief Set the specified RTC Alarm.
1073 * @note The Alarm register can only be written when the corresponding Alarm
1074 * is disabled (Use the RTC_AlarmCmd(DISABLE)).
1075 * @param RTC_Format: specifies the format of the returned parameters.
1076 * This parameter can be one of the following values:
1077 * @arg RTC_Format_BIN: Binary data format
1078 * @arg RTC_Format_BCD: BCD data format
1079 * @param RTC_Alarm: specifies the alarm to be configured.
1080 * This parameter can be one of the following values:
1081 * @arg RTC_Alarm_A: to select Alarm A
1082 * @arg RTC_Alarm_B: to select Alarm B
1083 * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that
1084 * contains the alarm configuration parameters.
1085 * @retval None
1086 */
1087 void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1088 {
1089 uint32_t tmpreg = 0;
1090
1091 /* Check the parameters */
1092 assert_param(IS_RTC_FORMAT(RTC_Format));
1093 assert_param(IS_RTC_ALARM(RTC_Alarm));
1094 assert_param(IS_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
1095 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
1096
1097 if (RTC_Format == RTC_Format_BIN)
1098 {
1099 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
1100 {
1101 assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1102 assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
1103 }
1104 else
1105 {
1106 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1107 assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1108 }
1109 assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
1110 assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
1111
1112 if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1113 {
1114 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1115 }
1116 else
1117 {
1118 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1119 }
1120 }
1121 else
1122 {
1123 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
1124 {
1125 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
1126 assert_param(IS_RTC_HOUR12(tmpreg));
1127 assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
1128 }
1129 else
1130 {
1131 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1132 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
1133 }
1134
1135 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
1136 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
1137
1138 if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1139 {
1140 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1141 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1142 }
1143 else
1144 {
1145 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1146 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1147 }
1148 }
1149
1150 /* Check the input parameters format */
1151 if (RTC_Format != RTC_Format_BIN)
1152 {
1153 tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
1154 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
1155 ((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
1156 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
1157 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
1158 ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
1159 ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
1160 }
1161 else
1162 {
1163 tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
1164 ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
1165 ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
1166 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
1167 ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
1168 ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
1169 ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
1170 }
1171
1172 /* Disable the write protection for RTC registers */
1173 RTC->WPR = 0xCA;
1174 RTC->WPR = 0x53;
1175
1176 /* Configure the Alarm register */
1177 if (RTC_Alarm == RTC_Alarm_A)
1178 {
1179 RTC->ALRMAR = (uint32_t)tmpreg;
1180 }
1181 else
1182 {
1183 RTC->ALRMBR = (uint32_t)tmpreg;
1184 }
1185
1186 /* Enable the write protection for RTC registers */
1187 RTC->WPR = 0xFF;
1188 }
1189
1190 /**
1191 * @brief Fills each RTC_AlarmStruct member with its default value
1192 * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
1193 * all fields are masked).
1194 * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
1195 * will be initialized.
1196 * @retval None
1197 */
1198 void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
1199 {
1200 /* Alarm Time Settings : Time = 00h:00mn:00sec */
1201 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
1202 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
1203 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
1204 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
1205
1206 /* Alarm Date Settings : Date = 1st day of the month */
1207 RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
1208 RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
1209
1210 /* Alarm Masks Settings : Mask = all fields are not masked */
1211 RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
1212 }
1213
1214 /**
1215 * @brief Get the RTC Alarm value and masks.
1216 * @param RTC_Format: specifies the format of the output parameters.
1217 * This parameter can be one of the following values:
1218 * @arg RTC_Format_BIN: Binary data format
1219 * @arg RTC_Format_BCD: BCD data format
1220 * @param RTC_Alarm: specifies the alarm to be read.
1221 * This parameter can be one of the following values:
1222 * @arg RTC_Alarm_A: to select Alarm A
1223 * @arg RTC_Alarm_B: to select Alarm B
1224 * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will
1225 * contains the output alarm configuration values.
1226 * @retval None
1227 */
1228 void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1229 {
1230 uint32_t tmpreg = 0;
1231
1232 /* Check the parameters */
1233 assert_param(IS_RTC_FORMAT(RTC_Format));
1234 assert_param(IS_RTC_ALARM(RTC_Alarm));
1235
1236 /* Get the RTC_ALRMxR register */
1237 if (RTC_Alarm == RTC_Alarm_A)
1238 {
1239 tmpreg = (uint32_t)(RTC->ALRMAR);
1240 }
1241 else
1242 {
1243 tmpreg = (uint32_t)(RTC->ALRMBR);
1244 }
1245
1246 /* Fill the structure with the read parameters */
1247 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \
1248 RTC_ALRMAR_HU)) >> 16);
1249 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \
1250 RTC_ALRMAR_MNU)) >> 8);
1251 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \
1252 RTC_ALRMAR_SU));
1253 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1254 RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1255 RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1256 RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);
1257
1258 if (RTC_Format == RTC_Format_BIN)
1259 {
1260 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1261 RTC_AlarmTime.RTC_Hours);
1262 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1263 RTC_AlarmTime.RTC_Minutes);
1264 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1265 RTC_AlarmTime.RTC_Seconds);
1266 RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1267 }
1268 }
1269
1270 /**
1271 * @brief Enables or disables the specified RTC Alarm.
1272 * @param RTC_Alarm: specifies the alarm to be configured.
1273 * This parameter can be any combination of the following values:
1274 * @arg RTC_Alarm_A: to select Alarm A
1275 * @arg RTC_Alarm_B: to select Alarm B
1276 * @param NewState: new state of the specified alarm.
1277 * This parameter can be: ENABLE or DISABLE.
1278 * @retval An ErrorStatus enumeration value:
1279 * - SUCCESS: RTC Alarm is enabled/disabled
1280 * - ERROR: RTC Alarm is not enabled/disabled
1281 */
1282 ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
1283 {
1284 __IO uint32_t alarmcounter = 0x00;
1285 uint32_t alarmstatus = 0x00;
1286 ErrorStatus status = ERROR;
1287
1288 /* Check the parameters */
1289 assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
1290 assert_param(IS_FUNCTIONAL_STATE(NewState));
1291
1292 /* Disable the write protection for RTC registers */
1293 RTC->WPR = 0xCA;
1294 RTC->WPR = 0x53;
1295
1296 /* Configure the Alarm state */
1297 if (NewState != DISABLE)
1298 {
1299 RTC->CR |= (uint32_t)RTC_Alarm;
1300
1301 status = SUCCESS;
1302 }
1303 else
1304 {
1305 /* Disable the Alarm in RTC_CR register */
1306 RTC->CR &= (uint32_t)~RTC_Alarm;
1307
1308 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1309 do
1310 {
1311 alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
1312 alarmcounter++;
1313 } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
1314
1315 if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
1316 {
1317 status = ERROR;
1318 }
1319 else
1320 {
1321 status = SUCCESS;
1322 }
1323 }
1324
1325 /* Enable the write protection for RTC registers */
1326 RTC->WPR = 0xFF;
1327
1328 return status;
1329 }
1330
1331 /**
1332 * @brief Configures the RTC AlarmA/B Subseconds value and mask.
1333 * @note This function is performed only when the Alarm is disabled.
1334 * @param RTC_Alarm: specifies the alarm to be configured.
1335 * This parameter can be one of the following values:
1336 * @arg RTC_Alarm_A: to select Alarm A
1337 * @arg RTC_Alarm_B: to select Alarm B
1338 * @param RTC_AlarmSubSecondValue: specifies the Subseconds value.
1339 * This parameter can be a value from 0 to 0x00007FFF.
1340 * @param RTC_AlarmSubSecondMask: specifies the Subseconds Mask.
1341 * This parameter can be any combination of the following values:
1342 * @arg RTC_AlarmSubSecondMask_All : All Alarm SS fields are masked.
1343 * There is no comparison on sub seconds for Alarm.
1344 * @arg RTC_AlarmSubSecondMask_SS14_1 : SS[14:1] are don't care in Alarm comparison.
1345 * Only SS[0] is compared
1346 * @arg RTC_AlarmSubSecondMask_SS14_2 : SS[14:2] are don't care in Alarm comparison.
1347 * Only SS[1:0] are compared
1348 * @arg RTC_AlarmSubSecondMask_SS14_3 : SS[14:3] are don't care in Alarm comparison.
1349 * Only SS[2:0] are compared
1350 * @arg RTC_AlarmSubSecondMask_SS14_4 : SS[14:4] are don't care in Alarm comparison.
1351 * Only SS[3:0] are compared
1352 * @arg RTC_AlarmSubSecondMask_SS14_5 : SS[14:5] are don't care in Alarm comparison.
1353 * Only SS[4:0] are compared
1354 * @arg RTC_AlarmSubSecondMask_SS14_6 : SS[14:6] are don't care in Alarm comparison.
1355 * Only SS[5:0] are compared
1356 * @arg RTC_AlarmSubSecondMask_SS14_7 : SS[14:7] are don't care in Alarm comparison.
1357 * Only SS[6:0] are compared
1358 * @arg RTC_AlarmSubSecondMask_SS14_8 : SS[14:8] are don't care in Alarm comparison.
1359 * Only SS[7:0] are compared
1360 * @arg RTC_AlarmSubSecondMask_SS14_9 : SS[14:9] are don't care in Alarm comparison.
1361 * Only SS[8:0] are compared
1362 * @arg RTC_AlarmSubSecondMask_SS14_10: SS[14:10] are don't care in Alarm comparison.
1363 * Only SS[9:0] are compared
1364 * @arg RTC_AlarmSubSecondMask_SS14_11: SS[14:11] are don't care in Alarm comparison.
1365 * Only SS[10:0] are compared
1366 * @arg RTC_AlarmSubSecondMask_SS14_12: SS[14:12] are don't care in Alarm comparison.
1367 * Only SS[11:0] are compared
1368 * @arg RTC_AlarmSubSecondMask_SS14_13: SS[14:13] are don't care in Alarm comparison.
1369 * Only SS[12:0] are compared
1370 * @arg RTC_AlarmSubSecondMask_SS14 : SS[14] is don't care in Alarm comparison.
1371 * Only SS[13:0] are compared
1372 * @arg RTC_AlarmSubSecondMask_None : SS[14:0] are compared and must match
1373 * to activate alarm
1374 * @retval None
1375 */
1376 void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)
1377 {
1378 uint32_t tmpreg = 0;
1379
1380 /* Check the parameters */
1381 assert_param(IS_RTC_ALARM(RTC_Alarm));
1382 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
1383 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(RTC_AlarmSubSecondMask));
1384
1385 /* Disable the write protection for RTC registers */
1386 RTC->WPR = 0xCA;
1387 RTC->WPR = 0x53;
1388
1389 /* Configure the Alarm A or Alarm B SubSecond registers */
1390 tmpreg = (uint32_t) (uint32_t)(RTC_AlarmSubSecondValue) | (uint32_t)(RTC_AlarmSubSecondMask);
1391
1392 if (RTC_Alarm == RTC_Alarm_A)
1393 {
1394 /* Configure the AlarmA SubSecond register */
1395 RTC->ALRMASSR = tmpreg;
1396 }
1397 else
1398 {
1399 /* Configure the Alarm B SubSecond register */
1400 RTC->ALRMBSSR = tmpreg;
1401 }
1402
1403 /* Enable the write protection for RTC registers */
1404 RTC->WPR = 0xFF;
1405
1406 }
1407
1408 /**
1409 * @brief Gets the RTC Alarm Subseconds value.
1410 * @param RTC_Alarm: specifies the alarm to be read.
1411 * This parameter can be one of the following values:
1412 * @arg RTC_Alarm_A: to select Alarm A
1413 * @arg RTC_Alarm_B: to select Alarm B
1414 * @param None
1415 * @retval RTC Alarm Subseconds value.
1416 */
1417 uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
1418 {
1419 uint32_t tmpreg = 0;
1420
1421 /* Get the RTC_ALRMxR register */
1422 if (RTC_Alarm == RTC_Alarm_A)
1423 {
1424 tmpreg = (uint32_t)((RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1425 }
1426 else
1427 {
1428 tmpreg = (uint32_t)((RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1429 }
1430
1431 return (tmpreg);
1432 }
1433
1434 /**
1435 * @}
1436 */
1437
1438 /** @defgroup RTC_Group4 WakeUp Timer configuration functions
1439 * @brief WakeUp Timer configuration functions
1440 *
1441 @verbatim
1442 ===============================================================================
1443 ##### WakeUp Timer configuration functions #####
1444 ===============================================================================
1445 [..] This section provide functions allowing to program and read the RTC WakeUp.
1446
1447 @endverbatim
1448 * @{
1449 */
1450
1451 /**
1452 * @brief Configures the RTC Wakeup clock source.
1453 * @note The WakeUp Clock source can only be changed when the RTC WakeUp
1454 * is disabled (Use the RTC_WakeUpCmd(DISABLE)).
1455 * @param RTC_WakeUpClock: Wakeup Clock source.
1456 * This parameter can be one of the following values:
1457 * @arg RTC_WakeUpClock_RTCCLK_Div16: RTC Wakeup Counter Clock = RTCCLK/16
1458 * @arg RTC_WakeUpClock_RTCCLK_Div8: RTC Wakeup Counter Clock = RTCCLK/8
1459 * @arg RTC_WakeUpClock_RTCCLK_Div4: RTC Wakeup Counter Clock = RTCCLK/4
1460 * @arg RTC_WakeUpClock_RTCCLK_Div2: RTC Wakeup Counter Clock = RTCCLK/2
1461 * @arg RTC_WakeUpClock_CK_SPRE_16bits: RTC Wakeup Counter Clock = CK_SPRE
1462 * @arg RTC_WakeUpClock_CK_SPRE_17bits: RTC Wakeup Counter Clock = CK_SPRE
1463 * @retval None
1464 */
1465 void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
1466 {
1467 /* Check the parameters */
1468 assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
1469
1470 /* Disable the write protection for RTC registers */
1471 RTC->WPR = 0xCA;
1472 RTC->WPR = 0x53;
1473
1474 /* Clear the Wakeup Timer clock source bits in CR register */
1475 RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1476
1477 /* Configure the clock source */
1478 RTC->CR |= (uint32_t)RTC_WakeUpClock;
1479
1480 /* Enable the write protection for RTC registers */
1481 RTC->WPR = 0xFF;
1482 }
1483
1484 /**
1485 * @brief Configures the RTC Wakeup counter.
1486 * @note The RTC WakeUp counter can only be written when the RTC WakeUp
1487 * is disabled (Use the RTC_WakeUpCmd(DISABLE)).
1488 * @param RTC_WakeUpCounter: specifies the WakeUp counter.
1489 * This parameter can be a value from 0x0000 to 0xFFFF.
1490 * @retval None
1491 */
1492 void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
1493 {
1494 /* Check the parameters */
1495 assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));
1496
1497 /* Disable the write protection for RTC registers */
1498 RTC->WPR = 0xCA;
1499 RTC->WPR = 0x53;
1500
1501 /* Configure the Wakeup Timer counter */
1502 RTC->WUTR = (uint32_t)RTC_WakeUpCounter;
1503
1504 /* Enable the write protection for RTC registers */
1505 RTC->WPR = 0xFF;
1506 }
1507
1508 /**
1509 * @brief Returns the RTC WakeUp timer counter value.
1510 * @param None
1511 * @retval The RTC WakeUp Counter value.
1512 */
1513 uint32_t RTC_GetWakeUpCounter(void)
1514 {
1515 /* Get the counter value */
1516 return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));
1517 }
1518
1519 /**
1520 * @brief Enables or Disables the RTC WakeUp timer.
1521 * @param NewState: new state of the WakeUp timer.
1522 * This parameter can be: ENABLE or DISABLE.
1523 * @retval None
1524 */
1525 ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
1526 {
1527 __IO uint32_t wutcounter = 0x00;
1528 uint32_t wutwfstatus = 0x00;
1529 ErrorStatus status = ERROR;
1530
1531 /* Check the parameters */
1532 assert_param(IS_FUNCTIONAL_STATE(NewState));
1533
1534 /* Disable the write protection for RTC registers */
1535 RTC->WPR = 0xCA;
1536 RTC->WPR = 0x53;
1537
1538 if (NewState != DISABLE)
1539 {
1540 /* Enable the Wakeup Timer */
1541 RTC->CR |= (uint32_t)RTC_CR_WUTE;
1542 status = SUCCESS;
1543 }
1544 else
1545 {
1546 /* Disable the Wakeup Timer */
1547 RTC->CR &= (uint32_t)~RTC_CR_WUTE;
1548 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1549 do
1550 {
1551 wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
1552 wutcounter++;
1553 } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
1554
1555 if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
1556 {
1557 status = ERROR;
1558 }
1559 else
1560 {
1561 status = SUCCESS;
1562 }
1563 }
1564
1565 /* Enable the write protection for RTC registers */
1566 RTC->WPR = 0xFF;
1567
1568 return status;
1569 }
1570
1571 /**
1572 * @}
1573 */
1574
1575 /** @defgroup RTC_Group5 Daylight Saving configuration functions
1576 * @brief Daylight Saving configuration functions
1577 *
1578 @verbatim
1579 ===============================================================================
1580 ##### Daylight Saving configuration functions #####
1581 ===============================================================================
1582 [..] This section provide functions allowing to configure the RTC DayLight Saving.
1583
1584 @endverbatim
1585 * @{
1586 */
1587
1588 /**
1589 * @brief Adds or substract one hour from the current time.
1590 * @param RTC_DayLightSaveOperation: the value of hour adjustment.
1591 * This parameter can be one of the following values:
1592 * @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)
1593 * @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)
1594 * @param RTC_StoreOperation: Specifies the value to be written in the BCK bit
1595 * in CR register to store the operation.
1596 * This parameter can be one of the following values:
1597 * @arg RTC_StoreOperation_Reset: BCK Bit Reset
1598 * @arg RTC_StoreOperation_Set: BCK Bit Set
1599 * @retval None
1600 */
1601 void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
1602 {
1603 /* Check the parameters */
1604 assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
1605 assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
1606
1607 /* Disable the write protection for RTC registers */
1608 RTC->WPR = 0xCA;
1609 RTC->WPR = 0x53;
1610
1611 /* Clear the bits to be configured */
1612 RTC->CR &= (uint32_t)~(RTC_CR_BCK);
1613
1614 /* Configure the RTC_CR register */
1615 RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
1616
1617 /* Enable the write protection for RTC registers */
1618 RTC->WPR = 0xFF;
1619 }
1620
1621 /**
1622 * @brief Returns the RTC Day Light Saving stored operation.
1623 * @param None
1624 * @retval RTC Day Light Saving stored operation.
1625 * - RTC_StoreOperation_Reset
1626 * - RTC_StoreOperation_Set
1627 */
1628 uint32_t RTC_GetStoreOperation(void)
1629 {
1630 return (RTC->CR & RTC_CR_BCK);
1631 }
1632
1633 /**
1634 * @}
1635 */
1636
1637 /** @defgroup RTC_Group6 Output pin Configuration function
1638 * @brief Output pin Configuration function
1639 *
1640 @verbatim
1641 ===============================================================================
1642 ##### Output pin Configuration function #####
1643 ===============================================================================
1644 [..] This section provide functions allowing to configure the RTC Output source.
1645
1646 @endverbatim
1647 * @{
1648 */
1649
1650 /**
1651 * @brief Configures the RTC output source (AFO_ALARM).
1652 * @param RTC_Output: Specifies which signal will be routed to the RTC output.
1653 * This parameter can be one of the following values:
1654 * @arg RTC_Output_Disable: No output selected
1655 * @arg RTC_Output_AlarmA: signal of AlarmA mapped to output
1656 * @arg RTC_Output_AlarmB: signal of AlarmB mapped to output
1657 * @arg RTC_Output_WakeUp: signal of WakeUp mapped to output
1658 * @param RTC_OutputPolarity: Specifies the polarity of the output signal.
1659 * This parameter can be one of the following:
1660 * @arg RTC_OutputPolarity_High: The output pin is high when the
1661 * ALRAF/ALRBF/WUTF is high (depending on OSEL)
1662 * @arg RTC_OutputPolarity_Low: The output pin is low when the
1663 * ALRAF/ALRBF/WUTF is high (depending on OSEL)
1664 * @retval None
1665 */
1666 void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
1667 {
1668 /* Check the parameters */
1669 assert_param(IS_RTC_OUTPUT(RTC_Output));
1670 assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
1671
1672 /* Disable the write protection for RTC registers */
1673 RTC->WPR = 0xCA;
1674 RTC->WPR = 0x53;
1675
1676 /* Clear the bits to be configured */
1677 RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
1678
1679 /* Configure the output selection and polarity */
1680 RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
1681
1682 /* Enable the write protection for RTC registers */
1683 RTC->WPR = 0xFF;
1684 }
1685
1686 /**
1687 * @}
1688 */
1689
1690 /** @defgroup RTC_Group7 Digital Calibration configuration functions
1691 * @brief Digital Calibration configuration functions
1692 *
1693 @verbatim
1694 ===============================================================================
1695 ##### Digital Calibration configuration functions #####
1696 ===============================================================================
1697
1698 @endverbatim
1699 * @{
1700 */
1701
1702 /**
1703 * @brief Enables or disables the RTC clock to be output through the relative
1704 * pin.
1705 * @param NewState: new state of the digital calibration Output.
1706 * This parameter can be: ENABLE or DISABLE.
1707 * @retval None
1708 */
1709 void RTC_CalibOutputCmd(FunctionalState NewState)
1710 {
1711 /* Check the parameters */
1712 assert_param(IS_FUNCTIONAL_STATE(NewState));
1713
1714 /* Disable the write protection for RTC registers */
1715 RTC->WPR = 0xCA;
1716 RTC->WPR = 0x53;
1717
1718 if (NewState != DISABLE)
1719 {
1720 /* Enable the RTC clock output */
1721 RTC->CR |= (uint32_t)RTC_CR_COE;
1722 }
1723 else
1724 {
1725 /* Disable the RTC clock output */
1726 RTC->CR &= (uint32_t)~RTC_CR_COE;
1727 }
1728
1729 /* Enable the write protection for RTC registers */
1730 RTC->WPR = 0xFF;
1731 }
1732
1733 /**
1734 * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1735 * @param RTC_CalibOutput : Select the Calibration output Selection .
1736 * This parameter can be one of the following values:
1737 * @arg RTC_CalibOutput_512Hz: A signal has a regular waveform at 512Hz.
1738 * @arg RTC_CalibOutput_1Hz : A signal has a regular waveform at 1Hz.
1739 * @retval None
1740 */
1741 void RTC_CalibOutputConfig(uint32_t RTC_CalibOutput)
1742 {
1743 /* Check the parameters */
1744 assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
1745
1746 /* Disable the write protection for RTC registers */
1747 RTC->WPR = 0xCA;
1748 RTC->WPR = 0x53;
1749
1750 /*clear flags before config*/
1751 RTC->CR &= (uint32_t)~(RTC_CR_COSEL);
1752
1753 /* Configure the RTC_CR register */
1754 RTC->CR |= (uint32_t)RTC_CalibOutput;
1755
1756 /* Enable the write protection for RTC registers */
1757 RTC->WPR = 0xFF;
1758 }
1759
1760 /**
1761 * @brief Configures the Smooth Calibration Settings.
1762 * @param RTC_SmoothCalibPeriod : Select the Smooth Calibration Period.
1763 * This parameter can be can be one of the following values:
1764 * @arg RTC_SmoothCalibPeriod_32sec : The smooth calibration periode is 32s.
1765 * @arg RTC_SmoothCalibPeriod_16sec : The smooth calibration periode is 16s.
1766 * @arg RTC_SmoothCalibPeriod_8sec : The smooth calibartion periode is 8s.
1767 * @param RTC_SmoothCalibPlusPulses : Select to Set or reset the CALP bit.
1768 * This parameter can be one of the following values:
1769 * @arg RTC_SmoothCalibPlusPulses_Set : Add one RTCCLK puls every 2**11 pulses.
1770 * @arg RTC_SmoothCalibPlusPulses_Reset: No RTCCLK pulses are added.
1771 * @param RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
1772 * This parameter can be one any value from 0 to 0x000001FF.
1773 * @retval An ErrorStatus enumeration value:
1774 * - SUCCESS: RTC Calib registers are configured
1775 * - ERROR: RTC Calib registers are not configured
1776 */
1777 ErrorStatus RTC_SmoothCalibConfig(uint32_t RTC_SmoothCalibPeriod,
1778 uint32_t RTC_SmoothCalibPlusPulses,
1779 uint32_t RTC_SmouthCalibMinusPulsesValue)
1780 {
1781 ErrorStatus status = ERROR;
1782 uint32_t recalpfcount = 0;
1783
1784 /* Check the parameters */
1785 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod));
1786 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
1787 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
1788
1789 /* Disable the write protection for RTC registers */
1790 RTC->WPR = 0xCA;
1791 RTC->WPR = 0x53;
1792
1793 /* check if a calibration is pending*/
1794 if ((RTC->ISR & RTC_ISR_RECALPF) != RESET)
1795 {
1796 /* wait until the Calibration is completed*/
1797 while (((RTC->ISR & RTC_ISR_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
1798 {
1799 recalpfcount++;
1800 }
1801 }
1802
1803 /* check if the calibration pending is completed or if there is no calibration operation at all*/
1804 if ((RTC->ISR & RTC_ISR_RECALPF) == RESET)
1805 {
1806 /* Configure the Smooth calibration settings */
1807 RTC->CALR = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
1808
1809 status = SUCCESS;
1810 }
1811 else
1812 {
1813 status = ERROR;
1814 }
1815
1816 /* Enable the write protection for RTC registers */
1817 RTC->WPR = 0xFF;
1818
1819 return (ErrorStatus)(status);
1820 }
1821
1822 /**
1823 * @}
1824 */
1825
1826
1827 /** @defgroup RTC_Group8 TimeStamp configuration functions
1828 * @brief TimeStamp configuration functions
1829 *
1830 @verbatim
1831 ===============================================================================
1832 ##### TimeStamp configuration functions #####
1833 ===============================================================================
1834
1835 @endverbatim
1836 * @{
1837 */
1838
1839 /**
1840 * @brief Enables or Disables the RTC TimeStamp functionality with the
1841 * specified time stamp pin stimulating edge.
1842 * @param RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is
1843 * activated.
1844 * This parameter can be one of the following:
1845 * @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising
1846 * edge of the related pin.
1847 * @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the
1848 * falling edge of the related pin.
1849 * @param NewState: new state of the TimeStamp.
1850 * This parameter can be: ENABLE or DISABLE.
1851 * @retval None
1852 */
1853 void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
1854 {
1855 uint32_t tmpreg = 0;
1856
1857 /* Check the parameters */
1858 assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
1859 assert_param(IS_FUNCTIONAL_STATE(NewState));
1860
1861 /* Get the RTC_CR register and clear the bits to be configured */
1862 tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
1863
1864 /* Get the new configuration */
1865 if (NewState != DISABLE)
1866 {
1867 tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
1868 }
1869 else
1870 {
1871 tmpreg |= (uint32_t)(RTC_TimeStampEdge);
1872 }
1873
1874 /* Disable the write protection for RTC registers */
1875 RTC->WPR = 0xCA;
1876 RTC->WPR = 0x53;
1877
1878 /* Configure the Time Stamp TSEDGE and Enable bits */
1879 RTC->CR = (uint32_t)tmpreg;
1880
1881 /* Enable the write protection for RTC registers */
1882 RTC->WPR = 0xFF;
1883 }
1884
1885 /**
1886 * @brief Gets the RTC TimeStamp value and masks.
1887 * @param RTC_Format: specifies the format of the output parameters.
1888 * This parameter can be one of the following values:
1889 * @arg RTC_Format_BIN: Binary data format
1890 * @arg RTC_Format_BCD: BCD data format
1891 * @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will
1892 * contains the TimeStamp time values.
1893 * @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will
1894 * contains the TimeStamp date values.
1895 * @retval None
1896 */
1897 void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct,
1898 RTC_DateTypeDef* RTC_StampDateStruct)
1899 {
1900 uint32_t tmptime = 0, tmpdate = 0;
1901
1902 /* Check the parameters */
1903 assert_param(IS_RTC_FORMAT(RTC_Format));
1904
1905 /* Get the TimeStamp time and date registers values */
1906 tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);
1907 tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);
1908
1909 /* Fill the Time structure fields with the read parameters */
1910 RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
1911 RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
1912 RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
1913 RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
1914
1915 /* Fill the Date structure fields with the read parameters */
1916 RTC_StampDateStruct->RTC_Year = 0;
1917 RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1918 RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
1919 RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
1920
1921 /* Check the input parameters format */
1922 if (RTC_Format == RTC_Format_BIN)
1923 {
1924 /* Convert the Time structure parameters to Binary format */
1925 RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);
1926 RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);
1927 RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);
1928
1929 /* Convert the Date structure parameters to Binary format */
1930 RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);
1931 RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);
1932 RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);
1933 }
1934 }
1935
1936 /**
1937 * @brief Gets the RTC timestamp Subseconds value.
1938 * @param None
1939 * @retval RTC current timestamp Subseconds value.
1940 */
1941 uint32_t RTC_GetTimeStampSubSecond(void)
1942 {
1943 /* Get timestamp subseconds values from the correspondent registers */
1944 return (uint32_t)(RTC->TSSSR);
1945 }
1946
1947 /**
1948 * @}
1949 */
1950
1951 /** @defgroup RTC_Group9 Tampers configuration functions
1952 * @brief Tampers configuration functions
1953 *
1954 @verbatim
1955 ===============================================================================
1956 ##### Tampers configuration functions #####
1957 ===============================================================================
1958
1959 @endverbatim
1960 * @{
1961 */
1962
1963 /**
1964 * @brief Configures the select Tamper pin edge.
1965 * @param RTC_Tamper: Selected tamper pin.
1966 * This parameter can be any combination of the following values:
1967 * @arg RTC_Tamper_1: Select Tamper 1.
1968 * @arg RTC_Tamper_2: Select Tamper 2.
1969 * @arg RTC_Tamper_3: Select Tamper 3.
1970 * @param RTC_TamperTrigger: Specifies the trigger on the tamper pin that
1971 * stimulates tamper event.
1972 * This parameter can be one of the following values:
1973 * @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
1974 * @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
1975 * @arg RTC_TamperTrigger_LowLevel: Low Level of the tamper pin causes tamper event.
1976 * @arg RTC_TamperTrigger_HighLevel: High Level of the tamper pin causes tamper event.
1977 * @retval None
1978 */
1979 void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
1980 {
1981 /* Check the parameters */
1982 assert_param(IS_RTC_TAMPER(RTC_Tamper));
1983 assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
1984
1985 /* Check if the active level for Tamper is rising edge (Low level)*/
1986 if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
1987 {
1988 /* Configure the RTC_TAFCR register */
1989 RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));
1990 }
1991 else
1992 {
1993 /* Configure the RTC_TAFCR register */
1994 RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);
1995 }
1996 }
1997
1998 /**
1999 * @brief Enables or Disables the Tamper detection.
2000 * @param RTC_Tamper: Selected tamper pin.
2001 * This parameter can be any combination of the following values:
2002 * @arg RTC_Tamper_1: Select Tamper 1.
2003 * @arg RTC_Tamper_2: Select Tamper 2.
2004 * @arg RTC_Tamper_3: Select Tamper 3.
2005 * @param NewState: new state of the tamper pin.
2006 * This parameter can be: ENABLE or DISABLE.
2007 * @retval None
2008 */
2009 void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
2010 {
2011 /* Check the parameters */
2012 assert_param(IS_RTC_TAMPER(RTC_Tamper));
2013 assert_param(IS_FUNCTIONAL_STATE(NewState));
2014
2015 if (NewState != DISABLE)
2016 {
2017 /* Enable the selected Tamper pin */
2018 RTC->TAFCR |= (uint32_t)RTC_Tamper;
2019 }
2020 else
2021 {
2022 /* Disable the selected Tamper pin */
2023 RTC->TAFCR &= (uint32_t)~RTC_Tamper;
2024 }
2025 }
2026
2027 /**
2028 * @brief Configures the Tampers Filter.
2029 * @param RTC_TamperFilter: Specifies the tampers filter.
2030 * This parameter can be one of the following values:
2031 * @arg RTC_TamperFilter_Disable: Tamper filter is disabled.
2032 * @arg RTC_TamperFilter_2Sample: Tamper is activated after 2 consecutive
2033 * samples at the active level
2034 * @arg RTC_TamperFilter_4Sample: Tamper is activated after 4 consecutive
2035 * samples at the active level
2036 * @arg RTC_TamperFilter_8Sample: Tamper is activated after 8 consecutive
2037 * samples at the active level
2038 * @retval None
2039 */
2040 void RTC_TamperFilterConfig(uint32_t RTC_TamperFilter)
2041 {
2042 /* Check the parameters */
2043 assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));
2044
2045 /* Clear TAMPFLT[1:0] bits in the RTC_TAFCR register */
2046 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFLT);
2047
2048 /* Configure the RTC_TAFCR register */
2049 RTC->TAFCR |= (uint32_t)RTC_TamperFilter;
2050 }
2051
2052 /**
2053 * @brief Configures the Tampers Sampling Frequency.
2054 * @param RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency.
2055 * This parameter can be one of the following values:
2056 * @arg RTC_TamperSamplingFreq_RTCCLK_Div32768: Each of the tamper inputs are sampled
2057 * with a frequency = RTCCLK / 32768
2058 * @arg RTC_TamperSamplingFreq_RTCCLK_Div16384: Each of the tamper inputs are sampled
2059 * with a frequency = RTCCLK / 16384
2060 * @arg RTC_TamperSamplingFreq_RTCCLK_Div8192: Each of the tamper inputs are sampled
2061 * with a frequency = RTCCLK / 8192
2062 * @arg RTC_TamperSamplingFreq_RTCCLK_Div4096: Each of the tamper inputs are sampled
2063 * with a frequency = RTCCLK / 4096
2064 * @arg RTC_TamperSamplingFreq_RTCCLK_Div2048: Each of the tamper inputs are sampled
2065 * with a frequency = RTCCLK / 2048
2066 * @arg RTC_TamperSamplingFreq_RTCCLK_Div1024: Each of the tamper inputs are sampled
2067 * with a frequency = RTCCLK / 1024
2068 * @arg RTC_TamperSamplingFreq_RTCCLK_Div512: Each of the tamper inputs are sampled
2069 * with a frequency = RTCCLK / 512
2070 * @arg RTC_TamperSamplingFreq_RTCCLK_Div256: Each of the tamper inputs are sampled
2071 * with a frequency = RTCCLK / 256
2072 * @retval None
2073 */
2074 void RTC_TamperSamplingFreqConfig(uint32_t RTC_TamperSamplingFreq)
2075 {
2076 /* Check the parameters */
2077 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq));
2078
2079 /* Clear TAMPFREQ[2:0] bits in the RTC_TAFCR register */
2080 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFREQ);
2081
2082 /* Configure the RTC_TAFCR register */
2083 RTC->TAFCR |= (uint32_t)RTC_TamperSamplingFreq;
2084 }
2085
2086 /**
2087 * @brief Configures the Tampers Pins input Precharge Duration.
2088 * @param RTC_TamperPrechargeDuration: Specifies the Tampers Pins input
2089 * Precharge Duration.
2090 * This parameter can be one of the following values:
2091 * @arg RTC_TamperPrechargeDuration_1RTCCLK: Tamper pins are pre-charged before sampling during 1 RTCCLK cycle
2092 * @arg RTC_TamperPrechargeDuration_2RTCCLK: Tamper pins are pre-charged before sampling during 2 RTCCLK cycle
2093 * @arg RTC_TamperPrechargeDuration_4RTCCLK: Tamper pins are pre-charged before sampling during 4 RTCCLK cycle
2094 * @arg RTC_TamperPrechargeDuration_8RTCCLK: Tamper pins are pre-charged before sampling during 8 RTCCLK cycle
2095 * @retval None
2096 */
2097 void RTC_TamperPinsPrechargeDuration(uint32_t RTC_TamperPrechargeDuration)
2098 {
2099 /* Check the parameters */
2100 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(RTC_TamperPrechargeDuration));
2101
2102 /* Clear TAMPPRCH[1:0] bits in the RTC_TAFCR register */
2103 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPPRCH);
2104
2105 /* Configure the RTC_TAFCR register */
2106 RTC->TAFCR |= (uint32_t)RTC_TamperPrechargeDuration;
2107 }
2108
2109 /**
2110 * @brief Enables or Disables the TimeStamp on Tamper Detection Event.
2111 * @note The timestamp is valid even the TSE bit in tamper control register
2112 * is reset.
2113 * @param NewState: new state of the timestamp on tamper event.
2114 * This parameter can be: ENABLE or DISABLE.
2115 * @retval None
2116 */
2117 void RTC_TimeStampOnTamperDetectionCmd(FunctionalState NewState)
2118 {
2119 /* Check the parameters */
2120 assert_param(IS_FUNCTIONAL_STATE(NewState));
2121
2122 if (NewState != DISABLE)
2123 {
2124 /* Save timestamp on tamper detection event */
2125 RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPTS;
2126 }
2127 else
2128 {
2129 /* Tamper detection does not cause a timestamp to be saved */
2130 RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPTS;
2131 }
2132 }
2133
2134 /**
2135 * @brief Enables or Disables the Precharge of Tamper pin.
2136 * @param NewState: new state of tamper pull up.
2137 * This parameter can be: ENABLE or DISABLE.
2138 * @retval None
2139 */
2140 void RTC_TamperPullUpCmd(FunctionalState NewState)
2141 {
2142 /* Check the parameters */
2143 assert_param(IS_FUNCTIONAL_STATE(NewState));
2144
2145 if (NewState != DISABLE)
2146 {
2147 /* Enable precharge of the selected Tamper pin */
2148 RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPPUDIS;
2149 }
2150 else
2151 {
2152 /* Disable precharge of the selected Tamper pin */
2153 RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPPUDIS;
2154 }
2155 }
2156
2157 /**
2158 * @}
2159 */
2160
2161 /** @defgroup RTC_Group10 Backup Data Registers configuration functions
2162 * @brief Backup Data Registers configuration functions
2163 *
2164 @verbatim
2165 ===============================================================================
2166 ##### Backup Data Registers configuration functions #####
2167 ===============================================================================
2168
2169 @endverbatim
2170 * @{
2171 */
2172
2173 /**
2174 * @brief Writes a data in a specified RTC Backup data register.
2175 * @param RTC_BKP_DR: RTC Backup data Register number.
2176 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 15 to
2177 * specify the register.
2178 * @param Data: Data to be written in the specified RTC Backup data register.
2179 * @retval None
2180 */
2181 void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
2182 {
2183 __IO uint32_t tmp = 0;
2184
2185 /* Check the parameters */
2186 assert_param(IS_RTC_BKP(RTC_BKP_DR));
2187
2188 tmp = RTC_BASE + 0x50;
2189 tmp += (RTC_BKP_DR * 4);
2190
2191 /* Write the specified register */
2192 *(__IO uint32_t *)tmp = (uint32_t)Data;
2193 }
2194
2195 /**
2196 * @brief Reads data from the specified RTC Backup data Register.
2197 * @param RTC_BKP_DR: RTC Backup data Register number.
2198 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 15 to
2199 * specify the register.
2200 * @retval None
2201 */
2202 uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
2203 {
2204 __IO uint32_t tmp = 0;
2205
2206 /* Check the parameters */
2207 assert_param(IS_RTC_BKP(RTC_BKP_DR));
2208
2209 tmp = RTC_BASE + 0x50;
2210 tmp += (RTC_BKP_DR * 4);
2211
2212 /* Read the specified register */
2213 return (*(__IO uint32_t *)tmp);
2214 }
2215
2216 /**
2217 * @}
2218 */
2219
2220 /** @defgroup RTC_Group11 Output Type Config configuration functions
2221 * @brief Output Type Config configuration functions
2222 *
2223 @verbatim
2224 ===============================================================================
2225 ##### Output Type Config configuration functions #####
2226 ===============================================================================
2227
2228 @endverbatim
2229 * @{
2230 */
2231
2232 /**
2233 * @brief Configures the RTC Output Pin mode.
2234 * @param RTC_OutputType: specifies the RTC Output (PC13) pin mode.
2235 * This parameter can be one of the following values:
2236 * @arg RTC_OutputType_OpenDrain: RTC Output (PC13) is configured in
2237 * Open Drain mode.
2238 * @arg RTC_OutputType_PushPull: RTC Output (PC13) is configured in
2239 * Push Pull mode.
2240 * @retval None
2241 */
2242 void RTC_OutputTypeConfig(uint32_t RTC_OutputType)
2243 {
2244 /* Check the parameters */
2245 assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
2246
2247 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);
2248 RTC->TAFCR |= (uint32_t)(RTC_OutputType);
2249 }
2250
2251 /**
2252 * @}
2253 */
2254
2255 /** @defgroup RTC_Group12 Shift control synchronisation functions
2256 * @brief Shift control synchronisation functions
2257 *
2258 @verbatim
2259 ===============================================================================
2260 ##### Shift control synchronisation functions #####
2261 ===============================================================================
2262
2263 @endverbatim
2264 * @{
2265 */
2266
2267 /**
2268 * @brief Configures the Synchronization Shift Control Settings.
2269 * @note When REFCKON is set, firmware must not write to Shift control register
2270 * @param RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar.
2271 * This parameter can be one of the following values :
2272 * @arg RTC_ShiftAdd1S_Set : Add one second to the clock calendar.
2273 * @arg RTC_ShiftAdd1S_Reset: No effect.
2274 * @param RTC_ShiftSubFS: Select the number of Second Fractions to Substitute.
2275 * This parameter can be one any value from 0 to 0x7FFF.
2276 * @retval An ErrorStatus enumeration value:
2277 * - SUCCESS: RTC Shift registers are configured
2278 * - ERROR: RTC Shift registers are not configured
2279 */
2280 ErrorStatus RTC_SynchroShiftConfig(uint32_t RTC_ShiftAdd1S, uint32_t RTC_ShiftSubFS)
2281 {
2282 ErrorStatus status = ERROR;
2283 uint32_t shpfcount = 0;
2284
2285 /* Check the parameters */
2286 assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
2287 assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));
2288
2289 /* Disable the write protection for RTC registers */
2290 RTC->WPR = 0xCA;
2291 RTC->WPR = 0x53;
2292
2293 /* Check if a Shift is pending*/
2294 if ((RTC->ISR & RTC_ISR_SHPF) != RESET)
2295 {
2296 /* Wait until the shift is completed*/
2297 while (((RTC->ISR & RTC_ISR_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
2298 {
2299 shpfcount++;
2300 }
2301 }
2302
2303 /* Check if the Shift pending is completed or if there is no Shift operation at all*/
2304 if ((RTC->ISR & RTC_ISR_SHPF) == RESET)
2305 {
2306 /* check if the reference clock detection is disabled */
2307 if((RTC->CR & RTC_CR_REFCKON) == RESET)
2308 {
2309 /* Configure the Shift settings */
2310 RTC->SHIFTR = (uint32_t)(uint32_t)(RTC_ShiftSubFS) | (uint32_t)(RTC_ShiftAdd1S);
2311
2312 if(RTC_WaitForSynchro() == ERROR)
2313 {
2314 status = ERROR;
2315 }
2316 else
2317 {
2318 status = SUCCESS;
2319 }
2320 }
2321 else
2322 {
2323 status = ERROR;
2324 }
2325 }
2326 else
2327 {
2328 status = ERROR;
2329 }
2330
2331 /* Enable the write protection for RTC registers */
2332 RTC->WPR = 0xFF;
2333
2334 return (ErrorStatus)(status);
2335 }
2336
2337 /**
2338 * @}
2339 */
2340
2341 /** @defgroup RTC_Group13 Interrupts and flags management functions
2342 * @brief Interrupts and flags management functions
2343 *
2344 @verbatim
2345 ===============================================================================
2346 ##### Interrupts and flags management functions #####
2347 ===============================================================================
2348 [..] All RTC interrupts are connected to the EXTI controller.
2349 (+) To enable the RTC Alarm interrupt, the following sequence is required:
2350 (++) Configure and enable the EXTI Line 17 in interrupt mode and select
2351 the rising edge sensitivity using the EXTI_Init() function.
2352 (++) Configure and enable the RTC_Alarm IRQ channel in the NVIC using
2353 the NVIC_Init() function.
2354 (++) Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B)
2355 using the RTC_SetAlarm() and RTC_AlarmCmd() functions.
2356 (+) To enable the RTC Wakeup interrupt, the following sequence is required:
2357 (++) Configure and enable the EXTI Line 20 in interrupt mode and select
2358 the rising edge sensitivity using the EXTI_Init() function.
2359 (++) Configure and enable the RTC_WKUP IRQ channel in the NVIC using
2360 the NVIC_Init() function.
2361 (++) Configure the RTC to generate the RTC wakeup timer event using the
2362 RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd()
2363 functions.
2364 (+) To enable the RTC Tamper interrupt, the following sequence is required:
2365 (++) Configure and enable the EXTI Line 19 in interrupt mode and select
2366 the rising edge sensitivity using the EXTI_Init() function.
2367 (++) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
2368 the NVIC_Init() function.
2369 (++) Configure the RTC to detect the RTC tamper event using the
2370 RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
2371 (+) To enable the RTC TimeStamp interrupt, the following sequence is required:
2372 (++) Configure and enable the EXTI Line 19 in interrupt mode and select
2373 the rising edge sensitivity using the EXTI_Init() function.
2374 (++) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
2375 the NVIC_Init() function.
2376 (++) Configure the RTC to detect the RTC time-stamp event using the
2377 RTC_TimeStampCmd() functions.
2378
2379 @endverbatim
2380 * @{
2381 */
2382
2383 /**
2384 * @brief Enables or disables the specified RTC interrupts.
2385 * @param RTC_IT: specifies the RTC interrupt sources to be enabled or disabled.
2386 * This parameter can be any combination of the following values:
2387 * @arg RTC_IT_TS: Time Stamp interrupt mask
2388 * @arg RTC_IT_WUT: WakeUp Timer interrupt mask
2389 * @arg RTC_IT_ALRB: Alarm B interrupt mask
2390 * @arg RTC_IT_ALRA: Alarm A interrupt mask
2391 * @arg RTC_IT_TAMP: Tamper event interrupt mask
2392 * @param NewState: new state of the specified RTC interrupts.
2393 * This parameter can be: ENABLE or DISABLE.
2394 * @retval None
2395 */
2396 void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
2397 {
2398 /* Check the parameters */
2399 assert_param(IS_RTC_CONFIG_IT(RTC_IT));
2400 assert_param(IS_FUNCTIONAL_STATE(NewState));
2401
2402 /* Disable the write protection for RTC registers */
2403 RTC->WPR = 0xCA;
2404 RTC->WPR = 0x53;
2405
2406 if (NewState != DISABLE)
2407 {
2408 /* Configure the Interrupts in the RTC_CR register */
2409 RTC->CR |= (uint32_t)(RTC_IT & ~RTC_TAFCR_TAMPIE);
2410 /* Configure the Tamper Interrupt in the RTC_TAFCR */
2411 RTC->TAFCR |= (uint32_t)(RTC_IT & RTC_TAFCR_TAMPIE);
2412 }
2413 else
2414 {
2415 /* Configure the Interrupts in the RTC_CR register */
2416 RTC->CR &= (uint32_t)~(RTC_IT & (uint32_t)~RTC_TAFCR_TAMPIE);
2417 /* Configure the Tamper Interrupt in the RTC_TAFCR */
2418 RTC->TAFCR &= (uint32_t)~(RTC_IT & RTC_TAFCR_TAMPIE);
2419 }
2420 /* Enable the write protection for RTC registers */
2421 RTC->WPR = 0xFF;
2422 }
2423
2424 /**
2425 * @brief Checks whether the specified RTC flag is set or not.
2426 * @param RTC_FLAG: specifies the flag to check.
2427 * This parameter can be one of the following values:
2428 * @arg RTC_FLAG_RECALPF: RECALPF event flag
2429 * @arg RTC_FLAG_TAMP3F: Tamper 3 event flag
2430 * @arg RTC_FLAG_TAMP2F: Tamper 2 event flag
2431 * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
2432 * @arg RTC_FLAG_TSOVF: Time Stamp OverFlow flag
2433 * @arg RTC_FLAG_TSF: Time Stamp event flag
2434 * @arg RTC_FLAG_WUTF: WakeUp Timer flag
2435 * @arg RTC_FLAG_ALRBF: Alarm B flag
2436 * @arg RTC_FLAG_ALRAF: Alarm A flag
2437 * @arg RTC_FLAG_INITF: Initialization mode flag
2438 * @arg RTC_FLAG_RSF: Registers Synchronized flag
2439 * @arg RTC_FLAG_INITS: Registers Configured flag
2440 * @argRTC_FLAG_SHPF : Shift operation pending flag.
2441 * @arg RTC_FLAG_WUTWF: WakeUp Timer Write flag
2442 * @arg RTC_FLAG_ALRBWF: Alarm B Write flag
2443 * @arg RTC_FLAG_ALRAWF: Alarm A write flag
2444 * @retval The new state of RTC_FLAG (SET or RESET).
2445 */
2446 FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
2447 {
2448 FlagStatus bitstatus = RESET;
2449 uint32_t tmpreg = 0;
2450
2451 /* Check the parameters */
2452 assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
2453
2454 /* Get all the flags */
2455 tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);
2456
2457 /* Return the status of the flag */
2458 if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)
2459 {
2460 bitstatus = SET;
2461 }
2462 else
2463 {
2464 bitstatus = RESET;
2465 }
2466 return bitstatus;
2467 }
2468
2469 /**
2470 * @brief Clears the RTC's pending flags.
2471 * @param RTC_FLAG: specifies the RTC flag to clear.
2472 * This parameter can be any combination of the following values:
2473 * @arg RTC_FLAG_TAMP3F: Tamper 3 event flag
2474 * @arg RTC_FLAG_TAMP2F: Tamper 2 event flag
2475 * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
2476 * @arg RTC_FLAG_TSOVF: Time Stamp Overflow flag
2477 * @arg RTC_FLAG_TSF: Time Stamp event flag
2478 * @arg RTC_FLAG_WUTF: WakeUp Timer flag
2479 * @arg RTC_FLAG_ALRBF: Alarm B flag
2480 * @arg RTC_FLAG_ALRAF: Alarm A flag
2481 * @arg RTC_FLAG_RSF: Registers Synchronized flag
2482 * @retval None
2483 */
2484 void RTC_ClearFlag(uint32_t RTC_FLAG)
2485 {
2486 /* Check the parameters */
2487 assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
2488
2489 /* Clear the Flags in the RTC_ISR register */
2490 RTC->ISR = (uint32_t)((uint32_t)(~((RTC_FLAG | RTC_ISR_INIT)& 0x0001FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
2491 }
2492
2493 /**
2494 * @brief Checks whether the specified RTC interrupt has occurred or not.
2495 * @param RTC_IT: specifies the RTC interrupt source to check.
2496 * This parameter can be one of the following values:
2497 * @arg RTC_IT_TS: Time Stamp interrupt
2498 * @arg RTC_IT_WUT: WakeUp Timer interrupt
2499 * @arg RTC_IT_ALRB: Alarm B interrupt
2500 * @arg RTC_IT_ALRA: Alarm A interrupt
2501 * @arg RTC_IT_TAMP1: Tamper1 event interrupt
2502 * @arg RTC_IT_TAMP2: Tamper2 event interrupt
2503 * @arg RTC_IT_TAMP3: Tamper3 event interrupt
2504 * @retval The new state of RTC_IT (SET or RESET).
2505 */
2506 ITStatus RTC_GetITStatus(uint32_t RTC_IT)
2507 {
2508 ITStatus bitstatus = RESET;
2509 uint32_t tmpreg = 0, enablestatus = 0;
2510
2511 /* Check the parameters */
2512 assert_param(IS_RTC_GET_IT(RTC_IT));
2513
2514 /* Get the TAMPER Interrupt enable bit and pending bit */
2515 tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));
2516
2517 /* Get the Interrupt enable Status */
2518 enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & ((RTC_IT >> (RTC_IT >> 18)) >> 15)));
2519
2520 /* Get the Interrupt pending bit */
2521 tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));
2522
2523 /* Get the status of the Interrupt */
2524 if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))
2525 {
2526 bitstatus = SET;
2527 }
2528 else
2529 {
2530 bitstatus = RESET;
2531 }
2532 return bitstatus;
2533 }
2534
2535 /**
2536 * @brief Clears the RTC's interrupt pending bits.
2537 * @param RTC_IT: specifies the RTC interrupt pending bit to clear.
2538 * This parameter can be any combination of the following values:
2539 * @arg RTC_IT_TS: Time Stamp interrupt
2540 * @arg RTC_IT_WUT: WakeUp Timer interrupt
2541 * @arg RTC_IT_ALRB: Alarm B interrupt
2542 * @arg RTC_IT_ALRA: Alarm A interrupt
2543 * @arg RTC_IT_TAMP1: Tamper1 event interrupt
2544 * @arg RTC_IT_TAMP2: Tamper2 event interrupt
2545 * @arg RTC_IT_TAMP3: Tamper3 event interrupt
2546 * @retval None
2547 */
2548 void RTC_ClearITPendingBit(uint32_t RTC_IT)
2549 {
2550 uint32_t tmpreg = 0;
2551
2552 /* Check the parameters */
2553 assert_param(IS_RTC_CLEAR_IT(RTC_IT));
2554
2555 /* Get the RTC_ISR Interrupt pending bits mask */
2556 tmpreg = (uint32_t)(RTC_IT >> 4);
2557
2558 /* Clear the interrupt pending bits in the RTC_ISR register */
2559 RTC->ISR = (uint32_t)((uint32_t)(~((tmpreg | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
2560 }
2561
2562 /**
2563 * @}
2564 */
2565
2566 /**
2567 * @brief Converts a 2 digit decimal to BCD format.
2568 * @param Value: Byte to be converted.
2569 * @retval Converted byte
2570 */
2571 static uint8_t RTC_ByteToBcd2(uint8_t Value)
2572 {
2573 uint8_t bcdhigh = 0;
2574
2575 while (Value >= 10)
2576 {
2577 bcdhigh++;
2578 Value -= 10;
2579 }
2580
2581 return ((uint8_t)(bcdhigh << 4) | Value);
2582 }
2583
2584 /**
2585 * @brief Convert from 2 digit BCD to Binary.
2586 * @param Value: BCD value to be converted.
2587 * @retval Converted word
2588 */
2589 static uint8_t RTC_Bcd2ToByte(uint8_t Value)
2590 {
2591 uint8_t tmp = 0;
2592 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
2593 return (tmp + (Value & (uint8_t)0x0F));
2594 }
2595
2596 /**
2597 * @}
2598 */
2599
2600 /**
2601 * @}
2602 */
2603
2604 /**
2605 * @}
2606 */
2607
2608 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum