Skip to main content
ivan239955_stm1_stmicro_com
Associate III
July 13, 2014
Question

RTC LSE/LSI settings from STM32CubeF4 does not work (SOLVED - 2 bugs in Cube)

  • July 13, 2014
  • 8 replies
  • 2018 views
Posted on July 13, 2014 at 15:52

I used STM32CubeMX for a configuration of RTC in STM32F405RG on the OLIMEX STM32-H405 board. It seems that RTC does not work although the OLIMEX example works. For sure, I tried LSI also.

I watched the ''time'' structure after HAL_RTC_GetTime function was processed but all zeros. Any advice? Thank you.

int main(void)
{
HAL_Init();
SystemClock_Config();
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
MX_GPIO_Init();
MX_RTC_Init();
while (1)
{
if (ButtonPressed())
{
HAL_RTC_GetTime(&hrtc, &time, FORMAT_BCD);
}
}
}
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; // _LSI
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
}

void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;
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.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&hrtc);
sTime.Hours = 0;
sTime.Minutes = 0;
sTime.Seconds = 0;
sTime.SubSeconds = 0;
sTime.TimeFormat = RTC_HOURFORMAT12_AM;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_SUB1H;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 0;
HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);
}

#stm32f4-rtc-stm32cube
This topic has been closed for replies.

8 replies

gk_it
Associate II
July 16, 2014
Posted on July 16, 2014 at 11:21

Sounds like RTC CLK is not on

Look to RTC->BDCR

If this is issue you can try

if(!(*(volatile uint32_t *) (BDCR_RTCEN_BB)))

 {

  __HAL_RCC_RTC_ENABLE();

  //

  // printf(''RTC battery reconnected. Set up date and  time''

  //

 }

ivan239955_stm1_stmicro_com
Associate III
July 16, 2014
Posted on July 16, 2014 at 14:46

Thank you, k.g., for the advice.

I put the setting at the end of SystemClock_Config().

Unfortunately it is not the case.

I wanted to adapt the RTC part of the OLIMEX example into my code but it is very complicated as the example uses StdPeriphLib.

gk_it
Associate II
July 18, 2014
Posted on July 18, 2014 at 08:15

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;

 RCC_OscInitStruct.HSEState = RCC_HSE_ON;

 RCC_OscInitStruct.LSEState = RCC_LSE_ON;

(edit: text big font problem)

ivan239955_stm1_stmicro_com
Associate III
July 21, 2014
Posted on July 21, 2014 at 09:48

That is, thank you, this setting works in a conjuction with the previous recommended one.

So it is probably bug in the STM32CubeMX - however, I will check again the setting of the RTC clock in the GUI.

However, sampling of the time gives me every next value the same as the first one - e.g. value

is 00:00:20 when I sample in 20 s after reset but the same after e.g. 25 s, 40 s etc.

// sampling by the command from PC over VCP

HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN);

sprintf(outstr, '' RTC = %02u:%02u:%02u\r\n'', (uint32_t)(time.Hours), (uint32_t)(time.Minutes), (uint32_t)(time.Seconds));

// output by CDC packet to PC terminal

Any next opinion or advice?

Thank you.

gk_it
Associate II
July 22, 2014
Posted on July 22, 2014 at 07:07

I am not sure if I understood problem, but try commend out:

HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);
HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);
from first post
ivan239955_stm1_stmicro_com
Associate III
July 23, 2014
Posted on July 23, 2014 at 19:21

It hasn't helped and, to be honest, it would be strange because these functions act during Init only.

For sure, I tried to output directly RTC->TR register without calling HAL_RTC_GetTime.

It works as expected.

Terrible bug in the function that stops RTC, isn't it?

However, I would like to have TR data converted into time items.

Yes, I could try to play with HAL_RTC_GetTime code but it isn't quite a help from STM.

ivan239955_stm1_stmicro_com
Associate III
July 23, 2014
Posted on July 23, 2014 at 19:54

There 2 bugs in STM32CubeF4 that can be fixed:

1.a)

In SystemClock_Config to add/change:

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

1.b)

E.g. in MX_RTC_Init to restart the RTC clock:

 if(!(*(volatile uint32_t *) (BDCR_RTCEN_BB)))

  __HAL_RCC_RTC_ENABLE();

Thanks to k.g.

2.

In HAL_RTC_GetTime to delete:

sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);

because it probably stops the RTC incrementation.

I don't know at the moment the workaround how to obtain SubSeconds.

kecskesgery
Associate III
November 16, 2015
Posted on November 16, 2015 at 16:46

Hey!

I've struggled with the same issue, util I've found this solution:

/* Allow access to RTC */
HAL_PWR_EnableBkUpAccess();
/* RTC Clock selection can be changed only if the Backup Domain is reset */
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();

This needs to be done where you configure the RTC clocksource and the RTC Init structure. Cheers!