hmmm experimenting with gpio directions
[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 from collections import OrderedDict
6
7 # Was thinking of using these functions, but skipped for simplicity for now
8 # XXX nope. the output from JSON file.
9 #from pinfunctions import (i2s, lpc, emmc, sdmmc, mspi, mquadspi, spi,
10 # quadspi, i2c, mi2c, jtag, uart, uartfull, rgbttl, ulpi, rgmii, flexbus1,
11 # flexbus2, sdram1, sdram2, sdram3, vss, vdd, sys, eint, pwm, gpio)
12
13 # File for stage 1 pinmux tested proposed by Luke,
14 # https://bugs.libre-soc.org/show_bug.cgi?id=50#c10
15
16
17 def dummy_pinset():
18 # sigh this needs to come from pinmux.
19 gpios = []
20 for i in range(16):
21 gpios.append("%d*" % i)
22 return {'uart': ['tx+', 'rx-'],
23 'gpio': gpios,
24 'i2c': ['sda*', 'scl+']}
25
26 """
27 a function is needed which turns the results of dummy_pinset()
28 into:
29
30 [UARTResource("uart", 0, tx=..., rx=..),
31 I2CResource("i2c", 0, scl=..., sda=...),
32 Resource("gpio", 0, Subsignal("i"...), Subsignal("o"...)
33 Resource("gpio", 1, Subsignal("i"...), Subsignal("o"...)
34 ...
35 ]
36 """
37
38
39 def create_resources(pinset):
40 resources = []
41 for periph, pins in pinset.items():
42 print(periph, pins)
43 if periph == 'i2c':
44 #print("I2C required!")
45 resources.append(I2CResource('i2c', 0, sda='sda', scl='scl'))
46 elif periph == 'uart':
47 #print("UART required!")
48 resources.append(UARTResource('uart', 0, tx='tx', rx='rx'))
49 elif periph == 'gpio':
50 #print("GPIO required!")
51 print ("GPIO is defined as '*' type, meaning i, o and oe needed")
52 ios = []
53 for pin in pins:
54 pname = "gpio"+pin[:-1] # strip "*" on end
55 ios.append(Subsignal(pname, Pins(pname, dir="io",
56 assert_width=1)))
57 resources.append(Resource.family(periph, 0, default_name="gpio",
58 ios=ios))
59
60 # add clock and reset
61 clk = Resource("clk", 0, Pins("sys_clk", dir="i"))
62 rst = Resource("rst", 0, Pins("sys_rst", dir="i"))
63 resources.append(clk)
64 resources.append(rst)
65 return resources
66
67
68 def UARTResource(*args, rx, tx):
69 io = []
70 io.append(Subsignal("rx", Pins(rx, dir="i", assert_width=1)))
71 io.append(Subsignal("tx", Pins(tx, dir="o", assert_width=1)))
72 return Resource.family(*args, default_name="uart", ios=io)
73
74
75 def I2CResource(*args, scl, sda):
76 io = []
77 io.append(Subsignal("scl", Pins(scl, dir="io", assert_width=1)))
78 io.append(Subsignal("sda", Pins(sda, dir="io", assert_width=1)))
79 return Resource.family(*args, default_name="i2c", ios=io)
80
81
82 # ridiculously-simple top-level module. doesn't even have a sync domain
83 # and can't have one until a clock has been established by DummyPlatform.
84 class Blinker(Elaboratable):
85 def __init__(self):
86 pass
87 def elaborate(self, platform):
88 m = Module()
89 count = Signal(5)
90 m.d.sync += count.eq(5)
91 print ("resources", platform.resources.items())
92 gpio = platform.request("gpio", 0)
93 print (gpio, gpio.layout, gpio.fields)
94 # get the GPIO bank, mess about with some of the pins
95 m.d.comb += gpio.gpio0.o.eq(1)
96 m.d.comb += gpio.gpio1.o.eq(gpio.gpio2.i)
97 m.d.comb += gpio.gpio1.oe.eq(count[4])
98 m.d.sync += count[0].eq(gpio.gpio1.i)
99 # get the UART resource, mess with the output tx
100 uart = platform.request("uart", 0)
101 print (uart, uart.fields)
102 m.d.comb += uart.tx.eq(1)
103 return m
104
105
106 '''
107 _trellis_command_templates = [
108 r"""
109 {{invoke_tool("yosys")}}
110 {{quiet("-q")}}
111 {{get_override("yosys_opts")|options}}
112 -l {{name}}.rpt
113 {{name}}.ys
114 """,
115 ]
116 '''
117
118 # sigh, have to create a dummy platform for now.
119 # TODO: investigate how the heck to get it to output ilang. or verilog.
120 # or, anything, really. but at least it doesn't barf
121 class DummyPlatform(TemplatedPlatform):
122 connectors = []
123 resources = OrderedDict()
124 required_tools = []
125 command_templates = ['/bin/true']
126 file_templates = {
127 **TemplatedPlatform.build_script_templates,
128 "{{name}}.il": r"""
129 # {{autogenerated}}
130 {{emit_rtlil()}}
131 """,
132 "{{name}}.debug.v": r"""
133 /* {{autogenerated}} */
134 {{emit_debug_verilog()}}
135 """,
136 }
137 toolchain = None
138 default_clk = "clk" # should be picked up / overridden by platform sys.clk
139 default_rst = "rst" # should be picked up / overridden by platform sys.rst
140 def __init__(self, resources):
141 super().__init__()
142 self.add_resources(resources)
143
144 """
145 and to create a Platform instance with that list, and build
146 something random
147
148 p=Platform()
149 p.resources=listofstuff
150 p.build(Blinker())
151 """
152 pinset = dummy_pinset()
153 resources = create_resources(pinset)
154 print(pinset)
155 print(resources)
156 p = DummyPlatform (resources)
157 p.build(Blinker())
158