Skip to main content
Associate II
November 15, 2024
Solved

STM32 setting AHB clock speed goes into infinite loop in HAL_RCC_ClockConfig

  • November 15, 2024
  • 3 replies
  • 2898 views

Hey everyone!

 

I'm trying to decrease the clock speed of TIM3 on my STM32L452RET6 (NUCLEO-L452RE) and i've run into issues. I think i figured it out already but I just don't understand why the problem is. My clock tree looks like this in CubeMX:

FlucTuAte_0-1731667205951.png

With this configuration and the default generated code the SystemClock_Config function looks like this:

 

 

 

 

 

void SystemClock_Config(void) {
	RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
	RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };

	/** Configure the main internal regulator output voltage
	 */
	if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) Error_Handler();

	/** Initializes the RCC Oscillators according to the specified parameters
	 * in the RCC_OscInitTypeDef structure.
	 */
	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
	RCC_OscInitStruct.HSIState = RCC_HSI_ON;
	RCC_OscInitStruct.HSICalibrationValue = 64;
	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
	RCC_OscInitStruct.PLL.PLLM = 1;
	RCC_OscInitStruct.PLL.PLLN = 8;
	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
	RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
	RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
	if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) Error_Handler();

	/** Initializes the CPU, AHB and APB buses 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_DIV16;
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16;
	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) Error_Handler();
}

 

 

 

 

 

 The problem is that it goes into an infinite loop at HAL_RCC_ClockConfig function. And no, it doesn't reach the Error_Handler function (according to the debugger). And after some digging I've found this table in the RM:

FlucTuAte_1-1731667481224.png

According to this the flash latency is correct, because my HCLK frequency is less than 16MHz. But if I set the flash latency to 1WS (FLASH_LATENCY_1) it starts working again. Does anyone have an explanation of why that is? It a bit annoying because i need to rewrite this every single time a regenerate the code.

Best answer by FlucTuAte

Okay I've kind of found out what the problem is, but not what caused it. So I forgot to mention that I'm not using the STM32CubeIDE for development, but I was using VSCode with PlatformIO and a tool called stm32pio to convert the CubeMX project structure to a PlatformIO one. I thought the problem might be with my setup so I tried the same configs in CubeIDE and it worked. Now as it turns out PlatformIO wasn't using the HAL libraries from the Drivers directory but it has downloaded it from its own registry that is 3 YEARS out of date (link). Also I was using the newest verison (6.12.1) of CubeMX. I haven't found out the exact cause, probably a bug in the old HAL library or something.

3 replies

mƎALLEm
ST Technical Moderator
November 15, 2024

Hello,

Could you please attach the ioc file?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
FlucTuAteAuthor
Associate II
November 15, 2024

Of course, here it is:

mƎALLEm
ST Technical Moderator
November 15, 2024

Thanks. I will try to run the code when I get NUCLEO-L452RE board on hands and will get back to you.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
mƎALLEm
ST Technical Moderator
November 15, 2024

Meanwhile, could you please disable all the peripherals and keep only RCC configured?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
FlucTuAteAuthorBest answer
Associate II
November 16, 2024

Okay I've kind of found out what the problem is, but not what caused it. So I forgot to mention that I'm not using the STM32CubeIDE for development, but I was using VSCode with PlatformIO and a tool called stm32pio to convert the CubeMX project structure to a PlatformIO one. I thought the problem might be with my setup so I tried the same configs in CubeIDE and it worked. Now as it turns out PlatformIO wasn't using the HAL libraries from the Drivers directory but it has downloaded it from its own registry that is 3 YEARS out of date (link). Also I was using the newest verison (6.12.1) of CubeMX. I haven't found out the exact cause, probably a bug in the old HAL library or something.