Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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