Overclocking STM32F103

What could be included in further releases, or for the forum.
victor_pv
Posts: 1603
Joined: Mon Apr 27, 2015 12:12 pm

Re: Overclocking STM32F103

Post by victor_pv » Sat Sep 05, 2015 12:21 am

Rick Kimball wrote:With all this GD32F103 speed daemon talk, I thought I'd try pushing my chips a little. I was surprised no one has mentioned how to output the clock to verify what speed you are running. You can output the PLL clock divided by 2 on PA8 if you use the following:

Code: Select all

  // configure PA8 to output PLL/2 clock
  gpio_set_mode(GPIOA, 8, GPIO_AF_OUTPUT_PP);
  *(volatile uint8_t *)(0x40021007) = 0x7 /*MCO_Config(PLL/2)*/;
I'm happily running at 96MHz with 1 wait state on a Red Pill. You do have to change some defines regarding systick and microsecond counting..

-rick
So 0 wait state crashes it, what about going any faster than 96? does it have multipliers for more? and if it does, it runs or crashes?

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

Re: Overclocking STM32F103

Post by RogerClark » Sat Sep 05, 2015 12:41 am

Victor,

I'm fairly sure I tried setting the main PLL to its maximum setting of 16 x and the red pill still worked ... at least for a while ;-)

The PLL setting is in one of the boards variant files (I can't remember which one as all the names are similar), just look for the definition that has xxx MUL_9 and change it to MUL 16. But all you can test is blinking as serial will be going at the wrong rate as well USB

User avatar
Rick Kimball
Posts: 989
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Overclocking STM32F103

Post by Rick Kimball » Sat Sep 05, 2015 2:32 am

victor_pv wrote:So 0 wait state crashes it, what about going any faster than 96? does it have multipliers for more? and if it does, it runs or crashes?
I tried 80,88,104,128 all seemed fine with WS1, but I didn't test out the other peripherals. I did try slower, 48MHz with WS0 which also seemed to work. All this stuff would need extensive testing over a longer period to see how feasible it is. To be honest, I'm fine with 72 MHz WS2 : )

I guess I was more intrigued that the GD32 might just be the same exact tech as the STM32. I thought the Gigadevice guys might just be pushing it harder and not worrying about the repercussions. However, it does appear that they must use better flash tech. The first thing I tried on my red pill was 96 MHz at 0 wait states and that didn't work for me.

-rick
-rick

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

Re: Overclocking STM32F103

Post by victor_pv » Sat Sep 05, 2015 2:49 am

Rick Kimball wrote:
victor_pv wrote:So 0 wait state crashes it, what about going any faster than 96? does it have multipliers for more? and if it does, it runs or crashes?
I tried 80,88,104,128 all seemed fine with WS1, but I didn't test out the other peripherals. I did try slower, 48MHz with WS0 which also seemed to work. All this stuff would need extensive testing over a longer period to see how feasible it is. To be honest, I'm fine with 72 MHz WS2 : )

I guess I was more intrigued that the GD32 might just be the same exact tech as the STM32. I thought the Gigadevice guys might just be pushing it harder and not worrying about the repercussions. However, it does appear that they must use better flash tech. The first thing I tried on my red pill was 96 MHz at 0 wait states and that didn't work for me.

-rick
At least their peripherals are also different, as there are small changes here and there, enough to show the silicon is not just the exact same.
But how does ARM license the core? the provide the exact implementation for the silicon so all the M3 cores are exactly the same, or leave that for the manufacturers?

About the speeds, I think it is pretty interesting to have the core being able to run at 48Mhz more than the over clocking, because that may provide power saving while still being able to use the USB. As a side effect of trying to run the GD32.

The overclock is out of curiosity though, I think it is fun to push it until it can not go any faster :D
I'm pretty sure someone will try next with a peltier or some other cooling method.

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

Re: Overclocking STM32F103

Post by RogerClark » Sat Sep 05, 2015 5:55 am

I get the impression that the GD32 is being produced using using a better fabrication plant than the STM32.

I've also seen something that mentions the core voltage is lower, but also that the range of voltages that the chip will operate over is a narrower band.

The Flash memory is almost certainly, completely, different. They mention millions of write cycles in their blurb, so I imagine the technology for their flash is different than in the STM32 and it has much faster read access as well as many times more erase cycles. This isn't so surprising as GigaDevices seem to be a memory manufacturer, so I suspect they just use the same memory they designed for other stuff and combined it with the STM32 silicon design.

With the STM32, on most of the boards available at the moment, you'll be limited to 128Mhz as thats the highest PLL multplier available (16 x ) of 8Mhz.

But if anyone has a 12Mhz crystal kicking around, you could swap it for the existing 8MHz, and just change the main PLL multplier to 6 (at least to start with), then push it the PLL multipler up to 192 (I don't think it will go that fast as I don't think my GD32 went that fast when I tried it)

What I guess I should do, is put the improvements I did for the GD32 in terms of setting the systick stuff to the correct value based on F_CPU rather than being hard coded just for 72Mhz, as it would then be easy to have menu for speed e.g. 72Mhz or 48Mhz, or to push it above that if people were happy to loose the USB functionality

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

Re: Overclocking STM32F103

Post by victor_pv » Sat Sep 05, 2015 11:00 pm

I just tried 48Mhz, 128Mhz and a few speeds in between, and as far as blinking the led, everything works, but apparently nothing that I do changes the multiplier for the USB port.

I tried to set the USB prescaler to 1, and the pll for 6, to get 48Mhz, and usb, but did not work.
I am not sure if I did something wrong or what, but right now I am unable to communicate with the usb port even after returning the pll to x9.
The bootloader works, so I know I my usb peripheral does work.

Was anyone ever able to set the usb prescaler to /1 and run an STM32 at 48Mhz?

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

Re: Overclocking STM32F103

Post by RogerClark » Sun Sep 06, 2015 12:14 am

Hi Victor

Are you using the Bootloader or STLink

It looks like the PLL startup code in the core presumes that the hardware is booting from cold, and if the PLL has already been set by the Bootloader to a different value, I was not able to get USB to work.

i.e If I compile the bootloader for 120Mhz I can run the GD32 at 120Mhz, but I can't run the GD32 at 96Mhz as the USB doesnt seem to work.

But if I use STLink I can choose any frequency I like and USB serial works if I have a valid USB clock frequency

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

Re: Overclocking STM32F103

Post by victor_pv » Sun Sep 06, 2015 12:28 am

RogerClark wrote:Hi Victor

Are you using the Bootloader or STLink

It looks like the PLL startup code in the core presumes that the hardware is booting from cold, and if the PLL has already been set by the Bootloader to a different value, I was not able to get USB to work.

i.e If I compile the bootloader for 120Mhz I can run the GD32 at 120Mhz, but I can't run the GD32 at 96Mhz as the USB doesnt seem to work.

But if I use STLink I can choose any frequency I like and USB serial works if I have a valid USB clock frequency
Thanks Roger, I remember you had mentioned that in the GD32 thread, I should have thought on it before.
Anyway, I went to check the STM manual, and found this:
Bit 22USBPRE:USB prescaler
Set and cleared by software to generate 48 MHz USB clock. This bit must be valid before
enabling the USB clock in the RCC_APB1ENR register. This bit can’t be reset if the USB
clock is enabled.

0: PLL clock is divided by 1.5
1: PLL clock is not divided

So, that explains why we can not change on the fly when using the bootloader, but I think we should be able to disable the USB clok in the RCC_APB1ENR register, then change the USB port prescaler, and enable the USB clock, and enumerate.

I'll give it a shot and let you know if it works, and if it does what did I change in the core, so you can test it out in the GD too, hopefully if we find a solution for one will be valid for both.

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

Re: Overclocking STM32F103

Post by RogerClark » Sun Sep 06, 2015 12:38 am

Hi Victor

Thanks.

I suspect it was something like that, but I've not had time to investigate

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

Re: Overclocking STM32F103

Post by victor_pv » Sun Sep 06, 2015 12:56 am

Success!!

I was writting my changes in a text file, so I can post them to you in a minute.
First of all, I applied the changes you did in the GD core to have several USB DIV options, so that is exactly as your GD core.
Then I added the functions needed in a few files to be able to disable peripheral clocks, and then before setting the prescaler, I disable the clock.

Interestingly enough, leaflabs had written a couple of funtions to enable a clock to a peripheral, but no function to disable the clock, which doesn't make much sense to me. It comes very handy in low power applications, where you can go a disable every peripheral that your dont need to save power.
I am sure Ahull, Madias, and Ray will benefit as they are always playing with bateries (and blowing them up... ;) ).
Anyway, let me do a sanity check, and will post you the changes.

EDIT: work fine for 48mhz div_1 and 72Mhz div_1_5.
Bit 23 doesnt seem to have any effect as div2_5 works as 1_5 (usb only works at 72mhz) and div_2 works as div_1 (only works fine at 48mhz).

So, to your GD32 core perform these changes:


To Arduino_STM32/STM32F1/system/libmaple/rcc_private.h
Added:

Code: Select all

static inline void rcc_do_clk_disable(__io uint32** enable_regs,
                                     rcc_clk_id id) {
    __io uint32 *enable_reg = enable_regs[rcc_dev_clk(id)];
    uint8 line_num = rcc_dev_table[id].line_num;
    bb_peri_set_bit(enable_reg, line_num, 0);
}
To STM32F1/system/libmaple/include/libmaple/rcc.h
Added:

Code: Select all

/**
 * @brief Turn off the clock line on a peripheral
 * @param id Clock ID of the peripheral to turn on.
 */
extern void rcc_clk_disable(rcc_clk_id id);
To STM32F1/cores/maple/libmaple/rcc_f1.c
Added:

Code: Select all

void rcc_clk_disable(rcc_clk_id id) {
    static __io uint32* enable_regs[] = {
        [APB1] = &RCC_BASE->APB1ENR,
        [APB2] = &RCC_BASE->APB2ENR,
        [AHB] = &RCC_BASE->AHBENR,
    };
    rcc_do_clk_disable(enable_regs, id);
}
And finally, in boards_setup.cpp, add one line to disable the usb clock before changing the prescaler. The Serial USB startup code takes care of enabling it again:

Code: Select all

        __weak void board_setup_clock_prescalers(void) {
            rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1);
            rcc_set_prescaler(RCC_PRESCALER_APB1, RCC_APB1_HCLK_DIV_2);
            rcc_set_prescaler(RCC_PRESCALER_APB2, RCC_APB2_HCLK_DIV_1);
			rcc_clk_disable(RCC_USB);
			rcc_set_prescaler(RCC_PRESCALER_USB, RCC_USB_SYSCLK_DIV_1);
        }
Last edited by victor_pv on Sun Sep 06, 2015 1:12 am, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest