forked from GlasgowEmbedded/glasgow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
187 lines (138 loc) · 6.28 KB
/
__init__.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
import logging
import asyncio
import math
from amaranth import *
import time
from ... import *
class WiegandSubtarget(Elaboratable):
def __init__(self, pads, in_fifo, out_fifo, pulse_width, pulse_gap):
self.pads = pads
self.in_fifo = in_fifo
self.out_fifo = out_fifo
self.pulse_width = pulse_width
self.limit = pulse_gap + pulse_width
self.bits = Signal(Shape(width = 16), reset=26)
self.ovf = Signal()
self.count = Signal(range(self.limit * 10 + 1))
self.bits_data = Signal(1024, reset=0b11111111111111111111111111)
self.bit_to_send = Signal()
def elaborate(self, platform):
m = Module()
m.d.comb += self.pads.d0_t.oe.eq(1)
m.d.comb += self.pads.d1_t.oe.eq(1)
m.d.comb += self.ovf.eq(self.count == self.limit)
with m.FSM() as fsm:
with m.State("WAIT"):
m.d.sync += self.bits.eq(0)
m.d.sync += self.pads.d0_t.o.eq(1)
m.d.sync += self.pads.d1_t.o.eq(1)
m.d.sync += self.out_fifo.r_en.eq(1)
with m.If(self.out_fifo.r_rdy):
m.d.sync += self.bits.eq(self.out_fifo.r_data)
m.next = "READ_LEN"
with m.State("READ_LEN"):
with m.If(self.out_fifo.r_rdy):
m.d.sync += self.bits.eq(Cat(self.out_fifo.r_data, self.bits))
m.next = "READ_DATA"
m.d.sync += self.out_fifo.r_en.eq(0)
with m.State("READ_DATA"):
m.d.sync += self.out_fifo.r_en.eq(1)
with m.If(self.out_fifo.r_rdy):
m.d.sync += self.bits_data.eq(Cat(self.out_fifo.r_data, self.bits_data))
m.d.sync += self.count.eq(self.count + 8)
with m.If(self.count >= self.bits):
m.d.sync += self.count.eq(0)
m.d.sync += self.out_fifo.r_en.eq(0)
m.next = "PREAMBLE_START"
with m.State("PREAMBLE_START"):
m.d.sync += self.count.eq(self.limit * 10)
m.next = "PREAMBLE"
with m.State("PREAMBLE"):
m.d.sync += self.pads.d0_t.o.eq(1)
m.d.sync += self.pads.d1_t.o.eq(1)
with m.If(self.count > 0):
m.d.sync += self.count.eq(self.count - 1)
with m.Else():
m.d.sync += self.count.eq(0)
m.d.sync += self.bit_to_send.eq(self.bits_data)
m.d.sync += self.bits_data.eq(self.bits_data.shift_right(1))
m.next = "SEND_BITS"
with m.State("SEND_BITS"):
m.d.sync += self.pads.d1_t.o.eq(1)
m.d.sync += self.pads.d0_t.o.eq(1)
m.d.sync += self.count.eq(self.count + 1)
with m.If(self.count > self.pulse_width):
m.next = "SEND_BITS_GAP"
with m.Else():
with m.If(~self.bit_to_send):
m.d.sync += self.pads.d0_t.o.eq(0)
with m.Else():
m.d.sync += self.pads.d1_t.o.eq(0)
with m.If(self.bits == 0):
m.next = "WAIT"
with m.State("SEND_BITS_GAP"):
m.d.sync += self.pads.d0_t.o.eq(1)
m.d.sync += self.pads.d1_t.o.eq(1)
with m.If(self.ovf):
m.d.sync += self.bits.eq(self.bits - 1)
m.d.sync += self.count.eq(0)
m.d.sync += self.bit_to_send.eq(self.bits_data)
m.d.sync += self.bits_data.eq(self.bits_data.shift_right(1))
m.next = "SEND_BITS"
with m.Else():
m.d.sync += self.count.eq(self.count + 1)
return m
class WiegandApplet(GlasgowApplet):
logger = logging.getLogger(__name__)
help = "boilerplate applet"
description = """
An example of the boilerplate code required to implement a minimal Glasgow applet.
The only things necessary for an applet are:
* a subtarget class,
* an applet class,
* the `build` and `run` methods of the applet class.
Everything else can be omitted and would be replaced by a placeholder implementation that does
nothing. Similarly, there is no requirement to use IN or OUT FIFOs, or any pins at all.
"""
__pins = ("d0", "d1")
@classmethod
def add_build_arguments(cls, parser, access):
super().add_build_arguments(parser, access)
for pin in cls.__pins:
access.add_pin_argument(parser, pin, default=True)
def build(self, target, args):
self.mux_interface = iface = target.multiplexer.claim_interface(self, args)
pulse_width = self.derive_clock(
input_hz=target.sys_clk_freq, output_hz=11904)
pulse_gap = self.derive_clock(
input_hz=target.sys_clk_freq, output_hz=3875)
iface.add_subtarget(WiegandSubtarget(
pads=iface.get_pads(args, pins=self.__pins),
in_fifo=iface.get_in_fifo(),
out_fifo=iface.get_out_fifo(),
pulse_width=pulse_width,
pulse_gap=pulse_gap
))
@classmethod
def add_run_arguments(cls, parser, access):
super().add_run_arguments(parser, access)
parser.add_argument(
"-w", "--wiegand", metavar="DATA", type=str, default='1' * 26,
help="Wiegand data to send")
async def run(self, device, args):
return await device.demultiplexer.claim_interface(self, self.mux_interface, args)
@classmethod
def add_interact_arguments(cls, parser):
pass
async def interact(self, device, args, iface):
wiegand_data = args.wiegand[::-1]
await iface.write((len(wiegand_data)).to_bytes(2, "big"))
wiegand_binary = int(wiegand_data, 2)
byte_count = math.ceil(len(wiegand_data) / 8)
await iface.write(wiegand_binary.to_bytes(byte_count, "big"))
await iface.flush()
# -------------------------------------------------------------------------------------------------
class WiegandAppletTestCase(GlasgowAppletTestCase, applet=WiegandApplet):
@synthesis_test
def test_build(self):
self.assertBuilds()