forked from BerkeleyHCI/PolymorphicBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_blinky.py
228 lines (179 loc) · 6.73 KB
/
test_blinky.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import unittest
from edg import *
class TestBlinkyBasic(BoardTop):
def contents(self):
super().contents()
self.mcu = self.Block(Nucleo_F303k8())
self.led = self.Block(IndicatorLed())
self.connect(self.mcu.gnd, self.led.gnd)
self.connect(self.mcu.new_io(DigitalBidir), self.led.signal)
def refinements(self) -> Refinements:
return super().refinements() + Refinements(
instance_values=[
(['mcu', 'pin_assigns'], "")
]
)
class TestBlinkySimple(BoardTop):
def contents(self):
super().contents()
self.mcu = self.Block(Nucleo_F303k8())
with self.implicit_connect(
ImplicitConnect(self.mcu.gnd, [Common]),
) as imp:
self.led = imp.Block(IndicatorLed())
self.sw = imp.Block(DigitalSwitch())
self.connect(self.mcu.new_io(DigitalBidir), self.led.signal)
self.connect(self.sw.out, self.mcu.new_io(DigitalBidir))
def refinements(self) -> Refinements:
return super().refinements() + Refinements(
instance_values=[
(['mcu', 'pin_assigns'], "")
]
)
class TestBlinkySimpleChain(BoardTop):
def contents(self):
super().contents()
self.mcu = self.Block(Nucleo_F303k8())
with self.implicit_connect(
ImplicitConnect(self.mcu.gnd, [Common]),
) as imp:
(self.led, ), _ = self.chain(self.mcu.new_io(DigitalBidir), imp.Block(IndicatorLed()))
(self.sw, ), _ = self.chain(imp.Block(DigitalSwitch()), self.mcu.new_io(DigitalBidir))
def refinements(self) -> Refinements:
return super().refinements() + Refinements(
instance_values=[
(['mcu', 'pin_assigns'], "")
]
)
class TestBlinkyBroken(BoardTop):
def contents(self):
super().contents()
self.usb = self.Block(UsbDeviceCReceptacle())
self.vusb = self.connect(self.usb.pwr)
self.gnd = self.connect(self.usb.gnd)
with self.implicit_connect(
ImplicitConnect(self.usb.pwr, [Power]),
ImplicitConnect(self.usb.gnd, [Common]),
) as imp:
self.mcu = imp.Block(Lpc1549_48())
(self.swd, ), _ = self.chain(imp.Block(SwdCortexTargetHeader()), self.mcu.swd)
def refinements(self) -> Refinements:
return super().refinements() + Refinements(
instance_values=[
(['mcu', 'pin_assigns'], "")
]
)
class TestBlinkyFlattened(BoardTop):
def contents(self):
super().contents()
self.usb = self.Block(UsbDeviceCReceptacle())
self.vusb = self.connect(self.usb.pwr)
self.gnd = self.connect(self.usb.gnd)
with self.implicit_connect(
ImplicitConnect(self.usb.pwr, [Power]),
ImplicitConnect(self.usb.gnd, [Common]),
) as imp:
self.usb_reg = imp.Block(BuckConverter(output_voltage=3.3*Volt(tol=0.05)))
self.v3v3 = self.connect(self.usb_reg.pwr_out)
with self.implicit_connect(
ImplicitConnect(self.usb_reg.pwr_out, [Power]),
ImplicitConnect(self.usb.gnd, [Common]),
) as imp:
self.mcu = imp.Block(Lpc1549_48())
(self.swd, ), _ = self.chain(imp.Block(SwdCortexTargetHeader()), self.mcu.swd)
(self.led, ), _ = self.chain(self.mcu.new_io(DigitalBidir), imp.Block(IndicatorLed()))
(self.sw, ), _ = self.chain(imp.Block(DigitalSwitch()), self.mcu.new_io(DigitalBidir))
def refinements(self) -> Refinements:
return super().refinements() + Refinements(
instance_refinements=[
(['usb_reg'], Tps561201),
],
instance_values=[
(['mcu', 'pin_assigns'], "")
]
)
class Mcp9700_Device(FootprintBlock):
def __init__(self) -> None:
super().__init__()
# block boundary (ports, parameters) definition here
self.vdd = self.Port(VoltageSink(
voltage_limits=(2.3, 5.5)*Volt, current_draw=(0, 15)*uAmp
), [Power])
self.vout = self.Port(AnalogSource(
voltage_out=(0.1, 2), current_limits=(0, 100)*uAmp,
impedance=(20, 20)*Ohm
), [Output])
self.gnd = self.Port(Ground(), [Common])
def contents(self) -> None:
super().contents()
# block implementation (subblocks, internal connections, footprint) here
self.footprint(
'U', 'Package_TO_SOT_SMD:SOT-23',
{
'1': self.vdd,
'2': self.vout,
'3': self.gnd,
},
mfr='Microchip Technology', part='MCP9700T-E/TT',
datasheet='http://ww1.microchip.com/downloads/en/DeviceDoc/20001942G.pdf'
)
class Mcp9700(Block):
def __init__(self) -> None:
super().__init__()
self.ic = self.Block(Mcp9700_Device())
self.pwr = self.Export(self.ic.vdd, [Power])
self.gnd = self.Export(self.ic.gnd, [Common])
self.out = self.Export(self.ic.vout, [Output])
def contents(self) -> None:
super().contents()
with self.implicit_connect(
ImplicitConnect(self.pwr, [Power]),
ImplicitConnect(self.gnd, [Common])
) as imp:
self.vdd_cap = imp.Block(DecouplingCapacitor(capacitance=0.1*uFarad(tol=0.2)))
class TestBlinkyComplete(BoardTop):
def contents(self):
super().contents()
self.usb = self.Block(UsbDeviceCReceptacle())
self.vusb = self.connect(self.usb.pwr)
self.gnd = self.connect(self.usb.gnd)
with self.implicit_connect(
ImplicitConnect(self.usb.pwr, [Power]),
ImplicitConnect(self.usb.gnd, [Common]),
) as imp:
self.usb_reg = imp.Block(BuckConverter(output_voltage=3.3*Volt(tol=0.05)))
self.v3v3 = self.connect(self.usb_reg.pwr_out)
with self.implicit_connect(
ImplicitConnect(self.usb_reg.pwr_out, [Power]),
ImplicitConnect(self.usb.gnd, [Common]),
) as imp:
self.mcu = imp.Block(Lpc1549_48())
(self.swd, ), _ = self.chain(imp.Block(SwdCortexTargetHeader()), self.mcu.swd)
self.led = ElementDict[IndicatorLed]()
for i in range(8):
(self.led[i], ), _ = self.chain(self.mcu.new_io(DigitalBidir), imp.Block(IndicatorLed()))
(self.sw, ), _ = self.chain(imp.Block(DigitalSwitch()), self.mcu.new_io(DigitalBidir))
(self.temp, ), _ = self.chain(imp.Block(Mcp9700()), self.mcu.new_io(AnalogSink))
def refinements(self) -> Refinements:
return super().refinements() + Refinements(
instance_refinements=[
(['usb_reg'], Tps561201),
],
instance_values=[
(['mcu', 'pin_assigns'], "")
]
)
class BlinkyTestCase(unittest.TestCase):
def test_design_basic(self) -> None:
compile_board_inplace(TestBlinkyBasic)
def test_design_simple(self) -> None:
compile_board_inplace(TestBlinkySimple)
def test_design_simple_chain(self) -> None:
compile_board_inplace(TestBlinkySimpleChain)
def test_design_broken(self) -> None:
with self.assertRaises(CompilerCheckError):
compile_board_inplace(TestBlinkyBroken)
def test_design_flat(self) -> None:
compile_board_inplace(TestBlinkyFlattened)
def test_design_complete(self) -> None:
compile_board_inplace(TestBlinkyComplete)