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

Re: Changing the XTAL frequency

Post by C_D » Mon Sep 04, 2017 7:57 pm

RogerClark wrote:
Mon Sep 04, 2017 5:41 am
...you are also a crystal which is beyond the spec of the MCU, which says 16Mhz is the highest frequency you can use.
Ah, so it is. I'm sure I saw somewhere else that it was 8-25MHz but I can't recall where. There is another IC on this board which uses a 24MHz crystal and I think the board designer has tried to reduce the BOM by using the same crystal for the micro.
RogerClark wrote:
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
Yeah that's my thought too. The fact that USB runs fine in the sketch leads me to believe that something is still configured incorrectly in the bootloader.
victor_pv wrote:
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?
The only change I have made so far is to add an #ifdef for XTAL24M in hardware.c

Code: Select all

    /* Configure PLL */
#ifdef XTAL24M
    SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00010400); /* pll=72Mhz(x3),APB1=36Mhz,AHB=72Mhz */
#elif 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: 1681
Joined: Mon Apr 27, 2015 12:12 pm

Re: Changing the XTAL frequency

Post by victor_pv » Tue Sep 05, 2017 12:42 am

This is wrong (thats multiplying by 2, so 48Mhz):

Code: Select all

   SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00010400); /* pll=72Mhz(x3),APB1=36Mhz,AHB=72Mhz */
This should work (multiplies by 3):

Code: Select all

   SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00050400); /* pll=72Mhz(x3),APB1=36Mhz,AHB=72Mhz */
Or this (divides by 2, then multiplies by 6):

Code: Select all

   SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00130400); /* pll=72Mhz(x6/2),APB1=36Mhz,AHB=72Mhz */
Reference, page 101 of the reference manual:
http://www.st.com/content/ccc/resource/ ... 171190.pdf

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

Re: Changing the XTAL frequency

Post by RogerClark » Tue Sep 05, 2017 12:50 am

Victor

I think I posted the same hex value into the other thread that C_D has going

i.e His bit pattern was wrong.

viewtopic.php?f=32&t=508&p=33990#p33990

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

Re: Changing the XTAL frequency

Post by C_D » Tue Sep 05, 2017 1:04 am

victor_pv wrote:
Tue Sep 05, 2017 12:42 am
This is wrong ...
This should work (multiplies by 3):

Code: Select all

   SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) | 0x00050400); /* pll=72Mhz(x3),APB1=36Mhz,AHB=72Mhz */
RogerClark wrote:
Tue Sep 05, 2017 12:50 am
His bit pattern was wrong.
:oops:
I am embarrassed to say I double checked that and still got it wrong.

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

Re: Changing the XTAL frequency

Post by RogerClark » Tue Sep 05, 2017 1:13 am

I wrote some code to do it.

Code: Select all

uint32_t v = 0x001D0400;// original  x9 bit pattern
uint32_t mask = (0B1111 << 18);// PLL mask 
uint32_t pattern = (0x1 << 18);// x 3 PLL bit pattern

v = (v & ~mask);
v = v | pattern;
Serial.println(v,HEX);

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

Re: Changing the XTAL frequency

Post by C_D » Tue Sep 05, 2017 1:19 am

And confirmed working 8-)

Thanks so much guys. Sorry to have taken up your time with a silly mistake, but on the plus side I know a heap more about what makes Arduino for STM32 work now!
Last edited by C_D on Tue Sep 05, 2017 4:45 am, edited 1 time in total.

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

Re: Changing the XTAL frequency

Post by RogerClark » Tue Sep 05, 2017 1:37 am

Looks like some sort of industrial controller

I hope you're not using the bootloader etc in any mission critical or safety related application, because as stated in the repo this code should be considered as experimental. It was never intended to be used in commercial applications and you use it at your own risk

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

Re: Changing the XTAL frequency

Post by C_D » Tue Sep 05, 2017 1:42 am

Fear not, its a prototype only :lol: And no it wont kill anyone if it catches fire.

But the Arduino environment is perfect for me to quickly mash out new features to play with and test. I actually arrived here having come from MBed and the Nucleo range because a few years ago the USB serial implementation was buggy as hell. The Arduino for STM32 USB serial code has never missed a beat :)

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

Re: Changing the XTAL frequency

Post by RogerClark » Tue Sep 05, 2017 1:59 am

You may want to consider using STM's own Arduino Core github.com/stm32duino as that uses the HAL, and it will be easier to port your final code to something that is not Arduino based.

However I don't think they support the bootloader very well at the moment, nor changing the PLL for the crystal

You should also replace your crystal with an 8Mhz or 16Mhz, as 24Mhz is well outside the max operating frequency and could prove unreliable in mass production.

Most boards seem to use 8Mhz (perhaps as its the cheapest and most stable option)

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

Re: Changing the XTAL frequency

Post by C_D » Tue Sep 05, 2017 2:12 am

Yup, the 24MHz crystal was a mistake for sure. I've got a handful of boards made like this which I will keep working with now I have the bootloader running. Next version will be back to 8MHz for sure.

I will look into the ST core too, I should at the very least have a play with it and see if its workable.
Last edited by C_D on Tue Sep 05, 2017 2:14 am, edited 2 times in total.

Post Reply