Skip to content

Commit 7924b31

Browse files
pi-anldpgeorge
authored andcommitted
stm32: Generate PLL tables from pre-processed headers.
Allows boards to configure their HSE and PLL values in variants. Signed-off-by: Andrew Leech <[email protected]>
1 parent 22804fc commit 7924b31

File tree

5 files changed

+67
-49
lines changed

5 files changed

+67
-49
lines changed

ports/stm32/Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,15 @@ $(BUILD)/%_$(BOARD).c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(HEADER_
640640
--output-source $(GEN_PINS_SRC) --output-header $(GEN_PINS_HDR) \
641641
--output-af-const $(GEN_PINS_AF_CONST) --output-af-defs $(GEN_PINS_AF_DEFS)
642642

643-
powerctrl.c: $(GEN_PLLFREQTABLE_HDR)
644-
$(GEN_PLLFREQTABLE_HDR): $(PLLVALUES) | $(HEADER_BUILD)
643+
$(BUILD)/powerctrl.o: $(GEN_PLLFREQTABLE_HDR)
644+
$(GEN_PLLFREQTABLE_HDR): $(PLLVALUES) | $(HEADER_BUILD)/qstr.i.last
645645
$(ECHO) "GEN $@"
646-
$(Q)$(PYTHON) $(PLLVALUES) -c -m $(CMSIS_MCU_LOWER) file:$(BOARD_DIR)/stm32$(MCU_SERIES)xx_hal_conf.h > $@
646+
$(Q)$(PYTHON) $(PLLVALUES) -c -m $(CMSIS_MCU_LOWER) file:$(HEADER_BUILD)/qstr.i.last > $@
647647

648-
$(TOP)/extmod/machine_i2s.c: $(GEN_PLLI2STABLE_HDR)
649-
$(GEN_PLLI2STABLE_HDR): $(PLLI2SVALUES) | $(HEADER_BUILD)
648+
$(BUILD)/extmod/machine_i2s.o: $(GEN_PLLI2STABLE_HDR)
649+
$(GEN_PLLI2STABLE_HDR): $(PLLI2SVALUES) | $(HEADER_BUILD)/qstr.i.last
650650
$(ECHO) "GEN $@"
651-
$(Q)$(PYTHON) $(PLLI2SVALUES) -c -m $(CMSIS_MCU_LOWER) hse:$(BOARD_DIR)/stm32$(MCU_SERIES)xx_hal_conf.h pllm:$(BOARD_DIR)/mpconfigboard.h > $@
651+
$(Q)$(PYTHON) $(PLLI2SVALUES) -c -m $(CMSIS_MCU_LOWER) file:$(HEADER_BUILD)/qstr.i.last > $@
652652

653653
$(BUILD)/modstm.o: $(GEN_STMCONST_HDR)
654654
$(HEADER_BUILD)/modstm_const.h: $(CMSIS_MCU_HDR) make-stmconst.py | $(HEADER_BUILD)

ports/stm32/boards/plli2svalues.py

+28-27
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,21 @@ def generate_c_table(plli2s_table, hse, pllm):
123123
print("}")
124124

125125

126-
def search_header(filename, re_include, re_define, lookup, val):
127-
regex_include = re.compile(re_include)
126+
def search_header(filename, re_define, lookup):
128127
regex_define = re.compile(re_define)
128+
val = None
129129
with open(filename) as f:
130130
for line in f:
131131
line = line.strip()
132-
m = regex_include.match(line)
133-
if m:
134-
# Search included file
135-
search_header(m.group(1), re_include, re_define, lookup, val)
136-
continue
137132
m = regex_define.match(line)
138133
if m:
139134
# found lookup value
135+
found = m.group(3)
136+
if "*" in found or "/" in found:
137+
# process define using multiply or divide to calculate value
138+
found = eval(found)
140139
if m.group(1) == lookup:
141-
val[0] = int(m.group(3))
140+
val = int(found)
142141
return val
143142

144143

@@ -166,35 +165,37 @@ def main():
166165
break
167166

168167
if mcu_series in mcu_support_plli2s:
169-
if len(argv) != 2:
168+
if len(argv) not in (1, 2):
170169
print("usage: pllvalues.py [-c] [-m <mcu_series>] <hse in MHz> <pllm in MHz>")
171170
sys.exit(1)
172171

173172
if argv[0].startswith("hse:"):
174-
# extract HSE_VALUE from header file
175-
(hse,) = search_header(
176-
argv[0][len("hse:") :],
177-
r'#include "(boards/[A-Za-z0-9_./]+)"',
178-
r"#define +(HSE_VALUE) +\((\(uint32_t\))?([0-9]+)\)",
179-
"HSE_VALUE",
180-
[None],
173+
hse = int(argv[0][len("hse:") :])
174+
175+
if argv[0].startswith("pllm:"):
176+
pllm = int(argv[0][len("pllm:") :])
177+
178+
if argv[0].startswith("file:"):
179+
# extract hse value from processed header files
180+
hse = search_header(
181+
argv[0][len("file:") :],
182+
r"static.* (micropy_hw_hse_value) = +\(*(\(uint32_t\))?([0-9 +-/\*]+)\)*;",
183+
"micropy_hw_hse_value",
181184
)
182185
if hse is None:
183-
raise ValueError("%s does not contain a definition of HSE_VALUE" % argv[0])
184-
argv.pop(0)
186+
raise ValueError(
187+
"%s does not contain a definition of micropy_hw_hse_value" % argv[0]
188+
)
185189

186-
if argv[0].startswith("pllm:"):
187-
# extract MICROPY_HW_CLK_PLLM from header file
188-
(pllm,) = search_header(
189-
argv[0][len("pllm:") :],
190-
r'#include "(boards/[A-Za-z0-9_./]+)"',
191-
r"#define +(MICROPY_HW_CLK_PLLM) +\((\(uint32_t\))?([0-9]+)\)",
192-
"MICROPY_HW_CLK_PLLM",
193-
[None],
190+
# extract pllm value from processed header files
191+
pllm = search_header(
192+
argv[0][len("file:") :],
193+
r"static.* (micropy_hw_clk_pllm) = +\(*(\(uint32_t\))?([0-9 +-/\*]+)\)*;",
194+
"micropy_hw_clk_pllm",
194195
)
195196
if pllm is None:
196197
raise ValueError(
197-
"%s does not contain a definition of MICROPY_HW_CLK_PLLM" % argv[0]
198+
"%s does not contain a definition of micropy_hw_clk_pllm" % argv[0]
198199
)
199200
argv.pop(0)
200201

ports/stm32/boards/pllvalues.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -228,26 +228,26 @@ def print_table(hse, valid_plls):
228228
print("found %u valid configurations" % len(valid_plls))
229229

230230

231-
def search_header_for_hsx_values(filename, vals):
232-
regex_inc = re.compile(r'#include "(boards/[A-Za-z0-9_./]+)"')
233-
regex_def = re.compile(r"#define +(HSE_VALUE|HSI_VALUE) +\((\(uint32_t\))?([0-9]+)\)")
231+
def search_header_for_hsx_values(filename):
232+
hse = hsi = None
233+
regex_def = re.compile(
234+
r"static.* +(micropy_hw_hs[ei]_value) = +\(*(\(uint32_t\))?([0-9 +-/\*]+)\)*;",
235+
)
234236
with open(filename) as f:
235237
for line in f:
236238
line = line.strip()
237-
m = regex_inc.match(line)
238-
if m:
239-
# Search included file
240-
search_header_for_hsx_values(m.group(1), vals)
241-
continue
242239
m = regex_def.match(line)
243240
if m:
244241
# Found HSE_VALUE or HSI_VALUE
245-
val = int(m.group(3)) // 1000000
246-
if m.group(1) == "HSE_VALUE":
247-
vals[0] = val
242+
found = m.group(3)
243+
if "*" in found or "/" in found:
244+
found = eval(found)
245+
val = int(found) // 1000000
246+
if m.group(1) == "micropy_hw_hse_value":
247+
hse = val
248248
else:
249-
vals[1] = val
250-
return vals
249+
hsi = val
250+
return hse, hsi
251251

252252

253253
def main():
@@ -280,9 +280,9 @@ def main():
280280

281281
if argv[0].startswith("file:"):
282282
# extract HSE_VALUE, and optionally HSI_VALUE, from header file
283-
hse, hsi = search_header_for_hsx_values(argv[0][5:], [None, None])
283+
hse, hsi = search_header_for_hsx_values(argv[0][5:])
284284
if hse is None:
285-
raise ValueError("%s does not contain a definition of HSE_VALUE" % argv[0])
285+
raise ValueError("%s does not contain a definition of micropy_hw_hse_value" % argv[0])
286286
else:
287287
# HSE given directly as an integer
288288
hse = int(argv[0])

ports/stm32/machine_i2s.c

+2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include "py/mphal.h"
3434
#include "pin.h"
3535
#include "dma.h"
36+
#ifndef NO_QSTR
3637
#include "genhdr/plli2stable.h"
38+
#endif
3739

3840
// Notes on this port's specific implementation of I2S:
3941
// - the DMA callbacks (1/2 complete and complete) are used to implement the asynchronous background operations

ports/stm32/powerctrl.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,23 @@
2828
#include "py/mphal.h"
2929
#include "powerctrl.h"
3030
#include "rtc.h"
31-
#include "genhdr/pllfreqtable.h"
3231
#include "extmod/modbluetooth.h"
32+
#include "py/mpconfig.h"
33+
#ifndef NO_QSTR
34+
#include "genhdr/pllfreqtable.h"
35+
#endif
36+
37+
// These will be defined / expanded in pre-processor output for use in the
38+
// boards/pllvalues.py script, then generally stripped from final firmware.
39+
#ifdef HSI_VALUE
40+
static uint32_t __attribute__((unused)) micropy_hw_hsi_value = HSI_VALUE;
41+
#endif
42+
#ifdef HSE_VALUE
43+
static uint32_t __attribute__((unused)) micropy_hw_hse_value = HSE_VALUE;
44+
#endif
45+
#ifdef MICROPY_HW_CLK_PLLM
46+
static uint32_t __attribute__((unused)) micropy_hw_clk_pllm = MICROPY_HW_CLK_PLLM;
47+
#endif
3348

3449
#if defined(STM32H5) || defined(STM32H7)
3550
#define RCC_SR RSR

0 commit comments

Comments
 (0)