mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri May 30 15:30:09 2014 +0100
Revision:
218:44081b78fdc2
Parent:
205:c41fc65bcfb4
Synchronized with git revision d854859072d318241476ccc5f335965444d4c1d8

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

[NUCLEO_F072RB] Update CubeF0 HAL driver

Who changed what in which revision?

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