2015-03-12 09:20:56 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
*******************************************************************************
|
2016-09-15 12:00:24 +00:00
|
|
|
* Copyright (c) 2016, STMicroelectronics
|
2015-03-12 09:20:56 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
#if DEVICE_RTC
|
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
#include "rtc_api.h"
|
|
|
|
#include "rtc_api_hal.h"
|
2015-03-12 09:20:56 +00:00
|
|
|
#include "mbed_error.h"
|
2017-06-08 02:59:17 +00:00
|
|
|
#include "mbed_mktime.h"
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
static RTC_HandleTypeDef RtcHandle;
|
|
|
|
|
2016-09-27 09:26:43 +00:00
|
|
|
#if RTC_LSI
|
2016-12-06 08:54:24 +00:00
|
|
|
#define RTC_CLOCK LSI_VALUE
|
2016-09-15 12:00:24 +00:00
|
|
|
#else
|
2016-12-06 08:54:24 +00:00
|
|
|
#define RTC_CLOCK LSE_VALUE
|
2016-09-15 12:00:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEVICE_LOWPOWERTIMER
|
2016-12-06 08:54:24 +00:00
|
|
|
#define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000)
|
|
|
|
#define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1)
|
2016-09-15 12:00:24 +00:00
|
|
|
#else
|
2016-12-06 08:54:24 +00:00
|
|
|
#define RTC_ASYNCH_PREDIV (0x007F)
|
|
|
|
#define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1)
|
2016-09-15 12:00:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEVICE_LOWPOWERTIMER
|
2016-12-06 08:54:24 +00:00
|
|
|
static void (*irq_handler)(void);
|
|
|
|
static void RTC_IRQHandler(void);
|
2016-09-15 12:00:24 +00:00
|
|
|
#endif
|
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
void rtc_init(void)
|
|
|
|
{
|
2017-10-06 13:44:16 +00:00
|
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
2015-03-12 09:20:56 +00:00
|
|
|
|
2016-04-14 12:48:11 +00:00
|
|
|
// Enable access to Backup domain
|
|
|
|
HAL_PWR_EnableBkUpAccess();
|
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
RtcHandle.Instance = RTC;
|
2017-10-10 14:42:08 +00:00
|
|
|
RtcHandle.State = HAL_RTC_STATE_RESET;
|
2016-11-23 15:41:32 +00:00
|
|
|
|
2016-09-27 09:26:43 +00:00
|
|
|
#if !RTC_LSI
|
2015-03-12 09:20:56 +00:00
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
|
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
|
2016-11-23 15:41:32 +00:00
|
|
|
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
|
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
|
|
|
|
|
2016-12-06 08:54:24 +00:00
|
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
|
2016-11-23 15:41:32 +00:00
|
|
|
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
|
|
|
|
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
|
2016-12-06 08:54:24 +00:00
|
|
|
} else {
|
|
|
|
error("Cannot initialize RTC with LSE\n");
|
2016-11-23 15:41:32 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 12:48:11 +00:00
|
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
|
|
|
|
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
|
2016-12-06 08:54:24 +00:00
|
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
|
2016-11-23 15:41:32 +00:00
|
|
|
error("PeriphClkInitStruct RTC failed with LSE\n");
|
2016-04-15 12:37:08 +00:00
|
|
|
}
|
2016-11-23 15:41:32 +00:00
|
|
|
#else /* !RTC_LSI */
|
2017-05-04 10:17:51 +00:00
|
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
2016-11-23 15:41:32 +00:00
|
|
|
|
2016-04-04 09:05:52 +00:00
|
|
|
// Reset Backup domain
|
|
|
|
__HAL_RCC_BACKUPRESET_FORCE();
|
|
|
|
__HAL_RCC_BACKUPRESET_RELEASE();
|
|
|
|
|
2016-03-25 13:56:56 +00:00
|
|
|
// Enable LSI clock
|
2016-11-23 15:41:32 +00:00
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
|
2016-03-25 13:56:56 +00:00
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
|
|
|
|
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
|
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
2016-12-06 08:54:24 +00:00
|
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
|
2016-11-23 15:41:32 +00:00
|
|
|
error("Cannot initialize RTC with LSI\n");
|
2016-03-25 13:56:56 +00:00
|
|
|
}
|
2016-11-23 15:41:32 +00:00
|
|
|
|
|
|
|
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
|
|
|
|
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
|
|
|
|
|
2016-04-14 12:48:11 +00:00
|
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
|
2016-04-15 12:37:08 +00:00
|
|
|
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
|
2016-12-06 08:54:24 +00:00
|
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
|
2016-11-23 15:41:32 +00:00
|
|
|
error("PeriphClkInitStruct RTC failed with LSI\n");
|
2016-04-15 12:37:08 +00:00
|
|
|
}
|
2016-11-23 15:41:32 +00:00
|
|
|
#endif /* !RTC_LSI */
|
2016-01-11 17:11:19 +00:00
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
// Enable RTC
|
|
|
|
__HAL_RCC_RTC_ENABLE();
|
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
#if TARGET_STM32F1
|
|
|
|
RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
|
|
|
|
#else /* TARGET_STM32F1 */
|
2015-03-12 09:20:56 +00:00
|
|
|
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
|
2016-09-15 12:00:24 +00:00
|
|
|
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
|
|
|
|
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
|
2015-03-12 09:20:56 +00:00
|
|
|
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
|
|
|
|
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
|
|
|
|
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
|
2016-11-23 15:41:32 +00:00
|
|
|
#endif /* TARGET_STM32F1 */
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
|
|
|
|
error("RTC error: RTC initialization failed.");
|
|
|
|
}
|
2016-09-15 12:00:24 +00:00
|
|
|
|
|
|
|
#if DEVICE_LOWPOWERTIMER
|
2016-11-23 15:41:32 +00:00
|
|
|
|
|
|
|
#if !RTC_LSI
|
|
|
|
if (!rtc_isenabled())
|
|
|
|
#endif /* !RTC_LSI */
|
|
|
|
{
|
2016-09-15 12:00:24 +00:00
|
|
|
rtc_write(0);
|
|
|
|
}
|
2016-11-23 15:41:32 +00:00
|
|
|
|
|
|
|
NVIC_ClearPendingIRQ(RTC_WKUP_IRQn);
|
|
|
|
NVIC_DisableIRQ(RTC_WKUP_IRQn);
|
|
|
|
NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
|
|
|
|
NVIC_EnableIRQ(RTC_WKUP_IRQn);
|
|
|
|
|
|
|
|
#endif /* DEVICE_LOWPOWERTIMER */
|
2015-03-12 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_free(void)
|
|
|
|
{
|
2016-09-27 09:26:43 +00:00
|
|
|
#if RTC_LSI
|
2015-03-12 09:20:56 +00:00
|
|
|
// Enable Power clock
|
2017-05-04 10:17:51 +00:00
|
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
// Enable access to Backup domain
|
|
|
|
HAL_PWR_EnableBkUpAccess();
|
|
|
|
|
|
|
|
// Reset Backup domain
|
|
|
|
__HAL_RCC_BACKUPRESET_FORCE();
|
|
|
|
__HAL_RCC_BACKUPRESET_RELEASE();
|
|
|
|
|
|
|
|
// Disable access to Backup domain
|
|
|
|
HAL_PWR_DisableBkUpAccess();
|
2016-04-04 09:05:52 +00:00
|
|
|
#endif
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
// Disable LSI and LSE clocks
|
2017-10-06 13:44:16 +00:00
|
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
2015-03-12 09:20:56 +00:00
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
|
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
|
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
|
|
|
|
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
|
|
|
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-02-22 07:50:29 +00:00
|
|
|
ST RTC_DateTypeDef structure
|
|
|
|
WeekDay 1=monday, 2=tuesday, ..., 7=sunday
|
|
|
|
Month 0x1=january, 0x2=february, ..., 0x12=december
|
|
|
|
Date day of the month 1-31
|
|
|
|
Year year 0-99
|
|
|
|
|
|
|
|
ST RTC_TimeTypeDef structure
|
|
|
|
Hours 0-12 if the RTC_HourFormat_12 is selected during init
|
|
|
|
0-23 if the RTC_HourFormat_24 is selected during init
|
|
|
|
Minutes 0-59
|
|
|
|
Seconds 0-59
|
|
|
|
TimeFormat RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM
|
|
|
|
SubSeconds time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity
|
|
|
|
SecondFraction range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S)
|
|
|
|
DayLightSaving RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE
|
|
|
|
StoreOperation RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET
|
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
struct tm
|
|
|
|
tm_sec seconds after the minute 0-61
|
|
|
|
tm_min minutes after the hour 0-59
|
|
|
|
tm_hour hours since midnight 0-23
|
|
|
|
tm_mday day of the month 1-31
|
|
|
|
tm_mon months since January 0-11
|
|
|
|
tm_year years since 1900
|
|
|
|
tm_wday days since Sunday 0-6
|
|
|
|
tm_yday days since January 1 0-365
|
|
|
|
tm_isdst Daylight Saving Time flag
|
|
|
|
*/
|
2017-02-22 07:50:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4:
|
|
|
|
BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits.
|
|
|
|
Because the first year is reserved to see if the RTC is init, the supposed range is 01-99.
|
|
|
|
1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t).
|
|
|
|
2nd point is to keep the year 1970 and the leap years synchronized.
|
|
|
|
|
|
|
|
So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038.
|
|
|
|
68 is also a multiple of 4 so it let the leap year synchronized.
|
|
|
|
|
|
|
|
Information about STM32F1:
|
|
|
|
32bit register is used (no BCD format) for the seconds and a software structure to store dates.
|
|
|
|
It is then not a problem to not use shifts.
|
|
|
|
*/
|
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
time_t rtc_read(void)
|
|
|
|
{
|
2017-10-06 13:44:16 +00:00
|
|
|
RTC_DateTypeDef dateStruct = {0};
|
|
|
|
RTC_TimeTypeDef timeStruct = {0};
|
2015-03-12 09:20:56 +00:00
|
|
|
struct tm timeinfo;
|
|
|
|
|
|
|
|
RtcHandle.Instance = RTC;
|
|
|
|
|
|
|
|
// Read actual date and time
|
|
|
|
// Warning: the time must be read first!
|
2017-02-22 07:50:29 +00:00
|
|
|
HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
|
|
|
|
HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
// Setup a tm structure based on the RTC
|
2017-02-22 07:50:29 +00:00
|
|
|
/* tm_wday information is ignored by mktime */
|
2015-03-12 09:20:56 +00:00
|
|
|
timeinfo.tm_mon = dateStruct.Month - 1;
|
|
|
|
timeinfo.tm_mday = dateStruct.Date;
|
2016-07-06 15:24:16 +00:00
|
|
|
timeinfo.tm_year = dateStruct.Year + 68;
|
2015-03-12 09:20:56 +00:00
|
|
|
timeinfo.tm_hour = timeStruct.Hours;
|
|
|
|
timeinfo.tm_min = timeStruct.Minutes;
|
|
|
|
timeinfo.tm_sec = timeStruct.Seconds;
|
2016-04-14 12:48:11 +00:00
|
|
|
// Daylight Saving Time information is not available
|
2016-11-23 15:41:32 +00:00
|
|
|
timeinfo.tm_isdst = -1;
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
// Convert to timestamp
|
2017-06-08 02:59:17 +00:00
|
|
|
time_t t = _rtc_mktime(&timeinfo);
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_write(time_t t)
|
|
|
|
{
|
2017-10-06 13:44:16 +00:00
|
|
|
RTC_DateTypeDef dateStruct = {0};
|
|
|
|
RTC_TimeTypeDef timeStruct = {0};
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
RtcHandle.Instance = RTC;
|
|
|
|
|
|
|
|
// Convert the time into a tm
|
2017-06-08 02:59:17 +00:00
|
|
|
struct tm timeinfo;
|
2017-06-08 04:24:48 +00:00
|
|
|
if (_rtc_localtime(t, &timeinfo) == false) {
|
2017-06-08 02:59:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
// Fill RTC structures
|
2017-06-08 02:59:17 +00:00
|
|
|
if (timeinfo.tm_wday == 0) {
|
2017-02-22 07:50:29 +00:00
|
|
|
dateStruct.WeekDay = 7;
|
|
|
|
} else {
|
2017-06-08 02:59:17 +00:00
|
|
|
dateStruct.WeekDay = timeinfo.tm_wday;
|
2017-02-22 07:50:29 +00:00
|
|
|
}
|
2017-06-08 02:59:17 +00:00
|
|
|
dateStruct.Month = timeinfo.tm_mon + 1;
|
|
|
|
dateStruct.Date = timeinfo.tm_mday;
|
|
|
|
dateStruct.Year = timeinfo.tm_year - 68;
|
|
|
|
timeStruct.Hours = timeinfo.tm_hour;
|
|
|
|
timeStruct.Minutes = timeinfo.tm_min;
|
|
|
|
timeStruct.Seconds = timeinfo.tm_sec;
|
2016-11-23 15:41:32 +00:00
|
|
|
|
|
|
|
#if !(TARGET_STM32F1)
|
2016-07-06 15:24:16 +00:00
|
|
|
timeStruct.TimeFormat = RTC_HOURFORMAT_24;
|
2015-03-12 09:20:56 +00:00
|
|
|
timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
|
|
|
|
timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
|
2016-11-23 15:41:32 +00:00
|
|
|
#endif /* TARGET_STM32F1 */
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
// Change the RTC current date/time
|
2017-02-22 07:50:29 +00:00
|
|
|
HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
|
|
|
|
HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
|
2015-03-12 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
int rtc_isenabled(void)
|
|
|
|
{
|
2017-02-22 07:50:29 +00:00
|
|
|
#if !(TARGET_STM32F1)
|
|
|
|
return ( ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) == RTC_ISR_RSF) );
|
|
|
|
#else /* TARGET_STM32F1 */
|
|
|
|
return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF);
|
|
|
|
#endif /* TARGET_STM32F1 */
|
2016-11-23 15:41:32 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 12:00:24 +00:00
|
|
|
#if DEVICE_LOWPOWERTIMER
|
|
|
|
|
|
|
|
static void RTC_IRQHandler(void)
|
|
|
|
{
|
2017-10-10 15:04:29 +00:00
|
|
|
/* Update HAL state */
|
2016-09-15 12:00:24 +00:00
|
|
|
HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
|
2017-10-10 15:04:29 +00:00
|
|
|
/* In case of registered handler, call it. */
|
2016-09-15 12:00:24 +00:00
|
|
|
if (irq_handler) {
|
|
|
|
irq_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_set_irq_handler(uint32_t handler)
|
|
|
|
{
|
|
|
|
irq_handler = (void (*)(void))handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t rtc_read_subseconds(void)
|
|
|
|
{
|
|
|
|
return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_set_wake_up_timer(uint32_t delta)
|
|
|
|
{
|
|
|
|
uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK);
|
2016-11-23 15:41:32 +00:00
|
|
|
|
2016-09-15 12:00:24 +00:00
|
|
|
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter,
|
|
|
|
RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) {
|
|
|
|
error("Set wake up timer failed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_deactivate_wake_up_timer(void)
|
|
|
|
{
|
|
|
|
HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_synchronize(void)
|
|
|
|
{
|
|
|
|
HAL_RTC_WaitForSynchro(&RtcHandle);
|
|
|
|
}
|
2016-11-23 15:41:32 +00:00
|
|
|
#endif /* DEVICE_LOWPOWERTIMER */
|
2016-09-15 12:00:24 +00:00
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
#endif /* DEVICE_RTC */
|