Changing PLL settings for 24Mhz XTAL

STM32duino bootloader aka Maple bootloader
C_D
Posts: 62
Joined: Mon May 11, 2015 3:27 am
Location: New Zealand

Changing PLL settings for 24Mhz XTAL

Post by C_D » Mon Aug 28, 2017 9:33 pm

I'm trying to use the STM32DUINO bootloader on a custom board, but I cant get the bootloader to run. The micro is an F103R8T7, it has the LED on PC13 and a pull up resistor on PA12 for the USB reset.

I have spotted that my board has a 24MHz crystal and I assume the bootloader will not be configured for this. The only reference I can find in the source is in hardware.c, is this the only thing I would need to change in the code for a 24MHz crystal?

Code: Select all

 /* Configure PLL */
#ifdef XTAL12M
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00110400); /* pll=72Mhz(x6),APB1=36Mhz,AHB=72Mhz */
#else
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz(x9),APB1=36Mhz,AHB=72Mhz */
#endif

    SET_REG(RCC_CR, GET_REG(RCC_CR)     | 0x01000000); /* enable the pll */
Last edited by C_D on Tue Aug 29, 2017 12:15 am, edited 2 times in total.

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

Re: Changing the XTAL frequency

Post by victor_pv » Mon Aug 28, 2017 9:54 pm

The USB has a divider setting. That divider can be set to PLL/1 or PLL/1.5.
The USB needs to run at 48Mhz exactly.
So the USB can run if the PLL can produce a frequency of either 48Mhz or 72Mhz, and the divider is set respectively to 1 or 1.5.

Check the reference manual for all the details it explains clearly what's possible and not, and how to achieve it (registers, frequencies etc)

User avatar
ddrown
Posts: 136
Joined: Sat Jan 09, 2016 4:49 am

Re: Changing the XTAL frequency

Post by ddrown » Mon Aug 28, 2017 10:03 pm

C_D wrote:
Mon Aug 28, 2017 9:33 pm
I'm trying to use the STM32DUINO bootloader on a custom board, but I cant get the bootloader to run. The micro is an F103R8T7, it has the LED on PC13 and a pull up resistor on PA12 for the USB reset.

I have spotted that my board has a 24MHz crystal and I assume the bootloader will not be configured for this. The only reference I can find in the source is in config.c, is this the only thing I would need to change in the code for a 24MHz crystal?

Code: Select all

 /* Configure PLL */
#ifdef XTAL12M
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00110400); /* pll=72Mhz(x6),APB1=36Mhz,AHB=72Mhz */
#else
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz(x9),APB1=36Mhz,AHB=72Mhz */
#endif

    SET_REG(RCC_CR, GET_REG(RCC_CR)     | 0x01000000); /* enable the pll */
EDIT:
From reading the Reference Manual I am not sure I can configure the clocks to get 48 or 72Mhz from a 24Mhz crystal. Anyone know if I can even do this?
It might help to use the CubeMX utility from stm32 as it has a PLL configuration screen. After getting the PLL settings right, have it generate source and take a look how it configures the RCC registers. The defines won't be exactly the same, but they should point you in the right direction.

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

Re: Changing the XTAL frequency

Post by RogerClark » Mon Aug 28, 2017 10:34 pm

The main PLL would need to be changed to 3 x instead of 9 x

The USB clock is derived from the main clock, so would not need to be changed if you fixed the main clock pll and set that to 24 x 3 = 72

Ps.

If you look at the settings for the GD32 board, it has a 12Mhz crystal, so you should be able to see how to do a similar change for a 24MHz crystal

C_D
Posts: 62
Joined: Mon May 11, 2015 3:27 am
Location: New Zealand

Re: Changing the XTAL frequency

Post by C_D » Mon Aug 28, 2017 11:30 pm

Just fumbling my way through STM32CubeMX, it doesn't like an input over 16MHz but the datasheet does say up tp 25Mhz so I guess its OK?

Does this look right?
Clock Configuration.jpg
Clock Configuration.jpg (113.49 KiB) Viewed 231 times

EDIT:
Ugh, trawling through the CubeMX code is pretty painful...

If the only thing I need to change is that RCC_CFGR register then it looks like this should do the trick:

Code: Select all

    /* Configure PLL */
#ifdef XTAL24M
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00010400); /* pll=72Mhz(x3),APB1=36Mhz,AHB=72Mhz */
#ifdef XTAL12M
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00110400); /* pll=72Mhz(x6),APB1=36Mhz,AHB=72Mhz */
#else
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz(x9),APB1=36Mhz,AHB=72Mhz */
#endif

    SET_REG(RCC_CR, GET_REG(RCC_CR)     | 0x01000000); /* enable the pll */

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

Re: Changing the XTAL frequency

Post by victor_pv » Tue Aug 29, 2017 1:28 am

That should probably be it.
If the 101 is indeed identical to the 103, everything else is fine for a 24Mhz XTAL.
I do not know if there is any other difference between the 101 and the 103, but the USB hardware seems to be there, at least often enough.
It's possible that there are different revisions of the 101, some with the usb and some without, same as some 103C8T6 have 64KB of flash, and some others 128KB.
It's also possible that the 101 may not work reliably at those speeds.
If so, then you can try 48MHZ as mentioned.

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

Re: Changing the XTAL frequency

Post by RogerClark » Tue Aug 29, 2017 2:09 am

@C_D

Yes those lines are the ones you'd need to change

https://github.com/rogerclarkmelbourne/ ... #L118-L122

If you look in

https://github.com/rogerclarkmelbourne/ ... #L576-L591

This lists the bit settings for the various PLL multiplies

i.e 9 x is

RCC_PLLMUL_9 = (0x7 << 18),

and you need 3 x

RCC_PLLMUL_3 = (0x1 << 18)

(note that reg controls other stuff as well, so you'll have to only change bits 18 - 22

C_D
Posts: 62
Joined: Mon May 11, 2015 3:27 am
Location: New Zealand

Re: Changing the XTAL frequency

Post by C_D » Mon Sep 04, 2017 3:26 am

OK, I am making progress but I haven't quite got it working yet.

I worked out how to recompile the bootloader and upload it using my STLink. I successfully recompiled and uploaded to an 8MHz blue pill and it worked.

I have also managed to compile a sketch (Blink n Count) for 24MHz operation by hacking

Code: Select all

...\Arduino_STM32\STM32F1\variants\generic_stm32f103r8\wirishboards_setup.cpp
and adding

Code: Select all

#define BOARD_RCC_PLLMUL RCC_PLLMUL_3

I can upload that sketch to 0x08000000 and it successfully enumerates as a USB COM port and the sketch runs, so my hardware is good.

Below is the messages I get with just the Blink n Count sketch (no bootloader)

Code: Select all

[24243.705172] usb 1-1.4.4.1: new full-speed USB device number 37 using dwc_otg
[24243.838222] usb 1-1.4.4.1: New USB device found, idVendor=1eaf, idProduct=0004
[24243.844297] usb 1-1.4.4.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[24243.850379] usb 1-1.4.4.1: Product: Maple
[24243.853403] usb 1-1.4.4.1: Manufacturer: LeafLabs
[24243.876342] cdc_acm 1-1.4.4.1:1.0: ttyACM0: USB ACM device
[24243.883040] usbcore: registered new interface driver cdc_acm
[24243.888985] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters
My 24MHz compiled bootloader runs (the LED on PC13 blinks at what looks like the correct rate), however I get the following.

Code: Select all

[22009.085166] usb 1-1.4.4.1: new full-speed USB device number 31 using dwc_otg
[22009.185153] usb 1-1.4.4.1: device descriptor read/64, error -32
[22009.405156] usb 1-1.4.4.1: device descriptor read/64, error -32
[22009.625153] usb 1-1.4.4.1: new full-speed USB device number 32 using dwc_otg
[22009.725154] usb 1-1.4.4.1: device descriptor read/64, error -32
[22009.945151] usb 1-1.4.4.1: device descriptor read/64, error -32
[22010.165156] usb 1-1.4.4.1: new full-speed USB device number 33 using dwc_otg
[22010.605158] usb 1-1.4.4.1: device not accepting address 33, error -32
[22010.705157] usb 1-1.4.4.1: new full-speed USB device number 34 using dwc_otg
[22011.145157] usb 1-1.4.4.1: device not accepting address 34, error -32
[22011.151150] usb 1-1.4.4-port1: unable to enumerate USB device

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

Re: Changing the XTAL frequency

Post by RogerClark » Mon Sep 04, 2017 5:41 am

If USB is not working, the most likely cause is that your main clock frequency is not what you think it is

You could be running the main clock at 2 x 24 = 48 and the LED would blink or 4 x 24 = 96 and the LED would blink, but USB would not work with either of these.


However, you are also a crystal which is beyond the spec of the MCU, which says 16Mhz is the highest frequency you can use.

http://www.st.com/content/ccc/resource/ ... 161566.pdf

Its possible that it may work sometimes with a 24MHz external crystal, but its potentially a problem waiting to happen if you don't replace the crystal with one in the operating range for the MCU

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

Re: Changing the XTAL frequency

Post by victor_pv » Mon Sep 04, 2017 3:50 pm

Can you post the changes you made to the bootloader to check if they are all that's needed?

Post Reply