Skip to content

STM32 RTC api minor update #3743

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
Feb 23, 2017
Merged
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
72 changes: 48 additions & 24 deletions targets/TARGET_STM/rtc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ void rtc_init(void)
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
error("PeriphClkInitStruct RTC failed with LSE\n");
}

#else /* !RTC_LSI */

__PWR_CLK_ENABLE();

// Reset Backup domain
Expand All @@ -108,7 +106,6 @@ void rtc_init(void)
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
error("PeriphClkInitStruct RTC failed with LSI\n");
}

#endif /* !RTC_LSI */

// Enable RTC
Expand Down Expand Up @@ -173,11 +170,23 @@ void rtc_free(void)
}

/*
RTC Registers
RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
RTC_Month 1=january, 2=february, ..., 12=december
RTC_Date day of the month 1-31
RTC_Year year 0-99
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

struct tm
tm_sec seconds after the minute 0-61
tm_min minutes after the hour 0-59
Expand All @@ -189,6 +198,22 @@ void rtc_free(void)
tm_yday days since January 1 0-365
tm_isdst Daylight Saving Time flag
*/

/*
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.
*/

time_t rtc_read(void)
{
RTC_DateTypeDef dateStruct;
Expand All @@ -199,11 +224,11 @@ time_t rtc_read(void)

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

// Setup a tm structure based on the RTC
timeinfo.tm_wday = dateStruct.WeekDay;
/* tm_wday information is ignored by mktime */
timeinfo.tm_mon = dateStruct.Month - 1;
timeinfo.tm_mday = dateStruct.Date;
timeinfo.tm_year = dateStruct.Year + 68;
Expand All @@ -230,7 +255,11 @@ void rtc_write(time_t t)
struct tm *timeinfo = localtime(&t);

// Fill RTC structures
dateStruct.WeekDay = timeinfo->tm_wday;
if (timeinfo->tm_wday == 0) {
dateStruct.WeekDay = 7;
} else {
dateStruct.WeekDay = timeinfo->tm_wday;
}
dateStruct.Month = timeinfo->tm_mon + 1;
dateStruct.Date = timeinfo->tm_mday;
dateStruct.Year = timeinfo->tm_year - 68;
Expand All @@ -245,21 +274,17 @@ void rtc_write(time_t t)
#endif /* TARGET_STM32F1 */

// Change the RTC current date/time
HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
}

int rtc_isenabled(void)
{
#if DEVICE_LOWPOWERTIMER
if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) {
return 1;
} else {
return 0;
}
#else /* DEVICE_LOWPOWERTIMER */
return 1;
#endif /* DEVICE_LOWPOWERTIMER */
#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 */
}

#if DEVICE_LOWPOWERTIMER
Expand Down Expand Up @@ -308,5 +333,4 @@ void rtc_synchronize(void)
}
#endif /* DEVICE_LOWPOWERTIMER */


#endif /* DEVICE_RTC */