python script to explore clock configs

Post your cool example code here.
Post Reply
ag123
Posts: 714
Joined: Thu Dec 19, 2019 5:30 am
Answers: 3

python script to explore clock configs

Post by ag123 »

note that ST has some pretty cool tools STM32 CUBE that'd aid the process
https://www.st.com/en/development-tools ... 32091.html
https://www.freertos.org/FreeRTOS-Plus/ ... _tree.html
http://www.emcu.it/STM32clk/STM32clk.html
one of them requires MS Excel which i did not managed to make it work in linux libreoffice

hence i hacked up a script to try to get some clock settings

Code: Select all

#!/usr/bin/python3
#FHSE in mhz
FHSE = 8

#cpu search frequency range
fmin = 70
fmax = 110

#clk1 = clk / m
m = list(range(1,64))
#vco = clk1 * n
n = list(range(0,512))
#vco / p = system clock - mcu speed (not systick)
p = list(range(2,9,2))
#vco / q = 48mhz - usb
q = list(range(1,16))
for mi in m:
    for ni in n:
        fvco = FHSE * ni / mi
        if fvco > 432:
            continue
        for qi in q:
            fusb = fvco / qi
            if not ( fusb == int(fusb) and fusb == 48 ):
                    continue
            for pi in p:
                fcpu = fvco / pi            
                if not fcpu == int(fcpu):
                    continue                
                if fusb == 48 \
                   and ( fcpu >= fmin and fcpu <= fmax):
                    print('FHSE:', FHSE,
                      'm:', mi,
                      'n:', ni,
                      'p:', pi,
                      '(RCC_PLLP_DIV' + str(pi) + ')',
                      'q:', qi,
                      'fusb:', fusb,
                      'fcpu:', fcpu)
in this script you need to edit it to specify FHSE the main crystal frequency and the search range fmin and fmax

apparently the script works in some 'online python' shell/ide as well
https://www.python.org/shell/
https://www.onlinegdb.com/online_python_compiler
https://repl.it/languages/python3

note that the above script is based on those stm32 mcu that use the formula:
FCPU (system clock) = FHSE * N / ( M * P )
FUSB (usb clock - has to be 48mhz) = FHSE * N / ( M * Q )
i'm not sure if this is applicable across the board

it would generate entries like such

Code: Select all

FHSE: 8 m: 1 n: 24 p: 2 (RCC_PLLP_DIV2) q: 4 fusb: 48.0 fcpu: 96.0
FHSE: 8 m: 1 n: 48 p: 4 (RCC_PLLP_DIV4) q: 8 fusb: 48.0 fcpu: 96.0
FHSE: 8 m: 1 n: 54 p: 4 (RCC_PLLP_DIV4) q: 9 fusb: 48.0 fcpu: 108.0
FHSE: 8 m: 2 n: 48 p: 2 (RCC_PLLP_DIV2) q: 4 fusb: 48.0 fcpu: 96.0
FHSE: 8 m: 2 n: 96 p: 4 (RCC_PLLP_DIV4) q: 8 fusb: 48.0 fcpu: 96.0
FHSE: 8 m: 2 n: 108 p: 4 (RCC_PLLP_DIV4) q: 9 fusb: 48.0 fcpu: 108.0
FHSE: 8 m: 3 n: 72 p: 2 (RCC_PLLP_DIV2) q: 4 fusb: 48.0 fcpu: 96.0
FHSE: 8 m: 3 n: 144 p: 4 (RCC_PLLP_DIV4) q: 8 fusb: 48.0 fcpu: 96.0
FHSE: 8 m: 3 n: 162 p: 4 (RCC_PLLP_DIV4) q: 9 fusb: 48.0 fcpu: 108.0
this may help somewhat if you started with a board that do not have an existing variant
https://github.com/stm32duino/wiki/wiki ... figuration
e.g.
https://github.com/stm32duino/Arduino_C ... t.cpp#L196

for steve's libmaple (for F4) it seem there are some 'standard' selections
https://github.com/stevstrong/Arduino_S ... cF4.c#L246

STM32F103 is different (simplier perhaps) which limits and reduce the permutations
for roger's libmaple (for F1) it seemed pre-defined for a few selections
https://github.com/rogerclarkmelbourne/ ... up.cpp#L50
https://github.com/rogerclarkmelbourne/ ... _f1.c#L126

messing with the prescalers m, n, p, q the system clock and usb clock = 48mhz, it may help if one starts with a variant for a different board, e.g. to get a blinky to run. but there will still be things to do like setting up the pin mappings and the clocks for other buses and peripherals (e.g. systick, adc etc)

of course, the more adventurous purpose of playing with these clocks is to overclock (or under clock) your stm32
;)
Last edited by ag123 on Thu Nov 19, 2020 4:55 am, edited 2 times in total.
Bingo600
Posts: 82
Joined: Sat Dec 21, 2019 3:56 pm

Re: python script to explore clock configs

Post by Bingo600 »

Thanx :)

Comes in handy , on a linux setup

/Bingo
Post Reply

Return to “Code snippets”