Skip to content

STM32F1 RTC : save values in register #8213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@
/** @defgroup RTC_Private_Functions RTC Private Functions
* @{
*/
static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc);
static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);
static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc);
static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter);
static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc);
Expand Down Expand Up @@ -1355,7 +1353,7 @@ HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
* the configuration information for RTC.
* @retval Time counter
*/
static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
{
uint16_t high1 = 0U, high2 = 0U, low = 0U;
uint32_t timecounter = 0U;
Expand Down Expand Up @@ -1385,10 +1383,10 @@ static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
* @param TimeCounter: Counter to write in RTC_CNT registers
* @retval HAL status
*/
static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter)
HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter)
{
HAL_StatusTypeDef status = HAL_OK;

/* Set Initialization mode */
if(RTC_EnterInitMode(hrtc) != HAL_OK)
{
Expand Down
3 changes: 3 additions & 0 deletions targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTim
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);

uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc);
HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);
/**
* @}
*/
Expand Down
57 changes: 15 additions & 42 deletions targets/TARGET_STM/rtc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,51 +149,15 @@ Information about STM32F1:
For date, there is no specific register, only a software structure.
It is then not a problem to not use shifts.
*/
#if TARGET_STM32F1
time_t rtc_read(void)
{
RTC_DateTypeDef dateStruct = {0};
RTC_TimeTypeDef timeStruct = {0};
struct tm timeinfo;
#if TARGET_STM32F1

RtcHandle.Instance = RTC;

// Read actual date and time
// Warning: the time must be read first!
HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);

/* date information is null before first write procedure */
/* set 01/01/1970 as default values */
if (dateStruct.Year == 0) {
dateStruct.Year = 2 ;
dateStruct.Month = 1 ;
dateStruct.Date = 1 ;
}

// Setup a tm structure based on the RTC
/* tm_wday information is ignored by _rtc_maketime */
/* tm_isdst information is ignored by _rtc_maketime */
timeinfo.tm_mon = dateStruct.Month - 1;
timeinfo.tm_mday = dateStruct.Date;
timeinfo.tm_year = dateStruct.Year + 68;
timeinfo.tm_hour = timeStruct.Hours;
timeinfo.tm_min = timeStruct.Minutes;
timeinfo.tm_sec = timeStruct.Seconds;

// Convert to timestamp
time_t t;
if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
return 0;
}

return t;
}
return RTC_ReadTimeCounter(&RtcHandle);

#else /* TARGET_STM32F1 */

time_t rtc_read(void)
{
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 */
Expand Down Expand Up @@ -231,12 +195,23 @@ time_t rtc_read(void)
}

return t;
}

#endif /* TARGET_STM32F1 */
}



void rtc_write(time_t t)
{
#if TARGET_STM32F1

RtcHandle.Instance = RTC;
if (RTC_WriteTimeCounter(&RtcHandle, t) != HAL_OK) {
error("RTC_WriteTimeCounter error\n");
}

#else /* TARGET_STM32F1 */

RTC_DateTypeDef dateStruct = {0};
RTC_TimeTypeDef timeStruct = {0};

Expand All @@ -261,12 +236,9 @@ void rtc_write(time_t t)
timeStruct.Hours = timeinfo.tm_hour;
timeStruct.Minutes = timeinfo.tm_min;
timeStruct.Seconds = timeinfo.tm_sec;

#if !(TARGET_STM32F1)
timeStruct.TimeFormat = RTC_HOURFORMAT_24;
timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
#endif /* TARGET_STM32F1 */

#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
/* Need to update LP_continuous_time value before new RTC time */
Expand All @@ -293,6 +265,7 @@ void rtc_write(time_t t)
#endif /* DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM */

core_util_critical_section_exit();
#endif /* TARGET_STM32F1 */
}

int rtc_isenabled(void)
Expand Down