Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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