2026-01-19 6:55 AM
I work on the STM32U5G9J-DK2 and what I noticed, the RTC doesn't accepted properly hour value which is greater than 12. Than calendar doesn't work, don't actuallize and the hours going UP for example from 23 to 24 and from 24 to 25 and so on... It's really strange bcos on the STM32F407 it work flawlesly without any problem. The solution is using 12 hour format write and sign AM/PM by RTC_TimeTypeDef.TimeFormat value 0 = AM, 1 = PM. Then calendar is properly running and if anyone like me want use 24 hour time format it need realised by software.
Or if anyone know for this MCU other method to use 24H format by hardware you could be posting below.
Solved! Go to Solution.
2026-01-19 11:39 AM
Thanks a lot @Andrew Neil , @waclawek.jan for the answers and hints. I'd start debug and noticed, the wrong initial value in RTC->CR Bit 6 (time format). Immediatelly after invoke HAL_RTC_Init(&hrtc).
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_PrivilegeStateTypeDef privilegeState = {0}; // note all structures are initialised by 0
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
hrtc.Init.BinMode = RTC_BINARY_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
I tried understand what is wrong and even I compare behavior in firmware example RTC which work properly. The problem was enabled D-CACHE's. BUT... now after disable D-CACHE's I can't set the time:
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc) ;
HAL_RTC_WaitForSynchro(&hrtc) ;
HAL_RTC_GetTime(&hrtc, &my_time, RTC_FORMAT_BIN) ;
HAL_RTC_GetDate(&hrtc, &my_date, RTC_FORMAT_BIN) ;
my_time.Minutes = 59;
my_time.Seconds = 56;
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc) ; // or without this procedure it doesn't work
HAL_RTC_WaitForSynchro(&hrtc) ; // like above
if (HAL_RTC_SetTime(&hrtc, &my_time, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}Do you have any idea what I'm doing wrong or what I should to do?
2026-01-19 7:07 AM
Read out and check/post content of RTC registers.
Quite likely, you have inadvertently set the 12-hour mode, i.e. RTC_CR.FMT = 1. This often happens by not initializing properly the struct used to set time in Cube/HAL.
JW
2026-01-19 7:22 AM
@waclawek.jan wrote:This often happens by not initializing properly the struct used to set time in Cube/HAL.
Indeed.
@wegi01 See, for example, this - which @waclawek.jan posted only the other week.
2026-01-19 11:39 AM
Thanks a lot @Andrew Neil , @waclawek.jan for the answers and hints. I'd start debug and noticed, the wrong initial value in RTC->CR Bit 6 (time format). Immediatelly after invoke HAL_RTC_Init(&hrtc).
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_PrivilegeStateTypeDef privilegeState = {0}; // note all structures are initialised by 0
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
hrtc.Init.BinMode = RTC_BINARY_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
I tried understand what is wrong and even I compare behavior in firmware example RTC which work properly. The problem was enabled D-CACHE's. BUT... now after disable D-CACHE's I can't set the time:
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc) ;
HAL_RTC_WaitForSynchro(&hrtc) ;
HAL_RTC_GetTime(&hrtc, &my_time, RTC_FORMAT_BIN) ;
HAL_RTC_GetDate(&hrtc, &my_date, RTC_FORMAT_BIN) ;
my_time.Minutes = 59;
my_time.Seconds = 56;
__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc) ; // or without this procedure it doesn't work
HAL_RTC_WaitForSynchro(&hrtc) ; // like above
if (HAL_RTC_SetTime(&hrtc, &my_time, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}Do you have any idea what I'm doing wrong or what I should to do?
2026-01-20 3:07 AM - edited 2026-01-20 3:07 AM
OK I don't use any of the 'U5/'H5, so there are new factors in the game.
Nonetheless, the principle is still the same: debug your code (and by that I mean also portions of Cube/HAL you've used, it's open source and forms part of your code since you've decided to use it) by observing content of the RTC registers and content of variables used to write and read from them, based on the behaviour described in the RM.
In particular, are all fields of hrtc.Init struct filled in, and if not, which bits of RTC may get inadvertently set/modified?
I don't say this is the only mode how things can go wrong, but is a relatively low hanging fruit worth an attempt to pick.
JW
2026-01-20 6:25 AM
Yes thank You People for help and caring about me. The second issue was my fault bcos I did wrong loop construction through an incorrect condition. Moreover - now it work when enable DCACHE's. So I must say this case make me *** :) Anyway thanks for help.
Dzięki Janku tak trzymaj
2026-01-20 6:35 AM
If your issue is solved, please mark the solution on the post which provided the answer.