@@ -27,28 +27,28 @@ module Uart8Receiver (
27
27
output reg [7 :0 ] out // received data
28
28
);
29
29
30
- reg [2 :0 ] state = `RESET;
31
- reg [2 :0 ] bitIndex = 3'b0 ; // index for 8-bit data
32
- reg [1 :0 ] inShiftReg = 2'b0 ; // shift reg for input signal conditioning
33
- reg [3 :0 ] inHoldReg = 4'b0 ; // shift reg for stop signal hold time check
34
- reg [3 :0 ] sampleCount = 4'b0 ; // count ticks for 16x oversample
35
- reg [3 :0 ] validCount = 4'b0 ; // count ticks before clearing output data
36
- reg [7 :0 ] receivedData = 8'b0 ; // storage for the deserialized data
37
- wire inSample ;
30
+ reg [2 :0 ] state = `RESET;
31
+ reg [2 :0 ] bit_index = 3'b0 ; // index for 8-bit data
32
+ reg [1 :0 ] in_shift_reg = 2'b0 ; // shift reg for input signal conditioning
33
+ reg [3 :0 ] in_hold_reg = 4'b0 ; // shift reg for stop signal hold time check
34
+ reg [3 :0 ] sample_count = 4'b0 ; // count ticks for 16x oversample
35
+ reg [3 :0 ] valid_count = 4'b0 ; // count ticks before clearing output data
36
+ reg [7 :0 ] received_data = 8'b0 ; // storage for the deserialized data
37
+ wire in_sample ;
38
38
39
39
/*
40
40
* Double-register the incoming data:
41
41
*
42
42
* This prevents metastability problems crossing into rx clock domain
43
43
*
44
- * After registering, only the {inSample } wire is to be accessed - the
44
+ * After registering, only the {in_sample } wire is to be accessed - the
45
45
* earlier, unconditioned signal {in} must be ignored
46
46
*/
47
47
always @(posedge clk) begin
48
- inShiftReg <= { inShiftReg [0 ], in };
48
+ in_shift_reg <= { in_shift_reg [0 ], in };
49
49
end
50
50
51
- assign inSample = inShiftReg [1 ];
51
+ assign in_sample = in_shift_reg [1 ];
52
52
53
53
/*
54
54
* End the validity of output data after precise time of one serial bit cycle:
@@ -61,10 +61,10 @@ assign inSample = inShiftReg[1];
61
61
* from STOP_BIT state
62
62
*/
63
63
always @(posedge clk) begin
64
- if (| validCount ) begin
65
- validCount <= validCount + 4'b1 ;
66
- if (& validCount ) begin // reached 15 - timed output interval ends
67
- out <= 8'b0 ;
64
+ if (| valid_count ) begin
65
+ valid_count <= valid_count + 4'b1 ;
66
+ if (& valid_count ) begin // reached 15 - timed output interval ends
67
+ out <= 8'b0 ;
68
68
end
69
69
end
70
70
end
@@ -76,14 +76,14 @@ always @(posedge clk) begin
76
76
77
77
case (state)
78
78
`RESET: begin
79
- busy <= 1'b0 ;
80
- done <= 1'b0 ;
81
- err <= 1'b0 ;
82
- sampleCount <= 4'b0 ;
83
- receivedData <= 8'b0 ;
84
- out <= 8'b0 ;
79
+ busy <= 1'b0 ;
80
+ done <= 1'b0 ;
81
+ err <= 1'b0 ;
82
+ sample_count <= 4'b0 ;
83
+ received_data <= 8'b0 ;
84
+ out <= 8'b0 ;
85
85
if (en) begin
86
- state <= `IDLE;
86
+ state <= `IDLE;
87
87
end
88
88
end
89
89
@@ -96,50 +96,50 @@ always @(posedge clk) begin
96
96
*
97
97
* Then start the count for the proceeding full baud intervals
98
98
*/
99
- if (! inSample ) begin
100
- sampleCount <= sampleCount + 4'b1 ;
101
- if (& sampleCount [2 :0 ]) begin // reached 7
102
- busy <= 1'b1 ;
103
- done <= 1'b0 ;
104
- err <= 1'b0 ;
105
- sampleCount <= 4'b0 ; // start the full interval count over
106
- state <= `START_BIT;
99
+ if (! in_sample ) begin
100
+ sample_count <= sample_count + 4'b1 ;
101
+ if (& sample_count [2 :0 ]) begin // reached 7
102
+ busy <= 1'b1 ;
103
+ done <= 1'b0 ;
104
+ err <= 1'b0 ;
105
+ sample_count <= 4'b0 ; // start the full interval count over
106
+ state <= `START_BIT;
107
107
end
108
- end else if (| sampleCount ) begin
108
+ end else if (| sample_count ) begin
109
109
// bit did not remain low while waiting till 7 -
110
110
// remain in IDLE state
111
- err <= 1'b1 ;
111
+ err <= 1'b1 ;
112
112
end
113
113
end
114
114
115
115
`START_BIT: begin
116
116
/*
117
117
* Wait one full baud interval to the mid-point of first bit
118
118
*/
119
- sampleCount <= sampleCount + 4'b1 ;
120
- if (& sampleCount ) begin // reached 15
121
- receivedData <= { 7'b0 , inSample };
122
- out <= 8'b0 ;
123
- bitIndex <= 3'b1 ;
124
- state <= `DATA_BITS;
119
+ sample_count <= sample_count + 4'b1 ;
120
+ if (& sample_count ) begin // reached 15
121
+ received_data <= { 7'b0 , in_sample };
122
+ out <= 8'b0 ;
123
+ bit_index <= 3'b1 ;
124
+ state <= `DATA_BITS;
125
125
end
126
126
end
127
127
128
128
`DATA_BITS: begin
129
129
/*
130
130
* Take 8 baud intervals to receive serial data
131
131
*/
132
- if (& sampleCount ) begin // save one bit of received data
133
- sampleCount <= 4'b0 ;
134
- receivedData[bitIndex ] <= inSample ;
135
- if (& bitIndex ) begin
136
- bitIndex <= 3'b0 ;
137
- state <= `STOP_BIT;
132
+ if (& sample_count ) begin // save one bit of received data
133
+ sample_count <= 4'b0 ;
134
+ received_data[bit_index ] <= in_sample ;
135
+ if (& bit_index ) begin
136
+ bit_index <= 3'b0 ;
137
+ state <= `STOP_BIT;
138
138
end else begin
139
- bitIndex <= bitIndex + 3'b1 ;
139
+ bit_index <= bit_index + 3'b1 ;
140
140
end
141
141
end else begin
142
- sampleCount <= sampleCount + 4'b1 ;
142
+ sample_count <= sample_count + 4'b1 ;
143
143
end
144
144
end
145
145
@@ -156,35 +156,35 @@ always @(posedge clk) begin
156
156
* precisely in reality, accept the transition to handling the
157
157
* next start bit any time after the stop bit mid-point
158
158
*/
159
- inHoldReg <= { inHoldReg [2 :0 ], inSample };
159
+ in_hold_reg <= { in_hold_reg [2 :0 ], in_sample };
160
160
161
- sampleCount <= sampleCount + 4'b1 ;
162
- if (sampleCount [3 ]) begin // reached 8 to 15
161
+ sample_count <= sample_count + 4'b1 ;
162
+ if (sample_count [3 ]) begin // reached 8 to 15
163
163
// in the second half of the baud interval
164
- if (! inSample ) begin
164
+ if (! in_sample ) begin
165
165
// accept that transmission has completed only if the stop
166
166
// signal held for a time of >= 4 rx clocks before it
167
167
// changed to a start signal
168
- if (& inHoldReg ) begin
168
+ if (& in_hold_reg ) begin
169
169
// can accept the transmitted data and output it
170
- done <= 1'b1 ;
171
- out <= receivedData ;
172
- validCount <= sampleCount ;
173
- sampleCount <= 4'b0 ;
174
- state <= `IDLE;
170
+ done <= 1'b1 ;
171
+ out <= received_data ;
172
+ valid_count <= sample_count ;
173
+ sample_count <= 4'b0 ;
174
+ state <= `IDLE;
175
175
end else begin
176
176
// bit did not go high or remain high while waiting
177
177
// till 8 - signal {err} for this transmit
178
- err <= 1'b1 ;
179
- sampleCount <= 4'b0 ;
180
- state <= `READY;
178
+ err <= 1'b1 ;
179
+ sample_count <= 4'b0 ;
180
+ state <= `READY;
181
181
end
182
- end else if (& sampleCount ) begin // reached 15
182
+ end else if (& sample_count ) begin // reached 15
183
183
// can accept the transmitted data and output it
184
- done <= 1'b1 ;
185
- out <= receivedData ;
186
- sampleCount <= 4'b0 ;
187
- state <= `READY;
184
+ done <= 1'b1 ;
185
+ out <= received_data ;
186
+ sample_count <= 4'b0 ;
187
+ state <= `READY;
188
188
end
189
189
end
190
190
end
@@ -194,11 +194,11 @@ always @(posedge clk) begin
194
194
* Wait one full bit cycle to sustain the {out} data, the
195
195
* {done} signal or the {err} signal
196
196
*/
197
- sampleCount <= sampleCount + 4'b1 ;
198
- if (& sampleCount [3 :1 ]) begin // reached 14 -
197
+ sample_count <= sample_count + 4'b1 ;
198
+ if (& sample_count [3 :1 ]) begin // reached 14 -
199
199
// additional tick 15 comes from transitting the READY state
200
200
// to the RESET state
201
- state <= `RESET;
201
+ state <= `RESET;
202
202
end
203
203
end
204
204
0 commit comments