summaryrefslogtreecommitdiff
path: root/hdl/rs232.sv
diff options
context:
space:
mode:
authorJulian Blake Kongslie2022-05-22 15:42:55 -0700
committerJulian Blake Kongslie2022-05-22 15:42:55 -0700
commitd6d6edfd57c3dc4db01666034d11ac84d31ec2ef (patch)
tree786a33b4a4c4c595d69275cd11550cf41e1e9a05 /hdl/rs232.sv
parentUpdate plan. (diff)
downloadmultipdp8-d6d6edfd57c3dc4db01666034d11ac84d31ec2ef.tar.xz
Only sample RS232 signals once per clock; use a delayed flop internally.
This removes metastability issues on inputs and makes everything work.
Diffstat (limited to 'hdl/rs232.sv')
-rw-r--r--hdl/rs232.sv28
1 files changed, 19 insertions, 9 deletions
diff --git a/hdl/rs232.sv b/hdl/rs232.sv
index 29ccb6c..d163cda 100644
--- a/hdl/rs232.sv
+++ b/hdl/rs232.sv
@@ -36,6 +36,8 @@ module rs232_tx
36 36
37 bit [RTS_BITS-1:0] rts_samples; 37 bit [RTS_BITS-1:0] rts_samples;
38 38
39 bit old_rts;
40
39 always @(posedge clock, posedge reset) begin 41 always @(posedge clock, posedge reset) begin
40 if (reset) begin 42 if (reset) begin
41 out_ready = 0; 43 out_ready = 0;
@@ -46,6 +48,7 @@ module rs232_tx
46 data_bits = 0; 48 data_bits = 0;
47 stop_bits = 0; 49 stop_bits = 0;
48 rts_samples = 0; 50 rts_samples = 0;
51 old_rts = 0;
49 end else begin 52 end else begin
50 if (out_ready && out_valid) begin 53 if (out_ready && out_valid) begin
51 hold_valid = 1; 54 hold_valid = 1;
@@ -57,7 +60,7 @@ module rs232_tx
57 end 60 end
58 61
59 rts_samples = rts_samples << 1; 62 rts_samples = rts_samples << 1;
60 rts_samples[0] = rts; 63 rts_samples[0] = old_rts;
61 64
62 if (hold_valid) begin 65 if (hold_valid) begin
63 case (state) 66 case (state)
@@ -100,6 +103,8 @@ module rs232_tx
100 end 103 end
101 104
102 out_ready = !hold_valid; 105 out_ready = !hold_valid;
106
107 old_rts = rts;
103 end 108 end
104 end 109 end
105 110
@@ -140,6 +145,8 @@ module rs232_rx
140 bit [$clog2(OVERSAMPLE+1):0] clock_counter; 145 bit [$clog2(OVERSAMPLE+1):0] clock_counter;
141 bit [$clog2(OVERSAMPLE+1):0] next_clock_counter; 146 bit [$clog2(OVERSAMPLE+1):0] next_clock_counter;
142 147
148 bit old_txd;
149
143 always @(posedge clock, posedge reset) begin 150 always @(posedge clock, posedge reset) begin
144 if (reset) begin 151 if (reset) begin
145 clock_out = 0; 152 clock_out = 0;
@@ -152,6 +159,7 @@ module rs232_rx
152 sample = 0; 159 sample = 0;
153 clock_counter = 0; 160 clock_counter = 0;
154 next_clock_counter = 0; 161 next_clock_counter = 0;
162 old_txd = 0;
155 end else begin 163 end else begin
156 automatic bit ok = 0; 164 automatic bit ok = 0;
157 165
@@ -190,7 +198,7 @@ module rs232_rx
190 case (state) 198 case (state)
191 199
192 START: begin 200 START: begin
193 if (txd == 0) begin 201 if (old_txd == 0) begin
194 state = state.next; 202 state = state.next;
195 if (OVERSAMPLE == 0) 203 if (OVERSAMPLE == 0)
196 state = state.next; 204 state = state.next;
@@ -205,8 +213,8 @@ module rs232_rx
205 213
206 DATA: begin 214 DATA: begin
207 buffer = buffer >> 1; 215 buffer = buffer >> 1;
208 buffer[`UART_BYTE_BITS-1] = txd; 216 buffer[`UART_BYTE_BITS-1] = old_txd;
209 parity = parity ^ txd; 217 parity = parity ^ old_txd;
210 if (data_bits == `UART_BYTE_BITS-1) 218 if (data_bits == `UART_BYTE_BITS-1)
211 state = state.next; 219 state = state.next;
212 else 220 else
@@ -214,26 +222,26 @@ module rs232_rx
214 end 222 end
215 223
216 PARITY_BIT: begin 224 PARITY_BIT: begin
217 parity = parity ^ txd; 225 parity = parity ^ old_txd;
218 state = state.next; 226 state = state.next;
219 end 227 end
220 228
221 STOP: begin 229 STOP: begin
222 if (!in_valid && txd == 1 && parity == 0) begin 230 if (!in_valid && old_txd == 1 && parity == 0) begin
223 in_valid = 1; 231 in_valid = 1;
224 in_data = buffer; 232 in_data = buffer;
225 end 233 end
226 if (txd == 1 && parity == 0) begin 234 if (old_txd == 1 && parity == 0) begin
227 next_clock_counter = (OVERSAMPLE+1)/2; 235 next_clock_counter = (OVERSAMPLE+1)/2;
228 end 236 end
229 if (txd == 1) 237 if (old_txd == 1)
230 state = state.first; 238 state = state.first;
231 else 239 else
232 state = state.next; 240 state = state.next;
233 end 241 end
234 242
235 REALSTOP: begin 243 REALSTOP: begin
236 if (txd == 1) 244 if (old_txd == 1)
237 state = state.first; 245 state = state.first;
238 end 246 end
239 247
@@ -241,6 +249,8 @@ module rs232_rx
241 end 249 end
242 250
243 cts = !(state == state.first && !in_valid); 251 cts = !(state == state.first && !in_valid);
252
253 old_txd = txd;
244 end 254 end
245 end 255 end
246 256