-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtop.py
More file actions
executable file
·159 lines (133 loc) · 4.11 KB
/
top.py
File metadata and controls
executable file
·159 lines (133 loc) · 4.11 KB
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
#!/usr/bin/env python3
import serial
from time import sleep
from amaranth import *
from amaranth_boards.icebreaker import ICEBreakerPlatform
from . import UART
from . import Ring_Oscillator
from . import Seven_seg, seven_seg_resource
from . import Switches, switches_resource
baud = 115200
class Serial_Top(Elaboratable):
def __init__(self):
self.byte_in = Switches()
self.byte_out = Seven_seg()
self.uart = None
def elaborate(self, platform):
m = Module()
m.submodules += [self.byte_in, self.byte_out]
uart_pins = platform.request("uart")
self.uart = UART(uart_pins, clk_freq=12000000, baud_rate=baud)
m.submodules += self.uart
# auto-reply with data immediately
data = Signal(8)
rx_strobe = Signal()
tx_strobe = Signal()
empty = Signal(reset=1)
# auto-reply with `data` I think?
m.d.comb += [
rx_strobe.eq(self.uart.rx_ready & empty),
tx_strobe.eq(self.uart.tx_ack & ~empty),
self.uart.rx_ack.eq(rx_strobe),
self.uart.tx_data.eq(data),
self.uart.tx_ready.eq(tx_strobe)
]
# handle setting local values
counter = Signal(24)
disp = Signal(8)
m.d.comb += self.byte_out.value.eq(disp)
with m.If(counter == 0):
m.d.sync += disp.eq(disp + self.byte_in.value)
m.d.sync += counter.eq(12000000)
with m.Else():
m.d.sync += counter.eq(counter - 1)
# send back the accumulator
with m.If(rx_strobe):
m.d.sync += [
#self.data.eq(self.uart.rx_data),
data.eq(disp),
empty.eq(0)
]
with m.If(tx_strobe):
m.d.sync += empty.eq(1)
return m
class Top(Elaboratable):
def __init__(self, x, y, cycle_count):
self.x = x
self.y = y
self.cycle_count = cycle_count
self.uart = None
self.ring = Ring_Oscillator(7, 8, self.x, self.y)
self.byte_out = Seven_seg()
def elaborate(self, platform):
m = Module()
m.submodules += [self.ring, self.byte_out]
uart_pins = platform.request("uart")
self.uart = UART(uart_pins, clk_freq=12000000, baud_rate=baud)
m.submodules += self.uart
read_state = Signal(8, reset=20+self.cycle_count)
# auto-reply with data immediately
data = Signal(8)
rx_strobe = Signal()
tx_strobe = Signal()
empty = Signal(reset=1)
# auto-reply with `data` I think?
m.d.comb += [
rx_strobe.eq(self.uart.rx_ready & empty),
tx_strobe.eq(self.uart.tx_ack & ~empty),
self.uart.rx_ack.eq(rx_strobe),
self.uart.tx_data.eq(data),
self.uart.tx_ready.eq(tx_strobe)
]
# send back the oscillator count
with m.If(rx_strobe):
m.d.sync += [
#self.data.eq(self.uart.rx_data), # data in from UART
self.byte_out.value.eq(data),
#data.eq(self.ring.),
empty.eq(0),
]
with m.If(read_state == 0):
# start countdown to next measurement
m.d.sync += read_state.eq(20+self.cycle_count)
with m.If(tx_strobe):
m.d.sync += empty.eq(1)
with m.If(read_state != 0):
m.d.sync += read_state.eq(read_state - 1)
with m.Switch(read_state):
with m.Case(20+self.cycle_count):
m.d.sync += self.ring.reset.eq(0) # enable
with m.Case(10+self.cycle_count):
m.d.sync += self.ring.enable.eq(1) # start counting
with m.Case(10):
m.d.sync += self.ring.enable.eq(0) # stop counting
with m.Case(1):
m.d.sync += data.eq(self.ring.counter) # copy value
with m.Case(0):
m.d.sync += self.ring.reset.eq(1) # reset counter
return m
def _flash(coords, cycle_count):
x,y = coords
board = ICEBreakerPlatform()
board.add_resources([seven_seg_resource, switches_resource])
board.build(Top(x, y, cycle_count), do_program=True,
nextpnr_opts="--ignore-loops --timing-allow-fail")
def run(coords, cycle_count=64, sample_count=100, sample_rate=100, flash=True, verbose=False):
if flash:
_flash(coords, cycle_count)
sleep(1)
ser = serial.Serial('/dev/ttyUSB1', 115200)
# discard first read
ser.write(b'0')
ser.read(1)
sleep(1.0/sample_rate)
records = []
for _ in range(sample_count):
ser.write(b'0') # trigger measurement
new_val = 12.0 * ord(ser.read(1)) / cycle_count
records.append(new_val)
print(new_val, end=", ", flush=True) if verbose else ()
sleep(1.0/sample_rate)
print() if verbose else ()
print(f"At {coords} the average is", round(sum(records)/len(records), 2))
return records