]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_rtc.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_rtc.c
1 /**
2 ******************************************************************************
3 * @file stm32f3xx_hal_rtc.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 12-Sept-2014
7 * @brief RTC HAL module driver.
8 *
9 * This file provides firmware functions to manage the following
10 * functionalities of the Real-Time Clock (RTC) peripheral:
11 * - Initialization
12 * - Calendar (Time and Date) configuration
13 * - Alarms (Alarm A and Alarm B) configuration
14 * - WakeUp Timer configuration
15 * - TimeStamp configuration
16 * - Tampers configuration
17 * - Backup Data Registers configuration
18 * - RTC Tamper and TimeStamp Pins Selection
19 * - Interrupts and flags management
20 *
21 @verbatim
22
23 ===============================================================================
24 ##### RTC Operating Condition #####
25 ===============================================================================
26 [..] The real-time clock (RTC) and the RTC backup registers can be powered
27 from the VBAT voltage when the main VDD supply is powered off.
28 To retain the content of the RTC backup registers and supply the RTC
29 when VDD is turned off, VBAT pin can be connected to an optional
30 standby voltage supplied by a battery or by another source.
31
32 [..] To allow the RTC to operate even when the main digital supply (VDD)
33 is turned off, the VBAT pin powers the following blocks:
34 (#) The RTC
35 (#) The LSE oscillator
36 (#) PC13 to PC15 I/Os (when available)
37
38 [..] When the backup domain is supplied by VDD (analog switch connected
39 to VDD), the following functions are available:
40 (#) PC14 and PC15 can be used as either GPIO or LSE pins
41 (#) PC13 can be used as a GPIO or as the RTC_AF pin
42
43 [..] When the backup domain is supplied by VBAT (analog switch connected
44 to VBAT because VDD is not present), the following functions are available:
45 (#) PC14 and PC15 can be used as LSE pins only
46 (#) PC13 can be used as the RTC_AF pin
47
48 ##### Backup Domain Reset #####
49 ===============================================================================
50 [..] The backup domain reset sets all RTC registers and the RCC_BDCR
51 register to their reset values.
52 A backup domain reset is generated when one of the following events
53 occurs:
54 (#) Software reset, triggered by setting the BDRST bit in the
55 RCC Backup domain control register (RCC_BDCR).
56 (#) VDD or VBAT power on, if both supplies have previously been
57 powered off.
58
59 ##### Backup Domain Access #####
60 ===================================================================
61 [..] After reset, the backup domain (RTC registers, RTC backup data
62 registers and backup SRAM) is protected against possible unwanted write
63 accesses.
64
65 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
66 (#) Enable the Power Controller (PWR) APB1 interface clock using the
67 __PWR_CLK_ENABLE() function.
68 (#) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
69 (#) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
70 (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
71
72
73 ##### How to use RTC Driver #####
74 ===================================================================
75 [..]
76 (+) Enable the RTC domain access (see description in the section above).
77 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
78 format using the HAL_RTC_Init() function.
79
80 *** Time and Date configuration ***
81 ===================================
82 [..]
83 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
84 and HAL_RTC_SetDate() functions.
85 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
86
87 *** Alarm configuration ***
88 ===========================
89 [..]
90 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
91 You can also configure the RTC Alarm with interrupt mode using the
92 HAL_RTC_SetAlarm_IT() function.
93 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
94
95 *** RTC Wakeup configuration ***
96 ================================
97 [..]
98 (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
99 function. You can also configure the RTC Wakeup timer with interrupt mode
100 using the HAL_RTC_SetWakeUpTimer_IT() function.
101 (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer()
102 function.
103
104 *** TimeStamp configuration ***
105 ===============================
106 [..]
107 (+) Configure the RTC_AF trigger and enables the RTC TimeStamp using the
108 HAL_RTC_SetTimeStamp() function. You can also configure the RTC TimeStamp with
109 interrupt mode using the HAL_RTC_SetTimeStamp_IT() function.
110 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
111 function.
112
113 *** Tamper configuration ***
114 ============================
115 [..]
116 (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge
117 or Level according to the Tamper filter (if equal to 0 Edge else Level)
118 value, sampling frequency, precharge or discharge and Pull-UP using the
119 HAL_RTC_SetTamper() function. You can configure RTC Tamper with interrupt
120 mode using HAL_RTC_SetTamper_IT() function.
121
122 *** Backup Data Registers configuration ***
123 ===========================================
124 [..]
125 (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
126 function.
127 (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
128 function.
129
130
131 ##### RTC and low power modes #####
132 ===================================================================
133 [..] The MCU can be woken up from a low power mode by an RTC alternate
134 function.
135 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
136 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
137 These RTC alternate functions can wake up the system from the Stop and
138 Standby low power modes.
139 [..] The system can also wake up from low power modes without depending
140 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
141 or the RTC wakeup events.
142 [..] The RTC provides a programmable time base for waking up from the
143 Stop or Standby mode at regular intervals.
144 Wakeup from STOP and Standby modes is possible only when the RTC clock source
145 is LSE or LSI.
146
147 @endverbatim
148
149 ******************************************************************************
150 * @attention
151 *
152 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
153 *
154 * Redistribution and use in source and binary forms, with or without modification,
155 * are permitted provided that the following conditions are met:
156 * 1. Redistributions of source code must retain the above copyright notice,
157 * this list of conditions and the following disclaimer.
158 * 2. Redistributions in binary form must reproduce the above copyright notice,
159 * this list of conditions and the following disclaimer in the documentation
160 * and/or other materials provided with the distribution.
161 * 3. Neither the name of STMicroelectronics nor the names of its contributors
162 * may be used to endorse or promote products derived from this software
163 * without specific prior written permission.
164 *
165 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
166 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
168 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
169 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
170 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
171 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
172 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
173 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
174 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175 *
176 ******************************************************************************
177 */
178
179 /* Includes ------------------------------------------------------------------*/
180 #include "stm32f3xx_hal.h"
181
182 /** @addtogroup STM32F3xx_HAL_Driver
183 * @{
184 */
185
186 /** @defgroup RTC RTC HAL module driver
187 * @brief RTC HAL module driver
188 * @{
189 */
190
191 #ifdef HAL_RTC_MODULE_ENABLED
192
193 /* Private typedef -----------------------------------------------------------*/
194 /* Private define ------------------------------------------------------------*/
195 /* Private macro -------------------------------------------------------------*/
196 /* Private variables ---------------------------------------------------------*/
197 /* Private function prototypes -----------------------------------------------*/
198 /* Exported functions ---------------------------------------------------------*/
199
200 /** @defgroup RTC_Exported_Functions RTC Exported Functions
201 * @{
202 */
203
204 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
205 * @brief Initialization and Configuration functions
206 *
207 @verbatim
208 ===============================================================================
209 ##### Initialization and de-initialization functions #####
210 ===============================================================================
211 [..] This section provide functions allowing to initialize and configure the
212 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
213 RTC registers Write protection, enter and exit the RTC initialization mode,
214 RTC registers synchronization check and reference clock detection enable.
215 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
216 It is split into 2 programmable prescalers to minimize power consumption.
217 (++) A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
218 (++) When both prescalers are used, it is recommended to configure the
219 asynchronous prescaler to a high value to minimize consumption.
220 (#) All RTC registers are Write protected. Writing to the RTC registers
221 is enabled by writing a key into the Write Protection register, RTC_WPR.
222 (#) To Configure the RTC Calendar, user application should enter
223 initialization mode. In this mode, the calendar counter is stopped
224 and its value can be updated. When the initialization sequence is
225 complete, the calendar restarts counting after 4 RTCCLK cycles.
226 (#) To read the calendar through the shadow registers after Calendar
227 initialization, calendar update or after wakeup from low power modes
228 the software must first clear the RSF flag. The software must then
229 wait until it is set again before reading the calendar, which means
230 that the calendar registers have been correctly copied into the
231 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
232 implements the above software sequence (RSF clear and RSF check).
233
234 @endverbatim
235 * @{
236 */
237
238 /**
239 * @brief Initializes the RTC peripheral
240 * @param hrtc: RTC handle
241 * @retval HAL status
242 */
243 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
244 {
245 /* Check the RTC peripheral state */
246 if(hrtc == HAL_NULL)
247 {
248 return HAL_ERROR;
249 }
250
251 /* Check the parameters */
252 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
253 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
254 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
255 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
256 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
257 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
258 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
259
260 if(hrtc->State == HAL_RTC_STATE_RESET)
261 {
262 /* Initialize RTC MSP */
263 HAL_RTC_MspInit(hrtc);
264 }
265
266 /* Set RTC state */
267 hrtc->State = HAL_RTC_STATE_BUSY;
268
269 /* Disable the write protection for RTC registers */
270 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
271
272 /* Set Initialization mode */
273 if(RTC_EnterInitMode(hrtc) != HAL_OK)
274 {
275 /* Enable the write protection for RTC registers */
276 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
277
278 /* Set RTC state */
279 hrtc->State = HAL_RTC_STATE_ERROR;
280
281 return HAL_ERROR;
282 }
283 else
284 {
285 /* Clear RTC_CR FMT, OSEL and POL Bits */
286 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
287 /* Set RTC_CR register */
288 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
289
290 /* Configure the RTC PRER */
291 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
292 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
293
294 /* Exit Initialization mode */
295 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
296
297 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
298 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
299
300 /* Enable the write protection for RTC registers */
301 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
302
303 /* Set RTC state */
304 hrtc->State = HAL_RTC_STATE_READY;
305
306 return HAL_OK;
307 }
308 }
309
310 /**
311 * @brief DeInitializes the RTC peripheral
312 * @param hrtc: RTC handle
313 * @note This function doesn't reset the RTC Backup Data registers.
314 * @retval HAL status
315 */
316 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
317 {
318 uint32_t tickstart = 0;
319
320 /* Check the parameters */
321 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
322
323 /* Set RTC state */
324 hrtc->State = HAL_RTC_STATE_BUSY;
325
326 /* Disable the write protection for RTC registers */
327 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
328
329 /* Set Initialization mode */
330 if(RTC_EnterInitMode(hrtc) != HAL_OK)
331 {
332 /* Enable the write protection for RTC registers */
333 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
334
335 /* Set RTC state */
336 hrtc->State = HAL_RTC_STATE_ERROR;
337
338 return HAL_ERROR;
339 }
340 else
341 {
342 /* Reset TR, DR and CR registers */
343 hrtc->Instance->TR = (uint32_t)0x00000000;
344 hrtc->Instance->DR = (uint32_t)0x00002101;
345 /* Reset All CR bits except CR[2:0] */
346 hrtc->Instance->CR &= (uint32_t)0x00000007;
347
348 tickstart = HAL_GetTick();
349
350 /* Wait till WUTWF flag is set and if Time out is reached exit */
351 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
352 {
353 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
354 {
355 /* Enable the write protection for RTC registers */
356 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
357
358 /* Set RTC state */
359 hrtc->State = HAL_RTC_STATE_TIMEOUT;
360
361 return HAL_TIMEOUT;
362 }
363 }
364
365 /* Reset all RTC CR register bits */
366 hrtc->Instance->CR &= (uint32_t)0x00000000;
367 hrtc->Instance->WUTR = (uint32_t)0x0000FFFF;
368 hrtc->Instance->PRER = (uint32_t)0x007F00FF;
369 hrtc->Instance->ALRMAR = (uint32_t)0x00000000;
370 hrtc->Instance->ALRMBR = (uint32_t)0x00000000;
371 hrtc->Instance->SHIFTR = (uint32_t)0x00000000;
372 hrtc->Instance->CALR = (uint32_t)0x00000000;
373 hrtc->Instance->ALRMASSR = (uint32_t)0x00000000;
374 hrtc->Instance->ALRMBSSR = (uint32_t)0x00000000;
375
376 /* Reset ISR register and exit initialization mode */
377 hrtc->Instance->ISR = (uint32_t)0x00000000;
378
379 /* Reset Tamper and alternate functions configuration register */
380 hrtc->Instance->TAFCR = 0x00000000;
381
382 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
383 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
384 {
385 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
386 {
387 /* Enable the write protection for RTC registers */
388 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
389
390 hrtc->State = HAL_RTC_STATE_ERROR;
391
392 return HAL_ERROR;
393 }
394 }
395 }
396
397 /* Enable the write protection for RTC registers */
398 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
399
400 /* De-Initialize RTC MSP */
401 HAL_RTC_MspDeInit(hrtc);
402
403 hrtc->State = HAL_RTC_STATE_RESET;
404
405 /* Release Lock */
406 __HAL_UNLOCK(hrtc);
407
408 return HAL_OK;
409 }
410
411 /**
412 * @brief Initializes the RTC MSP.
413 * @param hrtc: RTC handle
414 * @retval None
415 */
416 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
417 {
418 /* NOTE : This function Should not be modified, when the callback is needed,
419 the HAL_RTC_MspInit could be implenetd in the user file
420 */
421 }
422
423 /**
424 * @brief DeInitializes the RTC MSP.
425 * @param hrtc: RTC handle
426 * @retval None
427 */
428 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
429 {
430 /* NOTE : This function Should not be modified, when the callback is needed,
431 the HAL_RTC_MspDeInit could be implenetd in the user file
432 */
433 }
434
435 /**
436 * @}
437 */
438
439 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
440 * @brief RTC Time and Date functions
441 *
442 @verbatim
443 ===============================================================================
444 ##### RTC Time and Date functions #####
445 ===============================================================================
446
447 [..] This section provide functions allowing to configure Time and Date features
448
449 @endverbatim
450 * @{
451 */
452
453 /**
454 * @brief Sets RTC current time.
455 * @param hrtc: RTC handle
456 * @param sTime: Pointer to Time structure
457 * @param Format: Specifies the format of the entered parameters.
458 * This parameter can be one of the following values:
459 * @arg Format_BIN: Binary data format
460 * @arg Format_BCD: BCD data format
461 * @retval HAL status
462 */
463 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
464 {
465 uint32_t tmpreg = 0;
466
467 /* Check the parameters */
468 assert_param(IS_RTC_FORMAT(Format));
469 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
470 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
471
472 /* Process Locked */
473 __HAL_LOCK(hrtc);
474
475 hrtc->State = HAL_RTC_STATE_BUSY;
476
477 if(Format == FORMAT_BIN)
478 {
479 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
480 {
481 assert_param(IS_RTC_HOUR12(sTime->Hours));
482 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
483 }
484 else
485 {
486 sTime->TimeFormat = 0x00;
487 assert_param(IS_RTC_HOUR24(sTime->Hours));
488 }
489 assert_param(IS_RTC_MINUTES(sTime->Minutes));
490 assert_param(IS_RTC_SECONDS(sTime->Seconds));
491
492 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
493 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
494 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
495 (((uint32_t)sTime->TimeFormat) << 16));
496 }
497 else
498 {
499 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
500 {
501 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
502 assert_param(IS_RTC_HOUR12(tmpreg));
503 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
504 }
505 else
506 {
507 sTime->TimeFormat = 0x00;
508 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
509 }
510 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
511 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
512 tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
513 ((uint32_t)(sTime->Minutes) << 8) | \
514 ((uint32_t)sTime->Seconds) | \
515 ((uint32_t)(sTime->TimeFormat) << 16));
516 }
517
518 /* Disable the write protection for RTC registers */
519 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
520
521 /* Set Initialization mode */
522 if(RTC_EnterInitMode(hrtc) != HAL_OK)
523 {
524 /* Enable the write protection for RTC registers */
525 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
526
527 /* Set RTC state */
528 hrtc->State = HAL_RTC_STATE_ERROR;
529
530 /* Process Unlocked */
531 __HAL_UNLOCK(hrtc);
532
533 return HAL_ERROR;
534 }
535 else
536 {
537 /* Set the RTC_TR register */
538 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
539
540 /* Clear the bits to be configured */
541 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
542
543 /* Configure the RTC_CR register */
544 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
545
546 /* Exit Initialization mode */
547 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
548
549 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
550 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
551 {
552 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
553 {
554 /* Enable the write protection for RTC registers */
555 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
556
557 hrtc->State = HAL_RTC_STATE_ERROR;
558
559 /* Process Unlocked */
560 __HAL_UNLOCK(hrtc);
561
562 return HAL_ERROR;
563 }
564 }
565
566 /* Enable the write protection for RTC registers */
567 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
568
569 hrtc->State = HAL_RTC_STATE_READY;
570
571 __HAL_UNLOCK(hrtc);
572
573 return HAL_OK;
574 }
575 }
576
577 /**
578 * @brief Gets RTC current time.
579 * @param hrtc: RTC handle
580 * @param sTime: Pointer to Time structure
581 * @param Format: Specifies the format of the entered parameters.
582 * This parameter can be one of the following values:
583 * @arg Format_BIN: Binary data format
584 * @arg Format_BCD: BCD data format
585 * @note Call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
586 * in the higher-order calendar shadow registers.
587 * @retval HAL status
588 */
589 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
590 {
591 uint32_t tmpreg = 0;
592
593 /* Check the parameters */
594 assert_param(IS_RTC_FORMAT(Format));
595
596 /* Get subseconds values from the correspondent registers*/
597 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
598
599 /* Get the TR register */
600 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
601
602 /* Fill the structure fields with the read parameters */
603 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
604 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
605 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
606 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
607
608 /* Check the input parameters format */
609 if(Format == FORMAT_BIN)
610 {
611 /* Convert the time structure parameters to Binary format */
612 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
613 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
614 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
615 }
616
617 return HAL_OK;
618 }
619
620 /**
621 * @brief Sets RTC current date.
622 * @param hrtc: RTC handle
623 * @param sDate: Pointer to date structure
624 * @param Format: specifies the format of the entered parameters.
625 * This parameter can be one of the following values:
626 * @arg Format_BIN: Binary data format
627 * @arg Format_BCD: BCD data format
628 * @retval HAL status
629 */
630 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
631 {
632 uint32_t datetmpreg = 0;
633
634 /* Check the parameters */
635 assert_param(IS_RTC_FORMAT(Format));
636
637 /* Process Locked */
638 __HAL_LOCK(hrtc);
639
640 hrtc->State = HAL_RTC_STATE_BUSY;
641
642 if((Format == FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
643 {
644 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
645 }
646
647 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
648
649 if(Format == FORMAT_BIN)
650 {
651 assert_param(IS_RTC_YEAR(sDate->Year));
652 assert_param(IS_RTC_MONTH(sDate->Month));
653 assert_param(IS_RTC_DATE(sDate->Date));
654
655 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
656 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
657 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
658 ((uint32_t)sDate->WeekDay << 13));
659 }
660 else
661 {
662 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
663 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
664 assert_param(IS_RTC_MONTH(datetmpreg));
665 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
666 assert_param(IS_RTC_DATE(datetmpreg));
667
668 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
669 (((uint32_t)sDate->Month) << 8) | \
670 ((uint32_t)sDate->Date) | \
671 (((uint32_t)sDate->WeekDay) << 13));
672 }
673
674 /* Disable the write protection for RTC registers */
675 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
676
677 /* Set Initialization mode */
678 if(RTC_EnterInitMode(hrtc) != HAL_OK)
679 {
680 /* Enable the write protection for RTC registers */
681 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
682
683 /* Set RTC state*/
684 hrtc->State = HAL_RTC_STATE_ERROR;
685
686 /* Process Unlocked */
687 __HAL_UNLOCK(hrtc);
688
689 return HAL_ERROR;
690 }
691 else
692 {
693 /* Set the RTC_DR register */
694 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
695
696 /* Exit Initialization mode */
697 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
698
699 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
700 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
701 {
702 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
703 {
704 /* Enable the write protection for RTC registers */
705 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
706
707 hrtc->State = HAL_RTC_STATE_ERROR;
708
709 /* Process Unlocked */
710 __HAL_UNLOCK(hrtc);
711
712 return HAL_ERROR;
713 }
714 }
715
716 /* Enable the write protection for RTC registers */
717 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
718
719 hrtc->State = HAL_RTC_STATE_READY ;
720
721 /* Process Unlocked */
722 __HAL_UNLOCK(hrtc);
723
724 return HAL_OK;
725 }
726 }
727
728 /**
729 * @brief Gets RTC current date.
730 * @param hrtc: RTC handle
731 * @param sDate: Pointer to Date structure
732 * @param Format: Specifies the format of the entered parameters.
733 * This parameter can be one of the following values:
734 * @arg Format_BIN : Binary data format
735 * @arg Format_BCD : BCD data format
736 * @retval HAL status
737 */
738 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
739 {
740 uint32_t datetmpreg = 0;
741
742 /* Check the parameters */
743 assert_param(IS_RTC_FORMAT(Format));
744
745 /* Get the DR register */
746 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
747
748 /* Fill the structure fields with the read parameters */
749 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
750 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
751 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
752 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
753
754 /* Check the input parameters format */
755 if(Format == FORMAT_BIN)
756 {
757 /* Convert the date structure parameters to Binary format */
758 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
759 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
760 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
761 }
762 return HAL_OK;
763 }
764
765 /**
766 * @}
767 */
768
769 /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
770 * @brief RTC Alarm functions
771 *
772 @verbatim
773 ===============================================================================
774 ##### RTC Alarm functions #####
775 ===============================================================================
776
777 [..] This section provide functions allowing to configure Alarm feature
778
779 @endverbatim
780 * @{
781 */
782 /**
783 * @brief Sets the specified RTC Alarm.
784 * @param hrtc: RTC handle
785 * @param sAlarm: Pointer to Alarm structure
786 * @param Format: Specifies the format of the entered parameters.
787 * This parameter can be one of the following values:
788 * @arg Format_BIN: Binary data format
789 * @arg Format_BCD: BCD data format
790 * @retval HAL status
791 */
792 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
793 {
794 uint32_t tickstart = 0;
795 uint32_t tmpreg = 0, subsecondtmpreg = 0;
796
797 /* Check the parameters */
798 assert_param(IS_RTC_FORMAT(Format));
799 assert_param(IS_ALARM(sAlarm->Alarm));
800 assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
801 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
802 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
803 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
804
805 /* Process Locked */
806 __HAL_LOCK(hrtc);
807
808 hrtc->State = HAL_RTC_STATE_BUSY;
809
810 if(Format == FORMAT_BIN)
811 {
812 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
813 {
814 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
815 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
816 }
817 else
818 {
819 sAlarm->AlarmTime.TimeFormat = 0x00;
820 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
821 }
822 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
823 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
824
825 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
826 {
827 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
828 }
829 else
830 {
831 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
832 }
833
834 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
835 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
836 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
837 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
838 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
839 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
840 ((uint32_t)sAlarm->AlarmMask));
841 }
842 else
843 {
844 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
845 {
846 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
847 assert_param(IS_RTC_HOUR12(tmpreg));
848 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
849 }
850 else
851 {
852 sAlarm->AlarmTime.TimeFormat = 0x00;
853 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
854 }
855
856 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
857 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
858
859 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
860 {
861 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
862 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
863 }
864 else
865 {
866 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
867 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
868 }
869
870 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
871 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
872 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
873 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
874 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
875 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
876 ((uint32_t)sAlarm->AlarmMask));
877 }
878
879 /* Configure the Alarm A or Alarm B Sub Second registers */
880 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
881
882 /* Disable the write protection for RTC registers */
883 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
884
885 /* Configure the Alarm register */
886 if(sAlarm->Alarm == RTC_ALARM_A)
887 {
888 /* Disable the Alarm A interrupt */
889 __HAL_RTC_ALARMA_DISABLE(hrtc);
890
891 /* In case of interrupt mode is used, the interrupt source must disabled */
892 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
893
894 tickstart = HAL_GetTick();
895 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
896 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
897 {
898 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
899 {
900 /* Enable the write protection for RTC registers */
901 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
902
903 hrtc->State = HAL_RTC_STATE_TIMEOUT;
904
905 /* Process Unlocked */
906 __HAL_UNLOCK(hrtc);
907
908 return HAL_TIMEOUT;
909 }
910 }
911
912 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
913 /* Configure the Alarm A Sub Second register */
914 hrtc->Instance->ALRMASSR = subsecondtmpreg;
915 /* Configure the Alarm state: Enable Alarm */
916 __HAL_RTC_ALARMA_ENABLE(hrtc);
917 }
918 else
919 {
920 /* Disable the Alarm B interrupt */
921 __HAL_RTC_ALARMB_DISABLE(hrtc);
922
923 /* In case of interrupt mode is used, the interrupt source must disabled */
924 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
925
926 tickstart = HAL_GetTick();
927 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
928 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
929 {
930 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
931 {
932 /* Enable the write protection for RTC registers */
933 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
934
935 hrtc->State = HAL_RTC_STATE_TIMEOUT;
936
937 /* Process Unlocked */
938 __HAL_UNLOCK(hrtc);
939
940 return HAL_TIMEOUT;
941 }
942 }
943
944 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
945 /* Configure the Alarm B Sub Second register */
946 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
947 /* Configure the Alarm state: Enable Alarm */
948 __HAL_RTC_ALARMB_ENABLE(hrtc);
949 }
950
951 /* Enable the write protection for RTC registers */
952 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
953
954 /* Change RTC state */
955 hrtc->State = HAL_RTC_STATE_READY;
956
957 /* Process Unlocked */
958 __HAL_UNLOCK(hrtc);
959
960 return HAL_OK;
961 }
962
963 /**
964 * @brief Sets the specified RTC Alarm with Interrupt
965 * @param hrtc: RTC handle
966 * @param sAlarm: Pointer to Alarm structure
967 * @param Format: Specifies the format of the entered parameters.
968 * This parameter can be one of the following values:
969 * @arg Format_BIN: Binary data format
970 * @arg Format_BCD: BCD data format
971 * @note The Alarm register can only be written when the corresponding Alarm
972 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
973 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
974 * @retval HAL status
975 */
976 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
977 {
978 uint32_t tickstart = 0;
979 uint32_t tmpreg = 0, subsecondtmpreg = 0;
980
981 /* Check the parameters */
982 assert_param(IS_RTC_FORMAT(Format));
983 assert_param(IS_ALARM(sAlarm->Alarm));
984 assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
985 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
986 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
987 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
988
989 /* Process Locked */
990 __HAL_LOCK(hrtc);
991
992 hrtc->State = HAL_RTC_STATE_BUSY;
993
994 if(Format == FORMAT_BIN)
995 {
996 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
997 {
998 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
999 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1000 }
1001 else
1002 {
1003 sAlarm->AlarmTime.TimeFormat = 0x00;
1004 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1005 }
1006 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1007 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1008
1009 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1010 {
1011 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1012 }
1013 else
1014 {
1015 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1016 }
1017 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
1018 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
1019 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1020 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1021 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
1022 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1023 ((uint32_t)sAlarm->AlarmMask));
1024 }
1025 else
1026 {
1027 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1028 {
1029 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1030 assert_param(IS_RTC_HOUR12(tmpreg));
1031 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1032 }
1033 else
1034 {
1035 sAlarm->AlarmTime.TimeFormat = 0x00;
1036 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1037 }
1038
1039 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1040 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1041
1042 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1043 {
1044 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1045 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1046 }
1047 else
1048 {
1049 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1050 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1051 }
1052 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
1053 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
1054 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1055 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1056 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
1057 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1058 ((uint32_t)sAlarm->AlarmMask));
1059 }
1060 /* Configure the Alarm A or Alarm B Sub Second registers */
1061 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1062
1063 /* Disable the write protection for RTC registers */
1064 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1065
1066 /* Configure the Alarm register */
1067 if(sAlarm->Alarm == RTC_ALARM_A)
1068 {
1069 /* Disable the Alarm A interrupt */
1070 __HAL_RTC_ALARMA_DISABLE(hrtc);
1071
1072 /* Clear flag alarm A */
1073 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1074
1075 tickstart = HAL_GetTick();
1076 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1077 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1078 {
1079 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1080 {
1081 /* Enable the write protection for RTC registers */
1082 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1083
1084 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1085
1086 /* Process Unlocked */
1087 __HAL_UNLOCK(hrtc);
1088
1089 return HAL_TIMEOUT;
1090 }
1091 }
1092
1093 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1094 /* Configure the Alarm A Sub Second register */
1095 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1096 /* Configure the Alarm state: Enable Alarm */
1097 __HAL_RTC_ALARMA_ENABLE(hrtc);
1098 /* Configure the Alarm interrupt */
1099 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1100 }
1101 else
1102 {
1103 /* Disable the Alarm B interrupt */
1104 __HAL_RTC_ALARMB_DISABLE(hrtc);
1105
1106 /* Clear flag alarm B */
1107 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1108
1109 tickstart = HAL_GetTick();
1110 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1111 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1112 {
1113 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1114 {
1115 /* Enable the write protection for RTC registers */
1116 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1117
1118 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1119
1120 /* Process Unlocked */
1121 __HAL_UNLOCK(hrtc);
1122
1123 return HAL_TIMEOUT;
1124 }
1125 }
1126
1127 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1128 /* Configure the Alarm B Sub Second register */
1129 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1130 /* Configure the Alarm state: Enable Alarm */
1131 __HAL_RTC_ALARMB_ENABLE(hrtc);
1132 /* Configure the Alarm interrupt */
1133 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1134 }
1135
1136 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1137 __HAL_RTC_ENABLE_IT(RTC_EXTI_LINE_ALARM_EVENT);
1138
1139 EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
1140
1141 /* Enable the write protection for RTC registers */
1142 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1143
1144 hrtc->State = HAL_RTC_STATE_READY;
1145
1146 /* Process Unlocked */
1147 __HAL_UNLOCK(hrtc);
1148
1149 return HAL_OK;
1150 }
1151
1152 /**
1153 * @brief Deactive the specified RTC Alarm
1154 * @param hrtc: RTC handle
1155 * @param Alarm: Specifies the Alarm.
1156 * This parameter can be one of the following values:
1157 * @arg ALARM_A : AlarmA
1158 * @arg ALARM_B : AlarmB
1159 * @retval HAL status
1160 */
1161 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1162 {
1163 uint32_t tickstart = 0;
1164
1165 /* Check the parameters */
1166 assert_param(IS_ALARM(Alarm));
1167
1168 /* Process Locked */
1169 __HAL_LOCK(hrtc);
1170
1171 hrtc->State = HAL_RTC_STATE_BUSY;
1172
1173 /* Disable the write protection for RTC registers */
1174 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1175
1176 if(Alarm == RTC_ALARM_A)
1177 {
1178 /* AlarmA */
1179 __HAL_RTC_ALARMA_DISABLE(hrtc);
1180
1181 /* In case of interrupt mode is used, the interrupt source must disabled */
1182 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1183
1184 tickstart = HAL_GetTick();
1185
1186 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1187 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1188 {
1189 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1190 {
1191 /* Enable the write protection for RTC registers */
1192 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1193
1194 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1195
1196 /* Process Unlocked */
1197 __HAL_UNLOCK(hrtc);
1198
1199 return HAL_TIMEOUT;
1200 }
1201 }
1202 }
1203 else
1204 {
1205 /* AlarmB */
1206 __HAL_RTC_ALARMB_DISABLE(hrtc);
1207
1208 /* In case of interrupt mode is used, the interrupt source must disabled */
1209 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
1210
1211 tickstart = HAL_GetTick();
1212
1213 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1214 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1215 {
1216 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1217 {
1218 /* Enable the write protection for RTC registers */
1219 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1220
1221 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1222
1223 /* Process Unlocked */
1224 __HAL_UNLOCK(hrtc);
1225
1226 return HAL_TIMEOUT;
1227 }
1228 }
1229 }
1230 /* Enable the write protection for RTC registers */
1231 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1232
1233 hrtc->State = HAL_RTC_STATE_READY;
1234
1235 /* Process Unlocked */
1236 __HAL_UNLOCK(hrtc);
1237
1238 return HAL_OK;
1239 }
1240
1241 /**
1242 * @brief Gets the RTC Alarm value and masks.
1243 * @param hrtc: RTC handle
1244 * @param sAlarm: Pointer to Date structure
1245 * @param Alarm: Specifies the Alarm
1246 * This parameter can be one of the following values:
1247 * @arg ALARM_A: AlarmA
1248 * @arg ALARM_B: AlarmB
1249 * @param Format: Specifies the format of the entered parameters.
1250 * This parameter can be one of the following values:
1251 * @arg Format_BIN: Binary data format
1252 * @arg Format_BCD: BCD data format
1253 * @retval HAL status
1254 */
1255 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1256 {
1257 uint32_t tmpreg = 0, subsecondtmpreg = 0;
1258
1259 /* Check the parameters */
1260 assert_param(IS_RTC_FORMAT(Format));
1261 assert_param(IS_ALARM(Alarm));
1262
1263 if(Alarm == RTC_ALARM_A)
1264 {
1265 /* AlarmA */
1266 sAlarm->Alarm = RTC_ALARM_A;
1267
1268 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1269 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
1270 }
1271 else
1272 {
1273 sAlarm->Alarm = RTC_ALARM_B;
1274
1275 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1276 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1277 }
1278
1279 /* Fill the structure with the read parameters */
1280 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
1281 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
1282 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1283 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1284 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1285 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1286 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1287 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1288
1289 if(Format == FORMAT_BIN)
1290 {
1291 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1292 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1293 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1294 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1295 }
1296
1297 return HAL_OK;
1298 }
1299
1300 /**
1301 * @brief This function handles Alarm interrupt request.
1302 * @param hrtc: RTC handle
1303 * @retval None
1304 */
1305 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1306 {
1307 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
1308 {
1309 /* Get the status of the Interrupt */
1310 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
1311 {
1312 /* AlarmA callback */
1313 HAL_RTC_AlarmAEventCallback(hrtc);
1314
1315 /* Clear the Alarm interrupt pending bit */
1316 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
1317 }
1318 }
1319
1320 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
1321 {
1322 /* Get the status of the Interrupt */
1323 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
1324 {
1325 /* AlarmB callback */
1326 HAL_RTCEx_AlarmBEventCallback(hrtc);
1327
1328 /* Clear the Alarm interrupt pending bit */
1329 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
1330 }
1331 }
1332
1333 /* Clear the EXTI's line Flag for RTC Alarm */
1334 __HAL_RTC_CLEAR_FLAG(RTC_EXTI_LINE_ALARM_EVENT);
1335
1336 /* Change RTC state */
1337 hrtc->State = HAL_RTC_STATE_READY;
1338 }
1339
1340 /**
1341 * @brief Alarm A callback.
1342 * @param hrtc: RTC handle
1343 * @retval None
1344 */
1345 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1346 {
1347 /* NOTE : This function Should not be modified, when the callback is needed,
1348 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1349 */
1350 }
1351
1352 /**
1353 * @brief This function handles AlarmA Polling request.
1354 * @param hrtc: RTC handle
1355 * @param Timeout: Timeout duration
1356 * @retval HAL status
1357 */
1358 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1359 {
1360
1361 uint32_t tickstart = HAL_GetTick();
1362
1363 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1364 {
1365 if(Timeout != HAL_MAX_DELAY)
1366 {
1367 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1368 {
1369 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1370 return HAL_TIMEOUT;
1371 }
1372 }
1373 }
1374
1375 /* Clear the Alarm interrupt pending bit */
1376 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1377
1378 /* Change RTC state */
1379 hrtc->State = HAL_RTC_STATE_READY;
1380
1381 return HAL_OK;
1382 }
1383
1384 /**
1385 * @}
1386 */
1387
1388 /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions
1389 * @brief Peripheral Control functions
1390 *
1391 @verbatim
1392 ===============================================================================
1393 ##### Peripheral Control functions #####
1394 ===============================================================================
1395 [..]
1396 This subsection provides functions allowing to
1397 (+) Wait for RTC Time and Date Synchronization
1398
1399 @endverbatim
1400 * @{
1401 */
1402
1403 /**
1404 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1405 * synchronized with RTC APB clock.
1406 * @note The RTC Resynchronization mode is write protected, use the
1407 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1408 * @note To read the calendar through the shadow registers after Calendar
1409 * initialization, calendar update or after wakeup from low power modes
1410 * the software must first clear the RSF flag.
1411 * The software must then wait until it is set again before reading
1412 * the calendar, which means that the calendar registers have been
1413 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1414 * @param hrtc: RTC handle
1415 * @retval HAL status
1416 */
1417 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1418 {
1419 uint32_t tickstart = 0;
1420
1421 /* Clear RSF flag */
1422 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1423
1424 tickstart = HAL_GetTick();
1425
1426 /* Wait the registers to be synchronised */
1427 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1428 {
1429 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1430 {
1431 return HAL_TIMEOUT;
1432 }
1433 }
1434
1435 return HAL_OK;
1436 }
1437
1438 /**
1439 * @}
1440 */
1441
1442 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
1443 * @brief Peripheral State functions
1444 *
1445 @verbatim
1446 ===============================================================================
1447 ##### Peripheral State functions #####
1448 ===============================================================================
1449 [..]
1450 This subsection provides functions allowing to
1451 (+) Get RTC state
1452
1453 @endverbatim
1454 * @{
1455 */
1456 /**
1457 * @brief Returns the Alarm state.
1458 * @param hrtc: RTC handle
1459 * @retval HAL state
1460 */
1461 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1462 {
1463 return hrtc->State;
1464 }
1465
1466 /**
1467 * @}
1468 */
1469
1470 /**
1471 * @}
1472 */
1473
1474 /** @defgroup RTC_Private_Functions RTC Private Functions
1475 * @{
1476 */
1477
1478 /**
1479 * @brief Enters the RTC Initialization mode.
1480 * @note The RTC Initialization mode is write protected, use the
1481 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1482 * @param hrtc: RTC handle
1483 * @retval An ErrorStatus enumeration value:
1484 * - HAL_OK : RTC is in Init mode
1485 * - HAL_TIMEOUT : RTC is not in Init mode and in Timeout
1486 */
1487 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1488 {
1489 uint32_t tickstart = 0;
1490
1491 /* Check if the Initialization mode is set */
1492 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1493 {
1494 /* Set the Initialization mode */
1495 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1496
1497 tickstart = HAL_GetTick();
1498 /* Wait till RTC is in INIT state and if Time out is reached exit */
1499 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1500 {
1501 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1502 {
1503 return HAL_TIMEOUT;
1504 }
1505 }
1506 }
1507
1508 return HAL_OK;
1509 }
1510
1511
1512 /**
1513 * @brief Converts a 2 digit decimal to BCD format.
1514 * @param Value: Byte to be converted
1515 * @retval Converted byte
1516 */
1517 uint8_t RTC_ByteToBcd2(uint8_t Value)
1518 {
1519 uint32_t bcdhigh = 0;
1520
1521 while(Value >= 10)
1522 {
1523 bcdhigh++;
1524 Value -= 10;
1525 }
1526
1527 return ((uint8_t)(bcdhigh << 4) | Value);
1528 }
1529
1530 /**
1531 * @brief Converts from 2 digit BCD to Binary.
1532 * @param Value: BCD value to be converted
1533 * @retval Converted word
1534 */
1535 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1536 {
1537 uint32_t tmp = 0;
1538 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1539 return (tmp + (Value & (uint8_t)0x0F));
1540 }
1541
1542 /**
1543 * @}
1544 */
1545
1546 #endif /* HAL_RTC_MODULE_ENABLED */
1547
1548 /**
1549 * @}
1550 */
1551
1552 /**
1553 * @}
1554 */
1555
1556 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum