Skip to content

Commit 239e397

Browse files
erwangogalak
authored andcommitted
drivers/counter: stm32: Fix time_t usage
Following upgrade of newlib version in SDK 0.10.0, time_t changed from 4 to 8 bytes structure. As a consequence, ts requires a cast to u32_t before conversion to us to avoid overflow. Additionally, add a comment on RTC init value and fix a minor alignment issue. Signed-off-by: Erwan Gouriou <[email protected]>
1 parent 627fdd6 commit 239e397

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/counter/counter_ll_stm32_rtc.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
LOG_MODULE_REGISTER(counter_rtc_stm32, CONFIG_COUNTER_LOG_LEVEL);
2424

25-
#define T_TIME_OFFSET 2088656896
25+
#define T_TIME_OFFSET 2088656896 /* RTC Date reset value: Jan, 1st, xx00 */
2626

2727
#if defined(CONFIG_SOC_SERIES_STM32L4X)
2828
#define RTC_EXTI_LINE LL_EXTI_LINE_18
@@ -87,8 +87,7 @@ static u32_t rtc_stm32_read(struct device *dev)
8787
rtc_date = LL_RTC_DATE_Get(RTC);
8888

8989
/* Convert calendar datetime to UNIX timestamp */
90-
now.tm_year = __LL_RTC_CONVERT_BCD2BIN(
91-
__LL_RTC_GET_YEAR(rtc_date));
90+
now.tm_year = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_YEAR(rtc_date));
9291
now.tm_mon = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_MONTH(rtc_date));
9392
now.tm_mday = __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_DAY(rtc_date));
9493

@@ -101,7 +100,10 @@ static u32_t rtc_stm32_read(struct device *dev)
101100
/* Return number of seconds since RTC init */
102101
ts -= T_TIME_OFFSET;
103102

104-
ticks = counter_us_to_ticks(dev, (u64_t)(ts * USEC_PER_SEC));
103+
__ASSERT(sizeof(time_t) == 8, "unexpected time_t definition");
104+
/* Since SDK 0.10.0, newlib defines sizeof(time_t) = 8, */
105+
/* so ts requires cast to u32_t before conversion from s to us. */
106+
ticks = counter_us_to_ticks(dev, (u64_t)((u32_t)ts * USEC_PER_SEC));
105107

106108
return ticks;
107109
}

0 commit comments

Comments
 (0)