39
39
`define RESET 3'b001
40
40
`define IDLE 3'b010
41
41
`define DATA_BITS 3'b100
42
- `define WAIT_STOP 3'b101
43
- `define STOP_BIT 3'b110
42
+ `define STOP_BIT 3'b101
44
43
45
44
module UARTReceiver #(
46
45
parameter CLOCK_RATE = 50000000 ,
@@ -53,7 +52,8 @@ module UARTReceiver #(
53
52
input wire ready, // OK to transmit
54
53
output reg [7 :0 ] out, // received data
55
54
output reg valid, // RX completed
56
- output reg err // error while receiving data
55
+ output reg err, // frame error
56
+ output reg overrun // overrun
57
57
);
58
58
parameter MAX_RATE_RX = CLOCK_RATE / (BAUD_RATE * 16 ); // 16x oversample
59
59
parameter RX_CNT_WIDTH = $clog2(MAX_RATE_RX);
@@ -84,6 +84,7 @@ module UARTReceiver #(
84
84
`RESET: begin
85
85
out <= 8'b0 ;
86
86
err <= 0 ;
87
+ overrun <= 0 ;
87
88
valid <= 0 ;
88
89
inputSw <= 3'b111 ;
89
90
bitIdx <= 3'b0 ;
@@ -101,6 +102,7 @@ module UARTReceiver #(
101
102
clockCount <= 4'b0 ;
102
103
receivedData <= 8'b0 ;
103
104
err <= 0 ;
105
+ overrun <= 0 ;
104
106
end else if (! (| inputSw) || (| clockCount)) begin
105
107
// Check bit to make sure it's still low
106
108
if (| inputSw) begin
@@ -118,7 +120,7 @@ module UARTReceiver #(
118
120
receivedData[bitIdx] <= (inputSw[0 ] & inputSw[1 ]) | (inputSw[0 ] & inputSw[2 ]) | (inputSw[1 ] & inputSw[2 ]);
119
121
if (& bitIdx) begin
120
122
bitIdx <= 3'b0 ;
121
- state <= `WAIT_STOP ;
123
+ state <= `STOP_BIT ;
122
124
end else begin
123
125
bitIdx <= bitIdx + 1 ;
124
126
end
@@ -127,32 +129,19 @@ module UARTReceiver #(
127
129
end
128
130
end
129
131
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
140
132
`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;
154
142
end
155
143
end
144
+ clockCount <= clockCount + 1 ;
156
145
end
157
146
158
147
default : state <= `RESET;
0 commit comments