cancel
Showing results for 
Search instead for 
Did you mean: 

Problem initialising RTC on STM32F031 Nucleo-32 board

PMacp.1
Associate

I am using code generated using CubeMX to configure the RTC, however the HAL_RTC_Init() function is returning an error. The clock configuration is this -

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 /** Initializes the CPU, AHB and APB busses clocks

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

 RCC_OscInitStruct.LSIState = RCC_LSI_ON;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 /** Initializes the CPU, AHB and APB busses clocks

 */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

               |RCC_CLOCKTYPE_PCLK1;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

 {

  Error_Handler();

 }

 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;

 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;

 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

 {

  Error_Handler();

 }

}

Which I believe is correct - the Nucleo board doesn't have a 32KHz crystal so the LSI RC oscillator is selected.

The code to initialise the RTC is this -

 hrtc.Instance = RTC;

 hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

 hrtc.Init.AsynchPrediv = 127;

 hrtc.Init.SynchPrediv = 249;

 hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

 hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

 hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

 HAL_RTC_Init(&hrtc);

 if (HAL_RTC_Init(&hrtc) != HAL_OK)

 {

  Error_Handler();

 }

Both these snippets are generated by CubeMX, and the same as many examples I found with a Google search, so I must be missing a step somewhere. SysTick and GPIO configuration is working without problems, and the compiler doesn't generate any errors. Is there a simple way to debug what is happening in the HAL_RTC_Init() function to find my error, or have I overlooked a configuration step? All suggestions gratefully received.

2 REPLIES 2
TDK
Guru

You/CubeMX shouldn't be calling HAL_RTC_Init twice. Might be the source of the error.

> Is there a simple way to debug what is happening in the HAL_RTC_Init() function to find my error...

Set a breakpoint and step through the code to see where and why it returns the error and go from there. The HAL source code is part of your project.

If you feel a post has answered your question, please click "Accept as Solution".
PMacp.1
Associate

Thanks for spotting the duplication, that was legacy from some earlier experimentation, but unfortunately not the source of my problem. I was rather hoping that I have missed a step in the configuration process that I simply can't see from being too close, but I think it is time to configure the debugger.