2024-07-19 04:06 PM
HAL RTC functions for the STM32L4 should, like HAL functions for other peripherals, have an
assert_param(IS_RTC_INSTANCE(hrtc->Instance));
All of the HAL_RTC_* functions accept an RTC_HandleTypeDef argument. However, the implementation of these function mix the use of hrtc->Instance->REG and RTC->REG. This inconsistency has led to some bugs creeping into my code. For example, one can successfully set date and time (HAL_RTC_SetDate(), HAL_RTC_SetTime()) without first calling HAL_RTC_Init() to initialize hrtc. But with a change to the STM32L4 HAL between 1.17.1 and 1.18.0, these functions still successfully set the date and time, but now return HAL_TIMEOUT. I really do need to call HAL_RTC_Init(), but the HAL should help the developer here.
It would be best if these functions either used only RTC or only hrtc->Instance for access to the device registers rather than mixing the two. But most importantly, the RTC HAL functions should validate that the RTC handle is properly initialized before using it.
2024-07-30 04:10 AM
Hello @Rob.Riggs
There are two different implementations for accessing RTC registers in the HAL drivers for different STM32 series: the approach using the RTC base address (e.g., RTC->ICSR) is used in HAL drivers for series implementing version 3 of the RTC peripheral, while the approach using the handle instance (e.g., (__HANDLE__)->Instance->ISR) is used in HAL drivers for series implementing version 2 of the RTC peripheral. The STM32L4 series includes part numbers that implement both version 2 and version 3 of the RTC peripheral, so the HAL driver for this series contains a mix of the two implementations to support both versions.
The difference in implementation does not cause any functional issues because each approach is used consistently within the context of the specific RTC peripheral version
2024-07-30 09:12 AM
@Saket_Om The main issue that needs to be addressed is that the RTC handle (hrtc) is used in HAL RTC functions without being checked. Virtually all other HAL calls that use a handle assert that the handle is valid before use.
2024-07-30 09:18 AM
@Saket_Om wrote:the approach using the RTC base address (e.g., RTC->ICSR) is used in HAL drivers for series implementing version 3 of the RTC peripheral, while the approach using the handle instance (e.g., (__HANDLE__)->Instance->ISR) is used in HAL drivers for series implementing version 2 of the RTC peripheral.
But why are they different?
Why not be consistent?
2024-07-31 01:52 AM
Hello @Andrew Neil
From a historical point of view, the parameter Instance was not present initially within the RTC handle. A change request was later implemented to add this parameter in order to anticipate the eventual release of STM32 products with more than one RTC instance.
2024-07-31 02:13 AM
😀😂
2024-07-31 09:03 AM
@Saket_Om Will the code be cleaned up by ST now that these issues have come to light?