c14859888f50fc369439ba5cc3493a9d8856d804
[pinmux.git] / src / spec / testing_stage1.py
1 #!/usr/bin/env python3
2 from nmigen.build.dsl import Resource, Subsignal, Pins
3 from nmigen.build.plat import TemplatedPlatform
4 from nmigen import Elaboratable, Signal, Module
5
6 # Was thinking of using these functions, but skipped for simplicity for now
7 # XXX nope. the output from JSON file.
8 #from pinfunctions import (i2s, lpc, emmc, sdmmc, mspi, mquadspi, spi,
9 # quadspi, i2c, mi2c, jtag, uart, uartfull, rgbttl, ulpi, rgmii, flexbus1,
10 # flexbus2, sdram1, sdram2, sdram3, vss, vdd, sys, eint, pwm, gpio)
11
12 # File for stage 1 pinmux tested proposed by Luke,
13 https://bugs.libre-soc.org/show_bug.cgi?id=50#c10
14
15
16 def dummy_pinset():
17 # sigh this needs to come from pinmux.
18 gpios = []
19 for i in range(16):
20 gpios.append("%d*" % i)
21 return {'uart': ['tx+', 'rx-'],
22 'gpio': gpios,
23 'i2c': ['sda*', 'scl+']}
24
25 """
26 a function is needed which turns the results of dummy_pinset()
27 into:
28
29 [UARTResource("uart", 0, tx=..., rx=..),
30 I2CResource("i2c", 0, scl=..., sda=...),
31 Resource("gpio", 0, Subsignal("i"...), Subsignal("o"...)
32 Resource("gpio", 1, Subsignal("i"...), Subsignal("o"...)
33 ...
34 ]
35 """
36
37
38 def create_resources(pinset):
39 resources = []
40 for periph, pins in pinset.items():
41 print(periph, pins)
42 if periph == 'i2c':
43 #print("I2C required!")
44 resources.append(I2CResource('i2c', 0, sda='sda', scl='scl'))
45 elif periph == 'uart':
46 #print("UART required!")
47 resources.append(UARTResource('uart', 0, tx='tx', rx='rx'))
48 elif periph == 'gpio':
49 #print("GPIO required!")
50 print ("GPIO is defined as '*' type, meaning i, o and oe needed")
51 resources.append(Resource('gpio', 0,
52 Subsignal("i", Pins('i0', dir="i", conn=None, assert_width=1)),
53 Subsignal("oe", Pins('oe0', dir="o", conn=None, assert_width=1)),
54 Subsignal("o", Pins('o0', dir="o", conn=None, assert_width=1))))
55 return resources
56
57
58 def UARTResource(*args, rx, tx):
59 io = []
60 io.append(Subsignal("rx", Pins(rx, dir="i", assert_width=1)))
61 io.append(Subsignal("tx", Pins(tx, dir="o", assert_width=1)))
62 return Resource.family(*args, default_name="uart", ios=io)
63
64
65 def I2CResource(*args, scl, sda):
66 io = []
67 io.append(Subsignal("scl", Pins(scl, dir="io", assert_width=1)))
68 io.append(Subsignal("sda", Pins(sda, dir="io", assert_width=1)))
69 return Resource.family(*args, default_name="i2c", ios=io)
70
71
72 # ridiculously-simple top-level module. doesn't even have a sync domain
73 # and can't have one until a clock has been established by DummyPlatform.
74 class Blinker(Elaboratable):
75 def __init__(self):
76 pass
77 def elaborate(self, platform):
78 m = Module()
79 count = Signal(5)
80 m.d.comb += count.eq(5)
81 return m
82
83
84 # sigh, have to create a dummy platform for now.
85 # TODO: investigate how the heck to get it to output ilang. or verilog.
86 # or, anything, really. but at least it doesn't barf
87 class DummyPlatform(TemplatedPlatform):
88 resources = []
89 connectors = []
90 required_tools = []
91 command_templates = ['/bin/true']
92 file_templates = TemplatedPlatform.build_script_templates
93 toolchain = None
94 def __init__(self, resources):
95 self.resources = resources
96 super().__init__()
97
98 """
99 and to create a Platform instance with that list, and build
100 something random
101
102 p=Platform()
103 p.resources=listofstuff
104 p.build(Blinker())
105 """
106 pinset = dummy_pinset()
107 resources = create_resources(pinset)
108 print(pinset)
109 print(resources)
110 p = DummyPlatform (resources)
111 p.resources = create_resources(pinset)
112 p.build(Blinker())
113