cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 HAL RTC functions do not check hrtc->Instance before use

Rob.Riggs
Senior III

HAL RTC functions for the STM32L4 should, like HAL functions for other peripherals, have an 

IS_RTC_INSTANCE() macro and make use of it before using hrtc->Instance.
 
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.

 

6 REPLIES 6
Saket_Om
ST Employee

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

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

@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.


@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?

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.

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

AndrewNeil_0-1722417200426.jpeg

:grinning_face::face_with_tears_of_joy:

Rob.Riggs
Senior III

@Saket_Om Will the code be cleaned up by ST now that these issues have come to light?