]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_rtc.c
Merge commit '28203e909e83b1ac6becb45a3eadae23b190df32' into master-core-pull
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_rtc.c
1 /**
2 ******************************************************************************
3 * @file stm32l1xx_hal_rtc.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 5-September-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) and the RTC backup registers can be powered
21 from the VBAT voltage when the main VDD supply is powered off.
22 To retain the content of the RTC backup registers and supply the RTC
23 when VDD is turned off, VBAT pin can be connected to an optional
24 standby voltage supplied by a battery or by another source.
25
26 [..] To allow the RTC operating even when the main digital supply (VDD) is turned
27 off, the VBAT pin powers the following blocks:
28 (#) The RTC
29 (#) The LSE oscillator
30 (#) PC13 to PC15 I/Os (when available)
31
32 [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
33 the following pins are available:
34 (#) PC14 and PC15 can be used as either GPIO or LSE pins
35 (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
36
37 [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
38 because VDD is not present), the following pins are available:
39 (#) PC14 and PC15 can be used as LSE pins only
40 (#) PC13 can be used as the RTC_AF1 pin
41
42 ##### Backup Domain Reset #####
43 ==================================================================
44 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
45 to their reset values.
46 [..] A backup domain reset is generated when one of the following events occurs:
47 (#) Software reset, triggered by setting the BDRST bit in the
48 RCC Backup domain control register (RCC_BDCR).
49 (#) VDD or VBAT power on, if both supplies have previously been powered off.
50
51 ##### Backup Domain Access #####
52 ==================================================================
53 [..] After reset, the backup domain (RTC registers, RTC backup data
54 registers and backup SRAM) is protected against possible unwanted write
55 accesses.
56 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
57 (+) Enable the Power Controller (PWR) APB1 interface clock using the
58 __PWR_CLK_ENABLE() function.
59 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
60 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
61 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
62
63
64 ##### How to use this driver #####
65 ==================================================================
66 [..]
67 (+) Enable the RTC domain access (see description in the section above).
68 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
69 format using the HAL_RTC_Init() function.
70
71 *** Time and Date configuration ***
72 ===================================
73 [..]
74 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
75 and HAL_RTC_SetDate() functions.
76 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
77
78 *** Alarm configuration ***
79 ===========================
80 [..]
81 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
82 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
83 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
84
85 ##### RTC and low power modes #####
86 ==================================================================
87 [..] The MCU can be woken up from a low power mode by an RTC alternate
88 function.
89 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
90 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
91 These RTC alternate functions can wake up the system from the Stop and
92 Standby low power modes.
93 [..] The system can also wake up from low power modes without depending
94 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
95 or the RTC wakeup events.
96 [..] The RTC provides a programmable time base for waking up from the
97 Stop or Standby mode at regular intervals.
98 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
99 is LSE or LSI.
100
101 @endverbatim
102 ******************************************************************************
103 * @attention
104 *
105 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
106 *
107 * Redistribution and use in source and binary forms, with or without modification,
108 * are permitted provided that the following conditions are met:
109 * 1. Redistributions of source code must retain the above copyright notice,
110 * this list of conditions and the following disclaimer.
111 * 2. Redistributions in binary form must reproduce the above copyright notice,
112 * this list of conditions and the following disclaimer in the documentation
113 * and/or other materials provided with the distribution.
114 * 3. Neither the name of STMicroelectronics nor the names of its contributors
115 * may be used to endorse or promote products derived from this software
116 * without specific prior written permission.
117 *
118 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
119 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
120 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
121 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
122 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
123 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
124 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
125 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
126 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
127 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128 *
129 ******************************************************************************
130 */
131
132 /* Includes ------------------------------------------------------------------*/
133 #include "stm32l1xx_hal.h"
134
135 /** @addtogroup STM32L1xx_HAL_Driver
136 * @{
137 */
138
139 /** @defgroup RTC RTC
140 * @brief RTC HAL module driver
141 * @{
142 */
143
144 #ifdef HAL_RTC_MODULE_ENABLED
145
146 /* Private typedef -----------------------------------------------------------*/
147 /* Private define ------------------------------------------------------------*/
148 /* Private macro -------------------------------------------------------------*/
149 /* Private variables ---------------------------------------------------------*/
150 /* Private function prototypes -----------------------------------------------*/
151 /* Private functions ---------------------------------------------------------*/
152 /** @defgroup RTC_Exported_Functions RTC Exported Functions
153 * @{
154 */
155
156 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
157 * @brief Initialization and Configuration functions
158 *
159 @verbatim
160 ===============================================================================
161 ##### Initialization and de-initialization functions #####
162 ===============================================================================
163 [..] This section provides functions allowing to initialize and configure the
164 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
165 RTC registers Write protection, enter and exit the RTC initialization mode,
166 RTC registers synchronization check and reference clock detection enable.
167 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
168 It is split into 2 programmable prescalers to minimize power consumption.
169 (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
170 (++) When both prescalers are used, it is recommended to configure the
171 asynchronous prescaler to a high value to minimize power consumption.
172 (#) All RTC registers are Write protected. Writing to the RTC registers
173 is enabled by writing a key into the Write Protection register, RTC_WPR.
174 (#) To configure the RTC Calendar, user application should enter
175 initialization mode. In this mode, the calendar counter is stopped
176 and its value can be updated. When the initialization sequence is
177 complete, the calendar restarts counting after 4 RTCCLK cycles.
178 (#) To read the calendar through the shadow registers after Calendar
179 initialization, calendar update or after wakeup from low power modes
180 the software must first clear the RSF flag. The software must then
181 wait until it is set again before reading the calendar, which means
182 that the calendar registers have been correctly copied into the
183 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
184 implements the above software sequence (RSF clear and RSF check).
185
186 @endverbatim
187 * @{
188 */
189
190 /**
191 * @brief Initializes the RTC peripheral
192 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
193 * the configuration information for RTC.
194 * @retval HAL status
195 */
196 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
197 {
198 /* Check the RTC peripheral state */
199 if(hrtc == HAL_NULL)
200 {
201 return HAL_ERROR;
202 }
203
204 /* Check the parameters */
205 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
206 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
207 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
208 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
209 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
210 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
211 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
212
213 if(hrtc->State == HAL_RTC_STATE_RESET)
214 {
215 /* Initialize RTC MSP */
216 HAL_RTC_MspInit(hrtc);
217 }
218
219 /* Set RTC state */
220 hrtc->State = HAL_RTC_STATE_BUSY;
221
222 /* Disable the write protection for RTC registers */
223 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
224
225 /* Set Initialization mode */
226 if(RTC_EnterInitMode(hrtc) != HAL_OK)
227 {
228 /* Enable the write protection for RTC registers */
229 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
230
231 /* Set RTC state */
232 hrtc->State = HAL_RTC_STATE_ERROR;
233
234 return HAL_ERROR;
235 }
236 else
237 {
238 /* Clear RTC_CR FMT, OSEL and POL Bits */
239 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
240 /* Set RTC_CR register */
241 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
242
243 /* Configure the RTC PRER */
244 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
245 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
246
247 /* Exit Initialization mode */
248 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
249
250 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
251 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
252
253 /* Enable the write protection for RTC registers */
254 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
255
256 /* Set RTC state */
257 hrtc->State = HAL_RTC_STATE_READY;
258
259 return HAL_OK;
260 }
261 }
262
263 /**
264 * @brief DeInitializes the RTC peripheral
265 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
266 * the configuration information for RTC.
267 * @note This function doesn't reset the RTC Backup Data registers.
268 * @retval HAL status
269 */
270 __weak HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
271 {
272 /* Note : This function is defined into this file for library reference. */
273 /* Function content is located into file stm32l1xx_hal_rtc_ex.c */
274
275 /* Return function status */
276 return HAL_ERROR;
277 }
278
279 /**
280 * @brief Initializes the RTC MSP.
281 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
282 * the configuration information for RTC.
283 * @retval None
284 */
285 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
286 {
287 /* NOTE : This function Should not be modified, when the callback is needed,
288 the HAL_RTC_MspInit could be implemented in the user file
289 */
290 }
291
292 /**
293 * @brief DeInitializes the RTC MSP.
294 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
295 * the configuration information for RTC.
296 * @retval None
297 */
298 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
299 {
300 /* NOTE : This function Should not be modified, when the callback is needed,
301 the HAL_RTC_MspDeInit could be implemented in the user file
302 */
303 }
304
305 /**
306 * @}
307 */
308
309 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
310 * @brief RTC Time and Date functions
311 *
312 @verbatim
313 ===============================================================================
314 ##### RTC Time and Date functions #####
315 ===============================================================================
316
317 [..] This section provides functions allowing to configure Time and Date features
318
319 @endverbatim
320 * @{
321 */
322
323 /**
324 * @brief Sets RTC current time.
325 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
326 * the configuration information for RTC.
327 * @param sTime: Pointer to Time structure
328 * @param Format: Specifies the format of the entered parameters.
329 * This parameter can be one of the following values:
330 * @arg FORMAT_BIN: Binary data format
331 * @arg FORMAT_BCD: BCD data format
332 * @retval HAL status
333 */
334 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
335 {
336 uint32_t tmpreg = 0;
337
338 /* Check the parameters */
339 assert_param(IS_RTC_FORMAT(Format));
340 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
341 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
342
343 /* Process Locked */
344 __HAL_LOCK(hrtc);
345
346 hrtc->State = HAL_RTC_STATE_BUSY;
347
348 if(Format == FORMAT_BIN)
349 {
350 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
351 {
352 assert_param(IS_RTC_HOUR12(sTime->Hours));
353 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
354 }
355 else
356 {
357 sTime->TimeFormat = 0x00;
358 assert_param(IS_RTC_HOUR24(sTime->Hours));
359 }
360 assert_param(IS_RTC_MINUTES(sTime->Minutes));
361 assert_param(IS_RTC_SECONDS(sTime->Seconds));
362
363 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
364 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
365 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
366 (((uint32_t)sTime->TimeFormat) << 16));
367 }
368 else
369 {
370 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
371 {
372 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
373 assert_param(IS_RTC_HOUR12(tmpreg));
374 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
375 }
376 else
377 {
378 sTime->TimeFormat = 0x00;
379 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
380 }
381 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
382 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
383 tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
384 ((uint32_t)(sTime->Minutes) << 8) | \
385 ((uint32_t)sTime->Seconds) | \
386 ((uint32_t)(sTime->TimeFormat) << 16));
387 }
388
389 /* Disable the write protection for RTC registers */
390 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
391
392 /* Set Initialization mode */
393 if(RTC_EnterInitMode(hrtc) != HAL_OK)
394 {
395 /* Enable the write protection for RTC registers */
396 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
397
398 /* Set RTC state */
399 hrtc->State = HAL_RTC_STATE_ERROR;
400
401 /* Process Unlocked */
402 __HAL_UNLOCK(hrtc);
403
404 return HAL_ERROR;
405 }
406 else
407 {
408 /* Set the RTC_TR register */
409 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
410
411 /* Clear the bits to be configured */
412 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
413
414 /* Configure the RTC_CR register */
415 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
416
417 /* Exit Initialization mode */
418 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
419
420 /* Wait for synchro */
421 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
422 {
423 /* Enable the write protection for RTC registers */
424 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
425
426 hrtc->State = HAL_RTC_STATE_ERROR;
427
428 /* Process Unlocked */
429 __HAL_UNLOCK(hrtc);
430
431 return HAL_ERROR;
432 }
433
434 /* Enable the write protection for RTC registers */
435 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
436
437 hrtc->State = HAL_RTC_STATE_READY;
438
439 __HAL_UNLOCK(hrtc);
440
441 return HAL_OK;
442 }
443 }
444
445
446 /**
447 * @brief Sets RTC current date.
448 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
449 * the configuration information for RTC.
450 * @param sDate: Pointer to date structure
451 * @param Format: specifies the format of the entered parameters.
452 * This parameter can be one of the following values:
453 * @arg FORMAT_BIN: Binary data format
454 * @arg FORMAT_BCD: BCD data format
455 * @retval HAL status
456 */
457 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
458 {
459 uint32_t datetmpreg = 0;
460
461 /* Check the parameters */
462 assert_param(IS_RTC_FORMAT(Format));
463
464 /* Process Locked */
465 __HAL_LOCK(hrtc);
466
467 hrtc->State = HAL_RTC_STATE_BUSY;
468
469 if((Format == FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
470 {
471 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
472 }
473
474 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
475
476 if(Format == FORMAT_BIN)
477 {
478 assert_param(IS_RTC_YEAR(sDate->Year));
479 assert_param(IS_RTC_MONTH(sDate->Month));
480 assert_param(IS_RTC_DATE(sDate->Date));
481
482 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
483 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
484 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
485 ((uint32_t)sDate->WeekDay << 13));
486 }
487 else
488 {
489 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
490 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
491 assert_param(IS_RTC_MONTH(datetmpreg));
492 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
493 assert_param(IS_RTC_DATE(datetmpreg));
494
495 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
496 (((uint32_t)sDate->Month) << 8) | \
497 ((uint32_t)sDate->Date) | \
498 (((uint32_t)sDate->WeekDay) << 13));
499 }
500
501 /* Disable the write protection for RTC registers */
502 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
503
504 /* Set Initialization mode */
505 if(RTC_EnterInitMode(hrtc) != HAL_OK)
506 {
507 /* Enable the write protection for RTC registers */
508 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
509
510 /* Set RTC state*/
511 hrtc->State = HAL_RTC_STATE_ERROR;
512
513 /* Process Unlocked */
514 __HAL_UNLOCK(hrtc);
515
516 return HAL_ERROR;
517 }
518 else
519 {
520 /* Set the RTC_DR register */
521 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
522
523 /* Exit Initialization mode */
524 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
525
526 /* Wait for synchro */
527 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
528 {
529 /* Enable the write protection for RTC registers */
530 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
531
532 hrtc->State = HAL_RTC_STATE_ERROR;
533
534 /* Process Unlocked */
535 __HAL_UNLOCK(hrtc);
536
537 return HAL_ERROR;
538 }
539
540 /* Enable the write protection for RTC registers */
541 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
542
543 hrtc->State = HAL_RTC_STATE_READY ;
544
545 /* Process Unlocked */
546 __HAL_UNLOCK(hrtc);
547
548 return HAL_OK;
549 }
550 }
551
552 /**
553 * @brief Gets RTC current date.
554 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
555 * the configuration information for RTC.
556 * @param sDate: Pointer to Date structure
557 * @param Format: Specifies the format of the entered parameters.
558 * This parameter can be one of the following values:
559 * @arg FORMAT_BIN: Binary data format
560 * @arg FORMAT_BCD: BCD data format
561 * @retval HAL status
562 */
563 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
564 {
565 uint32_t datetmpreg = 0;
566
567 /* Check the parameters */
568 assert_param(IS_RTC_FORMAT(Format));
569
570 /* Get the DR register */
571 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
572
573 /* Fill the structure fields with the read parameters */
574 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
575 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
576 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
577 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
578
579 /* Check the input parameters format */
580 if(Format == FORMAT_BIN)
581 {
582 /* Convert the date structure parameters to Binary format */
583 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
584 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
585 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
586 }
587 return HAL_OK;
588 }
589
590 /**
591 * @}
592 */
593
594 /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
595 * @brief RTC Alarm functions
596 *
597 @verbatim
598 ===============================================================================
599 ##### RTC Alarm functions #####
600 ===============================================================================
601
602 [..] This section provides functions allowing to configure Alarm feature
603
604 @endverbatim
605 * @{
606 */
607
608 /**
609 * @brief Deactive the specified RTC Alarm
610 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
611 * the configuration information for RTC.
612 * @param Alarm: Specifies the Alarm.
613 * This parameter can be one of the following values:
614 * @arg RTC_ALARM_A: AlarmA
615 * @arg RTC_ALARM_B: AlarmB
616 * @retval HAL status
617 */
618 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
619 {
620 uint32_t tickstart = 0;
621
622 /* Check the parameters */
623 assert_param(IS_ALARM(Alarm));
624
625 /* Process Locked */
626 __HAL_LOCK(hrtc);
627
628 hrtc->State = HAL_RTC_STATE_BUSY;
629
630 /* Disable the write protection for RTC registers */
631 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
632
633 if(Alarm == RTC_ALARM_A)
634 {
635 /* AlarmA */
636 __HAL_RTC_ALARMA_DISABLE(hrtc);
637
638 /* In case of interrupt mode is used, the interrupt source must disabled */
639 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
640
641 tickstart = HAL_GetTick();
642
643 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
644 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
645 {
646 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
647 {
648 /* Enable the write protection for RTC registers */
649 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
650
651 hrtc->State = HAL_RTC_STATE_TIMEOUT;
652
653 /* Process Unlocked */
654 __HAL_UNLOCK(hrtc);
655
656 return HAL_TIMEOUT;
657 }
658 }
659 }
660 else
661 {
662 /* AlarmB */
663 __HAL_RTC_ALARMB_DISABLE(hrtc);
664
665 /* In case of interrupt mode is used, the interrupt source must disabled */
666 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
667
668 tickstart = HAL_GetTick();
669
670 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
671 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
672 {
673 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
674 {
675 /* Enable the write protection for RTC registers */
676 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
677
678 hrtc->State = HAL_RTC_STATE_TIMEOUT;
679
680 /* Process Unlocked */
681 __HAL_UNLOCK(hrtc);
682
683 return HAL_TIMEOUT;
684 }
685 }
686 }
687 /* Enable the write protection for RTC registers */
688 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
689
690 hrtc->State = HAL_RTC_STATE_READY;
691
692 /* Process Unlocked */
693 __HAL_UNLOCK(hrtc);
694
695 return HAL_OK;
696 }
697
698 /**
699 * @brief This function handles Alarm interrupt request.
700 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
701 * the configuration information for RTC.
702 * @retval None
703 */
704 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
705 {
706 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
707 {
708 /* Get the status of the Interrupt */
709 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
710 {
711 /* AlarmA callback */
712 HAL_RTC_AlarmAEventCallback(hrtc);
713
714 /* Clear the Alarm interrupt pending bit */
715 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
716 }
717 }
718
719 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
720 {
721 /* Get the status of the Interrupt */
722 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
723 {
724 /* AlarmB callback */
725 HAL_RTCEx_AlarmBEventCallback(hrtc);
726
727 /* Clear the Alarm interrupt pending bit */
728 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
729 }
730 }
731
732 /* Clear the EXTI's line Flag for RTC Alarm */
733 __HAL_RTC_CLEAR_FLAG(RTC_EXTI_LINE_ALARM_EVENT);
734
735 /* Change RTC state */
736 hrtc->State = HAL_RTC_STATE_READY;
737 }
738
739 /**
740 * @brief Alarm A callback.
741 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
742 * the configuration information for RTC.
743 * @retval None
744 */
745 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
746 {
747 /* NOTE : This function Should not be modified, when the callback is needed,
748 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
749 */
750 }
751
752 /**
753 * @brief This function handles AlarmA Polling request.
754 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
755 * the configuration information for RTC.
756 * @param Timeout: Timeout duration
757 * @retval HAL status
758 */
759 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
760 {
761
762 uint32_t tickstart = HAL_GetTick();
763
764 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
765 {
766 if(Timeout != HAL_MAX_DELAY)
767 {
768 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
769 {
770 hrtc->State = HAL_RTC_STATE_TIMEOUT;
771 return HAL_TIMEOUT;
772 }
773 }
774 }
775
776 /* Clear the Alarm interrupt pending bit */
777 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
778
779 /* Change RTC state */
780 hrtc->State = HAL_RTC_STATE_READY;
781
782 return HAL_OK;
783 }
784
785 /**
786 * @}
787 */
788
789 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
790 * @brief Peripheral State functions
791 *
792 @verbatim
793 ===============================================================================
794 ##### Peripheral State functions #####
795 ===============================================================================
796 [..]
797 This subsection provides functions allowing to
798 (+) Get RTC state
799
800 @endverbatim
801 * @{
802 */
803 /**
804 * @brief Returns the RTC state.
805 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
806 * the configuration information for RTC.
807 * @retval HAL state
808 */
809 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
810 {
811 return hrtc->State;
812 }
813
814 /**
815 * @}
816 */
817
818 /**
819 * @}
820 */
821
822 /** @defgroup RTC_Internal_Functions RTC Internal function
823 * @{
824 */
825
826 /**
827 * @brief Enters the RTC Initialization mode.
828 * @note The RTC Initialization mode is write protected, use the
829 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
830 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
831 * the configuration information for RTC.
832 * @retval HAL status
833 */
834 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
835 {
836 uint32_t tickstart = 0;
837
838 /* Check if the Initialization mode is set */
839 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
840 {
841 /* Set the Initialization mode */
842 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
843
844 tickstart = HAL_GetTick();
845 /* Wait till RTC is in INIT state and if Time out is reached exit */
846 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
847 {
848 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
849 {
850 return HAL_TIMEOUT;
851 }
852 }
853 }
854
855 return HAL_OK;
856 }
857
858 /**
859 * @brief Converts a 2 digit decimal to BCD format.
860 * @param Value: Byte to be converted
861 * @retval Converted byte
862 */
863 uint8_t RTC_ByteToBcd2(uint8_t Value)
864 {
865 uint32_t bcdhigh = 0;
866
867 while(Value >= 10)
868 {
869 bcdhigh++;
870 Value -= 10;
871 }
872
873 return ((uint8_t)(bcdhigh << 4) | Value);
874 }
875
876 /**
877 * @brief Converts from 2 digit BCD to Binary.
878 * @param Value: BCD value to be converted
879 * @retval Converted word
880 */
881 uint8_t RTC_Bcd2ToByte(uint8_t Value)
882 {
883 uint32_t tmp = 0;
884 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
885 return (tmp + (Value & (uint8_t)0x0F));
886 }
887
888
889 /**
890 * @}
891 */
892
893 #endif /* HAL_RTC_MODULE_ENABLED */
894 /**
895 * @}
896 */
897
898 /**
899 * @}
900 */
901
902 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum