blob: ba4a3c35cdf6eeda6bf9b9ac9bcecea75fef3e9b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
module demo_uart(
Reset_n,
Clk,
// Interface to UART PHY (RS232 level converter)
RXD_i,
TXD_o,
// Clk is divided by (Prescaler+1) to generate 16x the bitrate
Prescaler_i,
// Pulsed when RxData is valid
RxValid_o,
RxData_o,
// Asserted when ready for a new Tx byte
TxReady_o,
// Pulsed when TxData is valid
TxValid_i,
TxData_i
);
input Reset_n;
input Clk;
// Interface to UART PHY (RS232 level converter)
input RXD_i;
output TXD_o;
// Clk is divided by (Prescaler+1) to generate 16x the bitrate
input [15:0] Prescaler_i;
// Pulsed when RxData is valid
output RxValid_o;
output [7:0] RxData_o;
// Asserted when ready for a new Tx byte
output TxReady_o;
// Pulsed when TxData is valid
input TxValid_i;
input [7:0] TxData_i;
//-------------------------------------------------------------------------
// Local declarations
//-------------------------------------------------------------------------
reg TXD_o;
reg RxValid_o;
reg [7:0] RxData_o;
reg TxReady_o;
//-------------------------------------------------------------------------
// Instantiation of sub-modules
//-------------------------------------------------------------------------
//--- Prescaler generating 16x bitrate clock ------------------------------
reg Clk_16x;
reg [15:0] Prescaler;
always @( negedge Reset_n or posedge Clk )
if ( ~Reset_n )
begin
Prescaler <= 0;
Clk_16x <= 0;
end
else
begin
if ( Prescaler == Prescaler_i )
begin
Prescaler <= 0;
Clk_16x <= 1;
end
else
begin
Prescaler <= Prescaler + 1;
Clk_16x <= 0;
end
end
//--- Transmitter logic ---------------------------------------------------
reg [3:0] TxCounter;
reg TxSendBit;
always @( negedge Reset_n or posedge Clk )
if ( ~Reset_n )
begin
TxCounter <= 0;
TxSendBit <= 0;
end
else
begin
TxSendBit <= 0;
if ( Clk_16x )
begin
if ( TxCounter == 15 )
begin
TxCounter <= 0;
TxSendBit <= 1;
end
else
TxCounter <= TxCounter + 1;
end
end
reg [7:0] TxData_reg;
reg [3:0] TxBitCnt;
always @( negedge Reset_n or posedge Clk )
if ( ~Reset_n )
begin
TXD_o <= 1;
TxReady_o <= 1;
TxData_reg <= 0;
TxBitCnt <= 0;
end
else
begin
if ( TxReady_o )
begin
if ( TxValid_i )
begin
TxReady_o <= 0;
TxData_reg <= TxData_i;
TxBitCnt <= 0;
end
end
else
begin
if ( TxSendBit )
begin
// Only do anything on bit boundaries
casez ( TxBitCnt )
0: // Tx START bit
TXD_o <= 0;
10: // Tx second STOP bit
// Now we're done
TxReady_o <= 1;
default: // Tx data bit + first stop bit
begin
TXD_o <= TxData_reg[0];
TxData_reg <= { 1'b1, TxData_reg[7:1] };
end
endcase
TxBitCnt <= TxBitCnt+1;
end
end
end
//--- Receiver logic ------------------------------------------------------
reg RxHunt;
reg [3:0] RxCounter;
reg RxSampleBit;
reg RxDone;
always @( negedge Reset_n or posedge Clk )
if ( ~Reset_n )
begin
RxCounter <= 0;
RxSampleBit <= 0;
RxHunt <= 1;
end
else
begin
RxSampleBit <= 0;
if ( RxDone )
RxHunt <= 1;
if ( Clk_16x )
begin
if ( RxHunt )
begin
if ( RXD_i == 0 )
begin
// Receiving start bit!
RxHunt <= 0;
// Reset 16x bit counter
RxCounter <= 0;
end
end
else
begin
RxCounter <= RxCounter + 1;
if ( RxCounter == 7 )
// In middle of Rx bit in next cycle
RxSampleBit <= 1;
end
end
end
reg [3:0] RxBitCount;
always @( negedge Reset_n or posedge Clk )
if ( ~Reset_n )
begin
RxValid_o <= 0;
RxData_o <= 'b0;
RxBitCount <= 0;
RxDone <= 0;
end
else
begin
RxValid_o <= 0;
RxDone <= 0;
if ( RxSampleBit )
begin
RxBitCount <= RxBitCount + 1;
casez ( RxBitCount )
0: // START bit - just ignore it
;
9: // STOP bit - indicate we're ready again
begin
RxDone <= 1;
RxBitCount <= 0;
end
default: // Rx Data bits
begin
RxData_o <= { RXD_i, RxData_o[7:1] };
if ( RxBitCount == 8 )
// Last data bit just received
RxValid_o <= 1;
end
endcase
end
end
endmodule
|