modm_data.header2svd.stmicro

 1# Copyright 2022, Niklas Hauser
 2# SPDX-License-Identifier: MPL-2.0
 3
 4from .header import Header, getDefineForDevice
 5
 6from .tree import normalize_memory_map
 7
 8__all__ = [
 9    "Header",
10    "getDefineForDevice",
11    "normalize_memory_map",
12]
def getDefineForDevice(device_id, familyDefines):
37def getDefineForDevice(device_id, familyDefines):
38    # get all defines for this device name
39    devName = f"STM32{device_id.family.upper()}{device_id.name.upper()}"
40
41    # Map STM32F7x8 -> STM32F7x7
42    if device_id.family == "f7" and devName[8] == "8":
43        devName = devName[:8] + "7"
44
45    deviceDefines = sorted([define for define in familyDefines if define.startswith(devName)])
46    # if there is only one define thats the one
47    if len(deviceDefines) == 1:
48        return deviceDefines[0]
49
50    # sort with respecting variants
51    minlen = min(len(d) for d in deviceDefines)
52    deviceDefines.sort(key=lambda d: (d[:minlen], d[minlen:]))
53
54    # now we match for the size-id (and variant-id if applicable).
55    if device_id.family == "h7":
56        devNameMatch = devName + "xx"
57    else:
58        devNameMatch = devName + f"x{device_id.size.upper()}"
59    if device_id.family == "l1":
60        # Map STM32L1xxQC and STM32L1xxZC -> STM32L162QCxA variants
61        if device_id.pin in ["q", "z"] and device_id.size == "c":
62            devNameMatch += "A"
63        else:
64            devNameMatch += device_id.variant.upper()
65    elif device_id.family == "h7":
66        if device_id.variant:
67            devNameMatch += device_id.variant.upper()
68    for define in deviceDefines:
69        if devNameMatch <= define:
70            return define
71
72    # now we match for the pin-id.
73    devNameMatch = devName + f"{device_id.pin.upper()}x"
74    for define in deviceDefines:
75        if devNameMatch <= define:
76            return define
77
78    return None
def normalize_memory_map(memtree):
164def normalize_memory_map(memtree):
165    # print(RenderTree(memtree, maxlevel=2))
166    memtree = _normalize_subtypes(memtree, "DMA_TypeDef", "DMA_Channel_TypeDef", "DMA_Stream_TypeDef")
167    memtree = _normalize_subtypes(memtree, "MDMA_TypeDef", "MDMA_Channel_TypeDef")
168    memtree = _normalize_subtypes(memtree, "BDMA_TypeDef", "BDMA_Channel_TypeDef")
169    memtree = _normalize_subtypes(memtree, "LTDC_TypeDef", "LTDC_Layer_TypeDef")
170    memtree = _normalize_subtypes(memtree, "SAI_TypeDef", "SAI_Block_TypeDef")
171    memtree = _normalize_subtypes(memtree, "RAMECC_TypeDef", "RAMECC_MonitorTypeDef")
172
173    memtree = _normalize_dfsdm(memtree)
174    memtree = _normalize_dmamux(memtree)
175    memtree = _normalize_adc_common(memtree)
176
177    memtree = _normalize_duplicates(memtree, lambda n: "_COMMON" in n.name, lambda n: "_COMMON" not in n.name)
178    memtree = _normalize_duplicates(memtree, lambda n: "OPAMP" == n.name, lambda n: re.match(r"OPAMP\d$", n.name))
179
180    memtree = _normalize_instances(memtree)
181    memtree = _normalize_order(memtree)
182    return memtree