#!/usr/bin/env python3
import textwrap
import json
from svd.peripheral import peripheral as peripheral_gen
def bit_fields(name, bits, *, doc, one, onedoc, zero, zerodoc):
print(f"""\
""")
for i in reversed(bits):
print(f"""\
{name}{i}
Pin {i} {doc}
[{i}:{i}]
{zero}
{zerodoc}
0
{one}
{onedoc}
1
""")
print(f"""\
""")
def main():
print("""\
Atmel
ATmega32U4
0.1
8-bit Microcontroller with 32K bytes of ISP Flash and USB Controller
ATmega32U4
r0p0
little
false
true
8
false
43
8
8
read-write
0
0xff
""")
data = json.load(open("atmega32u4.json"))
for peripheral in data:
ty = peripheral["type"]
if ty == "port":
name = peripheral["name"]
doc_name = peripheral["doc_name"]
base_addr = peripheral["base"]
char = peripheral["chr"]
bits = peripheral["pins"]
print(f"""\
{name}
{base_addr}
0x00
0x03
registers
PIN
{doc_name} Input Pins Address
read-only
0x00""")
bit_fields(
f"P",
bits,
doc="Input",
one="HIGH",
onedoc="Pin is high",
zero="LOW",
zerodoc="Pin is low",
)
print(f"""\
DDR
{doc_name} Data Direction Register
0x01""")
bit_fields(
f"DD",
bits,
doc="Direction",
one="OUTPUT",
onedoc="Pin is configured as an output",
zero="INPUT",
zerodoc="Pin is configured as an input",
)
print(f"""\
PORT
{doc_name} Output/Data Register
0x02""")
bit_fields(
f"D",
bits,
doc="Output/Data",
one="HIGH",
onedoc="Pin is high",
zero="LOW",
zerodoc="Pin is low",
)
print(f"""\
""")
elif ty == "ext":
p = open(peripheral["path"]).read().strip()
print(textwrap.indent(p, " "))
elif ty == "toml":
peripheral_gen(peripheral["path"])
print("""\
""")
if __name__ == "__main__":
main()