I am using STM32L452VET. I have generated the startup code using latest STM32CubeMX 4.22.1. Here is my clock config:
My problem is the following:
If I turn switch the compiler optimization flag to either:
The generated clock config works and the program starts.
If I turn the following flags:
The generated program will not work. The problem is with the PLL config part. The program (when debugging) will stop at this line and not move further, the MCU has to be reset:
MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
The SYSCLKSource is 3, meaning it is trying to make the PLL the main clock source. This is called from the HAL_RCC_ClockConfig function which is located in stm32l4xx_hal_rcc.c file. Here are the parameters:
/**Initializes the CPU, AHB and APB busses clocks
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
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_4) != HAL_OK)
What could be the problem in here? Why cant the program run when using weak or no optimization?
I would appreciate all help, this is an urgent case for me.
PS: For the clarification, I did not write any code yet, everything is autogenerated from CubeMx.