[SOLVED] the best way to use 16MHz crystal (given board [STM32F1]) ?

If you made your own board, post here, unless you built a Maple or Maple mini clone etc
BlackBrix
Posts: 31
Joined: Wed Dec 06, 2017 7:49 am

[SOLVED] the best way to use 16MHz crystal (given board [STM32F1]) ?

Post by BlackBrix » Sun Dec 31, 2017 3:54 pm

my board uses a 16MHz crystal and I don't want to change it.
its a STM32F103VET6 and I want to use Rogers core

what is the best way to implement it into my additional board definitions & files for the core ?
(of course it would be nice if the modification can be done in some of the files in my added variant folder instead of modifying core files)

there is a suggest from Roger here -> viewtopic.php?p=13842#p13842
to change the boards_setup.cpp so I tried to insert something like this

Code: Select all

#ifdef XTAL16M
        // 16MHz crystal (HSE)
        // in this case we set additionally the Bit 17 (PLLXTPRE=1)  =>  then HSE clock is divided by 2 before PLL entry
        static stm32f1_rcc_pll_data pll_data = {BOARD_RCC_PLLMUL | (0x1 << 17) };
#else
        // default 8MHz crystal
        static stm32f1_rcc_pll_data pll_data = {BOARD_RCC_PLLMUL};
#endif
but this does not compile due to type mismatch since stm32f1_rcc_pll_data is a struct of an enum type ...
Last edited by BlackBrix on Sun Jan 21, 2018 11:01 am, edited 1 time in total.

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

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by Pito » Sun Dec 31, 2017 5:03 pm

It may work when you do in Arduino_STM32/STM32F1/cores/maple/libmaple/rcc_f1.c line 107:

Code: Select all

RCC_BASE->CFGR = pll_src | pll_mul | (0x3<<22) | (0x1<<17);
Bit 17 (PLLXTPRE) set divides the 16MHz by 2.
Not tested.

Mind the bootloader needs the change as well. You may try to flash your sketch with the ST-link instead.
Pukao Hats Cleaning Services Ltd.

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

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by RogerClark » Sun Dec 31, 2017 8:18 pm

Like Pito says.

You should only need to change the clock PLL multiplier

If you look at the GD32 variant ( which is no longer an option in the menu but the code is still included),it used a 12MHz crystal, so you can compare with something like the generic stm32c8 variant and not the difference to the PLL settings

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

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by Pito » Mon Jan 01, 2018 2:59 am

PF 2018!

72/16 = 4.5 thus the only way is to set the PLL prescaler as above (bit 17). The integer PLL multiplieries will not work for 72MHz and 16Mhz Xtal.
Pukao Hats Cleaning Services Ltd.

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

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by RogerClark » Mon Jan 01, 2018 3:06 am

Pito wrote:
Mon Jan 01, 2018 2:59 am
PF 2018!

72/16 = 4.5 thus the only way is to set the PLL prescaler as above (bit 17). The integer PLL multiplieries will not work for 72MHz and 16Mhz Xtal.
Good point

3 x PLL could be used, to give 48MHz and still maintain USB

Alternative the OP would need to use 64MHz to stay within spec or 80MHz to operate just beyond spec
(In normal domestic conditions I've found 128Mhz worked fine)

But for all these options the F_CPU defines would need to be changed, and running at frequencies other than 72MHz has not been extensively tested.

BTW. I recall someone else posting a similar question, but I think they may have been using an even higher frequency crystal

BlackBrix
Posts: 31
Joined: Wed Dec 06, 2017 7:49 am

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by BlackBrix » Mon Jan 01, 2018 10:00 am

Pito wrote:
Sun Dec 31, 2017 5:03 pm
It may work when you do in Arduino_STM32/STM32F1/cores/maple/libmaple/rcc_f1.c line 107:

Code: Select all

RCC_BASE->CFGR = pll_src | pll_mul | (0x3<<22) | (0x1<<17);
Bit 17 (PLLXTPRE) set divides the 16MHz by 2.
Not tested.

Mind the bootloader needs the change as well. You may try to flash your sketch with the ST-link instead.
thanks a lot Pito, I will check that,
unfortunately I will change core files then, thats what I wanted to avoid ...
(it would be better if the modification can be done in some of the files in my added variant folder instead of modifying core files)

the (modified) bootloader works already with the PLLXTPRE division by 2 trick --> viewtopic.php?p=39169#p39169

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

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by Pito » Mon Jan 01, 2018 11:52 am

unfortunately I will change core files then, thats what I wanted to avoid ...
(it would be better if the modification can be done in some of the files in my added variant folder instead of modifying core files)
There is the RCC_CFGR_PLLXTPRE defined in the F1 rcc.h, but not used.
It must be added into the core in order to use it in your variant (boards_setup.cpp), it seems..
Pukao Hats Cleaning Services Ltd.

BlackBrix
Posts: 31
Joined: Wed Dec 06, 2017 7:49 am

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by BlackBrix » Mon Jan 01, 2018 7:11 pm

Pito wrote:It may work when you do in Arduino_STM32/STM32F1/cores/maple/libmaple/rcc_f1.c line 107:
Pito, I tested your suggestion but it didn't work,
it seems that the function rcc_clk_init() is not used by the core anymore (?)

some lines below within the function rcc_configure_pll() I replaced line 135
with:

Code: Select all

#ifdef XTAL16M
    // 16MHz crystal (HSE)
    // in this case we additionally set the Bit 17 (PLLXTPRE=1)  =>  then HSE clock is divided by 2 before PLL entry
    RCC_BASE->CFGR = cfgr | RCC_CFGR_PLLXTPRE;
#else
    // default 8MHz or 12 MHz crystal (no division before PLL entry)
    RCC_BASE->CFGR = cfgr;
#endif
and then it works :D
thanks again Pito !



Do you think it could be added to the core?
Last edited by BlackBrix on Mon Jan 01, 2018 9:01 pm, edited 6 times in total.

User avatar
mrburnette
Posts: 2052
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by mrburnette » Mon Jan 01, 2018 7:34 pm

BlackBrix wrote:
Mon Jan 01, 2018 7:11 pm
Do you think it could be added to the core?
... Why should the core be changed for a one-off initiative?

The question would be if the forum wished to fully support the referenced board, the way we do Black, Blue, Red, and Maple Minis.
Example: http://wiki.stm32duino.com/index.php?title=Blue_Pill

I am pleased you have your unit functioning with a 16MHz crystal and this thread will document how you achieved the solution. Please feel free to ask for WiKi privileges and document the over-clocking. However, core changes introduce many problems even for things that seem overly simple as can be seen from the number of roll-backs last year.

My recommendation is just to use a Post Note-it to remind yourself to make a local change to your system everytime you update your PC from the master github.


Ray

BlackBrix
Posts: 31
Joined: Wed Dec 06, 2017 7:49 am

Re: [STM32F1] the best way to use 16MHz crystal (given board) ?

Post by BlackBrix » Mon Jan 01, 2018 9:10 pm

mrburnette wrote:Why should the core be changed for a one-off initiative ?
because the core could be executed on all extisting or future boards with 16MHz-crystal then.
(not thinking only about my "initiative" or especially this board)
mrburnette wrote:Please feel free to ask for WiKi privileges and document the over-clocking.
there's done no overclocking, it is still all the same like on the 8MHz-crystal boards:
pll=72Mhz / APB1=36Mhz / AHB=72Mhz / USB=48MHz

Post Reply