STMicroelectronics / STM32CubeU5

Full Firmware Package for the STM32U5 series: HAL+LL drivers, CMSIS, BSP, MW, plus a set of Projects (examples and demos) running on all boards provided by ST (Nucleo, Evaluation and Discovery Kits).
Other
121 stars 67 forks source link

stm32u5xx_hal_rtc.c : Error on asserts and wrong conversion to BCD #15

Closed PierreYvesLacroix closed 1 year ago

PierreYvesLacroix commented 2 years ago

It seems that the code is designed so that the year field is less than 99 (2 digits). But the year field for 2022 is 2022-1900 = 122 which is a value > 99 and 3 digits.

This causes 2 problems.

File stm32u5xx_hal_rtc.c (also applicable on other STM32)

1/ assert_param(IS_RTC_YEAR(sDate->Year));

We still have this assert since sDate->Year > 99 in 2022.

(Note that the control is not good on the month either since the valid values go from 0 to 11 and the control checks that the values go from 1 to 12)

2/ (uint32_t)RTC_ByteToBcd2(sDate->Year)) << RTC_DR_YU_Pos)

We have 3 digits in 2022, the conversion to BCD is done for 2 digits (no of the function and comment of the function) but it happens that it works and it will continue to work, the bug will appear only in 2060.

RJMSTM commented 1 year ago

Hello @PierreYvesLacroix,

First, thank you for this report.

The year field must be on 2 digits in BCD format as indicated in the RM0456 image

The value 00 in BCD does not necessarily mean 1900. It can also mean 2000. So, the idea is not to subtract the year 1900 from the current year.

For example, 22 in BCD for the year 2022, 60 in BCD for the year 2060, and 00 in BCD for the year 2100.

With Regards,