2015-03-12 09:20:56 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
*******************************************************************************
|
2018-03-28 13:00:53 +00:00
|
|
|
* Copyright (c) 2018, 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.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
2017-11-07 16:24:34 +00:00
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
#if DEVICE_RTC
|
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
#include "rtc_api_hal.h"
|
2017-06-08 02:59:17 +00:00
|
|
|
#include "mbed_mktime.h"
|
2018-03-28 13:00:53 +00:00
|
|
|
#include "mbed_error.h"
|
2018-06-29 12:03:32 +00:00
|
|
|
#include "mbed_critical.h"
|
|
|
|
|
|
|
|
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
|
2018-09-28 15:09:25 +00:00
|
|
|
volatile uint32_t LPTICKER_counter = 0;
|
|
|
|
volatile uint32_t LPTICKER_RTC_time = 0;
|
2018-06-29 12:03:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int RTC_inited = 0;
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
static RTC_HandleTypeDef RtcHandle;
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
if (RTC_inited) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RTC_inited = 1;
|
|
|
|
|
2016-04-14 12:48:11 +00:00
|
|
|
// Enable access to Backup domain
|
2017-11-17 11:11:22 +00:00
|
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
2016-04-14 12:48:11 +00:00
|
|
|
HAL_PWR_EnableBkUpAccess();
|
|
|
|
|
2017-12-04 13:00:30 +00:00
|
|
|
#if MBED_CONF_TARGET_LSE_AVAILABLE
|
2018-03-14 10:12:29 +00:00
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
|
2018-09-11 12:12:33 +00:00
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
2016-11-23 15:41:32 +00:00
|
|
|
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
2017-11-07 16:24:34 +00:00
|
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
|
2016-12-06 08:54:24 +00:00
|
|
|
error("Cannot initialize RTC with LSE\n");
|
2016-11-23 15:41:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:24:34 +00:00
|
|
|
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
|
|
|
|
|
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
|
|
|
}
|
2017-12-04 13:00:30 +00:00
|
|
|
#else /* MBED_CONF_TARGET_LSE_AVAILABLE */
|
2019-09-19 11:06:11 +00:00
|
|
|
#if TARGET_STM32WB
|
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
|
|
|
|
#else
|
2018-03-14 10:12:29 +00:00
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
|
2019-09-19 11:06:11 +00:00
|
|
|
#endif
|
2018-09-11 12:12:33 +00:00
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
2016-03-25 13:56:56 +00:00
|
|
|
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_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
|
|
|
}
|
2017-12-04 13:00:30 +00:00
|
|
|
#endif /* MBED_CONF_TARGET_LSE_AVAILABLE */
|
2016-01-11 17:11:19 +00:00
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
// Enable RTC
|
|
|
|
__HAL_RCC_RTC_ENABLE();
|
|
|
|
|
2018-11-22 15:52:09 +00:00
|
|
|
#if defined __HAL_RCC_RTCAPB_CLK_ENABLE /* part of STM32L4 */
|
|
|
|
__HAL_RCC_RTCAPB_CLK_ENABLE();
|
|
|
|
#endif /* __HAL_RCC_RTCAPB_CLK_ENABLE */
|
|
|
|
|
2017-11-07 16:24:34 +00:00
|
|
|
RtcHandle.Instance = RTC;
|
|
|
|
RtcHandle.State = HAL_RTC_STATE_RESET;
|
|
|
|
|
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;
|
2018-04-24 12:08:39 +00:00
|
|
|
RtcHandle.Init.AsynchPrediv = PREDIV_A_VALUE;
|
|
|
|
RtcHandle.Init.SynchPrediv = PREDIV_S_VALUE;
|
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;
|
2018-11-22 15:52:09 +00:00
|
|
|
#if defined (RTC_OUTPUT_REMAP_NONE)
|
|
|
|
RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
|
|
|
|
#endif /* defined (RTC_OUTPUT_REMAP_NONE) */
|
|
|
|
#if defined (RTC_OUTPUT_PULLUP_NONE)
|
|
|
|
RtcHandle.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
|
|
|
|
#endif /* defined (RTC_OUTPUT_PULLUP_NONE) */
|
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) {
|
2018-09-11 12:12:33 +00:00
|
|
|
error("RTC initialization failed\n");
|
2015-03-12 09:20:56 +00:00
|
|
|
}
|
2016-09-15 12:00:24 +00:00
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
#if !(TARGET_STM32F1) && !(TARGET_STM32F2)
|
|
|
|
/* STM32F1 : there are no shadow registers */
|
|
|
|
/* STM32F2 : shadow registers can not be bypassed */
|
|
|
|
if (HAL_RTCEx_EnableBypassShadow(&RtcHandle) != HAL_OK) {
|
2018-09-11 12:12:33 +00:00
|
|
|
error("EnableBypassShadow error\n");
|
2016-09-15 12:00:24 +00:00
|
|
|
}
|
2018-06-29 12:03:32 +00:00
|
|
|
#endif /* TARGET_STM32F1 || TARGET_STM32F2 */
|
2015-03-12 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_free(void)
|
|
|
|
{
|
2018-06-29 12:03:32 +00:00
|
|
|
/* RTC clock can not be reset */
|
2015-03-12 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
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:
|
2018-02-27 10:02:49 +00:00
|
|
|
32bit register is used (no BCD format) for the seconds.
|
|
|
|
For date, there is no specific register, only a software structure.
|
2017-02-22 07:50:29 +00:00
|
|
|
It is then not a problem to not use shifts.
|
|
|
|
*/
|
2015-03-12 09:20:56 +00:00
|
|
|
time_t rtc_read(void)
|
|
|
|
{
|
2018-09-21 08:34:01 +00:00
|
|
|
#if TARGET_STM32F1
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
RtcHandle.Instance = RTC;
|
2018-09-21 08:34:01 +00:00
|
|
|
return RTC_ReadTimeCounter(&RtcHandle);
|
2015-03-12 09:20:56 +00:00
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
#else /* TARGET_STM32F1 */
|
|
|
|
|
|
|
|
struct tm timeinfo;
|
|
|
|
|
|
|
|
/* Since the shadow registers are bypassed we have to read the time twice and compare them until both times are the same */
|
|
|
|
uint32_t Read_time = RTC->TR & RTC_TR_RESERVED_MASK;
|
|
|
|
uint32_t Read_date = RTC->DR & RTC_DR_RESERVED_MASK;
|
|
|
|
|
|
|
|
while ((Read_time != (RTC->TR & RTC_TR_RESERVED_MASK)) || (Read_date != (RTC->DR & RTC_DR_RESERVED_MASK))) {
|
|
|
|
Read_time = RTC->TR & RTC_TR_RESERVED_MASK;
|
|
|
|
Read_date = RTC->DR & RTC_DR_RESERVED_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup a tm structure based on the RTC
|
|
|
|
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_yday information is ignored by _rtc_maketime
|
|
|
|
tm_wday information is ignored by _rtc_maketime
|
|
|
|
tm_isdst information is ignored by _rtc_maketime
|
|
|
|
*/
|
|
|
|
timeinfo.tm_mday = RTC_Bcd2ToByte((uint8_t)(Read_date & (RTC_DR_DT | RTC_DR_DU)));
|
|
|
|
timeinfo.tm_mon = RTC_Bcd2ToByte((uint8_t)((Read_date & (RTC_DR_MT | RTC_DR_MU)) >> 8)) - 1;
|
|
|
|
timeinfo.tm_year = RTC_Bcd2ToByte((uint8_t)((Read_date & (RTC_DR_YT | RTC_DR_YU)) >> 16)) + 68;
|
|
|
|
timeinfo.tm_hour = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_HT | RTC_TR_HU)) >> 16));
|
|
|
|
timeinfo.tm_min = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_MNT | RTC_TR_MNU)) >> 8));
|
|
|
|
timeinfo.tm_sec = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_ST | RTC_TR_SU)) >> 0));
|
|
|
|
|
|
|
|
// Convert to timestamp
|
|
|
|
time_t t;
|
|
|
|
if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
|
|
|
#endif /* TARGET_STM32F1 */
|
2018-09-21 08:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
void rtc_write(time_t t)
|
|
|
|
{
|
2018-09-21 08:34:01 +00:00
|
|
|
#if TARGET_STM32F1
|
|
|
|
|
|
|
|
RtcHandle.Instance = RTC;
|
|
|
|
if (RTC_WriteTimeCounter(&RtcHandle, t) != HAL_OK) {
|
|
|
|
error("RTC_WriteTimeCounter error\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* TARGET_STM32F1 */
|
|
|
|
|
2017-10-06 13:44:16 +00:00
|
|
|
RTC_DateTypeDef dateStruct = {0};
|
|
|
|
RTC_TimeTypeDef timeStruct = {0};
|
2015-03-12 09:20:56 +00:00
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
core_util_critical_section_enter();
|
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-11-16 13:42:49 +00:00
|
|
|
if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
|
2017-06-08 02:59:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-12 09:20:56 +00:00
|
|
|
|
|
|
|
// Fill RTC structures
|
2018-08-13 15:04:56 +00:00
|
|
|
if (timeinfo.tm_wday == 0) { /* Sunday specific case */
|
|
|
|
dateStruct.WeekDay = RTC_WEEKDAY_SUNDAY;
|
2017-02-22 07:50:29 +00:00
|
|
|
} 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-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;
|
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
|
2018-09-28 15:09:25 +00:00
|
|
|
/* Before setting the new time, we need to update the LPTICKER_counter value */
|
|
|
|
/* rtc_read_lp function is then called */
|
2018-08-14 12:00:25 +00:00
|
|
|
rtc_read_lp();
|
2018-06-29 12:03:32 +00:00
|
|
|
|
2018-09-28 15:09:25 +00:00
|
|
|
/* In rtc_read_lp, LPTICKER_RTC_time value has been updated with the current time */
|
|
|
|
/* We need now to overwrite the value with the new RTC time */
|
|
|
|
/* Note that when a new RTC time is set by HW, the RTC SubSeconds counter is reset to PREDIV_S_VALUE */
|
|
|
|
LPTICKER_RTC_time = (timeStruct.Seconds + timeStruct.Minutes * 60 + timeStruct.Hours * 60 * 60) * PREDIV_S_VALUE;
|
2018-06-29 12:03:32 +00:00
|
|
|
#endif /* DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM */
|
|
|
|
|
2015-03-12 09:20:56 +00:00
|
|
|
// Change the RTC current date/time
|
2017-11-07 16:24:34 +00:00
|
|
|
if (HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN) != HAL_OK) {
|
2017-11-17 12:07:19 +00:00
|
|
|
error("HAL_RTC_SetDate error\n");
|
2017-11-07 16:24:34 +00:00
|
|
|
}
|
|
|
|
if (HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN) != HAL_OK) {
|
2017-11-17 12:07:19 +00:00
|
|
|
error("HAL_RTC_SetTime error\n");
|
2017-11-07 16:24:34 +00:00
|
|
|
}
|
2018-06-29 12:03:32 +00:00
|
|
|
|
|
|
|
core_util_critical_section_exit();
|
2018-09-21 08:34:01 +00:00
|
|
|
#endif /* TARGET_STM32F1 */
|
2015-03-12 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
int rtc_isenabled(void)
|
|
|
|
{
|
2018-11-22 15:52:09 +00:00
|
|
|
#if defined (RTC_FLAG_INITS) /* all STM32 except STM32F1 */
|
|
|
|
return LL_RTC_IsActiveFlag_INITS(RTC);
|
|
|
|
#else /* RTC_FLAG_INITS */ /* TARGET_STM32F1 */
|
2017-02-22 07:50:29 +00:00
|
|
|
return ((RTC->CRL & RTC_CRL_RSF) == RTC_CRL_RSF);
|
2018-11-22 15:52:09 +00:00
|
|
|
#endif /* RTC_FLAG_INITS */
|
2016-11-23 15:41:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:24:34 +00:00
|
|
|
|
2018-03-13 17:11:18 +00:00
|
|
|
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
|
2016-09-15 12:00:24 +00:00
|
|
|
|
2018-04-24 12:06:57 +00:00
|
|
|
static void RTC_IRQHandler(void);
|
|
|
|
static void (*irq_handler)(void);
|
|
|
|
|
|
|
|
volatile uint8_t lp_Fired = 0;
|
|
|
|
|
2016-09-15 12:00:24 +00:00
|
|
|
static void RTC_IRQHandler(void)
|
|
|
|
{
|
2017-10-10 15:04:29 +00:00
|
|
|
/* Update HAL state */
|
2017-12-21 09:50:47 +00:00
|
|
|
RtcHandle.Instance = RTC;
|
2018-06-27 12:21:07 +00:00
|
|
|
if (__HAL_RTC_WAKEUPTIMER_GET_IT(&RtcHandle, RTC_IT_WUT)) {
|
2018-04-24 12:06:57 +00:00
|
|
|
/* Get the status of the Interrupt */
|
2018-06-27 12:21:07 +00:00
|
|
|
if ((uint32_t)(RTC->CR & RTC_IT_WUT) != (uint32_t)RESET) {
|
2018-04-24 12:06:57 +00:00
|
|
|
/* Clear the WAKEUPTIMER interrupt pending bit */
|
|
|
|
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF);
|
|
|
|
|
|
|
|
lp_Fired = 0;
|
|
|
|
if (irq_handler) {
|
|
|
|
irq_handler();
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 12:00:24 +00:00
|
|
|
}
|
2018-04-24 12:06:57 +00:00
|
|
|
|
|
|
|
if (lp_Fired) {
|
|
|
|
lp_Fired = 0;
|
|
|
|
if (irq_handler) {
|
|
|
|
irq_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 15:52:09 +00:00
|
|
|
#ifdef __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG
|
2018-04-24 12:06:57 +00:00
|
|
|
__HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
|
2018-11-22 15:52:09 +00:00
|
|
|
#endif
|
2016-09-15 12:00:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 12:06:57 +00:00
|
|
|
uint32_t rtc_read_lp(void)
|
2016-09-15 12:00:24 +00:00
|
|
|
{
|
2018-09-28 15:09:25 +00:00
|
|
|
/* RTC_time_tick is the addition of the RTC time register (in second) and the RTC sub-second register
|
|
|
|
* This time value is breaking each 24h (= 86400s = 0x15180)
|
|
|
|
* In order to get a U32 continuous time information, we use an internal counter : LPTICKER_counter
|
|
|
|
* This counter is the addition of each spent time since last function call
|
|
|
|
* Current RTC time is saved into LPTICKER_RTC_time
|
|
|
|
* NB: rtc_read_lp() output is not the time in us, but the LPTICKER_counter (frequency LSE/4 = 8kHz => 122us)
|
|
|
|
*/
|
|
|
|
core_util_critical_section_enter();
|
2018-06-29 12:03:32 +00:00
|
|
|
struct tm timeinfo;
|
2018-01-12 10:52:47 +00:00
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
/* Since the shadow registers are bypassed we have to read the time twice and compare them until both times are the same */
|
|
|
|
/* We don't have to read date as we bypass shadow registers */
|
|
|
|
uint32_t Read_time = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
|
|
|
|
uint32_t Read_SubSeconds = (uint32_t)(RTC->SSR);
|
2018-01-12 10:52:47 +00:00
|
|
|
|
2018-06-29 12:03:32 +00:00
|
|
|
while ((Read_time != (RTC->TR & RTC_TR_RESERVED_MASK)) || (Read_SubSeconds != (RTC->SSR))) {
|
|
|
|
Read_time = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
|
|
|
|
Read_SubSeconds = (uint32_t)(RTC->SSR);
|
2018-01-12 10:52:47 +00:00
|
|
|
}
|
2018-06-29 12:03:32 +00:00
|
|
|
|
|
|
|
timeinfo.tm_hour = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_HT | RTC_TR_HU)) >> 16));
|
|
|
|
timeinfo.tm_min = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_MNT | RTC_TR_MNU)) >> 8));
|
|
|
|
timeinfo.tm_sec = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_ST | RTC_TR_SU)) >> 0));
|
|
|
|
|
2018-09-28 15:09:25 +00:00
|
|
|
uint32_t RTC_time_tick = (timeinfo.tm_sec + timeinfo.tm_min * 60 + timeinfo.tm_hour * 60 * 60) * PREDIV_S_VALUE + PREDIV_S_VALUE - Read_SubSeconds; // Max 0x0001-517F * 8191 + 8191 = 0x2A2E-AE80
|
2018-01-12 10:52:47 +00:00
|
|
|
|
2018-09-28 15:09:25 +00:00
|
|
|
if (LPTICKER_RTC_time <= RTC_time_tick) {
|
|
|
|
LPTICKER_counter += (RTC_time_tick - LPTICKER_RTC_time);
|
2018-04-24 12:06:57 +00:00
|
|
|
} else {
|
2018-09-28 15:09:25 +00:00
|
|
|
/* When RTC time is 0h00.01 and was 11H59.59, difference is "current time + 24h - previous time" */
|
|
|
|
LPTICKER_counter += (RTC_time_tick + 24 * 60 * 60 * PREDIV_S_VALUE - LPTICKER_RTC_time);
|
2018-04-24 12:06:57 +00:00
|
|
|
}
|
2018-09-28 15:09:25 +00:00
|
|
|
LPTICKER_RTC_time = RTC_time_tick;
|
2018-04-24 12:06:57 +00:00
|
|
|
|
2018-09-28 15:09:25 +00:00
|
|
|
core_util_critical_section_exit();
|
|
|
|
return LPTICKER_counter;
|
2016-09-15 12:00:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 12:06:57 +00:00
|
|
|
void rtc_set_wake_up_timer(timestamp_t timestamp)
|
2016-09-15 12:00:24 +00:00
|
|
|
{
|
2018-11-16 13:34:10 +00:00
|
|
|
/* RTC periodic auto wake up timer is used
|
|
|
|
* This WakeUpTimer is loaded to an init value => WakeUpCounter
|
|
|
|
* then timer starts counting down (even in low-power modes)
|
|
|
|
* When it reaches 0, the WUTF flag is set in the RTC_ISR register
|
|
|
|
*/
|
2018-03-15 10:12:09 +00:00
|
|
|
uint32_t WakeUpCounter;
|
2018-11-16 13:34:10 +00:00
|
|
|
uint32_t WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV4;
|
2018-04-24 12:06:57 +00:00
|
|
|
|
2018-11-16 13:34:10 +00:00
|
|
|
core_util_critical_section_enter();
|
2018-04-24 12:06:57 +00:00
|
|
|
|
2018-11-16 13:34:10 +00:00
|
|
|
/* MBED API gives the timestamp value to set
|
|
|
|
* WakeUpCounter is then the delta between timestamp and the current tick (LPTICKER_counter)
|
|
|
|
* If the current tick preceeds timestamp value, max U32 is added
|
|
|
|
*/
|
|
|
|
uint32_t current_lp_time = rtc_read_lp();
|
2018-04-24 12:06:57 +00:00
|
|
|
if (timestamp < current_lp_time) {
|
|
|
|
WakeUpCounter = 0xFFFFFFFF - current_lp_time + timestamp;
|
2018-03-15 10:12:09 +00:00
|
|
|
} else {
|
2018-04-24 12:06:57 +00:00
|
|
|
WakeUpCounter = timestamp - current_lp_time;
|
2017-11-07 16:24:34 +00:00
|
|
|
}
|
2016-11-23 15:41:32 +00:00
|
|
|
|
2018-11-16 13:34:10 +00:00
|
|
|
/* RTC WakeUpCounter is 16 bits
|
|
|
|
* Corresponding time value depends on WakeUpClock
|
|
|
|
* - RTC clock divided by 4 : max WakeUpCounter value is 8s (precision around 122 us)
|
|
|
|
* - RTC clock divided by 8 : max WakeUpCounter value is 16s (precision around 244 us)
|
|
|
|
* - RTC clock divided by 16 : max WakeUpCounter value is 32s (precision around 488 us)
|
|
|
|
* - 1 Hz internal clock 16b : max WakeUpCounter value is 18h (precision 1 s)
|
|
|
|
* - 1 Hz internal clock 17b : max WakeUpCounter value is 36h (precision 1 s)
|
|
|
|
*/
|
2018-04-24 12:06:57 +00:00
|
|
|
if (WakeUpCounter > 0xFFFF) {
|
2018-11-16 13:34:10 +00:00
|
|
|
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV8;
|
|
|
|
WakeUpCounter = WakeUpCounter / 2;
|
|
|
|
|
|
|
|
if (WakeUpCounter > 0xFFFF) {
|
|
|
|
WakeUpClock = RTC_WAKEUPCLOCK_RTCCLK_DIV16;
|
|
|
|
WakeUpCounter = WakeUpCounter / 2;
|
|
|
|
|
|
|
|
if (WakeUpCounter > 0xFFFF) {
|
|
|
|
/* Tick value needs to be translated in seconds : TICK * 16 (previous div16 value) / RTC clock (32768) */
|
|
|
|
WakeUpClock = RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
|
|
|
|
WakeUpCounter = WakeUpCounter / 2048;
|
|
|
|
|
|
|
|
if (WakeUpCounter > 0xFFFF) {
|
|
|
|
/* In this case 2^16 is added to the 16-bit counter value */
|
|
|
|
WakeUpClock = RTC_WAKEUPCLOCK_CK_SPRE_17BITS;
|
|
|
|
WakeUpCounter = WakeUpCounter - 0x10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 12:06:57 +00:00
|
|
|
}
|
2017-11-07 16:24:34 +00:00
|
|
|
|
2017-12-21 09:50:47 +00:00
|
|
|
RtcHandle.Instance = RTC;
|
2018-09-21 15:11:29 +00:00
|
|
|
HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
|
2018-11-16 13:34:10 +00:00
|
|
|
if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, WakeUpCounter, WakeUpClock) != HAL_OK) {
|
2018-03-15 10:12:09 +00:00
|
|
|
error("rtc_set_wake_up_timer init error\n");
|
2016-09-15 12:00:24 +00:00
|
|
|
}
|
2018-04-24 12:06:57 +00:00
|
|
|
|
|
|
|
NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
|
|
|
|
irq_handler = (void (*)(void))lp_ticker_irq_handler;
|
|
|
|
NVIC_EnableIRQ(RTC_WKUP_IRQn);
|
2018-09-28 15:09:25 +00:00
|
|
|
core_util_critical_section_exit();
|
2018-04-24 12:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
lp_Fired = 1;
|
|
|
|
NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
|
|
|
|
irq_handler = (void (*)(void))lp_ticker_irq_handler;
|
|
|
|
NVIC_SetPendingIRQ(RTC_WKUP_IRQn);
|
|
|
|
NVIC_EnableIRQ(RTC_WKUP_IRQn);
|
2016-09-15 12:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_deactivate_wake_up_timer(void)
|
|
|
|
{
|
2017-12-21 09:50:47 +00:00
|
|
|
RtcHandle.Instance = RTC;
|
2018-09-21 15:11:29 +00:00
|
|
|
HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
|
2018-06-29 12:03:32 +00:00
|
|
|
NVIC_DisableIRQ(RTC_WKUP_IRQn);
|
2016-09-15 12:00:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 17:11:18 +00:00
|
|
|
#endif /* DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM */
|
2016-09-15 12:00:24 +00:00
|
|
|
|
2016-11-23 15:41:32 +00:00
|
|
|
#endif /* DEVICE_RTC */
|