inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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