Skip to content

Commit 6f1b811

Browse files
committed
streamline stop bit checking, introduce overrun flag
1 parent 92f41a6 commit 6f1b811

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

uart/UARTReceiver.v

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
`define RESET 3'b001
4040
`define IDLE 3'b010
4141
`define DATA_BITS 3'b100
42-
`define WAIT_STOP 3'b101
43-
`define STOP_BIT 3'b110
42+
`define STOP_BIT 3'b101
4443

4544
module UARTReceiver #(
4645
parameter CLOCK_RATE = 50000000,
@@ -53,7 +52,8 @@ module UARTReceiver #(
5352
input wire ready, // OK to transmit
5453
output reg [7:0] out, // received data
5554
output reg valid, // RX completed
56-
output reg err // error while receiving data
55+
output reg err, // frame error
56+
output reg overrun // overrun
5757
);
5858
parameter MAX_RATE_RX = CLOCK_RATE / (BAUD_RATE * 16); // 16x oversample
5959
parameter RX_CNT_WIDTH = $clog2(MAX_RATE_RX);
@@ -84,6 +84,7 @@ module UARTReceiver #(
8484
`RESET: begin
8585
out <= 8'b0;
8686
err <= 0;
87+
overrun <= 0;
8788
valid <= 0;
8889
inputSw <= 3'b111;
8990
bitIdx <= 3'b0;
@@ -101,6 +102,7 @@ module UARTReceiver #(
101102
clockCount <= 4'b0;
102103
receivedData <= 8'b0;
103104
err <= 0;
105+
overrun <= 0;
104106
end else if (!(|inputSw) || (|clockCount)) begin
105107
// Check bit to make sure it's still low
106108
if (|inputSw) begin
@@ -118,7 +120,7 @@ module UARTReceiver #(
118120
receivedData[bitIdx] <= (inputSw[0] & inputSw[1]) | (inputSw[0] & inputSw[2]) | (inputSw[1] & inputSw[2]);
119121
if (&bitIdx) begin
120122
bitIdx <= 3'b0;
121-
state <= `WAIT_STOP;
123+
state <= `STOP_BIT;
122124
end else begin
123125
bitIdx <= bitIdx + 1;
124126
end
@@ -127,32 +129,19 @@ module UARTReceiver #(
127129
end
128130
end
129131

130-
`WAIT_STOP: begin
131-
if (&clockCount) begin
132-
clockCount <= 4'b0;
133-
state <= `STOP_BIT;
134-
end else begin
135-
clockCount <= clockCount + 1;
136-
end
137-
end
138-
139-
// check for at least half a stop bit
140132
`STOP_BIT: begin
141-
if (clockCount == 4'h8) begin
142-
state <= `IDLE;
143-
valid <= 1;
144-
if (!valid) begin // silently drop incoming byte in case of overrun
145-
out <= receivedData;
146-
end
147-
clockCount <= 4'b0;
148-
end else begin
149-
clockCount <= clockCount + 1;
150-
// Check bit to make sure it's still high
151-
if (!(&inputSw)) begin
152-
err <= 1;
153-
state <= `RESET;
133+
if (&clockCount) begin
134+
if (&inputSw) begin
135+
valid <= 1;
136+
if (!valid) begin
137+
out <= receivedData;
138+
end else begin
139+
overrun <= 1;
140+
end
141+
state <= `IDLE;
154142
end
155143
end
144+
clockCount <= clockCount + 1;
156145
end
157146

158147
default: state <= `RESET;

0 commit comments

Comments
 (0)