TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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