2022-03-13 05:59 AM
I have a very strange issue with an STM32F405RG/407VG regarding the RTC: it only works, when I am in debugging mode and add the “hrtc�? struct into live expression and the instance handle is open!
My code is pretty simple:
I have 2 globals:
RTC_TimeTypeDef st = {0,};
uint32_t sysTime = 0;
which are globals for debugging purposes,
Hardware Initialisation in main():
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CRC_Init();
MX_I2C1_Init();
MX_RNG_Init();
MX_RTC_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
uint16_t led = 500;
uint32_t sled = 0;
/* USER CODE END 2 */
a function, that gets the Time from the RTC in *USER CODE 4*:
void GetTime(void) {
HAL_RTC_GetTime(&hrtc, &st, RTC_FORMAT_BIN);
sysTime = (((((uint32_t)st.Hours) * 60) + ((uint32_t)st.Minutes)) * 60) + (uint32_t)st.Seconds;
}
And a simple main loop:
while(1) {
if(led) led--;
else {
led = 500;
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
}
if(sysTime != sled) {
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
sled = sysTime;
}
GetTime();
HAL_Delay(1);
}
The intended behaviour is:
a) Toggle the LED1 to indicate the program is running
b) Toggle the LED2 when the “sysTime�? variable has changed.
But what happens instead is:
When I am in debugging mode, and have hrtc->Instance open in the “Live Expressions Window�? LED1 and LED2 toggle as intended.
hrtc->Instance closed => "sysTime" is not updated
hrtc->Instance open in Live Expressions => "sysTime" is updated
When I close the debugging mode LED1 keeps toggling, but LED2 stops toggling, which means that “sysTime�? is no longer updated.
Has anyone an idea, what’s going on here?
I am using CubeID 1.9 with F4 Drivers “STM32Cube_FW_F4_V1.27.0�?
Some Additional Info:
This issue popped up in a bigger project with an STM32F407VE. For debugging purposed I build this simple programm on an F405RE, where i could reproduce the behavior.
When I close CubeIDE, remove the power from my test board, and power it up again after a few seconds, the LED2 does not toggle. This means the RTC is dead, except I am in Debuggin mode and having a probe set on hrtc->Instance
The same Code is working well on a Bluepill (F103C8).
Solved! Go to Solution.
2022-03-13 10:41 AM
2022-03-13 10:41 AM
After reading time, you have to read date too.
JW
2022-03-13 11:01 AM
There is an additional complication if one reads fractional seconds... a long thread on EEVBLOG and I think here too.
2022-03-13 11:55 AM
@JW thank you very much! This did the trick!
My GetTime() function now looks like this:
void GetTime(void) {
RTC_TimeTypeDef st;
HAL_RTC_GetTime(&hrtc, &st, RTC_FORMAT_BIN);
#ifdef __STM32F4xx_HAL_H
RTC_DateTypeDef sd;
HAL_RTC_GetDate(&hrtc, &sd, RTC_FORMAT_BIN);
UNUSED(sd);
#endif
sysTime = (((((uint32_t)st.Hours) * 60) + ((uint32_t)st.Minutes)) * 60) + (uint32_t)st.Seconds;
}
@PHolt: luckily i don't need fractional seconds ATM, my "complication bucket" is full for today ;) - but thanks for the warning!