70a96cb2b8928e26cb0ffb2160228dff8f5aca1c
[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.build.res import ResourceManager
5 from nmigen import Elaboratable, Signal, Module, Instance
6 from collections import OrderedDict
7 from jtag import JTAG
8 from copy import deepcopy
9
10 # Was thinking of using these functions, but skipped for simplicity for now
11 # XXX nope. the output from JSON file.
12 #from pinfunctions import (i2s, lpc, emmc, sdmmc, mspi, mquadspi, spi,
13 # quadspi, i2c, mi2c, jtag, uart, uartfull, rgbttl, ulpi, rgmii, flexbus1,
14 # flexbus2, sdram1, sdram2, sdram3, vss, vdd, sys, eint, pwm, gpio)
15
16 # File for stage 1 pinmux tested proposed by Luke,
17 # https://bugs.libre-soc.org/show_bug.cgi?id=50#c10
18
19
20 def dummy_pinset():
21 # sigh this needs to come from pinmux.
22 gpios = []
23 for i in range(16):
24 gpios.append("%d*" % i)
25 return {'uart': ['tx+', 'rx-'],
26 'gpio': gpios,
27 'i2c': ['sda*', 'scl+']}
28
29 """
30 a function is needed which turns the results of dummy_pinset()
31 into:
32
33 [UARTResource("uart", 0, tx=..., rx=..),
34 I2CResource("i2c", 0, scl=..., sda=...),
35 Resource("gpio", 0, Subsignal("i"...), Subsignal("o"...)
36 Resource("gpio", 1, Subsignal("i"...), Subsignal("o"...)
37 ...
38 ]
39 """
40
41
42 def create_resources(pinset):
43 resources = []
44 for periph, pins in pinset.items():
45 print(periph, pins)
46 if periph == 'i2c':
47 #print("I2C required!")
48 resources.append(I2CResource('i2c', 0, sda='sda', scl='scl'))
49 elif periph == 'uart':
50 #print("UART required!")
51 resources.append(UARTResource('uart', 0, tx='tx', rx='rx'))
52 elif periph == 'gpio':
53 #print("GPIO required!")
54 print ("GPIO is defined as '*' type, meaning i, o and oe needed")
55 ios = []
56 for pin in pins:
57 pname = "gpio"+pin[:-1] # strip "*" on end
58 pads = []
59 # urrrr... tristsate and io assume a single pin which is
60 # of course exactly what we don't want in an ASIC: we want
61 # *all three* pins but the damn port is not outputted
62 # as a triplet, it's a single Record named "io". sigh.
63 # therefore the only way to get a triplet of i/o/oe
64 # is to *actually* create explicit triple pins
65 pads.append(Subsignal("i",
66 Pins(pname+"_i", dir="i", assert_width=1)))
67 pads.append(Subsignal("o",
68 Pins(pname+"_o", dir="o", assert_width=1)))
69 pads.append(Subsignal("oe",
70 Pins(pname+"_oe", dir="oe", assert_width=1)))
71 ios.append(Resource.family(pname, 0, default_name=pname,
72 ios=pads))
73 resources.append(Resource.family(periph, 0, default_name="gpio",
74 ios=ios))
75
76 # add clock and reset
77 clk = Resource("clk", 0, Pins("sys_clk", dir="i"))
78 rst = Resource("rst", 0, Pins("sys_rst", dir="i"))
79 resources.append(clk)
80 resources.append(rst)
81 return resources
82
83
84 def UARTResource(*args, rx, tx):
85 io = []
86 io.append(Subsignal("rx", Pins(rx, dir="i", assert_width=1)))
87 io.append(Subsignal("tx", Pins(tx, dir="o", assert_width=1)))
88 return Resource.family(*args, default_name="uart", ios=io)
89
90
91 def I2CResource(*args, scl, sda):
92 io = []
93 io.append(Subsignal("scl", Pins(scl, dir="io", assert_width=1)))
94 io.append(Subsignal("sda", Pins(sda, dir="io", assert_width=1)))
95 return Resource.family(*args, default_name="i2c", ios=io)
96
97
98 # ridiculously-simple top-level module. doesn't even have a sync domain
99 # and can't have one until a clock has been established by DummyPlatform.
100 class Blinker(Elaboratable):
101 def __init__(self, pinset):
102 self.jtag = JTAG(pinset, "sync")
103
104 def elaborate(self, platform):
105 m = Module()
106 m.submodules.jtag = self.jtag
107 count = Signal(5)
108 m.d.sync += count.eq(5)
109 print ("resources", platform.resources.items())
110 gpio = platform.core['gpio']
111 print (gpio, gpio.layout, gpio.fields)
112 # get the GPIO bank, mess about with some of the pins
113 m.d.comb += gpio.gpio0.o.eq(1)
114 m.d.comb += gpio.gpio1.o.eq(gpio.gpio2.i)
115 m.d.comb += gpio.gpio1.oe.eq(count[4])
116 m.d.sync += count[0].eq(gpio.gpio1.i)
117 # get the UART resource, mess with the output tx
118 uart = platform.core['uart']
119 print (uart, uart.fields)
120 m.d.comb += uart.tx.eq(1)
121 return m
122
123
124 '''
125 _trellis_command_templates = [
126 r"""
127 {{invoke_tool("yosys")}}
128 {{quiet("-q")}}
129 {{get_override("yosys_opts")|options}}
130 -l {{name}}.rpt
131 {{name}}.ys
132 """,
133 ]
134 '''
135
136 # sigh, have to create a dummy platform for now.
137 # TODO: investigate how the heck to get it to output ilang. or verilog.
138 # or, anything, really. but at least it doesn't barf
139 class DummyPlatform(TemplatedPlatform):
140 connectors = []
141 resources = OrderedDict()
142 required_tools = []
143 command_templates = ['/bin/true']
144 file_templates = {
145 **TemplatedPlatform.build_script_templates,
146 "{{name}}.il": r"""
147 # {{autogenerated}}
148 {{emit_rtlil()}}
149 """,
150 "{{name}}.debug.v": r"""
151 /* {{autogenerated}} */
152 {{emit_debug_verilog()}}
153 """,
154 }
155 toolchain = None
156 default_clk = "clk" # should be picked up / overridden by platform sys.clk
157 default_rst = "rst" # should be picked up / overridden by platform sys.rst
158 def __init__(self, pinset):
159 self.pad_mgr = ResourceManager([], [])
160 super().__init__()
161 # create set of pin resources based on the pinset, this is for the core
162 resources = create_resources(pinset)
163 self.add_resources(resources)
164 # allocate all resources, right now, so that a lookup can be created
165 # between core IO names and pads
166 self.core = {}
167 self.pads = {}
168 # request every single peripheral in the pinset.
169 for periph, pins in pinset.items():
170 self.core[periph] = self.request(periph)
171 self.pads[periph] = self.pad_mgr.request(periph)
172 # now create a lookup between the pad and the core, so that
173 # JTAG boundary scan can be inserted in between
174 self.padlookup = {}
175 core = list(self.iter_single_ended_pins())
176 pads = list(self.pad_mgr.iter_single_ended_pins())
177 print ("core", core)
178 print ("pads", pads)
179 for pad, core in zip(pads, core):
180 print ("iter", pad)
181 self.padlookup[pad[0].name] = core
182
183 def add_resources(self, resources, no_boundary_scan=False):
184 super().add_resources(resources)
185 if no_boundary_scan:
186 return
187 # make a *second* - identical - set of pin resources for the IO ring
188 padres = deepcopy(resources)
189 self.pad_mgr.add_resources(padres)
190
191 # XXX these aren't strictly necessary right now but the next
192 # phase is to add JTAG Boundary Scan so it maaay be worth adding?
193 # at least for the print statements
194 def get_input(self, pin, port, attrs, invert):
195 self._check_feature("single-ended input", pin, attrs,
196 valid_xdrs=(0,), valid_attrs=None)
197
198 print (" get_input", pin, "port", port, port.layout)
199 if pin.name not in ['clk_0', 'rst_0']: # sigh
200 pad = self.padlookup[pin.name]
201 print (" pad", pad)
202 m = Module()
203 m.d.comb += pin.i.eq(self._invert_if(invert, port))
204 return m
205
206 def get_output(self, pin, port, attrs, invert):
207 self._check_feature("single-ended output", pin, attrs,
208 valid_xdrs=(0,), valid_attrs=None)
209
210 print (" get_output", pin, "port", port, port.layout)
211 m = Module()
212 m.d.comb += port.eq(self._invert_if(invert, pin.o))
213 return m
214
215 def get_tristate(self, pin, port, attrs, invert):
216 self._check_feature("single-ended tristate", pin, attrs,
217 valid_xdrs=(0,), valid_attrs=None)
218
219 m = Module()
220 m.submodules += Instance("$tribuf",
221 p_WIDTH=pin.width,
222 i_EN=pin.oe,
223 i_A=self._invert_if(invert, pin.o),
224 o_Y=port,
225 )
226 return m
227
228 def get_input_output(self, pin, port, attrs, invert):
229 self._check_feature("single-ended input/output", pin, attrs,
230 valid_xdrs=(0,), valid_attrs=None)
231 print (" get_input_output", pin, "port", port, port.layout)
232 m = Module()
233 m.submodules += Instance("$tribuf",
234 p_WIDTH=pin.width,
235 i_EN=pin.oe,
236 i_A=self._invert_if(invert, pin.o),
237 o_Y=port,
238 )
239 m.d.comb += pin.i.eq(self._invert_if(invert, port))
240 return m
241
242
243 """
244 and to create a Platform instance with that list, and build
245 something random
246
247 p=Platform()
248 p.resources=listofstuff
249 p.build(Blinker())
250 """
251 pinset = dummy_pinset()
252 print(pinset)
253 p = DummyPlatform (pinset)
254 p.build(Blinker(pinset))
255