2018-04-06 06:44 PM
I am working on a project with stm32l432kb. It uses 8 MHz external clock in order to achieve 80 MHz system clock. I've configured clock with the Cube. The diagram shows system clock at 80 MHz, nevertheless the real clock seems to be about 16 MHz. Reading clock with HAL_RCC_GetHCLKFreq() gives value of 15875000. I've verified that the defined HSE_VALUE is 8 MHz. Here is the Cube generated code for clock configuration:
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**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_4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
What can be wrong with configuration?
Thank you
Solved! Go to Solution.
2018-04-07 01:02 AM
Hello,
In one of non up to date SM32CubeMX revisions there was an issue with correct code generation for STM32L4 clock configuration. The issue was that PLLM parameter was set to different value than indicated by STM32CubeMX and as a result system clock frequency was not correct (lower than expected). For example STM32CubeMX was indicating PLLM = 1, while the generated code was using something else.
Here in your code I don't see PLLM parameter, so maybe this could be a root cause. Could you please try to add PLLM parameter between those two lines of code?:
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLN = 40;
I guess that in your case PLLM is equal to 1, so it would look like this:
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 40;
Could you please try this solution and let us know if it helps?
Anyway please make sure that you use the latest revisions of STM32CubeMX and STM32L4Cube package.
Regards
Szymon
2018-04-06 07:04 PM
Is this an external source, or a crystal?
Can you try selecting the various internal clocks and outputting via PA8 (MCO) pin?
2018-04-06 10:39 PM
It's an external clock. I will try to do what you recommend. Unfortunately, PA8 is an input from external source, But I will try to come up with the solution.
2018-04-07 12:43 AM
Read out and check/post the relevant RCC registers.
Did you fill in all RCC_OscInitStruct fields?
JW
2018-04-07 01:02 AM
Hello,
In one of non up to date SM32CubeMX revisions there was an issue with correct code generation for STM32L4 clock configuration. The issue was that PLLM parameter was set to different value than indicated by STM32CubeMX and as a result system clock frequency was not correct (lower than expected). For example STM32CubeMX was indicating PLLM = 1, while the generated code was using something else.
Here in your code I don't see PLLM parameter, so maybe this could be a root cause. Could you please try to add PLLM parameter between those two lines of code?:
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLN = 40;
I guess that in your case PLLM is equal to 1, so it would look like this:
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 40;
Could you please try this solution and let us know if it helps?
Anyway please make sure that you use the latest revisions of STM32CubeMX and STM32L4Cube package.
Regards
Szymon
2018-04-07 09:01 AM
Yes, PLLM assignment was missing, and I haven't paid attention.
Inserting
RCC_OscInitStruct.PLL.PLLM = 1;
solved the problem.
My fault - I needed to verify the registers
:(
Thanks a lot to everyone for taking time to help me with this issue.
Gennady
2018-04-07 09:09 AM
As it was also mentioned in another post (Szymon Panecki), the PLLM assignment was missing. Assigning it to 1 solved the problem.
Thanks a lot,
Gennady
2018-04-07 09:15 AM
BTW, both
STM32CubeMX and STM32L4Cube package I used are up to date. So it seems that the issue with clock configuration for
STM32L4
still exists.Gennady
2018-04-12 11:41 AM
Hi Gennady,
I am also trying achieve same same system clock frequency as what you are doing.
Shouldn't your PLL_N be set to 10 for VCO clock frequency of 80MHz with PLL clock input as 8MHz and PLL_M=1?
regards
R
2018-04-12 12:27 PM
I'd presume in the OPs case that the PLLM selection was something other than 1, but one would need to review the DS/RM to confirm the specific PLL comparison frequency range for the L4 which I don't think is the 1-2 MHz of the F4 series.