Skip to content

Commit

Permalink
Update xmlgen.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittich committed Feb 7, 2024
1 parent 787bb2a commit ad4c597
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/xmlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import argparse
import os
import pprint
from typing import IO, Any, Tuple
import yaml

import utils # local import
Expand Down Expand Up @@ -40,6 +41,50 @@ def type_to_format(thedict: dict) -> str:
return None
return fmt

def open_C_file(fname: str) -> IO[Any]:
"""open the C file and write the header"""
fout = open(fname, 'w', encoding='ascii')
print(r"// This file is generated by mcu_generate.py", file=fout)
print(r"// Do not edit this file directly", file=fout)
print(r"// Edit the yaml files in the data directory and run mcu_generate.py", file=fout)
print(r"// to generate this file", file=fout)
print(r"//", file=fout)
print(r"// This file contains the addresses of the zynqmon data", file=fout)
print(r"// and the C calls to initialize the zynqmon data", file=fout)
print(r"//", file=fout)
print("#include \"Tasks.h\"", file=fout)
# include the header file we will write later
header_fname = args.output.replace('.c', '.h')
print(f"#include \"{header_fname}\"", file=fout)
return fout

def write_C_header(fout: IO[Any], fname: str) -> None:
"""write the header file"""
with open(header_fname, encoding="ascii", mode='w') as fheader:
print(r"// This file is generated by mcu_generate.py", file=fheader)
print(r"// Do not edit this file directly", file=fheader)
print(r"// Edit the yaml files in the data directory and run mcu_generate.py", file=fheader)
print(r"// to generate this file", file=fheader)
print(r"//", file=fheader)
print("#ifndef ZYNQMON_ADDRESSES_H", file=fheader)
print("#define ZYNQMON_ADDRESSES_H", file=fheader)
print("#ifdef REV1", file=fheader)
print(f"#define ZMON_VALID_ENTRIES {ZMON_VALID_ENTRIES[0]}", file=fheader)
print("#elif defined(REV2)", file=fheader)
print(f"#define ZMON_VALID_ENTRIES {ZMON_VALID_ENTRIES[1]}", file=fheader)
print("#endif // REV1", file=fheader)
print("#endif // ZYNQMON_ADDRESSES_H", file=fheader)


def write_C_call(fout: IO[Any], c: dict, size: int) -> Tuple[int, int]:
"""write the C call to generate the filling of the data structures """
if c['mcu_extra_call'] is None:
print(f"zm_set_{c['mcu_call']}(&zynqmon_data[{size}], {c['start']});", file=fout)
else :
print(f"zm_set_{c['mcu_call']}(&zynqmon_data[{size}], {c['start']}, "
f"{c['mcu_extra_call']});", file=fout)
return (c['start'], c['start'] + int(c['count'])-1)


def make_node(parent: ET.Element, myid: str, thedict: dict, address: int) -> ET.Element:
"""create the node to be inserted into the xml tree"""
Expand All @@ -60,6 +105,7 @@ def make_node(parent: ET.Element, myid: str, thedict: dict, address: int) -> ET.
print(f"char_count: {thedict['char_count']}, val: {val}")
thenode.set('size', str(hex(val)))
else: # all other types have masks
# the masks here will be either 32 or 16 bits
mask = (1 << width) - 1
is_odd = address % 2 == 1
if not is_odd :
Expand Down Expand Up @@ -109,6 +155,8 @@ def main():
# start processing the yaml file
config = y['config']
sensor_count = 0 # in 16 bit words
summary = []
categories = []
for c in config: # loop over entries in configuration (sensor category)
if args.verbose:
print("category:",)
Expand All @@ -133,6 +181,7 @@ def main():
print(f"sensor: {device}.{sensor}")
make_node(pp, sensor, c, sensor_count)
sensor_count += sensor_size(c) // 2
summary.append((device + "." + sensor if device != "" else sensor, sensor_count))
if devices:
device = devices.pop(0)
else:
Expand All @@ -146,6 +195,6 @@ def main():
print("writing to file", out_name)
tree.write(out_name)


pprinter.pprint(summary)
if __name__ == "__main__":
main()

0 comments on commit ad4c597

Please sign in to comment.