Skip to content

Commit aecdfc1

Browse files
committed
stm32/Makefile: Generate PLL tables from pre-processed headers.
Allows boards to configure their HSE and PLL values in variants. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent cd707f4 commit aecdfc1

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

ports/stm32/Makefile

Lines changed: 6 additions & 6 deletions
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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ def search_header(filename, re_include, re_define, lookup, val):
137137
m = regex_define.match(line)
138138
if m:
139139
# found lookup value
140+
found = m.group(3)
141+
if "*" in found or "/" in found:
142+
found = eval(found)
140143
if m.group(1) == lookup:
141-
val[0] = int(m.group(3))
144+
val[0] = int(found)
142145
return val
143146

144147

@@ -166,35 +169,41 @@ def main():
166169
break
167170

168171
if mcu_series in mcu_support_plli2s:
169-
if len(argv) != 2:
172+
if len(argv) not in (1, 2):
170173
print("usage: pllvalues.py [-c] [-m <mcu_series>] <hse in MHz> <pllm in MHz>")
171174
sys.exit(1)
172175

173176
if argv[0].startswith("hse:"):
174-
# extract HSE_VALUE from header file
177+
hse = int(argv[0][len("hse:") :])
178+
179+
if argv[0].startswith("pllm:"):
180+
pllm = int(argv[0][len("pllm:") :])
181+
182+
if argv[0].startswith("file:"):
183+
# extract HSE_VALUE and from header file
175184
(hse,) = search_header(
176-
argv[0][len("hse:") :],
185+
argv[0][len("file:") :],
177186
r'#include "(boards/[A-Za-z0-9_./]+)"',
178-
r"#define +(HSE_VALUE) +\((\(uint32_t\))?([0-9]+)\)",
179-
"HSE_VALUE",
187+
r"static.* (micropy_hw_hse_value) = +\(*(\(uint32_t\))?([0-9 +-/\*]+)\)*;",
188+
"micropy_hw_hse_value",
180189
[None],
181190
)
182191
if hse is None:
183-
raise ValueError("%s does not contain a definition of HSE_VALUE" % argv[0])
184-
argv.pop(0)
192+
raise ValueError(
193+
"%s does not contain a definition of micropy_hw_hse_value" % argv[0]
194+
)
185195

186-
if argv[0].startswith("pllm:"):
187196
# extract MICROPY_HW_CLK_PLLM from header file
188197
(pllm,) = search_header(
189-
argv[0][len("pllm:") :],
198+
argv[0][len("file:") :],
190199
r'#include "(boards/[A-Za-z0-9_./]+)"',
191-
r"#define +(MICROPY_HW_CLK_PLLM) +\((\(uint32_t\))?([0-9]+)\)",
192-
"MICROPY_HW_CLK_PLLM",
200+
r"static.* (micropy_hw_clk_pllm) = +\(*(\(uint32_t\))?([0-9 +-/\*]+)\)*;",
201+
"micropy_hw_clk_pllm",
193202
[None],
194203
)
195204
if pllm is None:
196205
raise ValueError(
197-
"%s does not contain a definition of MICROPY_HW_CLK_PLLM" % argv[0]
206+
"%s does not contain a definition of micropy_hw_clk_pllm" % argv[0]
198207
)
199208
argv.pop(0)
200209

ports/stm32/boards/pllvalues.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ def print_table(hse, valid_plls):
230230

231231
def search_header_for_hsx_values(filename, vals):
232232
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]+)\)")
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()
@@ -242,8 +244,11 @@ def search_header_for_hsx_values(filename, vals):
242244
m = regex_def.match(line)
243245
if m:
244246
# Found HSE_VALUE or HSI_VALUE
245-
val = int(m.group(3)) // 1000000
246-
if m.group(1) == "HSE_VALUE":
247+
found = m.group(3)
248+
if "*" in found or "/" in found:
249+
found = eval(found)
250+
val = int(found) // 1000000
251+
if m.group(1) == "micropy_hw_hse_value":
247252
vals[0] = val
248253
else:
249254
vals[1] = val
@@ -282,7 +287,7 @@ def main():
282287
# extract HSE_VALUE, and optionally HSI_VALUE, from header file
283288
hse, hsi = search_header_for_hsx_values(argv[0][5:], [None, None])
284289
if hse is None:
285-
raise ValueError("%s does not contain a definition of HSE_VALUE" % argv[0])
290+
raise ValueError("%s does not contain a definition of micropy_hw_hse_value" % argv[0])
286291
else:
287292
# HSE given directly as an integer
288293
hse = int(argv[0])

ports/stm32/machine_i2s.c

Lines changed: 2 additions & 0 deletions
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

Lines changed: 16 additions & 1 deletion
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)