2024-11-15 02:52 AM - edited 2024-11-15 04:18 AM
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:
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:
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.
Solved! Go to Solution.
2024-11-16 05:35 AM
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.
2024-11-15 03:43 AM
Hello,
Could you please attach the ioc file?
2024-11-15 03:49 AM
2024-11-15 03:57 AM
Thanks. I will try to run the code when I get NUCLEO-L452RE board on hands and will get back to you.
2024-11-15 04:06 AM
Meanwhile, could you please disable all the peripherals and keep only RCC configured?
2024-11-16 05:35 AM
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.