@@ -55,15 +55,15 @@ module UARTReceiver #(
55
55
output reg error, // frame error
56
56
output reg overrun // overrun
57
57
);
58
- parameter RX_CLOCK_PERIOD = CLOCK_RATE / (BAUD_RATE * 16 ); // 16x oversample
58
+ parameter RX_CLOCK_PERIOD = $rtoi ( CLOCK_RATE / (BAUD_RATE * 16 ) + 0 . 5 ); // 16x oversample
59
59
parameter RX_CNT_WIDTH = $clog2(RX_CLOCK_PERIOD);
60
- reg [RX_CNT_WIDTH - 1 :0 ] rxCounter = 0 ;
60
+ reg [RX_CNT_WIDTH - 1 :0 ] rxCounter;
61
61
62
- reg [2 :0 ] state = `RESET;
63
- reg [2 :0 ] bitIndex = 3'b0 ; // for 8-bit data
64
- reg [2 :0 ] inputReg = 3'b111 ; // shift reg for input signal state
65
- reg [3 :0 ] clockCount = 4'b0 ; // count clocks for 16x oversample
66
- reg [7 :0 ] data = 8'b0 ; // temporary storage for input data
62
+ reg [2 :0 ] state; // FSM state
63
+ reg [2 :0 ] bitIndex; // for 8-bit data
64
+ reg [2 :0 ] inputReg; // shift reg for input signal
65
+ reg [3 :0 ] sampleCount; // clock count for 16x oversample
66
+ reg [7 :0 ] data; // input data buffer
67
67
68
68
always @(posedge clk) begin
69
69
if (reset || ! enable) begin
@@ -88,35 +88,35 @@ module UARTReceiver #(
88
88
valid <= 0 ;
89
89
inputReg <= 3'b111 ;
90
90
bitIndex <= 3'b0 ;
91
- clockCount <= 4'b0 ;
91
+ sampleCount <= 4'b0 ;
92
92
data <= 8'b0 ;
93
93
if (enable) begin
94
94
state <= `IDLE;
95
95
end
96
96
end
97
97
98
98
`IDLE: begin
99
- if (clockCount >= 4'h5 ) begin
99
+ if (sampleCount >= 4'h5 ) begin
100
100
state <= `DATA_BITS;
101
101
bitIndex <= 3'b0 ;
102
- clockCount <= 4'b0 ;
102
+ sampleCount <= 4'b0 ;
103
103
data <= 8'b0 ;
104
104
error <= 0 ;
105
105
overrun <= 0 ;
106
- end else if (! (| inputReg) || (| clockCount )) begin
106
+ end else if (! (| inputReg) || (| sampleCount )) begin
107
107
// Check bit to make sure it's still low
108
108
if (| inputReg) begin
109
109
error <= 1 ;
110
110
state <= `RESET;
111
111
end
112
- clockCount <= clockCount + 1 ;
112
+ sampleCount <= sampleCount + 1 ;
113
113
end
114
114
end
115
115
116
116
// receive 8 bits of data
117
117
`DATA_BITS: begin
118
- if (& clockCount ) begin // save one bit of received data
119
- clockCount <= 4'b0 ;
118
+ if (& sampleCount ) begin // save one bit of received data
119
+ sampleCount <= 4'b0 ;
120
120
data[bitIndex] <= (inputReg[0 ] & inputReg[1 ]) | (inputReg[0 ] & inputReg[2 ]) | (inputReg[1 ] & inputReg[2 ]);
121
121
if (& bitIndex) begin
122
122
bitIndex <= 3'b0 ;
@@ -125,12 +125,12 @@ module UARTReceiver #(
125
125
bitIndex <= bitIndex + 1 ;
126
126
end
127
127
end else begin
128
- clockCount <= clockCount + 1 ;
128
+ sampleCount <= sampleCount + 1 ;
129
129
end
130
130
end
131
131
132
132
`STOP_BIT: begin
133
- if (& clockCount ) begin
133
+ if (& sampleCount ) begin
134
134
if (& inputReg) begin
135
135
valid <= 1 ;
136
136
if (! valid) begin
@@ -141,7 +141,7 @@ module UARTReceiver #(
141
141
state <= `IDLE;
142
142
end
143
143
end
144
- clockCount <= clockCount + 1 ;
144
+ sampleCount <= sampleCount + 1 ;
145
145
end
146
146
147
147
default : state <= `RESET;
0 commit comments