Skip to main content
Rob.Riggs
Senior
July 19, 2024
Question

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

  • July 19, 2024
  • 6 replies
  • 2093 views

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.

 

This topic has been closed for replies.

6 replies

ST Technical Moderator
July 30, 2024

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

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
Andrew Neil
Super User
July 30, 2024

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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
ST Technical Moderator
July 31, 2024

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.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
Rob.Riggs
Rob.RiggsAuthor
Senior
July 31, 2024

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