Skip to content

Commit 2d86ce3

Browse files
committed
[stm32] Use register map query for RCC module
1 parent 97e2122 commit 2d86ce3

File tree

3 files changed

+168
-386
lines changed

3 files changed

+168
-386
lines changed

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def build(env):
3232
driver = device.get_driver("rcc")
3333
regs = env.query(":cmsis:device:registers")
3434

35-
properties = {}
35+
properties = {"regs": regs}
3636
properties["target"] = target = device.identifier
3737
properties["partname"] = device.partname
3838
properties["core"] = core = device.get_driver("core")["type"]
@@ -64,23 +64,34 @@ def build(env):
6464
properties["lsi_frequency"] = 32_000
6565
properties["boot_frequency"] = properties["hsi_frequency"]
6666

67+
properties["hsi48"] = regs.search(r"RCC_(.*?)_HSI48ON")
68+
properties["bdcr"] = regs.search(r"RCC_(.*?)_RTCSEL_\d")
69+
properties["usbprescaler"] = regs.search("RCC_CFGR_USBPRE")
70+
properties["pll_input_range"] = regs.findall("RCC_.*?_PLL1RGE_\d")
71+
72+
# STM32H7 map of domains to APB bus index
73+
name_map = {
74+
"D1": 3, "D21": 1, "D22": 2, "D3": 4,
75+
"CD": 3, "CD1": 1, "CD2": 2, "SRD": 4}
76+
bus_prescalers = defaultdict(list)
77+
for whole, reg, domain, index, div in regs.findall("(RCC_(.*?)_(.*?)PPRE(\d?)_DIV(\d+))"):
78+
bus_prescalers[name_map.get(domain+index, index)].append((whole, reg, div))
79+
# Not all header define the _DIV enumerations, so we manually add them here…
80+
if not bus_prescalers:
81+
for whole, reg, domain, index in regs.findall("(RCC_(.*?)_(.*?)PPRE(\d?))"):
82+
for div, value in enumerate((0b000, 0b100, 0b101, 0b110, 0b111)):
83+
value = (f"{value} << {whole}_Pos", reg, 2**div)
84+
bus_prescalers[name_map.get(domain+index, index)].append(value)
85+
properties["bus_prescalers"] = bus_prescalers
86+
import pprint
87+
pprint.pprint(bus_prescalers)
88+
6789
# TODO: Move this data into the device files
68-
properties["usbprescaler"] = device.has_driver("usb") and target.family in ["f0", "f1", "f3"]
6990
properties["pllprediv"] = \
7091
(target["family"] in ["f0", "f3"] or (target["family"] == "f1" and target["name"] in ["00", "05", "07"]))
7192
properties["pllprediv2"] = False # FIXME: not sure what value this should have
7293
properties["pll_hse_prediv2"] = target["family"] == "f1" and target["name"] in ["01", "02", "03"]
73-
properties["hsi48"] = \
74-
(target["family"] == "f0" and target["name"] in ["42", "48", "71", "72", "78", "91", "98"]) or \
75-
(target["family"] == "l4" and target["name"][0] not in ["7", "8"]) or \
76-
(target["family"] == "l5") or \
77-
(target["family"] == "u5")
78-
if target["family"] in ["l4", "l5"]:
79-
properties["hsi48_cr"] = "CRRCR"
80-
elif target["family"] in ["c0", "u5"]:
81-
properties["hsi48_cr"] = "CR"
82-
else:
83-
properties["hsi48_cr"] = "CR2"
94+
8495
properties["pll_p"] = ((target["family"] == "l4" and target["name"] not in ["12", "22"]) or target["family"] == "g4")
8596
properties["overdrive"] = (target["family"] == "f7") or \
8697
((target["family"] == "f4") and target["name"] in ["27", "29", "37", "39", "46", "69", "79"])
@@ -127,7 +138,6 @@ def build(env):
127138
if target.family == "h7" else ""
128139
properties["cfgr3"] = ("SRDCFGR" if target.name in ["a0", "a3", "b0", "b3"] else "D3CFGR")
129140
properties["d3"] = ("SRD" if target.name in ["a0", "a3", "b0", "b3"] else "D3")
130-
properties["bdcr"] = "CSR1" if target.family in ["c0"] else "CSR" if target.family in ["l0", "l1"] else "BDCR"
131141
properties["pll_ids"] = ["1", "2", "3"] if target.family in ["h7", "u5"] else [] if target.family in ["c0"] else [""]
132142
properties["has_smps"] = target["family"] == "h7" and (target["name"] in ["25", "35", "45", "47", "55", "57"] or \
133143
(target["name"] in ["30", "a3", "b0", "b3"] and target["variant"] == "q"))
@@ -145,7 +155,7 @@ def build(env):
145155

146156
all_peripherals = env.query(":cmsis:device:peripherals")
147157
rcc_map = defaultdict(dict)
148-
for (reg, per, typ) in regs.findall(r"RCC_([A-Z0-9]*?)_([A-Z0-9]+?)(EN|RST)"):
158+
for (reg, per, typ) in regs.findall(r"RCC_(A[HP]B\d?(?:ENR|RSTR)\d?)_(.*?)(EN|RST)"):
149159
rcc_map[per][typ] = reg
150160
rcc_enable = {}
151161
rcc_reset = {}

src/modm/platform/clock/stm32/rcc.cpp.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ bool
4444
Rcc::enableInternalClockMHz48(uint32_t waitCycles)
4545
{
4646
bool retval;
47-
RCC->{{hsi48_cr}} |= RCC_{{hsi48_cr}}_HSI48ON;
48-
while (not (retval = (RCC->{{hsi48_cr}} & RCC_{{hsi48_cr}}_HSI48RDY)) and --waitCycles)
47+
RCC->{{hsi48}} |= RCC_{{hsi48}}_HSI48ON;
48+
while (not (retval = (RCC->{{hsi48}} & RCC_{{hsi48}}_HSI48RDY)) and --waitCycles)
4949
;
5050
return retval;
5151
}

0 commit comments

Comments
 (0)