Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ubruhin committed Sep 11, 2023
1 parent d6619be commit 56f5f3a
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions generate_stm_mcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,26 @@ def __str__(self) -> str:
return '{}/{}'.format(self.generic, self.concrete)


class Part:
"""
Class representing a part with MPN
"""
def __init__(self, mpn: str, t_min: Optional[float], t_max: Optional[float],
packing_type: Optional[str], status: Optional[str]):
self.mpn = mpn
self.t_min = t_min
self.t_max = t_max
self.packing_type = packing_type
self.status = status


class MCU:
"""
Data class for a MCU.
"""

def __init__(self, ref: str, info: Dict[str, Any], pins: Iterable[Pin]):
def __init__(self, ref: str, info: Dict[str, Any], pins: Iterable[Pin],
parts: Iterable[Part]):
# Note: Don't use this directly, use `from_json` instead
self.ref = ref
self.name = info['names']['name']
Expand All @@ -174,6 +188,7 @@ def __init__(self, ref: str, info: Dict[str, Any], pins: Iterable[Pin]):
self.family = info['names']['family']
self.package = info['package']
self.pins = list(pins)
self.parts = list(parts)
self.flash = '{} KiB'.format(info['info']['flash'])
self.ram = '{} KiB'.format(info['info']['ram'])
self.io_count = info['info']['io'] # type: int
Expand Down Expand Up @@ -215,6 +230,10 @@ def _cleanup_pin_name(pin_name: str) -> str:
val = re.sub(r'\s*/\s*OSC', r'-OSC', val)
val = re.sub(r'([0-9])OSC', r'\1-OSC', val)

# Remove brackets and their contents
if '(' in pin_name:
val = re.sub(r'\(.*\)', r'', val)

# Remove everything after the first space
val = val.split(' ')[0]

Expand All @@ -227,6 +246,27 @@ def _cleanup_pin_name(pin_name: str) -> str:

@classmethod
def from_json(cls, ref: str, info: Dict[str, Any]) -> 'MCU':
# Collect parts, but not those with preview status.
parts = [] # type: List[Part]
skipped_status = ['Coming soon', 'Evaluation', 'Preview', 'Proposal']
for entry in info.get('parts', []): # TODO
part = Part(
mpn=entry['mpn'],
t_min=entry['temperature_min'],
t_max=entry['temperature_max'],
packing_type=entry['packing_type'],
status=entry['status'],
)
if part.status in ['Active', 'NRND', 'Obsolete']:
parts.append(part)
else:
assert part.status in skipped_status, part.status

# Skip MCUs which have no active parts
if len(parts) == 0:
print('Skipped MCU without parts: {}'.format(ref))
return None

# Collect pins, grouped by number
pin_map = defaultdict(list) # type: Dict[str, List[Pin]]
for entry in info['pinout']:
Expand All @@ -252,14 +292,14 @@ def from_json(cls, ref: str, info: Dict[str, Any]) -> 'MCU':
# Merge MonoIO into IO
types.remove('MonoIO')
types.add('IO')
assert len(types) == 1, (types, info)
#assert len(types) == 1, (types, info)

# Update the first pin
pin = group[0]
pin.name = merged_name
pins.append(pin)

return MCU(ref, info, pins)
return MCU(ref, info, pins, parts)

def pin_types(self) -> Set[str]:
"""
Expand Down Expand Up @@ -338,10 +378,12 @@ def ref_without_flash(self) -> str:
'C': 256,
'D': 384,
'E': 512,
'Y': 640, # Only STM32WB55VY
'F': 768,
'G': 1024,
'H': 1536,
'I': 2048,
'J': 4096,
}
assert size in flash_sizes, \
"{}: Flash size {} doesn't look valid".format(self.ref, size)
Expand Down Expand Up @@ -876,9 +918,10 @@ def generate(data: Dict[str, MCU], base_lib_path: str, debug: bool = False) -> N
with open(info_path, 'r') as f:
info = json.loads(f.read())
mcu = MCU.from_json(mcu_ref, info)
if mcu is not None:
assert None not in mcu.pin_types()
assert mcu_ref not in data
data[mcu_ref] = mcu
assert mcu_ref not in data
data[mcu_ref] = mcu

# Generate library elements
generate(data, args.base_lib, args.debug)
Expand Down

0 comments on commit 56f5f3a

Please sign in to comment.