OverClock. at Runtime ~90MHz

Post here first, or if you can't find a relevant section!
alexandros
Posts: 52
Joined: Mon Oct 02, 2017 6:51 pm

Re: OverClock. at Runtime ~90MHz

Post by alexandros » Fri Nov 24, 2017 12:10 pm

Pito wrote:
Fri Nov 24, 2017 11:19 am
You can change the cpu clock at runtime with F103.
I did it on F103 in past (with mecrisp forth) and it worked perfectly. I was changing on-the-fly the clock in a loop in a sequence like 72MHz/8MHz/128MHz/48MHz without a crash. Mind the clock's PLL setting (the phase loop needs to lock itself at the new freq) takes some time thus there always will be a small "time gap" while the PLL locks.
You have to take the clock setting routine from the core, and you want to add ticks and baudrate settings change such you stay consistent with ms/micros and uart speed. I did not test with USB as it is supported by a few clock freqs only.

Code: Select all

void set_clock(uint32_t clk_khz, uint32_t baudrate) {
set_cpu_clk(clk_khz);  // see core
set_ticks(clk_khz);  // see core
set_baudrate(baudrate);  // see core
}
..
#define baudrate 115200;
set_clock(8000, baudrate);
set_clock(128000, baudrate);
set_clock(48000, baudrate);
set_clock(72000, baudrate);
set_clock(96000, baudrate);
..
For the set_cpu_clk
set_ticks
set_baudrate

What i have to declare in order to get them inside my project? i mean the library

User avatar
Pito
Posts: 1699
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: OverClock. at Runtime ~90MHz

Post by Pito » Fri Nov 24, 2017 12:17 pm

There is not such a library.
You have to open the libmaple core files and look for the functions (or the related parts of the functions which handle the settings). I think all the stuff is there (must be there of course :) ).
Then put the stuff into those above functions. An easy DIY exercise :)
Last edited by Pito on Fri Nov 24, 2017 12:20 pm, edited 1 time in total.
Pukao Hats Cleaning Services Ltd.

alexandros
Posts: 52
Joined: Mon Oct 02, 2017 6:51 pm

Re: OverClock. at Runtime ~90MHz

Post by alexandros » Fri Nov 24, 2017 12:19 pm

Pito wrote:
Fri Nov 24, 2017 12:17 pm
There is not such a library.
You have to open the libmaple core files and look for the functions (or the related parts of the functions which handle the settings). I think all stuff is there (must be there of course :) ).
Then put them into those above functions. An easy DIY exercise :)
yea i just realise it! :lol: thanks! ;)

User avatar
Pito
Posts: 1699
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: OverClock. at Runtime ~90MHz

Post by Pito » Fri Nov 24, 2017 12:25 pm

Look for files like

Code: Select all

rcc.c
systick.c 
usart.c 
in ..\STM32F1\cores\maple\libmaple

and in ..\STM32F1\variants\generic_stm32f103c\wirish

Code: Select all

boards.cpp
boards_setup.cpp
and use the functions which init the stuff..
Pukao Hats Cleaning Services Ltd.

victor_pv
Posts: 1791
Joined: Mon Apr 27, 2015 12:12 pm

Re: OverClock. at Runtime ~90MHz

Post by victor_pv » Fri Nov 24, 2017 3:27 pm

I also did test changing the core speed at runtime in the past, to 48 adn 96 MHz.
The code may be in some old post. What I remember is that I had to change the clock source to HSI, then change the PLL settings, and change back to HSE. Did work fine for both, but USB only worked with at 48Mhz and not 96 as expected. I did not care about systick or anything else, but as long as you take care of those things, you should be good.
I would think the best approach would be to change F_CPU to an variable rather than a macro, and change it accordingly when you change speed, and if you are using any peripheral with speed settings, re-initialize them. May be a bit of work to modify all that's needed, but should be perfectly possible.

As Roger, Ray, and others I can't remember have said before, it seems to be better to run the CPU at the max speed you need, and then skeep often, rather than speed up and slow down, as far as perfomance per watt of energy used.

User avatar
zoomx
Posts: 550
Joined: Mon Apr 27, 2015 2:28 pm
Location: Mt.Etna, Italy

Re: OverClock. at Runtime ~90MHz

Post by zoomx » Fri Nov 24, 2017 3:43 pm

Pito wrote:
Fri Nov 24, 2017 11:19 am
You have to take the clock setting routine from the core,
Uh, wich core? Arduino_STM32 doesn't have any set_cpu_clk, set_ticks or set_baudrate. I used the search in GitHub

rcc.c is in
Arduino_STM32/STM32F1/cores/maple/libmaple/rcc_f1.c
and I found this

Code: Select all

__deprecated
void rcc_clk_init(rcc_sysclk_src sysclk_src,
                  rcc_pllsrc pll_src,
                  rcc_pll_multiplier pll_mul);
                  

User avatar
Pito
Posts: 1699
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: OverClock. at Runtime ~90MHz

Post by Pito » Fri Nov 24, 2017 5:28 pm

Uh, wich core? Arduino_STM32 doesn't have any set_cpu_clk, set_ticks or set_baudrate.
All cores we have got include routines/functions/methods for setting up the CPU clock, SysTick reload value and Uart Baudrate settings..
Pukao Hats Cleaning Services Ltd.

alexandros
Posts: 52
Joined: Mon Oct 02, 2017 6:51 pm

Re: OverClock. at Runtime ~90MHz

Post by alexandros » Fri Nov 24, 2017 6:47 pm

I think that have to be made a library for this "tool" , is very interesting

User avatar
Pito
Posts: 1699
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: OverClock. at Runtime ~90MHz

Post by Pito » Fri Nov 24, 2017 7:39 pm

Add the 38kHz CPU clock setting and you may run the complete BluePill at 38kHz while taking maybe 20uA of current. Except USB and uart (or do 300baud?) everything will work as usual.

Code: Select all

..
set_clock(128000, 460800); 
// do math, send/receive big data..
set_clock(38, 300); 
// scan sensors, buttons, write slow data to SPI flash 
// or onto Sdcard, send/receive slow telemetry
set_clock(128000, 460800); 
// do math, send/receive big data..
..
Pukao Hats Cleaning Services Ltd.

User avatar
RogerClark
Posts: 7541
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: OverClock. at Runtime ~90MHz

Post by RogerClark » Fri Nov 24, 2017 9:18 pm

Pito

Did you notice if the USB stops working if you switch to 96 MHz for a short time ?

It normally takes the PC host quite a long time to notice the USB device is not working,

So I wonder if switching to 120 MHz for 100ms would kill the USB?

Post Reply