mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Thu Jul 02 16:30:08 2015 +0100
Revision:
581:39197bcd20f2
Parent:
489:119543c9f674
Synchronized with git revision ae2d3cdffe70184eb8736d94f76c45c93f4b7724

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

Make it possible to build the core mbed library with yotta

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 489:119543c9f674 1 /**
mbed_official 489:119543c9f674 2 ******************************************************************************
mbed_official 489:119543c9f674 3 * @file stm32f1xx_hal_rtc.c
mbed_official 489:119543c9f674 4 * @author MCD Application Team
mbed_official 489:119543c9f674 5 * @version V1.0.0
mbed_official 489:119543c9f674 6 * @date 15-December-2014
mbed_official 489:119543c9f674 7 * @brief RTC HAL module driver.
mbed_official 489:119543c9f674 8 * This file provides firmware functions to manage the following
mbed_official 489:119543c9f674 9 * functionalities of the Real Time Clock (RTC) peripheral:
mbed_official 489:119543c9f674 10 * + Initialization and de-initialization functions
mbed_official 489:119543c9f674 11 * + RTC Time and Date functions
mbed_official 489:119543c9f674 12 * + RTC Alarm functions
mbed_official 489:119543c9f674 13 * + Peripheral Control functions
mbed_official 489:119543c9f674 14 * + Peripheral State functions
mbed_official 489:119543c9f674 15 *
mbed_official 489:119543c9f674 16 @verbatim
mbed_official 489:119543c9f674 17 ==============================================================================
mbed_official 489:119543c9f674 18 ##### How to use this driver #####
mbed_official 489:119543c9f674 19 ==================================================================
mbed_official 489:119543c9f674 20 [..]
mbed_official 489:119543c9f674 21 (+) Enable the RTC domain access (see description in the section above).
mbed_official 489:119543c9f674 22 (+) Configure the RTC Prescaler (Asynchronous prescaler to generate RTC 1Hz time base)
mbed_official 489:119543c9f674 23 using the HAL_RTC_Init() function.
mbed_official 489:119543c9f674 24
mbed_official 489:119543c9f674 25 *** Time and Date configuration ***
mbed_official 489:119543c9f674 26 ===================================
mbed_official 489:119543c9f674 27 [..]
mbed_official 489:119543c9f674 28 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
mbed_official 489:119543c9f674 29 and HAL_RTC_SetDate() functions.
mbed_official 489:119543c9f674 30 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
mbed_official 489:119543c9f674 31
mbed_official 489:119543c9f674 32 *** Alarm configuration ***
mbed_official 489:119543c9f674 33 ===========================
mbed_official 489:119543c9f674 34 [..]
mbed_official 489:119543c9f674 35 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
mbed_official 489:119543c9f674 36 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
mbed_official 489:119543c9f674 37 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
mbed_official 489:119543c9f674 38
mbed_official 489:119543c9f674 39 *** Tamper configuration ***
mbed_official 489:119543c9f674 40 ============================
mbed_official 489:119543c9f674 41 [..]
mbed_official 489:119543c9f674 42 (+) Enable the RTC Tamper and configure the Tamper Level using the
mbed_official 489:119543c9f674 43 HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
mbed_official 489:119543c9f674 44 mode using HAL_RTCEx_SetTamper_IT() function.
mbed_official 489:119543c9f674 45 (+) The TAMPER1 alternate function can be mapped to PC13
mbed_official 489:119543c9f674 46
mbed_official 489:119543c9f674 47 *** Backup Data Registers configuration ***
mbed_official 489:119543c9f674 48 ===========================================
mbed_official 489:119543c9f674 49 [..]
mbed_official 489:119543c9f674 50 (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
mbed_official 489:119543c9f674 51 function.
mbed_official 489:119543c9f674 52 (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
mbed_official 489:119543c9f674 53 function.
mbed_official 489:119543c9f674 54
mbed_official 489:119543c9f674 55 ##### WARNING: Drivers Restrictions #####
mbed_official 489:119543c9f674 56 ==================================================================
mbed_official 489:119543c9f674 57 [..] RTC version used on STM32F1 families is version V1. All the features supported by V2
mbed_official 489:119543c9f674 58 (other families) will be not supported on F1.
mbed_official 489:119543c9f674 59 [..] As on V2, main RTC features are managed by HW. But on F1, date feature is completely
mbed_official 489:119543c9f674 60 managed by SW.
mbed_official 489:119543c9f674 61 [..] Then, there are some restrictions compared to other families:
mbed_official 489:119543c9f674 62 (+) Only format 24 hours supported in HAL (format 12 hours not supported)
mbed_official 489:119543c9f674 63 (+) Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost.
mbed_official 489:119543c9f674 64 User should implement a way to save date before entering in low power mode (an
mbed_official 489:119543c9f674 65 example is provided with firmware package based on backup registers)
mbed_official 489:119543c9f674 66 (+) Date is automatically updated each time a HAL_RTC_GetTime or HAL_RTC_GetDate is called.
mbed_official 489:119543c9f674 67 (+) Alarm detection is limited to 1 day. It will expire only 1 time (no alarm repetition, need
mbed_official 489:119543c9f674 68 to program a new alarm)
mbed_official 489:119543c9f674 69
mbed_official 489:119543c9f674 70 ##### Backup Domain Operating Condition #####
mbed_official 489:119543c9f674 71 ==============================================================================
mbed_official 489:119543c9f674 72 [..] The real-time clock (RTC) and the RTC backup registers can be powered
mbed_official 489:119543c9f674 73 from the VBAT voltage when the main VDD supply is powered off.
mbed_official 489:119543c9f674 74 To retain the content of the RTC backup registers and supply the RTC
mbed_official 489:119543c9f674 75 when VDD is turned off, VBAT pin can be connected to an optional
mbed_official 489:119543c9f674 76 standby voltage supplied by a battery or by another source.
mbed_official 489:119543c9f674 77
mbed_official 489:119543c9f674 78 [..] To allow the RTC operating even when the main digital supply (VDD) is turned
mbed_official 489:119543c9f674 79 off, the VBAT pin powers the following blocks:
mbed_official 489:119543c9f674 80 (+) The RTC
mbed_official 489:119543c9f674 81 (+) The LSE oscillator
mbed_official 489:119543c9f674 82 (+) PC13 I/O
mbed_official 489:119543c9f674 83
mbed_official 489:119543c9f674 84 [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
mbed_official 489:119543c9f674 85 the following pins are available:
mbed_official 489:119543c9f674 86 (+) PC13 can be used as a Tamper pin
mbed_official 489:119543c9f674 87
mbed_official 489:119543c9f674 88 [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
mbed_official 489:119543c9f674 89 because VDD is not present), the following pins are available:
mbed_official 489:119543c9f674 90 (+) PC13 can be used as the Tamper pin
mbed_official 489:119543c9f674 91
mbed_official 489:119543c9f674 92 ##### Backup Domain Reset #####
mbed_official 489:119543c9f674 93 ==================================================================
mbed_official 489:119543c9f674 94 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
mbed_official 489:119543c9f674 95 to their reset values.
mbed_official 489:119543c9f674 96 [..] A backup domain reset is generated when one of the following events occurs:
mbed_official 489:119543c9f674 97 (#) Software reset, triggered by setting the BDRST bit in the
mbed_official 489:119543c9f674 98 RCC Backup domain control register (RCC_BDCR).
mbed_official 489:119543c9f674 99 (#) VDD or VBAT power on, if both supplies have previously been powered off.
mbed_official 489:119543c9f674 100 (#) Tamper detection event resets all data backup registers.
mbed_official 489:119543c9f674 101
mbed_official 489:119543c9f674 102 ##### Backup Domain Access #####
mbed_official 489:119543c9f674 103 ==================================================================
mbed_official 489:119543c9f674 104 [..] After reset, the backup domain (RTC registers, RTC backup data
mbed_official 489:119543c9f674 105 registers and backup SRAM) is protected against possible unwanted write
mbed_official 489:119543c9f674 106 accesses.
mbed_official 489:119543c9f674 107 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
mbed_official 489:119543c9f674 108 (+) Call the function HAL_RCCEx_PeriphCLKConfig in using RCC_PERIPHCLK_RTC for
mbed_official 489:119543c9f674 109 PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSE)
mbed_official 489:119543c9f674 110 (+) Enable the BKP clock in using __HAL_RCC_BKP_CLK_ENABLE()
mbed_official 489:119543c9f674 111
mbed_official 489:119543c9f674 112 ##### RTC and low power modes #####
mbed_official 489:119543c9f674 113 ==================================================================
mbed_official 489:119543c9f674 114 [..] The MCU can be woken up from a low power mode by an RTC alternate
mbed_official 489:119543c9f674 115 function.
mbed_official 489:119543c9f674 116 [..] The RTC alternate functions are the RTC alarms (Alarm A),
mbed_official 489:119543c9f674 117 and RTC tamper event detection.
mbed_official 489:119543c9f674 118 These RTC alternate functions can wake up the system from the Stop and
mbed_official 489:119543c9f674 119 Standby low power modes.
mbed_official 489:119543c9f674 120 [..] The system can also wake up from low power modes without depending
mbed_official 489:119543c9f674 121 on an external interrupt (Auto-wakeup mode), by using the RTC alarm.
mbed_official 489:119543c9f674 122
mbed_official 489:119543c9f674 123 @endverbatim
mbed_official 489:119543c9f674 124 ******************************************************************************
mbed_official 489:119543c9f674 125 * @attention
mbed_official 489:119543c9f674 126 *
mbed_official 489:119543c9f674 127 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 489:119543c9f674 128 *
mbed_official 489:119543c9f674 129 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 489:119543c9f674 130 * are permitted provided that the following conditions are met:
mbed_official 489:119543c9f674 131 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 489:119543c9f674 132 * this list of conditions and the following disclaimer.
mbed_official 489:119543c9f674 133 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 489:119543c9f674 134 * this list of conditions and the following disclaimer in the documentation
mbed_official 489:119543c9f674 135 * and/or other materials provided with the distribution.
mbed_official 489:119543c9f674 136 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 489:119543c9f674 137 * may be used to endorse or promote products derived from this software
mbed_official 489:119543c9f674 138 * without specific prior written permission.
mbed_official 489:119543c9f674 139 *
mbed_official 489:119543c9f674 140 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 489:119543c9f674 141 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 489:119543c9f674 142 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 489:119543c9f674 143 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 489:119543c9f674 144 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 489:119543c9f674 145 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 489:119543c9f674 146 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 489:119543c9f674 147 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 489:119543c9f674 148 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 489:119543c9f674 149 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 489:119543c9f674 150 *
mbed_official 489:119543c9f674 151 ******************************************************************************
mbed_official 489:119543c9f674 152 */
mbed_official 489:119543c9f674 153
mbed_official 489:119543c9f674 154 /* Includes ------------------------------------------------------------------*/
mbed_official 489:119543c9f674 155 #include "stm32f1xx_hal.h"
mbed_official 489:119543c9f674 156
mbed_official 489:119543c9f674 157 /** @addtogroup STM32F1xx_HAL_Driver
mbed_official 489:119543c9f674 158 * @{
mbed_official 489:119543c9f674 159 */
mbed_official 489:119543c9f674 160
mbed_official 489:119543c9f674 161 /** @defgroup RTC RTC
mbed_official 489:119543c9f674 162 * @brief RTC HAL module driver
mbed_official 489:119543c9f674 163 * @{
mbed_official 489:119543c9f674 164 */
mbed_official 489:119543c9f674 165
mbed_official 489:119543c9f674 166 #ifdef HAL_RTC_MODULE_ENABLED
mbed_official 489:119543c9f674 167
mbed_official 489:119543c9f674 168 /* Private typedef -----------------------------------------------------------*/
mbed_official 489:119543c9f674 169 /* Private define ------------------------------------------------------------*/
mbed_official 489:119543c9f674 170 /** @defgroup RTC_Private_Constants RTC Private Constants
mbed_official 489:119543c9f674 171 * @{
mbed_official 489:119543c9f674 172 */
mbed_official 489:119543c9f674 173 #define RTC_ALARM_RESETVALUE_REGISTER (uint16_t)0xFFFF
mbed_official 489:119543c9f674 174 #define RTC_ALARM_RESETVALUE (uint32_t)0xFFFFFFFF
mbed_official 489:119543c9f674 175
mbed_official 489:119543c9f674 176 /**
mbed_official 489:119543c9f674 177 * @}
mbed_official 489:119543c9f674 178 */
mbed_official 489:119543c9f674 179
mbed_official 489:119543c9f674 180 /* Private macro -------------------------------------------------------------*/
mbed_official 489:119543c9f674 181 /** @defgroup RTC_Private_Macros RTC Private Macros
mbed_official 489:119543c9f674 182 * @{
mbed_official 489:119543c9f674 183 */
mbed_official 489:119543c9f674 184 /**
mbed_official 489:119543c9f674 185 * @}
mbed_official 489:119543c9f674 186 */
mbed_official 489:119543c9f674 187
mbed_official 489:119543c9f674 188 /* Private variables ---------------------------------------------------------*/
mbed_official 489:119543c9f674 189 /* Private function prototypes -----------------------------------------------*/
mbed_official 489:119543c9f674 190 /** @defgroup RTC_Private_Functions RTC Private Functions
mbed_official 489:119543c9f674 191 * @{
mbed_official 489:119543c9f674 192 */
mbed_official 489:119543c9f674 193 static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc);
mbed_official 489:119543c9f674 194 static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);
mbed_official 489:119543c9f674 195 static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc);
mbed_official 489:119543c9f674 196 static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter);
mbed_official 489:119543c9f674 197 static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc);
mbed_official 489:119543c9f674 198 static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef* hrtc);
mbed_official 489:119543c9f674 199 static uint8_t RTC_ByteToBcd2(uint8_t Value);
mbed_official 489:119543c9f674 200 static uint8_t RTC_Bcd2ToByte(uint8_t Value);
mbed_official 489:119543c9f674 201 static uint8_t RTC_IsLeapYear(uint16_t nYear);
mbed_official 489:119543c9f674 202 static void RTC_DateUpdate(RTC_HandleTypeDef* hrtc, uint32_t DayElapsed);
mbed_official 489:119543c9f674 203 static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay);
mbed_official 489:119543c9f674 204
mbed_official 489:119543c9f674 205 /**
mbed_official 489:119543c9f674 206 * @}
mbed_official 489:119543c9f674 207 */
mbed_official 489:119543c9f674 208
mbed_official 489:119543c9f674 209 /* Private functions ---------------------------------------------------------*/
mbed_official 489:119543c9f674 210 /** @defgroup RTC_Exported_Functions RTC Exported Functions
mbed_official 489:119543c9f674 211 * @{
mbed_official 489:119543c9f674 212 */
mbed_official 489:119543c9f674 213
mbed_official 489:119543c9f674 214 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 489:119543c9f674 215 * @brief Initialization and Configuration functions
mbed_official 489:119543c9f674 216 *
mbed_official 489:119543c9f674 217 @verbatim
mbed_official 489:119543c9f674 218 ===============================================================================
mbed_official 489:119543c9f674 219 ##### Initialization and de-initialization functions #####
mbed_official 489:119543c9f674 220 ===============================================================================
mbed_official 489:119543c9f674 221 [..] This section provides functions allowing to initialize and configure the
mbed_official 489:119543c9f674 222 RTC Prescaler (Asynchronous), disable RTC registers Write protection,
mbed_official 489:119543c9f674 223 enter and exit the RTC initialization mode,
mbed_official 489:119543c9f674 224 RTC registers synchronization check and reference clock detection enable.
mbed_official 489:119543c9f674 225 (#) The RTC Prescaler should be programmed to generate the RTC 1Hz time base.
mbed_official 489:119543c9f674 226 (#) All RTC registers are Write protected. Writing to the RTC registers
mbed_official 489:119543c9f674 227 is enabled by setting the CNF bit in the RTC_CRL register.
mbed_official 489:119543c9f674 228 (#) To read the calendar after wakeup from low power modes (Standby or Stop)
mbed_official 489:119543c9f674 229 the software must first wait for the RSF bit (Register Synchronized Flag)
mbed_official 489:119543c9f674 230 in the RTC_CRL register to be set by hardware.
mbed_official 489:119543c9f674 231 The HAL_RTC_WaitForSynchro() function implements the above software
mbed_official 489:119543c9f674 232 sequence (RSF clear and RSF check).
mbed_official 489:119543c9f674 233
mbed_official 489:119543c9f674 234 @endverbatim
mbed_official 489:119543c9f674 235 * @{
mbed_official 489:119543c9f674 236 */
mbed_official 489:119543c9f674 237
mbed_official 489:119543c9f674 238 /**
mbed_official 489:119543c9f674 239 * @brief Initializes the RTC peripheral
mbed_official 489:119543c9f674 240 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 241 * the configuration information for RTC.
mbed_official 489:119543c9f674 242 * @retval HAL status
mbed_official 489:119543c9f674 243 */
mbed_official 489:119543c9f674 244 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
mbed_official 489:119543c9f674 245 {
mbed_official 489:119543c9f674 246 uint32_t prescaler = 0;
mbed_official 489:119543c9f674 247 /* Check input parameters */
mbed_official 489:119543c9f674 248 if(hrtc == NULL)
mbed_official 489:119543c9f674 249 {
mbed_official 489:119543c9f674 250 return HAL_ERROR;
mbed_official 489:119543c9f674 251 }
mbed_official 489:119543c9f674 252
mbed_official 489:119543c9f674 253 /* Check the parameters */
mbed_official 489:119543c9f674 254 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
mbed_official 489:119543c9f674 255 assert_param(IS_RTC_CALIB_OUTPUT(hrtc->Init.OutPut));
mbed_official 489:119543c9f674 256 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
mbed_official 489:119543c9f674 257
mbed_official 489:119543c9f674 258 if(hrtc->State == HAL_RTC_STATE_RESET)
mbed_official 489:119543c9f674 259 {
mbed_official 489:119543c9f674 260 /* Allocate lock resource and initialize it */
mbed_official 489:119543c9f674 261 hrtc-> Lock = HAL_UNLOCKED;
mbed_official 489:119543c9f674 262
mbed_official 489:119543c9f674 263 /* Initialize RTC MSP */
mbed_official 489:119543c9f674 264 HAL_RTC_MspInit(hrtc);
mbed_official 489:119543c9f674 265 }
mbed_official 489:119543c9f674 266
mbed_official 489:119543c9f674 267 /* Set RTC state */
mbed_official 489:119543c9f674 268 hrtc->State = HAL_RTC_STATE_BUSY;
mbed_official 489:119543c9f674 269
mbed_official 489:119543c9f674 270 /* Waiting for synchro */
mbed_official 489:119543c9f674 271 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 272 {
mbed_official 489:119543c9f674 273 /* Set RTC state */
mbed_official 489:119543c9f674 274 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 275
mbed_official 489:119543c9f674 276 return HAL_ERROR;
mbed_official 489:119543c9f674 277 }
mbed_official 489:119543c9f674 278
mbed_official 489:119543c9f674 279 /* Set Initialization mode */
mbed_official 489:119543c9f674 280 if(RTC_EnterInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 281 {
mbed_official 489:119543c9f674 282 /* Set RTC state */
mbed_official 489:119543c9f674 283 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 284
mbed_official 489:119543c9f674 285 return HAL_ERROR;
mbed_official 489:119543c9f674 286 }
mbed_official 489:119543c9f674 287 else
mbed_official 489:119543c9f674 288 {
mbed_official 489:119543c9f674 289 /* Clear Flags Bits */
mbed_official 489:119543c9f674 290 CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_OW | RTC_FLAG_ALRAF | RTC_FLAG_SEC));
mbed_official 489:119543c9f674 291
mbed_official 489:119543c9f674 292 if(hrtc->Init.OutPut != RTC_OUTPUTSOURCE_NONE)
mbed_official 489:119543c9f674 293 {
mbed_official 489:119543c9f674 294 /* Disable the selected Tamper pin */
mbed_official 489:119543c9f674 295 CLEAR_BIT(BKP->CR, BKP_CR_TPE);
mbed_official 489:119543c9f674 296 }
mbed_official 489:119543c9f674 297
mbed_official 489:119543c9f674 298 /* Set the signal which will be routed to RTC Tamper pin*/
mbed_official 489:119543c9f674 299 MODIFY_REG(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE | BKP_RTCCR_ASOS), hrtc->Init.OutPut);
mbed_official 489:119543c9f674 300
mbed_official 489:119543c9f674 301 if (hrtc->Init.AsynchPrediv != RTC_AUTO_1_SECOND)
mbed_official 489:119543c9f674 302 {
mbed_official 489:119543c9f674 303 /* RTC Prescaler provided directly by end-user*/
mbed_official 489:119543c9f674 304 prescaler = hrtc->Init.AsynchPrediv;
mbed_official 489:119543c9f674 305 }
mbed_official 489:119543c9f674 306 else
mbed_official 489:119543c9f674 307 {
mbed_official 489:119543c9f674 308 /* RTC Prescaler will be automatically calculated to get 1 second timebase */
mbed_official 489:119543c9f674 309 /* Get the RTCCLK frequency */
mbed_official 489:119543c9f674 310 prescaler = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_RTC);
mbed_official 489:119543c9f674 311
mbed_official 489:119543c9f674 312 /* Check that RTC clock is enabled*/
mbed_official 489:119543c9f674 313 if (prescaler == 0)
mbed_official 489:119543c9f674 314 {
mbed_official 489:119543c9f674 315 /* Should not happen. Frequency is not available*/
mbed_official 489:119543c9f674 316 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 317 return HAL_ERROR;
mbed_official 489:119543c9f674 318 }
mbed_official 489:119543c9f674 319 else
mbed_official 489:119543c9f674 320 {
mbed_official 489:119543c9f674 321 /* RTC period = RTCCLK/(RTC_PR + 1) */
mbed_official 489:119543c9f674 322 prescaler = prescaler - 1;
mbed_official 489:119543c9f674 323 }
mbed_official 489:119543c9f674 324 }
mbed_official 489:119543c9f674 325
mbed_official 489:119543c9f674 326 /* Configure the RTC_PRLH / RTC_PRLL */
mbed_official 489:119543c9f674 327 MODIFY_REG(hrtc->Instance->PRLH, RTC_PRLH_PRL, (prescaler >> 16));
mbed_official 489:119543c9f674 328 MODIFY_REG(hrtc->Instance->PRLL, RTC_PRLL_PRL, (prescaler & RTC_PRLL_PRL));
mbed_official 489:119543c9f674 329
mbed_official 489:119543c9f674 330 /* Wait for synchro */
mbed_official 489:119543c9f674 331 if(RTC_ExitInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 332 {
mbed_official 489:119543c9f674 333 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 334
mbed_official 489:119543c9f674 335 return HAL_ERROR;
mbed_official 489:119543c9f674 336 }
mbed_official 489:119543c9f674 337
mbed_official 489:119543c9f674 338 /* Initialize date to 1st of January 2000 */
mbed_official 489:119543c9f674 339 hrtc->DateToUpdate.Year = 0x00;
mbed_official 489:119543c9f674 340 hrtc->DateToUpdate.Month = RTC_MONTH_JANUARY;
mbed_official 489:119543c9f674 341 hrtc->DateToUpdate.Date = 0x01;
mbed_official 489:119543c9f674 342
mbed_official 489:119543c9f674 343 /* Set RTC state */
mbed_official 489:119543c9f674 344 hrtc->State = HAL_RTC_STATE_READY;
mbed_official 489:119543c9f674 345
mbed_official 489:119543c9f674 346 return HAL_OK;
mbed_official 489:119543c9f674 347 }
mbed_official 489:119543c9f674 348 }
mbed_official 489:119543c9f674 349
mbed_official 489:119543c9f674 350 /**
mbed_official 489:119543c9f674 351 * @brief DeInitializes the RTC peripheral
mbed_official 489:119543c9f674 352 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 353 * the configuration information for RTC.
mbed_official 489:119543c9f674 354 * @note This function does not reset the RTC Backup Data registers.
mbed_official 489:119543c9f674 355 * @retval HAL status
mbed_official 489:119543c9f674 356 */
mbed_official 489:119543c9f674 357 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
mbed_official 489:119543c9f674 358 {
mbed_official 489:119543c9f674 359 /* Check input parameters */
mbed_official 489:119543c9f674 360 if(hrtc == NULL)
mbed_official 489:119543c9f674 361 {
mbed_official 489:119543c9f674 362 return HAL_ERROR;
mbed_official 489:119543c9f674 363 }
mbed_official 489:119543c9f674 364
mbed_official 489:119543c9f674 365 /* Check the parameters */
mbed_official 489:119543c9f674 366 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
mbed_official 489:119543c9f674 367
mbed_official 489:119543c9f674 368 /* Set RTC state */
mbed_official 489:119543c9f674 369 hrtc->State = HAL_RTC_STATE_BUSY;
mbed_official 489:119543c9f674 370
mbed_official 489:119543c9f674 371 /* Set Initialization mode */
mbed_official 489:119543c9f674 372 if(RTC_EnterInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 373 {
mbed_official 489:119543c9f674 374 /* Set RTC state */
mbed_official 489:119543c9f674 375 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 376
mbed_official 489:119543c9f674 377 /* Release Lock */
mbed_official 489:119543c9f674 378 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 379
mbed_official 489:119543c9f674 380 return HAL_ERROR;
mbed_official 489:119543c9f674 381 }
mbed_official 489:119543c9f674 382 else
mbed_official 489:119543c9f674 383 {
mbed_official 489:119543c9f674 384 CLEAR_REG(hrtc->Instance->CNTL);
mbed_official 489:119543c9f674 385 CLEAR_REG(hrtc->Instance->CNTH);
mbed_official 489:119543c9f674 386 WRITE_REG(hrtc->Instance->PRLL, 0x00008000);
mbed_official 489:119543c9f674 387 CLEAR_REG(hrtc->Instance->PRLH);
mbed_official 489:119543c9f674 388
mbed_official 489:119543c9f674 389 /* Reset All CRH/CRL bits */
mbed_official 489:119543c9f674 390 CLEAR_REG(hrtc->Instance->CRH);
mbed_official 489:119543c9f674 391 CLEAR_REG(hrtc->Instance->CRL);
mbed_official 489:119543c9f674 392
mbed_official 489:119543c9f674 393 if(RTC_ExitInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 394 {
mbed_official 489:119543c9f674 395 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 396
mbed_official 489:119543c9f674 397 /* Process Unlocked */
mbed_official 489:119543c9f674 398 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 399
mbed_official 489:119543c9f674 400 return HAL_ERROR;
mbed_official 489:119543c9f674 401 }
mbed_official 489:119543c9f674 402 }
mbed_official 489:119543c9f674 403
mbed_official 489:119543c9f674 404 /* Wait for synchro*/
mbed_official 489:119543c9f674 405 HAL_RTC_WaitForSynchro(hrtc);
mbed_official 489:119543c9f674 406
mbed_official 489:119543c9f674 407 /* Clear RSF flag */
mbed_official 489:119543c9f674 408 CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
mbed_official 489:119543c9f674 409
mbed_official 489:119543c9f674 410 /* De-Initialize RTC MSP */
mbed_official 489:119543c9f674 411 HAL_RTC_MspDeInit(hrtc);
mbed_official 489:119543c9f674 412
mbed_official 489:119543c9f674 413 hrtc->State = HAL_RTC_STATE_RESET;
mbed_official 489:119543c9f674 414
mbed_official 489:119543c9f674 415 /* Release Lock */
mbed_official 489:119543c9f674 416 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 417
mbed_official 489:119543c9f674 418 return HAL_OK;
mbed_official 489:119543c9f674 419 }
mbed_official 489:119543c9f674 420
mbed_official 489:119543c9f674 421 /**
mbed_official 489:119543c9f674 422 * @brief Initializes the RTC MSP.
mbed_official 489:119543c9f674 423 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 424 * the configuration information for RTC.
mbed_official 489:119543c9f674 425 * @retval None
mbed_official 489:119543c9f674 426 */
mbed_official 489:119543c9f674 427 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 428 {
mbed_official 489:119543c9f674 429 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 489:119543c9f674 430 the HAL_RTC_MspInit could be implemented in the user file
mbed_official 489:119543c9f674 431 */
mbed_official 489:119543c9f674 432 }
mbed_official 489:119543c9f674 433
mbed_official 489:119543c9f674 434 /**
mbed_official 489:119543c9f674 435 * @brief DeInitializes the RTC MSP.
mbed_official 489:119543c9f674 436 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 437 * the configuration information for RTC.
mbed_official 489:119543c9f674 438 * @retval None
mbed_official 489:119543c9f674 439 */
mbed_official 489:119543c9f674 440 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 441 {
mbed_official 489:119543c9f674 442 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 489:119543c9f674 443 the HAL_RTC_MspDeInit could be implemented in the user file
mbed_official 489:119543c9f674 444 */
mbed_official 489:119543c9f674 445 }
mbed_official 489:119543c9f674 446
mbed_official 489:119543c9f674 447 /**
mbed_official 489:119543c9f674 448 * @}
mbed_official 489:119543c9f674 449 */
mbed_official 489:119543c9f674 450
mbed_official 489:119543c9f674 451 /** @defgroup RTC_Exported_Functions_Group2 Time and Date functions
mbed_official 489:119543c9f674 452 * @brief RTC Time and Date functions
mbed_official 489:119543c9f674 453 *
mbed_official 489:119543c9f674 454 @verbatim
mbed_official 489:119543c9f674 455 ===============================================================================
mbed_official 489:119543c9f674 456 ##### RTC Time and Date functions #####
mbed_official 489:119543c9f674 457 ===============================================================================
mbed_official 489:119543c9f674 458
mbed_official 489:119543c9f674 459 [..] This section provides functions allowing to configure Time and Date features
mbed_official 489:119543c9f674 460
mbed_official 489:119543c9f674 461 @endverbatim
mbed_official 489:119543c9f674 462 * @{
mbed_official 489:119543c9f674 463 */
mbed_official 489:119543c9f674 464
mbed_official 489:119543c9f674 465 /**
mbed_official 489:119543c9f674 466 * @brief Sets RTC current time.
mbed_official 489:119543c9f674 467 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 468 * the configuration information for RTC.
mbed_official 489:119543c9f674 469 * @param sTime: Pointer to Time structure
mbed_official 489:119543c9f674 470 * @param Format: Specifies the format of the entered parameters.
mbed_official 489:119543c9f674 471 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 472 * @arg RTC_FORMAT_BIN: Binary data format
mbed_official 489:119543c9f674 473 * @arg RTC_FORMAT_BCD: BCD data format
mbed_official 489:119543c9f674 474 * @retval HAL status
mbed_official 489:119543c9f674 475 */
mbed_official 489:119543c9f674 476 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
mbed_official 489:119543c9f674 477 {
mbed_official 489:119543c9f674 478 uint32_t counter_time = 0, counter_alarm = 0;
mbed_official 489:119543c9f674 479
mbed_official 489:119543c9f674 480 /* Check input parameters */
mbed_official 489:119543c9f674 481 if((hrtc == NULL) || (sTime == NULL))
mbed_official 489:119543c9f674 482 {
mbed_official 489:119543c9f674 483 return HAL_ERROR;
mbed_official 489:119543c9f674 484 }
mbed_official 489:119543c9f674 485
mbed_official 489:119543c9f674 486 /* Check the parameters */
mbed_official 489:119543c9f674 487 assert_param(IS_RTC_FORMAT(Format));
mbed_official 489:119543c9f674 488
mbed_official 489:119543c9f674 489 /* Process Locked */
mbed_official 489:119543c9f674 490 __HAL_LOCK(hrtc);
mbed_official 489:119543c9f674 491
mbed_official 489:119543c9f674 492 hrtc->State = HAL_RTC_STATE_BUSY;
mbed_official 489:119543c9f674 493
mbed_official 489:119543c9f674 494 if(Format == RTC_FORMAT_BIN)
mbed_official 489:119543c9f674 495 {
mbed_official 489:119543c9f674 496 assert_param(IS_RTC_HOUR24(sTime->Hours));
mbed_official 489:119543c9f674 497 assert_param(IS_RTC_MINUTES(sTime->Minutes));
mbed_official 489:119543c9f674 498 assert_param(IS_RTC_SECONDS(sTime->Seconds));
mbed_official 489:119543c9f674 499
mbed_official 489:119543c9f674 500 counter_time = (uint32_t)(((uint32_t)sTime->Hours * 3600) + \
mbed_official 489:119543c9f674 501 ((uint32_t)sTime->Minutes * 60) + \
mbed_official 489:119543c9f674 502 ((uint32_t)sTime->Seconds));
mbed_official 489:119543c9f674 503 }
mbed_official 489:119543c9f674 504 else
mbed_official 489:119543c9f674 505 {
mbed_official 489:119543c9f674 506 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
mbed_official 489:119543c9f674 507 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
mbed_official 489:119543c9f674 508 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
mbed_official 489:119543c9f674 509
mbed_official 489:119543c9f674 510 counter_time = (((uint32_t)(RTC_Bcd2ToByte(sTime->Hours)) * 3600) + \
mbed_official 489:119543c9f674 511 ((uint32_t)(RTC_Bcd2ToByte(sTime->Minutes)) * 60) + \
mbed_official 489:119543c9f674 512 ((uint32_t)(RTC_Bcd2ToByte(sTime->Seconds))));
mbed_official 489:119543c9f674 513 }
mbed_official 489:119543c9f674 514
mbed_official 489:119543c9f674 515 /* Write time counter in RTC registers */
mbed_official 489:119543c9f674 516 if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
mbed_official 489:119543c9f674 517 {
mbed_official 489:119543c9f674 518 /* Set RTC state */
mbed_official 489:119543c9f674 519 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 520
mbed_official 489:119543c9f674 521 /* Process Unlocked */
mbed_official 489:119543c9f674 522 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 523
mbed_official 489:119543c9f674 524 return HAL_ERROR;
mbed_official 489:119543c9f674 525 }
mbed_official 489:119543c9f674 526 else
mbed_official 489:119543c9f674 527 {
mbed_official 489:119543c9f674 528 /* Clear Second and overflow flags */
mbed_official 489:119543c9f674 529 CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
mbed_official 489:119543c9f674 530
mbed_official 489:119543c9f674 531 /* Read current Alarm counter in RTC registers */
mbed_official 489:119543c9f674 532 counter_alarm = RTC_ReadAlarmCounter(hrtc);
mbed_official 489:119543c9f674 533
mbed_official 489:119543c9f674 534 /* Set again alarm to match with new time if enabled */
mbed_official 489:119543c9f674 535 if (counter_alarm != RTC_ALARM_RESETVALUE)
mbed_official 489:119543c9f674 536 {
mbed_official 489:119543c9f674 537 if(counter_alarm < counter_time)
mbed_official 489:119543c9f674 538 {
mbed_official 489:119543c9f674 539 /* Add 1 day to alarm counter*/
mbed_official 489:119543c9f674 540 counter_alarm += (uint32_t)(24 * 3600);
mbed_official 489:119543c9f674 541
mbed_official 489:119543c9f674 542 /* Write new Alarm counter in RTC registers */
mbed_official 489:119543c9f674 543 if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
mbed_official 489:119543c9f674 544 {
mbed_official 489:119543c9f674 545 /* Set RTC state */
mbed_official 489:119543c9f674 546 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 547
mbed_official 489:119543c9f674 548 /* Process Unlocked */
mbed_official 489:119543c9f674 549 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 550
mbed_official 489:119543c9f674 551 return HAL_ERROR;
mbed_official 489:119543c9f674 552 }
mbed_official 489:119543c9f674 553 }
mbed_official 489:119543c9f674 554 }
mbed_official 489:119543c9f674 555
mbed_official 489:119543c9f674 556 hrtc->State = HAL_RTC_STATE_READY;
mbed_official 489:119543c9f674 557
mbed_official 489:119543c9f674 558 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 559
mbed_official 489:119543c9f674 560 return HAL_OK;
mbed_official 489:119543c9f674 561 }
mbed_official 489:119543c9f674 562 }
mbed_official 489:119543c9f674 563
mbed_official 489:119543c9f674 564 /**
mbed_official 489:119543c9f674 565 * @brief Gets RTC current time.
mbed_official 489:119543c9f674 566 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 567 * the configuration information for RTC.
mbed_official 489:119543c9f674 568 * @param sTime: Pointer to Time structure
mbed_official 489:119543c9f674 569 * @param Format: Specifies the format of the entered parameters.
mbed_official 489:119543c9f674 570 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 571 * @arg RTC_FORMAT_BIN: Binary data format
mbed_official 489:119543c9f674 572 * @arg RTC_FORMAT_BCD: BCD data format
mbed_official 489:119543c9f674 573 * @retval HAL status
mbed_official 489:119543c9f674 574 */
mbed_official 489:119543c9f674 575 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
mbed_official 489:119543c9f674 576 {
mbed_official 489:119543c9f674 577 uint32_t counter_time = 0, counter_alarm = 0, days_elapsed = 0, hours = 0;
mbed_official 489:119543c9f674 578
mbed_official 489:119543c9f674 579 /* Check input parameters */
mbed_official 489:119543c9f674 580 if((hrtc == NULL) || (sTime == NULL))
mbed_official 489:119543c9f674 581 {
mbed_official 489:119543c9f674 582 return HAL_ERROR;
mbed_official 489:119543c9f674 583 }
mbed_official 489:119543c9f674 584
mbed_official 489:119543c9f674 585 /* Check the parameters */
mbed_official 489:119543c9f674 586 assert_param(IS_RTC_FORMAT(Format));
mbed_official 489:119543c9f674 587
mbed_official 489:119543c9f674 588 /* Check if counter overflow occurred */
mbed_official 489:119543c9f674 589 if (__HAL_RTC_OVERFLOW_GET_FLAG(hrtc, RTC_FLAG_OW))
mbed_official 489:119543c9f674 590 {
mbed_official 489:119543c9f674 591 return HAL_ERROR;
mbed_official 489:119543c9f674 592 }
mbed_official 489:119543c9f674 593
mbed_official 489:119543c9f674 594 /* Read the time counter*/
mbed_official 489:119543c9f674 595 counter_time = RTC_ReadTimeCounter(hrtc);
mbed_official 489:119543c9f674 596
mbed_official 489:119543c9f674 597 /* Fill the structure fields with the read parameters */
mbed_official 489:119543c9f674 598 hours = counter_time / 3600;
mbed_official 489:119543c9f674 599 sTime->Minutes = (uint8_t)((counter_time % 3600) / 60);
mbed_official 489:119543c9f674 600 sTime->Seconds = (uint8_t)((counter_time % 3600) % 60);
mbed_official 489:119543c9f674 601
mbed_official 489:119543c9f674 602 if (hours >= 24)
mbed_official 489:119543c9f674 603 {
mbed_official 489:119543c9f674 604 /* Get number of days elapsed from last calculation */
mbed_official 489:119543c9f674 605 days_elapsed = (hours / 24);
mbed_official 489:119543c9f674 606
mbed_official 489:119543c9f674 607 /* Set Hours in RTC_TimeTypeDef structure*/
mbed_official 489:119543c9f674 608 sTime->Hours = (hours % 24);
mbed_official 489:119543c9f674 609
mbed_official 489:119543c9f674 610 /* Read Alarm counter in RTC registers */
mbed_official 489:119543c9f674 611 counter_alarm = RTC_ReadAlarmCounter(hrtc);
mbed_official 489:119543c9f674 612
mbed_official 489:119543c9f674 613 /* Calculate remaining time to reach alarm (only if set and not yet expired)*/
mbed_official 489:119543c9f674 614 if ((counter_alarm != RTC_ALARM_RESETVALUE) && (counter_alarm > counter_time))
mbed_official 489:119543c9f674 615 {
mbed_official 489:119543c9f674 616 counter_alarm -= counter_time;
mbed_official 489:119543c9f674 617 }
mbed_official 489:119543c9f674 618 else
mbed_official 489:119543c9f674 619 {
mbed_official 489:119543c9f674 620 /* In case of counter_alarm < counter_time */
mbed_official 489:119543c9f674 621 /* Alarm expiration already occurred but alarm not deactivated */
mbed_official 489:119543c9f674 622 counter_alarm = RTC_ALARM_RESETVALUE;
mbed_official 489:119543c9f674 623 }
mbed_official 489:119543c9f674 624
mbed_official 489:119543c9f674 625 /* Set updated time in decreasing counter by number of days elapsed */
mbed_official 489:119543c9f674 626 counter_time -= (days_elapsed * 24 * 3600);
mbed_official 489:119543c9f674 627
mbed_official 489:119543c9f674 628 /* Write time counter in RTC registers */
mbed_official 489:119543c9f674 629 if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
mbed_official 489:119543c9f674 630 {
mbed_official 489:119543c9f674 631 return HAL_ERROR;
mbed_official 489:119543c9f674 632 }
mbed_official 489:119543c9f674 633
mbed_official 489:119543c9f674 634 /* Set updated alarm to be set */
mbed_official 489:119543c9f674 635 if (counter_alarm != RTC_ALARM_RESETVALUE)
mbed_official 489:119543c9f674 636 {
mbed_official 489:119543c9f674 637 counter_alarm += counter_time;
mbed_official 489:119543c9f674 638
mbed_official 489:119543c9f674 639 /* Write time counter in RTC registers */
mbed_official 489:119543c9f674 640 if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
mbed_official 489:119543c9f674 641 {
mbed_official 489:119543c9f674 642 return HAL_ERROR;
mbed_official 489:119543c9f674 643 }
mbed_official 489:119543c9f674 644 }
mbed_official 489:119543c9f674 645 else
mbed_official 489:119543c9f674 646 {
mbed_official 489:119543c9f674 647 /* Alarm already occurred. Set it to reset values to avoid unexpected expiration */
mbed_official 489:119543c9f674 648 if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
mbed_official 489:119543c9f674 649 {
mbed_official 489:119543c9f674 650 return HAL_ERROR;
mbed_official 489:119543c9f674 651 }
mbed_official 489:119543c9f674 652 }
mbed_official 489:119543c9f674 653
mbed_official 489:119543c9f674 654 /* Update date */
mbed_official 489:119543c9f674 655 RTC_DateUpdate(hrtc, days_elapsed);
mbed_official 489:119543c9f674 656 }
mbed_official 489:119543c9f674 657 else
mbed_official 489:119543c9f674 658 {
mbed_official 489:119543c9f674 659 sTime->Hours = hours;
mbed_official 489:119543c9f674 660 }
mbed_official 489:119543c9f674 661
mbed_official 489:119543c9f674 662 /* Check the input parameters format */
mbed_official 489:119543c9f674 663 if(Format != RTC_FORMAT_BIN)
mbed_official 489:119543c9f674 664 {
mbed_official 489:119543c9f674 665 /* Convert the time structure parameters to BCD format */
mbed_official 489:119543c9f674 666 sTime->Hours = (uint8_t)RTC_ByteToBcd2(sTime->Hours);
mbed_official 489:119543c9f674 667 sTime->Minutes = (uint8_t)RTC_ByteToBcd2(sTime->Minutes);
mbed_official 489:119543c9f674 668 sTime->Seconds = (uint8_t)RTC_ByteToBcd2(sTime->Seconds);
mbed_official 489:119543c9f674 669 }
mbed_official 489:119543c9f674 670
mbed_official 489:119543c9f674 671 return HAL_OK;
mbed_official 489:119543c9f674 672 }
mbed_official 489:119543c9f674 673
mbed_official 489:119543c9f674 674
mbed_official 489:119543c9f674 675 /**
mbed_official 489:119543c9f674 676 * @brief Sets RTC current date.
mbed_official 489:119543c9f674 677 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 678 * the configuration information for RTC.
mbed_official 489:119543c9f674 679 * @param sDate: Pointer to date structure
mbed_official 489:119543c9f674 680 * @param Format: specifies the format of the entered parameters.
mbed_official 489:119543c9f674 681 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 682 * @arg RTC_FORMAT_BIN: Binary data format
mbed_official 489:119543c9f674 683 * @arg RTC_FORMAT_BCD: BCD data format
mbed_official 489:119543c9f674 684 * @retval HAL status
mbed_official 489:119543c9f674 685 */
mbed_official 489:119543c9f674 686 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
mbed_official 489:119543c9f674 687 {
mbed_official 489:119543c9f674 688 uint32_t counter_time = 0, counter_alarm = 0, hours = 0;
mbed_official 489:119543c9f674 689
mbed_official 489:119543c9f674 690 /* Check input parameters */
mbed_official 489:119543c9f674 691 if((hrtc == NULL) || (sDate == NULL))
mbed_official 489:119543c9f674 692 {
mbed_official 489:119543c9f674 693 return HAL_ERROR;
mbed_official 489:119543c9f674 694 }
mbed_official 489:119543c9f674 695
mbed_official 489:119543c9f674 696 /* Check the parameters */
mbed_official 489:119543c9f674 697 assert_param(IS_RTC_FORMAT(Format));
mbed_official 489:119543c9f674 698
mbed_official 489:119543c9f674 699 /* Process Locked */
mbed_official 489:119543c9f674 700 __HAL_LOCK(hrtc);
mbed_official 489:119543c9f674 701
mbed_official 489:119543c9f674 702 hrtc->State = HAL_RTC_STATE_BUSY;
mbed_official 489:119543c9f674 703
mbed_official 489:119543c9f674 704 if(Format == RTC_FORMAT_BIN)
mbed_official 489:119543c9f674 705 {
mbed_official 489:119543c9f674 706 assert_param(IS_RTC_YEAR(sDate->Year));
mbed_official 489:119543c9f674 707 assert_param(IS_RTC_MONTH(sDate->Month));
mbed_official 489:119543c9f674 708 assert_param(IS_RTC_DATE(sDate->Date));
mbed_official 489:119543c9f674 709
mbed_official 489:119543c9f674 710 /* Change the current date */
mbed_official 489:119543c9f674 711 hrtc->DateToUpdate.Year = sDate->Year;
mbed_official 489:119543c9f674 712 hrtc->DateToUpdate.Month = sDate->Month;
mbed_official 489:119543c9f674 713 hrtc->DateToUpdate.Date = sDate->Date;
mbed_official 489:119543c9f674 714 }
mbed_official 489:119543c9f674 715 else
mbed_official 489:119543c9f674 716 {
mbed_official 489:119543c9f674 717 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
mbed_official 489:119543c9f674 718 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
mbed_official 489:119543c9f674 719 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
mbed_official 489:119543c9f674 720
mbed_official 489:119543c9f674 721 /* Change the current date */
mbed_official 489:119543c9f674 722 hrtc->DateToUpdate.Year = RTC_Bcd2ToByte(sDate->Year);
mbed_official 489:119543c9f674 723 hrtc->DateToUpdate.Month = RTC_Bcd2ToByte(sDate->Month);
mbed_official 489:119543c9f674 724 hrtc->DateToUpdate.Date = RTC_Bcd2ToByte(sDate->Date);
mbed_official 489:119543c9f674 725 }
mbed_official 489:119543c9f674 726
mbed_official 489:119543c9f674 727 /* WeekDay set by user can be ignored because automatically calculated */
mbed_official 489:119543c9f674 728 hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(hrtc->DateToUpdate.Year, hrtc->DateToUpdate.Month, hrtc->DateToUpdate.Date);
mbed_official 489:119543c9f674 729 sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
mbed_official 489:119543c9f674 730
mbed_official 489:119543c9f674 731 /* Reset time to be aligned on the same day */
mbed_official 489:119543c9f674 732 /* Read the time counter*/
mbed_official 489:119543c9f674 733 counter_time = RTC_ReadTimeCounter(hrtc);
mbed_official 489:119543c9f674 734
mbed_official 489:119543c9f674 735 /* Fill the structure fields with the read parameters */
mbed_official 489:119543c9f674 736 hours = counter_time / 3600;
mbed_official 489:119543c9f674 737 if (hours > 24)
mbed_official 489:119543c9f674 738 {
mbed_official 489:119543c9f674 739 /* Set updated time in decreasing counter by number of days elapsed */
mbed_official 489:119543c9f674 740 counter_time -= ((hours / 24) * 24 * 3600);
mbed_official 489:119543c9f674 741 /* Write time counter in RTC registers */
mbed_official 489:119543c9f674 742 if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
mbed_official 489:119543c9f674 743 {
mbed_official 489:119543c9f674 744 /* Set RTC state */
mbed_official 489:119543c9f674 745 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 746
mbed_official 489:119543c9f674 747 /* Process Unlocked */
mbed_official 489:119543c9f674 748 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 749
mbed_official 489:119543c9f674 750 return HAL_ERROR;
mbed_official 489:119543c9f674 751 }
mbed_official 489:119543c9f674 752
mbed_official 489:119543c9f674 753 /* Read current Alarm counter in RTC registers */
mbed_official 489:119543c9f674 754 counter_alarm = RTC_ReadAlarmCounter(hrtc);
mbed_official 489:119543c9f674 755
mbed_official 489:119543c9f674 756 /* Set again alarm to match with new time if enabled */
mbed_official 489:119543c9f674 757 if (counter_alarm != RTC_ALARM_RESETVALUE)
mbed_official 489:119543c9f674 758 {
mbed_official 489:119543c9f674 759 if(counter_alarm < counter_time)
mbed_official 489:119543c9f674 760 {
mbed_official 489:119543c9f674 761 /* Add 1 day to alarm counter*/
mbed_official 489:119543c9f674 762 counter_alarm += (uint32_t)(24 * 3600);
mbed_official 489:119543c9f674 763
mbed_official 489:119543c9f674 764 /* Write new Alarm counter in RTC registers */
mbed_official 489:119543c9f674 765 if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
mbed_official 489:119543c9f674 766 {
mbed_official 489:119543c9f674 767 /* Set RTC state */
mbed_official 489:119543c9f674 768 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 769
mbed_official 489:119543c9f674 770 /* Process Unlocked */
mbed_official 489:119543c9f674 771 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 772
mbed_official 489:119543c9f674 773 return HAL_ERROR;
mbed_official 489:119543c9f674 774 }
mbed_official 489:119543c9f674 775 }
mbed_official 489:119543c9f674 776 }
mbed_official 489:119543c9f674 777
mbed_official 489:119543c9f674 778
mbed_official 489:119543c9f674 779 }
mbed_official 489:119543c9f674 780
mbed_official 489:119543c9f674 781 hrtc->State = HAL_RTC_STATE_READY ;
mbed_official 489:119543c9f674 782
mbed_official 489:119543c9f674 783 /* Process Unlocked */
mbed_official 489:119543c9f674 784 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 785
mbed_official 489:119543c9f674 786 return HAL_OK;
mbed_official 489:119543c9f674 787 }
mbed_official 489:119543c9f674 788
mbed_official 489:119543c9f674 789 /**
mbed_official 489:119543c9f674 790 * @brief Gets RTC current date.
mbed_official 489:119543c9f674 791 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 792 * the configuration information for RTC.
mbed_official 489:119543c9f674 793 * @param sDate: Pointer to Date structure
mbed_official 489:119543c9f674 794 * @param Format: Specifies the format of the entered parameters.
mbed_official 489:119543c9f674 795 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 796 * @arg RTC_FORMAT_BIN: Binary data format
mbed_official 489:119543c9f674 797 * @arg RTC_FORMAT_BCD: BCD data format
mbed_official 489:119543c9f674 798 * @retval HAL status
mbed_official 489:119543c9f674 799 */
mbed_official 489:119543c9f674 800 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
mbed_official 489:119543c9f674 801 {
mbed_official 489:119543c9f674 802 RTC_TimeTypeDef stime = {0};
mbed_official 489:119543c9f674 803
mbed_official 489:119543c9f674 804 /* Check input parameters */
mbed_official 489:119543c9f674 805 if((hrtc == NULL) || (sDate == NULL))
mbed_official 489:119543c9f674 806 {
mbed_official 489:119543c9f674 807 return HAL_ERROR;
mbed_official 489:119543c9f674 808 }
mbed_official 489:119543c9f674 809
mbed_official 489:119543c9f674 810 /* Check the parameters */
mbed_official 489:119543c9f674 811 assert_param(IS_RTC_FORMAT(Format));
mbed_official 489:119543c9f674 812
mbed_official 489:119543c9f674 813 /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
mbed_official 489:119543c9f674 814 if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
mbed_official 489:119543c9f674 815 {
mbed_official 489:119543c9f674 816 return HAL_ERROR;
mbed_official 489:119543c9f674 817 }
mbed_official 489:119543c9f674 818
mbed_official 489:119543c9f674 819 /* Fill the structure fields with the read parameters */
mbed_official 489:119543c9f674 820 sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
mbed_official 489:119543c9f674 821 sDate->Year = hrtc->DateToUpdate.Year;
mbed_official 489:119543c9f674 822 sDate->Month = hrtc->DateToUpdate.Month;
mbed_official 489:119543c9f674 823 sDate->Date = hrtc->DateToUpdate.Date;
mbed_official 489:119543c9f674 824
mbed_official 489:119543c9f674 825 /* Check the input parameters format */
mbed_official 489:119543c9f674 826 if(Format != RTC_FORMAT_BIN)
mbed_official 489:119543c9f674 827 {
mbed_official 489:119543c9f674 828 /* Convert the date structure parameters to BCD format */
mbed_official 489:119543c9f674 829 sDate->Year = (uint8_t)RTC_ByteToBcd2(sDate->Year);
mbed_official 489:119543c9f674 830 sDate->Month = (uint8_t)RTC_ByteToBcd2(sDate->Month);
mbed_official 489:119543c9f674 831 sDate->Date = (uint8_t)RTC_ByteToBcd2(sDate->Date);
mbed_official 489:119543c9f674 832 }
mbed_official 489:119543c9f674 833 return HAL_OK;
mbed_official 489:119543c9f674 834 }
mbed_official 489:119543c9f674 835
mbed_official 489:119543c9f674 836 /**
mbed_official 489:119543c9f674 837 * @}
mbed_official 489:119543c9f674 838 */
mbed_official 489:119543c9f674 839
mbed_official 489:119543c9f674 840 /** @defgroup RTC_Exported_Functions_Group3 Alarm functions
mbed_official 489:119543c9f674 841 * @brief RTC Alarm functions
mbed_official 489:119543c9f674 842 *
mbed_official 489:119543c9f674 843 @verbatim
mbed_official 489:119543c9f674 844 ===============================================================================
mbed_official 489:119543c9f674 845 ##### RTC Alarm functions #####
mbed_official 489:119543c9f674 846 ===============================================================================
mbed_official 489:119543c9f674 847
mbed_official 489:119543c9f674 848 [..] This section provides functions allowing to configure Alarm feature
mbed_official 489:119543c9f674 849
mbed_official 489:119543c9f674 850 @endverbatim
mbed_official 489:119543c9f674 851 * @{
mbed_official 489:119543c9f674 852 */
mbed_official 489:119543c9f674 853
mbed_official 489:119543c9f674 854 /**
mbed_official 489:119543c9f674 855 * @brief Sets the specified RTC Alarm.
mbed_official 489:119543c9f674 856 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 857 * the configuration information for RTC.
mbed_official 489:119543c9f674 858 * @param sAlarm: Pointer to Alarm structure
mbed_official 489:119543c9f674 859 * @param Format: Specifies the format of the entered parameters.
mbed_official 489:119543c9f674 860 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 861 * @arg RTC_FORMAT_BIN: Binary data format
mbed_official 489:119543c9f674 862 * @arg RTC_FORMAT_BCD: BCD data format
mbed_official 489:119543c9f674 863 * @retval HAL status
mbed_official 489:119543c9f674 864 */
mbed_official 489:119543c9f674 865 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
mbed_official 489:119543c9f674 866 {
mbed_official 489:119543c9f674 867 uint32_t counter_alarm = 0, counter_time;
mbed_official 489:119543c9f674 868 RTC_TimeTypeDef stime = {0};
mbed_official 489:119543c9f674 869
mbed_official 489:119543c9f674 870 /* Check input parameters */
mbed_official 489:119543c9f674 871 if((hrtc == NULL) || (sAlarm == NULL))
mbed_official 489:119543c9f674 872 {
mbed_official 489:119543c9f674 873 return HAL_ERROR;
mbed_official 489:119543c9f674 874 }
mbed_official 489:119543c9f674 875
mbed_official 489:119543c9f674 876 /* Check the parameters */
mbed_official 489:119543c9f674 877 assert_param(IS_RTC_FORMAT(Format));
mbed_official 489:119543c9f674 878 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
mbed_official 489:119543c9f674 879
mbed_official 489:119543c9f674 880 /* Process Locked */
mbed_official 489:119543c9f674 881 __HAL_LOCK(hrtc);
mbed_official 489:119543c9f674 882
mbed_official 489:119543c9f674 883 hrtc->State = HAL_RTC_STATE_BUSY;
mbed_official 489:119543c9f674 884
mbed_official 489:119543c9f674 885 /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
mbed_official 489:119543c9f674 886 if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
mbed_official 489:119543c9f674 887 {
mbed_official 489:119543c9f674 888 return HAL_ERROR;
mbed_official 489:119543c9f674 889 }
mbed_official 489:119543c9f674 890
mbed_official 489:119543c9f674 891 /* Convert time in seconds */
mbed_official 489:119543c9f674 892 counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600) + \
mbed_official 489:119543c9f674 893 ((uint32_t)stime.Minutes * 60) + \
mbed_official 489:119543c9f674 894 ((uint32_t)stime.Seconds));
mbed_official 489:119543c9f674 895
mbed_official 489:119543c9f674 896 if(Format == RTC_FORMAT_BIN)
mbed_official 489:119543c9f674 897 {
mbed_official 489:119543c9f674 898 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
mbed_official 489:119543c9f674 899 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
mbed_official 489:119543c9f674 900 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
mbed_official 489:119543c9f674 901
mbed_official 489:119543c9f674 902 counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600) + \
mbed_official 489:119543c9f674 903 ((uint32_t)sAlarm->AlarmTime.Minutes * 60) + \
mbed_official 489:119543c9f674 904 ((uint32_t)sAlarm->AlarmTime.Seconds));
mbed_official 489:119543c9f674 905 }
mbed_official 489:119543c9f674 906 else
mbed_official 489:119543c9f674 907 {
mbed_official 489:119543c9f674 908 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
mbed_official 489:119543c9f674 909 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
mbed_official 489:119543c9f674 910 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
mbed_official 489:119543c9f674 911
mbed_official 489:119543c9f674 912 counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600) + \
mbed_official 489:119543c9f674 913 ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60) + \
mbed_official 489:119543c9f674 914 ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
mbed_official 489:119543c9f674 915 }
mbed_official 489:119543c9f674 916
mbed_official 489:119543c9f674 917 /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
mbed_official 489:119543c9f674 918 if (counter_alarm < counter_time)
mbed_official 489:119543c9f674 919 {
mbed_official 489:119543c9f674 920 /* Add 1 day to alarm counter*/
mbed_official 489:119543c9f674 921 counter_alarm += (uint32_t)(24 * 3600);
mbed_official 489:119543c9f674 922 }
mbed_official 489:119543c9f674 923
mbed_official 489:119543c9f674 924 /* Write Alarm counter in RTC registers */
mbed_official 489:119543c9f674 925 if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
mbed_official 489:119543c9f674 926 {
mbed_official 489:119543c9f674 927 /* Set RTC state */
mbed_official 489:119543c9f674 928 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 929
mbed_official 489:119543c9f674 930 /* Process Unlocked */
mbed_official 489:119543c9f674 931 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 932
mbed_official 489:119543c9f674 933 return HAL_ERROR;
mbed_official 489:119543c9f674 934 }
mbed_official 489:119543c9f674 935 else
mbed_official 489:119543c9f674 936 {
mbed_official 489:119543c9f674 937 hrtc->State = HAL_RTC_STATE_READY;
mbed_official 489:119543c9f674 938
mbed_official 489:119543c9f674 939 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 940
mbed_official 489:119543c9f674 941 return HAL_OK;
mbed_official 489:119543c9f674 942 }
mbed_official 489:119543c9f674 943 }
mbed_official 489:119543c9f674 944
mbed_official 489:119543c9f674 945 /**
mbed_official 489:119543c9f674 946 * @brief Sets the specified RTC Alarm with Interrupt
mbed_official 489:119543c9f674 947 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 948 * the configuration information for RTC.
mbed_official 489:119543c9f674 949 * @param sAlarm: Pointer to Alarm structure
mbed_official 489:119543c9f674 950 * @param Format: Specifies the format of the entered parameters.
mbed_official 489:119543c9f674 951 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 952 * @arg RTC_FORMAT_BIN: Binary data format
mbed_official 489:119543c9f674 953 * @arg RTC_FORMAT_BCD: BCD data format
mbed_official 489:119543c9f674 954 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
mbed_official 489:119543c9f674 955 * @retval HAL status
mbed_official 489:119543c9f674 956 */
mbed_official 489:119543c9f674 957 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
mbed_official 489:119543c9f674 958 {
mbed_official 489:119543c9f674 959 uint32_t counter_alarm = 0, counter_time;
mbed_official 489:119543c9f674 960 RTC_TimeTypeDef stime = {0};
mbed_official 489:119543c9f674 961
mbed_official 489:119543c9f674 962 /* Check input parameters */
mbed_official 489:119543c9f674 963 if((hrtc == NULL) || (sAlarm == NULL))
mbed_official 489:119543c9f674 964 {
mbed_official 489:119543c9f674 965 return HAL_ERROR;
mbed_official 489:119543c9f674 966 }
mbed_official 489:119543c9f674 967
mbed_official 489:119543c9f674 968 /* Check the parameters */
mbed_official 489:119543c9f674 969 assert_param(IS_RTC_FORMAT(Format));
mbed_official 489:119543c9f674 970 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
mbed_official 489:119543c9f674 971
mbed_official 489:119543c9f674 972 /* Process Locked */
mbed_official 489:119543c9f674 973 __HAL_LOCK(hrtc);
mbed_official 489:119543c9f674 974
mbed_official 489:119543c9f674 975 hrtc->State = HAL_RTC_STATE_BUSY;
mbed_official 489:119543c9f674 976
mbed_official 489:119543c9f674 977 /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
mbed_official 489:119543c9f674 978 if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
mbed_official 489:119543c9f674 979 {
mbed_official 489:119543c9f674 980 return HAL_ERROR;
mbed_official 489:119543c9f674 981 }
mbed_official 489:119543c9f674 982
mbed_official 489:119543c9f674 983 /* Convert time in seconds */
mbed_official 489:119543c9f674 984 counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600) + \
mbed_official 489:119543c9f674 985 ((uint32_t)stime.Minutes * 60) + \
mbed_official 489:119543c9f674 986 ((uint32_t)stime.Seconds));
mbed_official 489:119543c9f674 987
mbed_official 489:119543c9f674 988 if(Format == RTC_FORMAT_BIN)
mbed_official 489:119543c9f674 989 {
mbed_official 489:119543c9f674 990 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
mbed_official 489:119543c9f674 991 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
mbed_official 489:119543c9f674 992 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
mbed_official 489:119543c9f674 993
mbed_official 489:119543c9f674 994 counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600) + \
mbed_official 489:119543c9f674 995 ((uint32_t)sAlarm->AlarmTime.Minutes * 60) + \
mbed_official 489:119543c9f674 996 ((uint32_t)sAlarm->AlarmTime.Seconds));
mbed_official 489:119543c9f674 997 }
mbed_official 489:119543c9f674 998 else
mbed_official 489:119543c9f674 999 {
mbed_official 489:119543c9f674 1000 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
mbed_official 489:119543c9f674 1001 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
mbed_official 489:119543c9f674 1002 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
mbed_official 489:119543c9f674 1003
mbed_official 489:119543c9f674 1004 counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600) + \
mbed_official 489:119543c9f674 1005 ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60) + \
mbed_official 489:119543c9f674 1006 ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
mbed_official 489:119543c9f674 1007 }
mbed_official 489:119543c9f674 1008
mbed_official 489:119543c9f674 1009 /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
mbed_official 489:119543c9f674 1010 if (counter_alarm < counter_time)
mbed_official 489:119543c9f674 1011 {
mbed_official 489:119543c9f674 1012 /* Add 1 day to alarm counter*/
mbed_official 489:119543c9f674 1013 counter_alarm += (uint32_t)(24 * 3600);
mbed_official 489:119543c9f674 1014 }
mbed_official 489:119543c9f674 1015
mbed_official 489:119543c9f674 1016 /* Write alarm counter in RTC registers */
mbed_official 489:119543c9f674 1017 if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
mbed_official 489:119543c9f674 1018 {
mbed_official 489:119543c9f674 1019 /* Set RTC state */
mbed_official 489:119543c9f674 1020 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 1021
mbed_official 489:119543c9f674 1022 /* Process Unlocked */
mbed_official 489:119543c9f674 1023 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 1024
mbed_official 489:119543c9f674 1025 return HAL_ERROR;
mbed_official 489:119543c9f674 1026 }
mbed_official 489:119543c9f674 1027 else
mbed_official 489:119543c9f674 1028 {
mbed_official 489:119543c9f674 1029 /* Clear flag alarm A */
mbed_official 489:119543c9f674 1030 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
mbed_official 489:119543c9f674 1031
mbed_official 489:119543c9f674 1032 /* Configure the Alarm interrupt */
mbed_official 489:119543c9f674 1033 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
mbed_official 489:119543c9f674 1034
mbed_official 489:119543c9f674 1035 /* RTC Alarm Interrupt Configuration: EXTI configuration */
mbed_official 489:119543c9f674 1036 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
mbed_official 489:119543c9f674 1037
mbed_official 489:119543c9f674 1038 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
mbed_official 489:119543c9f674 1039
mbed_official 489:119543c9f674 1040 hrtc->State = HAL_RTC_STATE_READY;
mbed_official 489:119543c9f674 1041
mbed_official 489:119543c9f674 1042 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 1043
mbed_official 489:119543c9f674 1044 return HAL_OK;
mbed_official 489:119543c9f674 1045 }
mbed_official 489:119543c9f674 1046 }
mbed_official 489:119543c9f674 1047
mbed_official 489:119543c9f674 1048 /**
mbed_official 489:119543c9f674 1049 * @brief Gets the RTC Alarm value and masks.
mbed_official 489:119543c9f674 1050 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1051 * the configuration information for RTC.
mbed_official 489:119543c9f674 1052 * @param sAlarm: Pointer to Date structure
mbed_official 489:119543c9f674 1053 * @param Alarm: Specifies the Alarm.
mbed_official 489:119543c9f674 1054 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 1055 * @arg RTC_ALARM_A: Alarm
mbed_official 489:119543c9f674 1056 * @param Format: Specifies the format of the entered parameters.
mbed_official 489:119543c9f674 1057 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 1058 * @arg RTC_FORMAT_BIN: Binary data format
mbed_official 489:119543c9f674 1059 * @arg RTC_FORMAT_BCD: BCD data format
mbed_official 489:119543c9f674 1060 * @retval HAL status
mbed_official 489:119543c9f674 1061 */
mbed_official 489:119543c9f674 1062 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
mbed_official 489:119543c9f674 1063 {
mbed_official 489:119543c9f674 1064 uint32_t counter_alarm = 0;
mbed_official 489:119543c9f674 1065
mbed_official 489:119543c9f674 1066 /* Check input parameters */
mbed_official 489:119543c9f674 1067 if((hrtc == NULL) || (sAlarm == NULL))
mbed_official 489:119543c9f674 1068 {
mbed_official 489:119543c9f674 1069 return HAL_ERROR;
mbed_official 489:119543c9f674 1070 }
mbed_official 489:119543c9f674 1071
mbed_official 489:119543c9f674 1072 /* Check the parameters */
mbed_official 489:119543c9f674 1073 assert_param(IS_RTC_FORMAT(Format));
mbed_official 489:119543c9f674 1074 assert_param(IS_RTC_ALARM(Alarm));
mbed_official 489:119543c9f674 1075
mbed_official 489:119543c9f674 1076 /* Read Alarm counter in RTC registers */
mbed_official 489:119543c9f674 1077 counter_alarm = RTC_ReadAlarmCounter(hrtc);
mbed_official 489:119543c9f674 1078
mbed_official 489:119543c9f674 1079 /* Fill the structure with the read parameters */
mbed_official 489:119543c9f674 1080 /* Set hours in a day range (between 0 to 24)*/
mbed_official 489:119543c9f674 1081 sAlarm->AlarmTime.Hours = (uint32_t)((counter_alarm / 3600) % 24);
mbed_official 489:119543c9f674 1082 sAlarm->AlarmTime.Minutes = (uint32_t)((counter_alarm % 3600) / 60);
mbed_official 489:119543c9f674 1083 sAlarm->AlarmTime.Seconds = (uint32_t)((counter_alarm % 3600) % 60);
mbed_official 489:119543c9f674 1084
mbed_official 489:119543c9f674 1085 if(Format != RTC_FORMAT_BIN)
mbed_official 489:119543c9f674 1086 {
mbed_official 489:119543c9f674 1087 sAlarm->AlarmTime.Hours = RTC_ByteToBcd2(sAlarm->AlarmTime.Hours);
mbed_official 489:119543c9f674 1088 sAlarm->AlarmTime.Minutes = RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes);
mbed_official 489:119543c9f674 1089 sAlarm->AlarmTime.Seconds = RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds);
mbed_official 489:119543c9f674 1090 }
mbed_official 489:119543c9f674 1091
mbed_official 489:119543c9f674 1092 return HAL_OK;
mbed_official 489:119543c9f674 1093 }
mbed_official 489:119543c9f674 1094
mbed_official 489:119543c9f674 1095 /**
mbed_official 489:119543c9f674 1096 * @brief Deactive the specified RTC Alarm
mbed_official 489:119543c9f674 1097 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1098 * the configuration information for RTC.
mbed_official 489:119543c9f674 1099 * @param Alarm: Specifies the Alarm.
mbed_official 489:119543c9f674 1100 * This parameter can be one of the following values:
mbed_official 489:119543c9f674 1101 * @arg RTC_ALARM_A: AlarmA
mbed_official 489:119543c9f674 1102 * @retval HAL status
mbed_official 489:119543c9f674 1103 */
mbed_official 489:119543c9f674 1104 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
mbed_official 489:119543c9f674 1105 {
mbed_official 489:119543c9f674 1106 /* Check the parameters */
mbed_official 489:119543c9f674 1107 assert_param(IS_RTC_ALARM(Alarm));
mbed_official 489:119543c9f674 1108
mbed_official 489:119543c9f674 1109 /* Check input parameters */
mbed_official 489:119543c9f674 1110 if(hrtc == NULL)
mbed_official 489:119543c9f674 1111 {
mbed_official 489:119543c9f674 1112 return HAL_ERROR;
mbed_official 489:119543c9f674 1113 }
mbed_official 489:119543c9f674 1114
mbed_official 489:119543c9f674 1115 /* Process Locked */
mbed_official 489:119543c9f674 1116 __HAL_LOCK(hrtc);
mbed_official 489:119543c9f674 1117
mbed_official 489:119543c9f674 1118 hrtc->State = HAL_RTC_STATE_BUSY;
mbed_official 489:119543c9f674 1119
mbed_official 489:119543c9f674 1120 /* In case of interrupt mode is used, the interrupt source must disabled */
mbed_official 489:119543c9f674 1121 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
mbed_official 489:119543c9f674 1122
mbed_official 489:119543c9f674 1123 /* Set Initialization mode */
mbed_official 489:119543c9f674 1124 if(RTC_EnterInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 1125 {
mbed_official 489:119543c9f674 1126 /* Set RTC state */
mbed_official 489:119543c9f674 1127 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 1128
mbed_official 489:119543c9f674 1129 /* Process Unlocked */
mbed_official 489:119543c9f674 1130 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 1131
mbed_official 489:119543c9f674 1132 return HAL_ERROR;
mbed_official 489:119543c9f674 1133 }
mbed_official 489:119543c9f674 1134 else
mbed_official 489:119543c9f674 1135 {
mbed_official 489:119543c9f674 1136 /* Clear flag alarm A */
mbed_official 489:119543c9f674 1137 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
mbed_official 489:119543c9f674 1138
mbed_official 489:119543c9f674 1139 /* Set to default values ALRH & ALRL registers */
mbed_official 489:119543c9f674 1140 WRITE_REG(hrtc->Instance->ALRH, RTC_ALARM_RESETVALUE_REGISTER);
mbed_official 489:119543c9f674 1141 WRITE_REG(hrtc->Instance->ALRL, RTC_ALARM_RESETVALUE_REGISTER);
mbed_official 489:119543c9f674 1142
mbed_official 489:119543c9f674 1143 /* RTC Alarm Interrupt Configuration: Disable EXTI configuration */
mbed_official 489:119543c9f674 1144 __HAL_RTC_ALARM_EXTI_DISABLE_IT();
mbed_official 489:119543c9f674 1145
mbed_official 489:119543c9f674 1146 /* Wait for synchro */
mbed_official 489:119543c9f674 1147 if(RTC_ExitInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 1148 {
mbed_official 489:119543c9f674 1149 hrtc->State = HAL_RTC_STATE_ERROR;
mbed_official 489:119543c9f674 1150
mbed_official 489:119543c9f674 1151 /* Process Unlocked */
mbed_official 489:119543c9f674 1152 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 1153
mbed_official 489:119543c9f674 1154 return HAL_ERROR;
mbed_official 489:119543c9f674 1155 }
mbed_official 489:119543c9f674 1156 }
mbed_official 489:119543c9f674 1157 hrtc->State = HAL_RTC_STATE_READY;
mbed_official 489:119543c9f674 1158
mbed_official 489:119543c9f674 1159 /* Process Unlocked */
mbed_official 489:119543c9f674 1160 __HAL_UNLOCK(hrtc);
mbed_official 489:119543c9f674 1161
mbed_official 489:119543c9f674 1162 return HAL_OK;
mbed_official 489:119543c9f674 1163 }
mbed_official 489:119543c9f674 1164
mbed_official 489:119543c9f674 1165 /**
mbed_official 489:119543c9f674 1166 * @brief This function handles Alarm interrupt request.
mbed_official 489:119543c9f674 1167 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1168 * the configuration information for RTC.
mbed_official 489:119543c9f674 1169 * @retval None
mbed_official 489:119543c9f674 1170 */
mbed_official 489:119543c9f674 1171 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 1172 {
mbed_official 489:119543c9f674 1173 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA))
mbed_official 489:119543c9f674 1174 {
mbed_official 489:119543c9f674 1175 /* Get the status of the Interrupt */
mbed_official 489:119543c9f674 1176 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != (uint32_t)RESET)
mbed_official 489:119543c9f674 1177 {
mbed_official 489:119543c9f674 1178 /* AlarmA callback */
mbed_official 489:119543c9f674 1179 HAL_RTC_AlarmAEventCallback(hrtc);
mbed_official 489:119543c9f674 1180
mbed_official 489:119543c9f674 1181 /* Clear the Alarm interrupt pending bit */
mbed_official 489:119543c9f674 1182 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
mbed_official 489:119543c9f674 1183 }
mbed_official 489:119543c9f674 1184 }
mbed_official 489:119543c9f674 1185
mbed_official 489:119543c9f674 1186 /* Clear the EXTI's line Flag for RTC Alarm */
mbed_official 489:119543c9f674 1187 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
mbed_official 489:119543c9f674 1188
mbed_official 489:119543c9f674 1189 /* Change RTC state */
mbed_official 489:119543c9f674 1190 hrtc->State = HAL_RTC_STATE_READY;
mbed_official 489:119543c9f674 1191 }
mbed_official 489:119543c9f674 1192
mbed_official 489:119543c9f674 1193 /**
mbed_official 489:119543c9f674 1194 * @brief Alarm A callback.
mbed_official 489:119543c9f674 1195 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1196 * the configuration information for RTC.
mbed_official 489:119543c9f674 1197 * @retval None
mbed_official 489:119543c9f674 1198 */
mbed_official 489:119543c9f674 1199 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
mbed_official 489:119543c9f674 1200 {
mbed_official 489:119543c9f674 1201 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 489:119543c9f674 1202 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
mbed_official 489:119543c9f674 1203 */
mbed_official 489:119543c9f674 1204 }
mbed_official 489:119543c9f674 1205
mbed_official 489:119543c9f674 1206 /**
mbed_official 489:119543c9f674 1207 * @brief This function handles AlarmA Polling request.
mbed_official 489:119543c9f674 1208 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1209 * the configuration information for RTC.
mbed_official 489:119543c9f674 1210 * @param Timeout: Timeout duration
mbed_official 489:119543c9f674 1211 * @retval HAL status
mbed_official 489:119543c9f674 1212 */
mbed_official 489:119543c9f674 1213 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
mbed_official 489:119543c9f674 1214 {
mbed_official 489:119543c9f674 1215 uint32_t tickstart = HAL_GetTick();
mbed_official 489:119543c9f674 1216
mbed_official 489:119543c9f674 1217 /* Check input parameters */
mbed_official 489:119543c9f674 1218 if(hrtc == NULL)
mbed_official 489:119543c9f674 1219 {
mbed_official 489:119543c9f674 1220 return HAL_ERROR;
mbed_official 489:119543c9f674 1221 }
mbed_official 489:119543c9f674 1222
mbed_official 489:119543c9f674 1223 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
mbed_official 489:119543c9f674 1224 {
mbed_official 489:119543c9f674 1225 if(Timeout != HAL_MAX_DELAY)
mbed_official 489:119543c9f674 1226 {
mbed_official 489:119543c9f674 1227 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
mbed_official 489:119543c9f674 1228 {
mbed_official 489:119543c9f674 1229 hrtc->State = HAL_RTC_STATE_TIMEOUT;
mbed_official 489:119543c9f674 1230 return HAL_TIMEOUT;
mbed_official 489:119543c9f674 1231 }
mbed_official 489:119543c9f674 1232 }
mbed_official 489:119543c9f674 1233 }
mbed_official 489:119543c9f674 1234
mbed_official 489:119543c9f674 1235 /* Clear the Alarm interrupt pending bit */
mbed_official 489:119543c9f674 1236 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
mbed_official 489:119543c9f674 1237
mbed_official 489:119543c9f674 1238 /* Change RTC state */
mbed_official 489:119543c9f674 1239 hrtc->State = HAL_RTC_STATE_READY;
mbed_official 489:119543c9f674 1240
mbed_official 489:119543c9f674 1241 return HAL_OK;
mbed_official 489:119543c9f674 1242 }
mbed_official 489:119543c9f674 1243
mbed_official 489:119543c9f674 1244 /**
mbed_official 489:119543c9f674 1245 * @}
mbed_official 489:119543c9f674 1246 */
mbed_official 489:119543c9f674 1247
mbed_official 489:119543c9f674 1248 /** @defgroup RTC_Exported_Functions_Group4 Peripheral State functions
mbed_official 489:119543c9f674 1249 * @brief Peripheral State functions
mbed_official 489:119543c9f674 1250 *
mbed_official 489:119543c9f674 1251 @verbatim
mbed_official 489:119543c9f674 1252 ===============================================================================
mbed_official 489:119543c9f674 1253 ##### Peripheral State functions #####
mbed_official 489:119543c9f674 1254 ===============================================================================
mbed_official 489:119543c9f674 1255 [..]
mbed_official 489:119543c9f674 1256 This subsection provides functions allowing to
mbed_official 489:119543c9f674 1257 (+) Get RTC state
mbed_official 489:119543c9f674 1258
mbed_official 489:119543c9f674 1259 @endverbatim
mbed_official 489:119543c9f674 1260 * @{
mbed_official 489:119543c9f674 1261 */
mbed_official 489:119543c9f674 1262 /**
mbed_official 489:119543c9f674 1263 * @brief Returns the RTC state.
mbed_official 489:119543c9f674 1264 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1265 * the configuration information for RTC.
mbed_official 489:119543c9f674 1266 * @retval HAL state
mbed_official 489:119543c9f674 1267 */
mbed_official 489:119543c9f674 1268 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 1269 {
mbed_official 489:119543c9f674 1270 return hrtc->State;
mbed_official 489:119543c9f674 1271 }
mbed_official 489:119543c9f674 1272
mbed_official 489:119543c9f674 1273 /**
mbed_official 489:119543c9f674 1274 * @}
mbed_official 489:119543c9f674 1275 */
mbed_official 489:119543c9f674 1276
mbed_official 489:119543c9f674 1277 /** @defgroup RTC_Exported_Functions_Group5 Peripheral Control functions
mbed_official 489:119543c9f674 1278 * @brief Peripheral Control functions
mbed_official 489:119543c9f674 1279 *
mbed_official 489:119543c9f674 1280 @verbatim
mbed_official 489:119543c9f674 1281 ===============================================================================
mbed_official 489:119543c9f674 1282 ##### Peripheral Control functions #####
mbed_official 489:119543c9f674 1283 ===============================================================================
mbed_official 489:119543c9f674 1284 [..]
mbed_official 489:119543c9f674 1285 This subsection provides functions allowing to
mbed_official 489:119543c9f674 1286 (+) Wait for RTC Time and Date Synchronization
mbed_official 489:119543c9f674 1287
mbed_official 489:119543c9f674 1288 @endverbatim
mbed_official 489:119543c9f674 1289 * @{
mbed_official 489:119543c9f674 1290 */
mbed_official 489:119543c9f674 1291
mbed_official 489:119543c9f674 1292 /**
mbed_official 489:119543c9f674 1293 * @brief Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
mbed_official 489:119543c9f674 1294 * are synchronized with RTC APB clock.
mbed_official 489:119543c9f674 1295 * @note This function must be called before any read operation after an APB reset
mbed_official 489:119543c9f674 1296 * or an APB clock stop.
mbed_official 489:119543c9f674 1297 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1298 * the configuration information for RTC.
mbed_official 489:119543c9f674 1299 * @retval HAL status
mbed_official 489:119543c9f674 1300 */
mbed_official 489:119543c9f674 1301 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 1302 {
mbed_official 489:119543c9f674 1303 uint32_t tickstart = 0;
mbed_official 489:119543c9f674 1304
mbed_official 489:119543c9f674 1305 /* Check input parameters */
mbed_official 489:119543c9f674 1306 if(hrtc == NULL)
mbed_official 489:119543c9f674 1307 {
mbed_official 489:119543c9f674 1308 return HAL_ERROR;
mbed_official 489:119543c9f674 1309 }
mbed_official 489:119543c9f674 1310
mbed_official 489:119543c9f674 1311 /* Clear RSF flag */
mbed_official 489:119543c9f674 1312 CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
mbed_official 489:119543c9f674 1313
mbed_official 489:119543c9f674 1314 tickstart = HAL_GetTick();
mbed_official 489:119543c9f674 1315
mbed_official 489:119543c9f674 1316 /* Wait the registers to be synchronised */
mbed_official 489:119543c9f674 1317 while((hrtc->Instance->CRL & RTC_FLAG_RSF) == (uint32_t)RESET)
mbed_official 489:119543c9f674 1318 {
mbed_official 489:119543c9f674 1319 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
mbed_official 489:119543c9f674 1320 {
mbed_official 489:119543c9f674 1321 return HAL_TIMEOUT;
mbed_official 489:119543c9f674 1322 }
mbed_official 489:119543c9f674 1323 }
mbed_official 489:119543c9f674 1324
mbed_official 489:119543c9f674 1325 return HAL_OK;
mbed_official 489:119543c9f674 1326 }
mbed_official 489:119543c9f674 1327
mbed_official 489:119543c9f674 1328 /**
mbed_official 489:119543c9f674 1329 * @}
mbed_official 489:119543c9f674 1330 */
mbed_official 489:119543c9f674 1331
mbed_official 489:119543c9f674 1332
mbed_official 489:119543c9f674 1333 /**
mbed_official 489:119543c9f674 1334 * @}
mbed_official 489:119543c9f674 1335 */
mbed_official 489:119543c9f674 1336
mbed_official 489:119543c9f674 1337 /** @addtogroup RTC_Private_Functions
mbed_official 489:119543c9f674 1338 * @{
mbed_official 489:119543c9f674 1339 */
mbed_official 489:119543c9f674 1340
mbed_official 489:119543c9f674 1341
mbed_official 489:119543c9f674 1342 /**
mbed_official 489:119543c9f674 1343 * @brief Read the time counter available in RTC_CNT registers.
mbed_official 489:119543c9f674 1344 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1345 * the configuration information for RTC.
mbed_official 489:119543c9f674 1346 * @retval Time counter
mbed_official 489:119543c9f674 1347 */
mbed_official 489:119543c9f674 1348 static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 1349 {
mbed_official 489:119543c9f674 1350 uint16_t high1 = 0, high2 = 0, low = 0;
mbed_official 489:119543c9f674 1351 uint32_t timecounter = 0;
mbed_official 489:119543c9f674 1352
mbed_official 489:119543c9f674 1353 high1 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
mbed_official 489:119543c9f674 1354 low = READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT);
mbed_official 489:119543c9f674 1355 high2 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
mbed_official 489:119543c9f674 1356
mbed_official 489:119543c9f674 1357 if (high1 != high2)
mbed_official 489:119543c9f674 1358 { /* In this case the counter roll over during reading of CNTL and CNTH registers,
mbed_official 489:119543c9f674 1359 read again CNTL register then return the counter value */
mbed_official 489:119543c9f674 1360 timecounter = (((uint32_t) high2 << 16 ) | READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT));
mbed_official 489:119543c9f674 1361 }
mbed_official 489:119543c9f674 1362 else
mbed_official 489:119543c9f674 1363 { /* No counter roll over during reading of CNTL and CNTH registers, counter
mbed_official 489:119543c9f674 1364 value is equal to first value of CNTL and CNTH */
mbed_official 489:119543c9f674 1365 timecounter = (((uint32_t) high1 << 16 ) | low);
mbed_official 489:119543c9f674 1366 }
mbed_official 489:119543c9f674 1367
mbed_official 489:119543c9f674 1368 return timecounter;
mbed_official 489:119543c9f674 1369 }
mbed_official 489:119543c9f674 1370
mbed_official 489:119543c9f674 1371 /**
mbed_official 489:119543c9f674 1372 * @brief Write the time counter in RTC_CNT registers.
mbed_official 489:119543c9f674 1373 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1374 * the configuration information for RTC.
mbed_official 489:119543c9f674 1375 * @param TimeCounter: Counter to write in RTC_CNT registers
mbed_official 489:119543c9f674 1376 * @retval HAL status
mbed_official 489:119543c9f674 1377 */
mbed_official 489:119543c9f674 1378 static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter)
mbed_official 489:119543c9f674 1379 {
mbed_official 489:119543c9f674 1380 HAL_StatusTypeDef status = HAL_OK;
mbed_official 489:119543c9f674 1381
mbed_official 489:119543c9f674 1382 /* Set Initialization mode */
mbed_official 489:119543c9f674 1383 if(RTC_EnterInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 1384 {
mbed_official 489:119543c9f674 1385 status = HAL_ERROR;
mbed_official 489:119543c9f674 1386 }
mbed_official 489:119543c9f674 1387 else
mbed_official 489:119543c9f674 1388 {
mbed_official 489:119543c9f674 1389 /* Set RTC COUNTER MSB word */
mbed_official 489:119543c9f674 1390 WRITE_REG(hrtc->Instance->CNTH, (TimeCounter >> 16));
mbed_official 489:119543c9f674 1391 /* Set RTC COUNTER LSB word */
mbed_official 489:119543c9f674 1392 WRITE_REG(hrtc->Instance->CNTL, (TimeCounter & RTC_CNTL_RTC_CNT));
mbed_official 489:119543c9f674 1393
mbed_official 489:119543c9f674 1394 /* Wait for synchro */
mbed_official 489:119543c9f674 1395 if(RTC_ExitInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 1396 {
mbed_official 489:119543c9f674 1397 status = HAL_ERROR;
mbed_official 489:119543c9f674 1398 }
mbed_official 489:119543c9f674 1399 }
mbed_official 489:119543c9f674 1400
mbed_official 489:119543c9f674 1401 return status;
mbed_official 489:119543c9f674 1402 }
mbed_official 489:119543c9f674 1403
mbed_official 489:119543c9f674 1404 /**
mbed_official 489:119543c9f674 1405 * @brief Read the time counter available in RTC_ALR registers.
mbed_official 489:119543c9f674 1406 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1407 * the configuration information for RTC.
mbed_official 489:119543c9f674 1408 * @retval Time counter
mbed_official 489:119543c9f674 1409 */
mbed_official 489:119543c9f674 1410 static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 1411 {
mbed_official 489:119543c9f674 1412 uint16_t high1 = 0, low = 0;
mbed_official 489:119543c9f674 1413
mbed_official 489:119543c9f674 1414 high1 = READ_REG(hrtc->Instance->ALRH & RTC_CNTH_RTC_CNT);
mbed_official 489:119543c9f674 1415 low = READ_REG(hrtc->Instance->ALRL & RTC_CNTL_RTC_CNT);
mbed_official 489:119543c9f674 1416
mbed_official 489:119543c9f674 1417 return (((uint32_t) high1 << 16 ) | low);
mbed_official 489:119543c9f674 1418 }
mbed_official 489:119543c9f674 1419
mbed_official 489:119543c9f674 1420 /**
mbed_official 489:119543c9f674 1421 * @brief Write the time counter in RTC_ALR registers.
mbed_official 489:119543c9f674 1422 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1423 * the configuration information for RTC.
mbed_official 489:119543c9f674 1424 * @param AlarmCounter: Counter to write in RTC_ALR registers
mbed_official 489:119543c9f674 1425 * @retval HAL status
mbed_official 489:119543c9f674 1426 */
mbed_official 489:119543c9f674 1427 static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter)
mbed_official 489:119543c9f674 1428 {
mbed_official 489:119543c9f674 1429 HAL_StatusTypeDef status = HAL_OK;
mbed_official 489:119543c9f674 1430
mbed_official 489:119543c9f674 1431 /* Set Initialization mode */
mbed_official 489:119543c9f674 1432 if(RTC_EnterInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 1433 {
mbed_official 489:119543c9f674 1434 status = HAL_ERROR;
mbed_official 489:119543c9f674 1435 }
mbed_official 489:119543c9f674 1436 else
mbed_official 489:119543c9f674 1437 {
mbed_official 489:119543c9f674 1438 /* Set RTC COUNTER MSB word */
mbed_official 489:119543c9f674 1439 WRITE_REG(hrtc->Instance->ALRH, (AlarmCounter >> 16));
mbed_official 489:119543c9f674 1440 /* Set RTC COUNTER LSB word */
mbed_official 489:119543c9f674 1441 WRITE_REG(hrtc->Instance->ALRL, (AlarmCounter & RTC_ALRL_RTC_ALR));
mbed_official 489:119543c9f674 1442
mbed_official 489:119543c9f674 1443 /* Wait for synchro */
mbed_official 489:119543c9f674 1444 if(RTC_ExitInitMode(hrtc) != HAL_OK)
mbed_official 489:119543c9f674 1445 {
mbed_official 489:119543c9f674 1446 status = HAL_ERROR;
mbed_official 489:119543c9f674 1447 }
mbed_official 489:119543c9f674 1448 }
mbed_official 489:119543c9f674 1449
mbed_official 489:119543c9f674 1450 return status;
mbed_official 489:119543c9f674 1451 }
mbed_official 489:119543c9f674 1452
mbed_official 489:119543c9f674 1453 /**
mbed_official 489:119543c9f674 1454 * @brief Enters the RTC Initialization mode.
mbed_official 489:119543c9f674 1455 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1456 * the configuration information for RTC.
mbed_official 489:119543c9f674 1457 * @retval HAL status
mbed_official 489:119543c9f674 1458 */
mbed_official 489:119543c9f674 1459 static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 1460 {
mbed_official 489:119543c9f674 1461 uint32_t tickstart = 0;
mbed_official 489:119543c9f674 1462
mbed_official 489:119543c9f674 1463 tickstart = HAL_GetTick();
mbed_official 489:119543c9f674 1464 /* Wait till RTC is in INIT state and if Time out is reached exit */
mbed_official 489:119543c9f674 1465 while((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
mbed_official 489:119543c9f674 1466 {
mbed_official 489:119543c9f674 1467 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
mbed_official 489:119543c9f674 1468 {
mbed_official 489:119543c9f674 1469 return HAL_TIMEOUT;
mbed_official 489:119543c9f674 1470 }
mbed_official 489:119543c9f674 1471 }
mbed_official 489:119543c9f674 1472
mbed_official 489:119543c9f674 1473 /* Disable the write protection for RTC registers */
mbed_official 489:119543c9f674 1474 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
mbed_official 489:119543c9f674 1475
mbed_official 489:119543c9f674 1476
mbed_official 489:119543c9f674 1477 return HAL_OK;
mbed_official 489:119543c9f674 1478 }
mbed_official 489:119543c9f674 1479
mbed_official 489:119543c9f674 1480 /**
mbed_official 489:119543c9f674 1481 * @brief Exit the RTC Initialization mode.
mbed_official 489:119543c9f674 1482 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1483 * the configuration information for RTC.
mbed_official 489:119543c9f674 1484 * @retval HAL status
mbed_official 489:119543c9f674 1485 */
mbed_official 489:119543c9f674 1486 static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef* hrtc)
mbed_official 489:119543c9f674 1487 {
mbed_official 489:119543c9f674 1488 uint32_t tickstart = 0;
mbed_official 489:119543c9f674 1489
mbed_official 489:119543c9f674 1490 /* Disable the write protection for RTC registers */
mbed_official 489:119543c9f674 1491 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
mbed_official 489:119543c9f674 1492
mbed_official 489:119543c9f674 1493 tickstart = HAL_GetTick();
mbed_official 489:119543c9f674 1494 /* Wait till RTC is in INIT state and if Time out is reached exit */
mbed_official 489:119543c9f674 1495 while((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
mbed_official 489:119543c9f674 1496 {
mbed_official 489:119543c9f674 1497 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
mbed_official 489:119543c9f674 1498 {
mbed_official 489:119543c9f674 1499 return HAL_TIMEOUT;
mbed_official 489:119543c9f674 1500 }
mbed_official 489:119543c9f674 1501 }
mbed_official 489:119543c9f674 1502
mbed_official 489:119543c9f674 1503 return HAL_OK;
mbed_official 489:119543c9f674 1504 }
mbed_official 489:119543c9f674 1505
mbed_official 489:119543c9f674 1506 /**
mbed_official 489:119543c9f674 1507 * @brief Converts a 2 digit decimal to BCD format.
mbed_official 489:119543c9f674 1508 * @param Value: Byte to be converted
mbed_official 489:119543c9f674 1509 * @retval Converted byte
mbed_official 489:119543c9f674 1510 */
mbed_official 489:119543c9f674 1511 static uint8_t RTC_ByteToBcd2(uint8_t Value)
mbed_official 489:119543c9f674 1512 {
mbed_official 489:119543c9f674 1513 uint32_t bcdhigh = 0;
mbed_official 489:119543c9f674 1514
mbed_official 489:119543c9f674 1515 while(Value >= 10)
mbed_official 489:119543c9f674 1516 {
mbed_official 489:119543c9f674 1517 bcdhigh++;
mbed_official 489:119543c9f674 1518 Value -= 10;
mbed_official 489:119543c9f674 1519 }
mbed_official 489:119543c9f674 1520
mbed_official 489:119543c9f674 1521 return ((uint8_t)(bcdhigh << 4) | Value);
mbed_official 489:119543c9f674 1522 }
mbed_official 489:119543c9f674 1523
mbed_official 489:119543c9f674 1524 /**
mbed_official 489:119543c9f674 1525 * @brief Converts from 2 digit BCD to Binary.
mbed_official 489:119543c9f674 1526 * @param Value: BCD value to be converted
mbed_official 489:119543c9f674 1527 * @retval Converted word
mbed_official 489:119543c9f674 1528 */
mbed_official 489:119543c9f674 1529 static uint8_t RTC_Bcd2ToByte(uint8_t Value)
mbed_official 489:119543c9f674 1530 {
mbed_official 489:119543c9f674 1531 uint32_t tmp = 0;
mbed_official 489:119543c9f674 1532 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
mbed_official 489:119543c9f674 1533 return (tmp + (Value & (uint8_t)0x0F));
mbed_official 489:119543c9f674 1534 }
mbed_official 489:119543c9f674 1535
mbed_official 489:119543c9f674 1536 /**
mbed_official 489:119543c9f674 1537 * @brief Updates date when time is 23:59:59.
mbed_official 489:119543c9f674 1538 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
mbed_official 489:119543c9f674 1539 * the configuration information for RTC.
mbed_official 489:119543c9f674 1540 * @param DayElapsed: Number of days elapsed from last date update
mbed_official 489:119543c9f674 1541 * @retval None
mbed_official 489:119543c9f674 1542 */
mbed_official 489:119543c9f674 1543 static void RTC_DateUpdate(RTC_HandleTypeDef* hrtc, uint32_t DayElapsed)
mbed_official 489:119543c9f674 1544 {
mbed_official 489:119543c9f674 1545 uint32_t year = 0, month = 0, day = 0;
mbed_official 489:119543c9f674 1546 uint32_t loop = 0;
mbed_official 489:119543c9f674 1547
mbed_official 489:119543c9f674 1548 /* Get the current year*/
mbed_official 489:119543c9f674 1549 year = hrtc->DateToUpdate.Year;
mbed_official 489:119543c9f674 1550
mbed_official 489:119543c9f674 1551 /* Get the current month and day */
mbed_official 489:119543c9f674 1552 month = hrtc->DateToUpdate.Month;
mbed_official 489:119543c9f674 1553 day = hrtc->DateToUpdate.Date;
mbed_official 489:119543c9f674 1554
mbed_official 489:119543c9f674 1555 for (loop = 0; loop < DayElapsed; loop++)
mbed_official 489:119543c9f674 1556 {
mbed_official 489:119543c9f674 1557 if((month == 1) || (month == 3) || (month == 5) || (month == 7) || \
mbed_official 489:119543c9f674 1558 (month == 8) || (month == 10) || (month == 12))
mbed_official 489:119543c9f674 1559 {
mbed_official 489:119543c9f674 1560 if(day < 31)
mbed_official 489:119543c9f674 1561 {
mbed_official 489:119543c9f674 1562 day++;
mbed_official 489:119543c9f674 1563 }
mbed_official 489:119543c9f674 1564 /* Date structure member: day = 31 */
mbed_official 489:119543c9f674 1565 else
mbed_official 489:119543c9f674 1566 {
mbed_official 489:119543c9f674 1567 if(month != 12)
mbed_official 489:119543c9f674 1568 {
mbed_official 489:119543c9f674 1569 month++;
mbed_official 489:119543c9f674 1570 day = 1;
mbed_official 489:119543c9f674 1571 }
mbed_official 489:119543c9f674 1572 /* Date structure member: day = 31 & month =12 */
mbed_official 489:119543c9f674 1573 else
mbed_official 489:119543c9f674 1574 {
mbed_official 489:119543c9f674 1575 month = 1;
mbed_official 489:119543c9f674 1576 day = 1;
mbed_official 489:119543c9f674 1577 year++;
mbed_official 489:119543c9f674 1578 }
mbed_official 489:119543c9f674 1579 }
mbed_official 489:119543c9f674 1580 }
mbed_official 489:119543c9f674 1581 else if((month == 4) || (month == 6) || (month == 9) || (month == 11))
mbed_official 489:119543c9f674 1582 {
mbed_official 489:119543c9f674 1583 if(day < 30)
mbed_official 489:119543c9f674 1584 {
mbed_official 489:119543c9f674 1585 day++;
mbed_official 489:119543c9f674 1586 }
mbed_official 489:119543c9f674 1587 /* Date structure member: day = 30 */
mbed_official 489:119543c9f674 1588 else
mbed_official 489:119543c9f674 1589 {
mbed_official 489:119543c9f674 1590 month++;
mbed_official 489:119543c9f674 1591 day = 1;
mbed_official 489:119543c9f674 1592 }
mbed_official 489:119543c9f674 1593 }
mbed_official 489:119543c9f674 1594 else if(month == 2)
mbed_official 489:119543c9f674 1595 {
mbed_official 489:119543c9f674 1596 if(day < 28)
mbed_official 489:119543c9f674 1597 {
mbed_official 489:119543c9f674 1598 day++;
mbed_official 489:119543c9f674 1599 }
mbed_official 489:119543c9f674 1600 else if(day == 28)
mbed_official 489:119543c9f674 1601 {
mbed_official 489:119543c9f674 1602 /* Leap year */
mbed_official 489:119543c9f674 1603 if(RTC_IsLeapYear(year))
mbed_official 489:119543c9f674 1604 {
mbed_official 489:119543c9f674 1605 day++;
mbed_official 489:119543c9f674 1606 }
mbed_official 489:119543c9f674 1607 else
mbed_official 489:119543c9f674 1608 {
mbed_official 489:119543c9f674 1609 month++;
mbed_official 489:119543c9f674 1610 day = 1;
mbed_official 489:119543c9f674 1611 }
mbed_official 489:119543c9f674 1612 }
mbed_official 489:119543c9f674 1613 else if(day == 29)
mbed_official 489:119543c9f674 1614 {
mbed_official 489:119543c9f674 1615 month++;
mbed_official 489:119543c9f674 1616 day = 1;
mbed_official 489:119543c9f674 1617 }
mbed_official 489:119543c9f674 1618 }
mbed_official 489:119543c9f674 1619 }
mbed_official 489:119543c9f674 1620
mbed_official 489:119543c9f674 1621 /* Update year */
mbed_official 489:119543c9f674 1622 hrtc->DateToUpdate.Year = year;
mbed_official 489:119543c9f674 1623
mbed_official 489:119543c9f674 1624 /* Update day and month */
mbed_official 489:119543c9f674 1625 hrtc->DateToUpdate.Month = month;
mbed_official 489:119543c9f674 1626 hrtc->DateToUpdate.Date = day;
mbed_official 489:119543c9f674 1627
mbed_official 489:119543c9f674 1628 /* Update day of the week */
mbed_official 489:119543c9f674 1629 hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(year, month, day);
mbed_official 489:119543c9f674 1630 }
mbed_official 489:119543c9f674 1631
mbed_official 489:119543c9f674 1632 /**
mbed_official 489:119543c9f674 1633 * @brief Check whether the passed year is Leap or not.
mbed_official 489:119543c9f674 1634 * @param nYear year to check
mbed_official 489:119543c9f674 1635 * @retval 1: leap year
mbed_official 489:119543c9f674 1636 * 0: not leap year
mbed_official 489:119543c9f674 1637 */
mbed_official 489:119543c9f674 1638 static uint8_t RTC_IsLeapYear(uint16_t nYear)
mbed_official 489:119543c9f674 1639 {
mbed_official 489:119543c9f674 1640 if((nYear % 4) != 0)
mbed_official 489:119543c9f674 1641 {
mbed_official 489:119543c9f674 1642 return 0;
mbed_official 489:119543c9f674 1643 }
mbed_official 489:119543c9f674 1644
mbed_official 489:119543c9f674 1645 if((nYear % 100) != 0)
mbed_official 489:119543c9f674 1646 {
mbed_official 489:119543c9f674 1647 return 1;
mbed_official 489:119543c9f674 1648 }
mbed_official 489:119543c9f674 1649
mbed_official 489:119543c9f674 1650 if((nYear % 400) == 0)
mbed_official 489:119543c9f674 1651 {
mbed_official 489:119543c9f674 1652 return 1;
mbed_official 489:119543c9f674 1653 }
mbed_official 489:119543c9f674 1654 else
mbed_official 489:119543c9f674 1655 {
mbed_official 489:119543c9f674 1656 return 0;
mbed_official 489:119543c9f674 1657 }
mbed_official 489:119543c9f674 1658 }
mbed_official 489:119543c9f674 1659
mbed_official 489:119543c9f674 1660 /**
mbed_official 489:119543c9f674 1661 * @brief Determines the week number, the day number and the week day number.
mbed_official 489:119543c9f674 1662 * @param nYear year to check
mbed_official 489:119543c9f674 1663 * @param nMonth Month to check
mbed_official 489:119543c9f674 1664 * @param nDay Day to check
mbed_official 489:119543c9f674 1665 * @note Day is calculated with hypothesis that year > 2000
mbed_official 489:119543c9f674 1666 * @retval Value which can take one of the following parameters:
mbed_official 489:119543c9f674 1667 * @arg RTC_WEEKDAY_MONDAY
mbed_official 489:119543c9f674 1668 * @arg RTC_WEEKDAY_TUESDAY
mbed_official 489:119543c9f674 1669 * @arg RTC_WEEKDAY_WEDNESDAY
mbed_official 489:119543c9f674 1670 * @arg RTC_WEEKDAY_THURSDAY
mbed_official 489:119543c9f674 1671 * @arg RTC_WEEKDAY_FRIDAY
mbed_official 489:119543c9f674 1672 * @arg RTC_WEEKDAY_SATURDAY
mbed_official 489:119543c9f674 1673 * @arg RTC_WEEKDAY_SUNDAY
mbed_official 489:119543c9f674 1674 */
mbed_official 489:119543c9f674 1675 static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay)
mbed_official 489:119543c9f674 1676 {
mbed_official 489:119543c9f674 1677 uint32_t year = 0, weekday = 0;
mbed_official 489:119543c9f674 1678
mbed_official 489:119543c9f674 1679 year = 2000 + nYear;
mbed_official 489:119543c9f674 1680
mbed_official 489:119543c9f674 1681 if(nMonth < 3)
mbed_official 489:119543c9f674 1682 {
mbed_official 489:119543c9f674 1683 /*D = { [(23 x month)/9] + day + 4 + year + [(year-1)/4] - [(year-1)/100] + [(year-1)/400] } mod 7*/
mbed_official 489:119543c9f674 1684 weekday = (((23 * nMonth)/9) + nDay + 4 + year + ((year-1)/4) - ((year-1)/100) + ((year-1)/400)) % 7;
mbed_official 489:119543c9f674 1685 }
mbed_official 489:119543c9f674 1686 else
mbed_official 489:119543c9f674 1687 {
mbed_official 489:119543c9f674 1688 /*D = { [(23 x month)/9] + day + 4 + year + [year/4] - [year/100] + [year/400] - 2 } mod 7*/
mbed_official 489:119543c9f674 1689 weekday = (((23 * nMonth)/9) + nDay + 4 + year + (year/4) - (year/100) + (year/400) - 2 ) % 7;
mbed_official 489:119543c9f674 1690 }
mbed_official 489:119543c9f674 1691
mbed_official 489:119543c9f674 1692 return (uint8_t)weekday;
mbed_official 489:119543c9f674 1693 }
mbed_official 489:119543c9f674 1694
mbed_official 489:119543c9f674 1695 /**
mbed_official 489:119543c9f674 1696 * @}
mbed_official 489:119543c9f674 1697 */
mbed_official 489:119543c9f674 1698
mbed_official 489:119543c9f674 1699 #endif /* HAL_RTC_MODULE_ENABLED */
mbed_official 489:119543c9f674 1700 /**
mbed_official 489:119543c9f674 1701 * @}
mbed_official 489:119543c9f674 1702 */
mbed_official 489:119543c9f674 1703
mbed_official 489:119543c9f674 1704 /**
mbed_official 489:119543c9f674 1705 * @}
mbed_official 489:119543c9f674 1706 */
mbed_official 489:119543c9f674 1707
mbed_official 489:119543c9f674 1708 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/