mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Nov 07 08:15:08 2014 +0000
Revision:
392:2b59412bb664
Parent:
targets/cmsis/TARGET_STM/TARGET_NUCLEO_F091RC/stm32f0xx_hal_rtc.c@340:28d1f895c6fe
Child:
441:d2c15dda23c1
Synchronized with git revision eec0be05cd92349bee83c65f9e1302b25b5badf4

Full URL: https://github.com/mbedmicro/mbed/commit/eec0be05cd92349bee83c65f9e1302b25b5badf4/

Targets: STM32F0 - Factorisation of NUCLEO_F030R8/F072RB/F091RC cmsis folders

Who changed what in which revision?

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