drivers: rtc: stm32 rtc driver configure time and Date
Set a Calendar using the LL functions LL_RTC_TIME_Init and LL_RTC_DATE_Init. Add more DBG info On the stm32F2 serie, check RS Flag after setting calendar registers. Signed-off-by: Francois Ramu <francois.ramu@st.com>
This commit is contained in:
parent
362588037e
commit
2499a4b226
1 changed files with 46 additions and 30 deletions
|
@ -392,6 +392,8 @@ static int rtc_stm32_init(const struct device *dev)
|
||||||
static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *timeptr)
|
static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *timeptr)
|
||||||
{
|
{
|
||||||
struct rtc_stm32_data *data = dev->data;
|
struct rtc_stm32_data *data = dev->data;
|
||||||
|
LL_RTC_TimeTypeDef rtc_time;
|
||||||
|
LL_RTC_DateTypeDef rtc_date;
|
||||||
uint32_t real_year = timeptr->tm_year + TM_YEAR_REF;
|
uint32_t real_year = timeptr->tm_year + TM_YEAR_REF;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
|
@ -416,42 +418,47 @@ static int rtc_stm32_set_time(const struct device *dev, const struct rtc_time *t
|
||||||
LL_PWR_EnableBkUpAccess();
|
LL_PWR_EnableBkUpAccess();
|
||||||
#endif /* RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION */
|
#endif /* RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION */
|
||||||
|
|
||||||
LL_RTC_DisableWriteProtection(RTC);
|
/* Enter Init mode inside the LL_RTC_Time and Date Init functions */
|
||||||
|
rtc_time.Hours = bin2bcd(timeptr->tm_hour);
|
||||||
|
rtc_time.Minutes = bin2bcd(timeptr->tm_min);
|
||||||
|
rtc_time.Seconds = bin2bcd(timeptr->tm_sec);
|
||||||
|
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_time);
|
||||||
|
|
||||||
ErrorStatus status = LL_RTC_EnterInitMode(RTC);
|
/* Set Date after Time to be sure the DR is correctly updated on stm32F2 serie. */
|
||||||
|
rtc_date.Year = bin2bcd((real_year - RTC_YEAR_REF));
|
||||||
if (status != SUCCESS) {
|
rtc_date.Month = bin2bcd((timeptr->tm_mon + 1));
|
||||||
err = -EIO;
|
rtc_date.Day = bin2bcd(timeptr->tm_mday);
|
||||||
goto protect_unlock_return;
|
rtc_date.WeekDay = ((timeptr->tm_wday == 0) ? (LL_RTC_WEEKDAY_SUNDAY) : (timeptr->tm_wday));
|
||||||
}
|
/* WeekDay sunday (tm_wday = 0) is not represented by the same value in hardware,
|
||||||
|
* all the other values are consistent with what is expected by hardware.
|
||||||
LL_RTC_DATE_SetYear(RTC, bin2bcd(real_year - RTC_YEAR_REF));
|
*/
|
||||||
LL_RTC_DATE_SetMonth(RTC, bin2bcd(timeptr->tm_mon + 1));
|
LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_date);
|
||||||
LL_RTC_DATE_SetDay(RTC, bin2bcd(timeptr->tm_mday));
|
|
||||||
|
|
||||||
if (timeptr->tm_wday == 0) {
|
|
||||||
/* sunday (tm_wday = 0) is not represented by the same value in hardware */
|
|
||||||
LL_RTC_DATE_SetWeekDay(RTC, LL_RTC_WEEKDAY_SUNDAY);
|
|
||||||
} else {
|
|
||||||
/* all the other values are consistent with what is expected by hardware */
|
|
||||||
LL_RTC_DATE_SetWeekDay(RTC, timeptr->tm_wday);
|
|
||||||
}
|
|
||||||
|
|
||||||
LL_RTC_TIME_SetHour(RTC, bin2bcd(timeptr->tm_hour));
|
|
||||||
LL_RTC_TIME_SetMinute(RTC, bin2bcd(timeptr->tm_min));
|
|
||||||
LL_RTC_TIME_SetSecond(RTC, bin2bcd(timeptr->tm_sec));
|
|
||||||
|
|
||||||
LL_RTC_DisableInitMode(RTC);
|
|
||||||
|
|
||||||
protect_unlock_return:
|
|
||||||
LL_RTC_EnableWriteProtection(RTC);
|
|
||||||
|
|
||||||
#if RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION
|
#if RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION
|
||||||
LL_PWR_DisableBkUpAccess();
|
LL_PWR_DisableBkUpAccess();
|
||||||
#endif /* RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION */
|
#endif /* RTC_STM32_BACKUP_DOMAIN_WRITE_PROTECTION */
|
||||||
|
|
||||||
|
#ifdef CONFIG_SOC_SERIES_STM32F2X
|
||||||
|
/*
|
||||||
|
* Because stm32F2 serie has no shadow registers,
|
||||||
|
* wait until TR and DR registers are synchronised : flag RS
|
||||||
|
*/
|
||||||
|
while (LL_RTC_IsActiveFlag_RS(RTC) != 1) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
#endif /* CONFIG_SOC_SERIES_STM32F2X */
|
||||||
|
|
||||||
k_mutex_unlock(&data->lock);
|
k_mutex_unlock(&data->lock);
|
||||||
|
|
||||||
|
LOG_DBG("Calendar set : %d/%d/%d - %dh%dm%ds",
|
||||||
|
LL_RTC_DATE_GetDay(RTC),
|
||||||
|
LL_RTC_DATE_GetMonth(RTC),
|
||||||
|
LL_RTC_DATE_GetYear(RTC),
|
||||||
|
LL_RTC_TIME_GetHour(RTC),
|
||||||
|
LL_RTC_TIME_GetMinute(RTC),
|
||||||
|
LL_RTC_TIME_GetSecond(RTC)
|
||||||
|
);
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -504,7 +511,8 @@ static int rtc_stm32_get_time(const struct device *dev, struct rtc_time *timeptr
|
||||||
|
|
||||||
k_mutex_unlock(&data->lock);
|
k_mutex_unlock(&data->lock);
|
||||||
|
|
||||||
timeptr->tm_year = bcd2bin(__LL_RTC_GET_YEAR(rtc_date)) + RTC_YEAR_REF - TM_YEAR_REF;
|
/* tm_year is the value since 1900 and Rtc year is from 2000 */
|
||||||
|
timeptr->tm_year = bcd2bin(__LL_RTC_GET_YEAR(rtc_date)) + (RTC_YEAR_REF - TM_YEAR_REF);
|
||||||
/* tm_mon allowed values are 0-11 */
|
/* tm_mon allowed values are 0-11 */
|
||||||
timeptr->tm_mon = bcd2bin(__LL_RTC_GET_MONTH(rtc_date)) - 1;
|
timeptr->tm_mon = bcd2bin(__LL_RTC_GET_MONTH(rtc_date)) - 1;
|
||||||
timeptr->tm_mday = bcd2bin(__LL_RTC_GET_DAY(rtc_date));
|
timeptr->tm_mday = bcd2bin(__LL_RTC_GET_DAY(rtc_date));
|
||||||
|
@ -530,11 +538,19 @@ static int rtc_stm32_get_time(const struct device *dev, struct rtc_time *timeptr
|
||||||
#else
|
#else
|
||||||
timeptr->tm_nsec = 0;
|
timeptr->tm_nsec = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* unknown values */
|
/* unknown values */
|
||||||
timeptr->tm_yday = -1;
|
timeptr->tm_yday = -1;
|
||||||
timeptr->tm_isdst = -1;
|
timeptr->tm_isdst = -1;
|
||||||
|
|
||||||
|
/* __LL_RTC_GET_YEAR(rtc_date)is the real year (from 2000) */
|
||||||
|
LOG_DBG("Calendar get : %d/%d/%d - %dh%dm%ds",
|
||||||
|
timeptr->tm_mday,
|
||||||
|
timeptr->tm_mon,
|
||||||
|
__LL_RTC_GET_YEAR(rtc_date),
|
||||||
|
timeptr->tm_hour,
|
||||||
|
timeptr->tm_min,
|
||||||
|
timeptr->tm_sec);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue