Clock frequency

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Clock frequency

Post by mebab »

Hi everybody,
I have already tested NucleoL64 with regard to changing the clock frequency. I have developed an STM32L476 based system where I change the clock frequency again to see the possible power saving. I use CubeMX to generate the code and update the variant_generic.cpp afterward. However, there is no change in power consumption changing frequency from 2 to 80 MHz. The following represents the WEAK related to System Clock Config:

Code: Select all

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

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  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_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_I2C3
                              |RCC_PERIPHCLK_ADC;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_SYSCLK;
  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
}
I know that it is better to include this WEAK in my Arduino code. But for now, I want to know if there is anything wrong with changing the clock frequency?

Thanks for any help
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Clock frequency

Post by dannyf »

take a look at the datasheet, or port over the CMSIS SystemCoreClockUpdate() to your environment and see what it reports.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Clock frequency

Post by ag123 »

if you want to reduce power you need to turn off the *peripherals* like your gpios, spi, uart, i2c, adc, timers, usb etc
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Clock frequency

Post by mebab »

Thank you! I will do so.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Clock frequency

Post by ag123 »

and normally to do nothing there is a strange command

Code: Select all

void loop() {
	asm("wfi");
}
wait for interrupt, normally you are waiting for systick interrupt 1ms intervals, if you disable systick, millis() and delay() won't work.
but otherwise this means sleep till the next interrupt (which is normally systick)
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Clock frequency

Post by mebab »

ag123 wrote: Thu Jun 10, 2021 12:37 pm and normally to do nothing there is a strange command

Code: Select all

void loop() {
	asm("wfi");
}
wait for interrupt, normally you are waiting for systick interrupt 1ms intervals, if you disable systick, millis() and delay() won't work.
but otherwise this means sleep till the next interrupt (which is normally systick)
Thank you! I have already used this.
Post Reply

Return to “General discussion”