Hal Drivers for L4

Dependents:   BSP OneHopeOnePrayer FINAL_AUDIO_RECORD AudioDemo

Fork of STM32L4xx_HAL_Driver by Senior Design: Sound Monitor

Committer:
EricLew
Date:
Wed Nov 25 17:30:43 2015 +0000
Revision:
2:7aef7655b0a8
Parent:
0:80ee8f3b695e
commit;

Who changed what in which revision?

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