Skip to content

Commit 6596883

Browse files
committed
serial: fix issue where data latched can be invalid
1 parent d973f00 commit 6596883

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

nmigen_stdio/serial.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,21 @@ def elaborate(self, platform):
182182
m.d.sync += [
183183
self.o.eq(0), # Issue start bit ASAP
184184
shreg.start .eq(0),
185-
shreg.data .eq(self.data),
186185
shreg.parity.eq(_compute_parity_bit(self.data, self._parity)),
187186
shreg.stop .eq(1),
188187
bits_left.eq(len(self.shreg)),
189188
timer.eq(self.divisor)
190189
]
191-
m.next = "BUSY"
190+
m.next = "LATCH-DATA"
191+
192+
with m.State("LATCH-DATA"):
193+
# Since the data is valid only when ACK is high,
194+
# only latch the data now such that it is from the first clock since ACK is high
195+
m.d.sync += [
196+
shreg.data.eq(self.data),
197+
timer.eq(timer - 1)
198+
]
199+
m.next = "BUSY"
192200

193201
with m.State("BUSY"):
194202
with m.If(timer != 0):
@@ -216,13 +224,12 @@ def elaborate(self, platform):
216224
m.d.sync += [
217225
self.o.eq(0), # Issue start bit ASAP
218226
shreg.start .eq(0),
219-
shreg.data .eq(self.data),
220227
shreg.parity.eq(_compute_parity_bit(self.data, self._parity)),
221228
shreg.stop .eq(1),
222229
bits_left.eq(len(self.shreg)),
223230
timer.eq(self.divisor)
224231
]
225-
m.next = "BUSY"
232+
m.next = "LATCH-DATA"
226233
# Set back to IDLE if ACK is low
227234
with m.Else():
228235
m.d.sync += self.o.eq(1)

0 commit comments

Comments
 (0)