I would need a step-by-step guide for over- and under-clocking.
Although I have found here
some relevant info, but it is only partial, I don't have a complete picture about:
- where to change CPU clock? Are flash wait-states involved in final CPU operating speed? How?
- where to change peripheral (SPI, GPIO?) clock? Is it related to changing CPU clock? How?
- can I decrease flash wait-states (to zero maybe) if I under-clock to 48MHz?
- under which circumstances (which CPU/peripheral clock combinations) is USB still working?Example:
- would it be possible to underclock to 48MHz, decrease flash-wait states (to zero?), set SPI to 24/48 MHz ?
I would greatly appreciate if someone could collect and post this (and more?) info here and/or on wiki.
Steve, I played a bit with speed changes around the time Roger discovered the GD32 mcus. Those allow the clock to be divider by 2 for the USB peripheral, so it still works with a main clock at 96Mhz.
While Roger was testing on the GD, I did some testing on the STM32 to see if had an undocumented DIV/2 setting for the USB, but wasn't working. Anyway I played a bit with PLL, dividers etc, so from what I remember from that time, I will give you some info:
1.-To change clock, you change the main clock PLL multiplier. Easier than replacing the XTAL.
2.- USB peripheral only has div/1 and div/1.5, since it needs to run at 48Mhz, only a main clock of 48Mhz and 72Mhz work for it.
3.- Rest of peripherals, all if not most, derive their frequency from main clock. They are set during the init() routines that are called before main(). SPI port settings in particular work as DIV/x, minimum I believe is DIV/2, so depending of your main clock, the SPI port speed will be different while applying the same divider. Most peripherals work like that.
I don't remember if libmaple setupclocks() function had any calculation to allow for different main clocks, or it was all hard coded. Sorry.
3.- Wait states. Yes they affect performance. Wait states are added wasted cycles per transaction to the flash. a single flash transaction can read 8 or 16 bytes, don't remember, but more than 1 word, so adding 1 wait state doesn't add 1 cycle to a single 32bit read, but rather a 25% time to 1 word, since 4 words are read at once. The datasheet explains this better than me.
4.- STM specifies how many wait states to add depending on main clock speed, so as you increase main clock, the flash access speed scales more slowly due to the added wait states.
5.- To run reliably, on the full temp range, comercial product... blah blah you should follow the datasheet, but it seems that you can reduce the number of wait states a bit without crashing.
5.- Same with max frequency, theory is 72Mhz, I have overclocked a couple of mcus for a few days without issues, but who knows over a wider temperature range and voltage range what would happen. If you dont care about a crash, or you just want to see how fast you can go, I believe I went to 128Mhz without adding more wait states than at 72Mhz. Not sure if even more could be added, I don't remember of the top of my head.\
You can underclock to 48Mhz, set wait states to 0, and run the SPI at 24Mhz. I believe the SPI minimum divider is /2.
On toggling pins, GPIO is not affected by wait states, neither RAM or any other peripheral, only flahs, BUT, if the code toggling a pin is in flash, it may be slower as you add wait states, but again since the flash ready 128bits at once, if it is a very tight loop that falls within 4 instructions, you may end up with a single flash read every 4 instructions. On the other hand, if you jump back and forward, and force 1 flash read for every 1 or 2 instructions, the wait states will impact you. Also, if the loop toggling the pin instead has any other wait on something else, then having the flash run faster or slower may not affect, if that wait for something else is even longer.
Try a loop that does a few jumps back and forth for more than 4 addresses, so forcing several flash reads, and make sure the compiler doesn't unroll it, and you should see a measurable difference depending how many wait states you have.