cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX wrong RCC init when using HSE as source for RTC on STM32L072

Sigi
Associate II

New in CubeMX, so read this with "nonexpert" flag set 😉

If HSE is used as source for RTC on STM32L072 device, it fails to initialize clock source for RTC.

Running RTC with LSI or LSE both work fine, but with HSE it fails, regardless of prescaller setting.

I think problem is in HAL_RCCEx_PeriphCLKConfig() function, it fails to set HSE as RTC source because HSE is allready enabled and this block of code fails:

   /* Check if user wants to change HSE RTC prescaler whereas HSE is enabled */

   temp_reg = (RCC->CR & RCC_CR_RTCPRE);

   if ((temp_reg != (PeriphClkInit->RTCClockSelection & RCC_CR_RTCPRE))

#if defined (LCD)

    || (temp_reg != (PeriphClkInit->LCDClockSelection & RCC_CR_RTCPRE))

#endif /* LCD */

      )

   { /* Check HSE State */

     if ((PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL) == RCC_CSR_RTCSEL_HSE)

     {

       if (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY))

       {

         /* To update HSE divider, first switch-OFF HSE clock oscillator*/

         return HAL_ERROR;

       }

     }

   }

If in HAL_RCCEx_PeriphCLKConfig() call to HAL_RCCEx_PeriphCLKConfig() is done before HAL_RCC_ClockConfig(), then problem dissapears (but I'm not sure if anything else might go wrong because of that swap).

CubeMX 5.6.1 , tested on custom STM32L072RB board and on Nucleo-L073RZ with soldered crystal for HSE, the same issue.

There is no such problem on Discovery with STM32F051 (I had it available and tested quickly).

Regards

4 REPLIES 4
Vcouc.1
Associate II

I have exactly the same problem also on the STM32L072 . I run CubeIDE STM32CubeIDE

Version: 1.3.0

Build: 5720_20200220_1053 (UTC)

OS: Linux, v.5.4.0-40-generic, x86_64 / gtk 3.24.20

Java version: 1.8.0_242

It seems to be a bug in the code generation out of the IOC file

Vcouc.1
Associate II

As I understand it well is your workaround swapping the order of HAL_RCCEx_PeriphCLKConfig() and HAL_RCCEx_PeriphCLKConfig() in the Systemclock_Config() ? this is not fixing the problem in my setup

Vcouc.1
Associate II

If I put the HSE clk devider on 2 instead of 8 the issue is solved (I'm using a 8MHz crystal)

Sigi
Associate II

Hello Vcouc.1

Interesting, it looks as that error happens if init code tries to change HSE divider while HSE is allready running. I will try your solution tomorrow.

And yes, my workaround in SystemClock_Config() looks like this (this works for me so far):

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 /** Configure the main internal regulator output voltage

 */

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

// *** this block of code is cut from original position at the end of SystemClock_Config() and pasted here :

 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2

                             |RCC_PERIPHCLK_RTC;

 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;

 PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV16;

 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

 {

   Error_Handler();

 }

// *** end of cut/paste block

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

 */

 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.PLLMUL = RCC_PLLMUL_8;

 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;

 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_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

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

 {

   Error_Handler();

 }

}

But it is PITA to do this cut/paste stuff each time I change something and generate code with CubeMX.

Thanks for posting your solution!