cancel
Showing results for 
Search instead for 
Did you mean: 

Please help on the clock configration of STM32G431. The clock is mush slower than what I set. it is only 20MHz roughly

wmao.1
Associate II

I use cubemx to generate clock config

And I use a flip pin and oscilloscope to check the speed.

here is what i did

clock setting

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_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
  RCC_OscInitStruct.PLL.PLLN = 40; //160MHz
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  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 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_7) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks 
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_I2C3|RCC_PERIPHCLK_USB;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

in the main function

I use a flip to check the waveform

//testing
 
while(1)
{
	FlipBlueLED();
  TM_DelayMillis(2);
}
 

void TM_DelayMillis(uint32_t millis) {
    /* Multiply millis with multipler */
    /* Substract 10 */
    millis = 1000 * millis * multiplier - 10;
    /* 4 cycles for one loop */
    while (millis--);
}

Suppose it shall be 2ms waveform, but it is 9ms waveform (period is 18ms)

So it is about 4.5times slower.

Even I change the PLL setting, it doesn't gets faster..

Anyone, please help.

5 REPLIES 5
TDK
Guru

What is HSE frequency? 40Mhz seems high. What is multiplier?

Your mistake is likely assuming the loop takes exactly 4 ticks as well as using a loop which can be optimized out.

Use a hardware based time rather than a software loop subject to the whims of the compiler.

If you feel a post has answered your question, please click "Accept as Solution".
Nikita91
Lead II

To check the frequency with an oscilloscope you have to use the MCO output.

I didn't see MCO output in cubemx. I am using stm32G431

which pin is used for this purpose.

wmao.1
Associate II

8MHz

wmao.1
Associate II

Hello

Thanks for your reply

I tried again from scratch using cubemx to generate a new project.

Also I use a PWM to get the pulse. Starting from brand new project with cubemx.The clock is shows as what supposed to be.

And then I copied the code from old project to new project section by section, now it looks everything is working well. I checked the pin for LED with oscilloscope, the frequency is correct.

Really don't know what is going on.

Using cubemx to generate a brand new project and start from scratch, it is an easy way to test.