comparison Common/Drivers/STM32F4xx_HAL_DRIVER_v120/Src/stm32f4xx_hal_rtc.c @ 38:5f11787b4f42

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