Skip to content

Commit 38ddee9

Browse files
committed
[delay] Use max device frequency to derive us_shift
1 parent 82bc4a9 commit 38ddee9

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

ext/modm-devices

Submodule modm-devices updated 106 files

src/modm/platform/core/sam/module.lb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ def build(env):
3333
env.template("startup_platform.c.in")
3434

3535
# delay code that must be tuned for each family
36-
# (cycles per loop, setup cost in loops, us shift)
36+
# (cycles per loop, setup cost in loops, max cpu frequency)
3737
tuning = {
38-
"d": (3, 4, 5), # CM0 tested on D21 in RAM
39-
"g": (6, 4, 5), # G55
40-
"v": (4, 4, 3), # V70
41-
}.get(target.family, (4, 4, 5))
38+
"d": (3, 4, 48), # CM0 tested on D21 in RAM
39+
"g": (6, 4, 120), # G55
40+
"v": (4, 4, 300), # V70
41+
}[target.family]
42+
43+
# us_shift is an optimization to limit error via fractional math
44+
us_shift = 32 - math.ceil(math.log2(tuning[2] * 1e6))
4245

4346
env.substitutions.update({
4447
"with_cm0": env[":target"].has_driver("core:cortex-m0*"),
4548
"with_cm7": env[":target"].has_driver("core:cortex-m7*"),
4649
"loop": tuning[0],
4750
"shift": int(math.log2(tuning[1])),
48-
"us_shift": tuning[2],
51+
"us_shift": us_shift,
4952
"with_assert": env.has_module(":architecture:assert")
5053
})
5154
env.template("../cortex/delay_ns.cpp.in", "delay_ns.cpp")

src/modm/platform/core/stm32/module.lb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,34 @@ def build(env):
3333
env.template("startup_platform.c.in")
3434

3535
# delay code that must be tuned for each family
36-
# (cycles per loop, setup cost in loops, max us shift)
36+
# (cycles per loop, setup cost in loops)
3737
tuning = {
38-
"g4": (3, 4, 5), # CM4 tested on G476 in RAM
39-
"l0": (3, 4, 6), # CM0+ tested on L031 in RAM
40-
"g0": (3, 4, 5), # CM0+ tested on G072 in RAM
41-
"f7": (4, 4, 4), # CM7 tested on F767 in ITCM
42-
"h7": (4, 4, 3), # CM7 tested on H743 in ITCM
43-
"l4": (3, 4, 5), # CM4 tested on L476 in SRAM2
44-
"f3": (3, 4, 5), # CM4 tested on F334, F303 in CCM RAM
38+
"g4": (3, 4), # CM4 tested on G476 in RAM
39+
"l0": (3, 4), # CM0+ tested on L031 in RAM
40+
"g0": (3, 4), # CM0+ tested on G072 in RAM
41+
"f7": (4, 4), # CM7 tested on F767 in ITCM
42+
"h7": (4, 4), # CM7 tested on H743 in ITCM
43+
"l4": (3, 4), # CM4 tested on L476 in SRAM2
44+
"f3": (3, 4), # CM4 tested on F334, F303 in CCM RAM
4545

4646
# Defaults of (4, 4) for these families:
47-
"l1": (4, 4, 6), # CM3 tested on L152 in RAM
48-
"f0": (4, 4, 6), # CM3 tested on F071 in RAM
49-
"f1": (4, 4, 5), # CM3 tested on F103 in RAM
50-
"f4": (4, 4, 4), # CM4 tested on F401, F411, F446 in RAM
51-
"f2": (4, 4, 4), # CM3 guessed based on documentation
47+
"l1": (4, 4), # CM3 tested on L152 in RAM
48+
"f0": (4, 4), # CM3 tested on F071 in RAM
49+
"f1": (4, 4), # CM3 tested on F103 in RAM
50+
"f4": (4, 4), # CM4 tested on F401, F411, F446 in RAM
51+
"f2": (4, 4), # CM3 guessed based on documentation
5252
}[target.family]
5353

54+
# us_shift is an optimization to limit error via fractional math
55+
frequency = int(env[":target"].get_driver("rcc")["max-frequency"][0])
56+
us_shift = 32 - math.ceil(math.log2(frequency))
57+
5458
env.substitutions.update({
5559
"with_cm0": env[":target"].has_driver("core:cortex-m0*"),
5660
"with_cm7": env[":target"].has_driver("core:cortex-m7*"),
5761
"loop": tuning[0],
5862
"shift": int(math.log2(tuning[1])),
59-
"us_shift": tuning[2],
63+
"us_shift": us_shift,
6064
"with_assert": env.has_module(":architecture:assert")
6165
})
6266
env.template("../cortex/delay_ns.cpp.in", "delay_ns.cpp")

0 commit comments

Comments
 (0)