Page 1 of 2

Blue Pill clock define

Posted: Wed Feb 26, 2020 9:59 pm
by Renato
Using Arduino IDE how can I set Blue Pill cpu clock to 1 MHz, for example?

Re: Blue Pill clock define

Posted: Fri Mar 13, 2020 7:47 am
by alfsch
what i did, just for testing at different clocks:
look for the "variant.cpp" file, related to the used board; maybe...you use: in arduino board settings -> "generic STM32F1 series" and board part "BluePill F103C8", what should be the "blue pill" (with F103 cpu) ;
then in the related file "variant.cpp" , in my system located at : ".arduino15/packages/STM32/hardware/stm32/1.8.0/variants/PILL_F103XX/variant.cpp ",
i modified the RCC clock setting:
> WEAK void SystemClock_Config(void)
>...
>RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // set to other value, to test; also mod i.e. RCC_HSE_PREDIV_DIV1
to run 1 MHz , you could set:
RCC_PLL_MUL2 ( -> 16Mhz PLL ) and RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1 -> RCC_SYSCLK_DIV16 ( -> 1MHz clock)

so i made quick test with other master clock settings;

Re: Blue Pill clock define

Posted: Fri Mar 13, 2020 9:35 am
by fpiSTM
As it is a weak, you can redefine it at sketch level

Code: Select all

extern "C" void SystemClock_Config(void) {...}

Re: Blue Pill clock define

Posted: Fri Mar 13, 2020 1:52 pm
by alfsch
fpiSTM wrote: Fri Mar 13, 2020 9:35 am As it is a weak, you can redefine it at sketch level

Code: Select all

extern "C" void SystemClock_Config(void) {...}
great! so i can set it so easy to...whatever i want.
is other peripheral setting same? (no, i dont think so..)
so: why not make all definitions set "weak", so the the user can change everything very easy, just by "redefine it at sketch level" ?

Re: Blue Pill clock define

Posted: Tue May 26, 2020 12:09 am
by Renato
Please, show a short SystemClock_Config example when I want 2 MHz cpu clock, with internal RC oscilator HSI 16 MHz.
Thanks

Re: Blue Pill clock define

Posted: Tue May 26, 2020 7:41 am
by fpiSTM
Renato wrote: Tue May 26, 2020 12:09 am Please, show a short SystemClock_Config example when I want 2 MHz cpu clock, with internal RC oscilator HSI 16 MHz.
Thanks
Dis you try to do it yourself using STM32CubeMX ?

Re: Blue Pill clock define

Posted: Tue May 26, 2020 8:01 am
by ag123
note that running at a lower system clock would also mean that the peripherals busses run at lower speeds. e.g. if you are using a 1mhz system clock, chances are that spi etc would be lower than that probably 1/2 of it, so do most other peripherals. and usb won't work, usb requires a 48 mhz clock.

Re: Blue Pill clock define

Posted: Tue May 26, 2020 9:23 pm
by Renato
I do not have the hardware to test yet.
Previously I'm studing how to use STM32G0 with low power run mode.
My doubt is if can I use only:

Code: Select all

 
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;  /** 16 MHz */
 RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV8;   /**   2 MHz  ahed */
 
or have to use like below (code generate by CubeMx) ?
Thanks friends.

Code: Select all

extern "C" void SystemClock_Config(void)

void setup() {
.....
}

void loop() {

SystemClock_Config()
....
}

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);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV8;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks 
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

Re: Blue Pill clock define

Posted: Tue May 26, 2020 10:58 pm
by stas2z
Yes, you need whole SystemClock_Config from cube, and don't call it from loop or from anywhere else, it will be called at preinit

Re: Blue Pill clock define

Posted: Fri May 29, 2020 2:17 am
by Renato
stas2z wrote: Tue May 26, 2020 10:58 pm Yes, you need whole SystemClock_Config from cube, and don't call it from loop or from anywhere else, it will be called at preinit
So, I supose that Arduino IDE find this information in specific folder.
What I need to do in instalation process to integrate Arduino IDE and CubeMx to take this ?
Tks