1 |
746 |
lampret |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// uart_transmitter.v ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// ////
|
6 |
|
|
//// This file is part of the "UART 16550 compatible" project ////
|
7 |
|
|
//// http://www.opencores.org/cores/uart16550/ ////
|
8 |
|
|
//// ////
|
9 |
|
|
//// Documentation related to this project: ////
|
10 |
|
|
//// - http://www.opencores.org/cores/uart16550/ ////
|
11 |
|
|
//// ////
|
12 |
|
|
//// Projects compatibility: ////
|
13 |
|
|
//// - WISHBONE ////
|
14 |
|
|
//// RS232 Protocol ////
|
15 |
|
|
//// 16550D uart (mostly supported) ////
|
16 |
|
|
//// ////
|
17 |
|
|
//// Overview (main Features): ////
|
18 |
|
|
//// UART core transmitter logic ////
|
19 |
|
|
//// ////
|
20 |
|
|
//// Known problems (limits): ////
|
21 |
|
|
//// None known ////
|
22 |
|
|
//// ////
|
23 |
|
|
//// To Do: ////
|
24 |
|
|
//// Thourough testing. ////
|
25 |
|
|
//// ////
|
26 |
|
|
//// Author(s): ////
|
27 |
|
|
//// - gorban@opencores.org ////
|
28 |
|
|
//// - Jacob Gorban ////
|
29 |
|
|
//// - Igor Mohor (igorm@opencores.org) ////
|
30 |
|
|
//// ////
|
31 |
|
|
//// Created: 2001/05/12 ////
|
32 |
|
|
//// Last Updated: 2001/05/17 ////
|
33 |
|
|
//// (See log for the revision history) ////
|
34 |
|
|
//// ////
|
35 |
|
|
//// ////
|
36 |
|
|
//////////////////////////////////////////////////////////////////////
|
37 |
|
|
//// ////
|
38 |
|
|
//// Copyright (C) 2000, 2001 Authors ////
|
39 |
|
|
//// ////
|
40 |
|
|
//// This source file may be used and distributed without ////
|
41 |
|
|
//// restriction provided that this copyright statement is not ////
|
42 |
|
|
//// removed from the file and that any derivative work contains ////
|
43 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
44 |
|
|
//// ////
|
45 |
|
|
//// This source file is free software; you can redistribute it ////
|
46 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
47 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
48 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
49 |
|
|
//// later version. ////
|
50 |
|
|
//// ////
|
51 |
|
|
//// This source is distributed in the hope that it will be ////
|
52 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
53 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
54 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
55 |
|
|
//// details. ////
|
56 |
|
|
//// ////
|
57 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
58 |
|
|
//// Public License along with this source; if not, download it ////
|
59 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
60 |
|
|
//// ////
|
61 |
|
|
//////////////////////////////////////////////////////////////////////
|
62 |
|
|
//
|
63 |
|
|
// CVS Revision History
|
64 |
|
|
//
|
65 |
|
|
// $Log: not supported by cvs2svn $
|
66 |
|
|
// Revision 1.17 2002/02/14 15:31:54 gorban
|
67 |
|
|
// tf_overrun signal was disabled since it was not used
|
68 |
|
|
//
|
69 |
|
|
// Revision 1.16 2002/01/08 11:29:40 mohor
|
70 |
|
|
// tf_pop was too wide. Now it is only 1 clk cycle width.
|
71 |
|
|
//
|
72 |
|
|
// Revision 1.15 2001/12/17 14:46:48 mohor
|
73 |
|
|
// overrun signal was moved to separate block because many sequential lsr
|
74 |
|
|
// reads were preventing data from being written to rx fifo.
|
75 |
|
|
// underrun signal was not used and was removed from the project.
|
76 |
|
|
//
|
77 |
|
|
// Revision 1.14 2001/12/03 21:44:29 gorban
|
78 |
|
|
// Updated specification documentation.
|
79 |
|
|
// Added full 32-bit data bus interface, now as default.
|
80 |
|
|
// Address is 5-bit wide in 32-bit data bus mode.
|
81 |
|
|
// Added wb_sel_i input to the core. It's used in the 32-bit mode.
|
82 |
|
|
// Added debug interface with two 32-bit read-only registers in 32-bit mode.
|
83 |
|
|
// Bits 5 and 6 of LSR are now only cleared on TX FIFO write.
|
84 |
|
|
// My small test bench is modified to work with 32-bit mode.
|
85 |
|
|
//
|
86 |
|
|
// Revision 1.13 2001/11/08 14:54:23 mohor
|
87 |
|
|
// Comments in Slovene language deleted, few small fixes for better work of
|
88 |
|
|
// old tools. IRQs need to be fix.
|
89 |
|
|
//
|
90 |
|
|
// Revision 1.12 2001/11/07 17:51:52 gorban
|
91 |
|
|
// Heavily rewritten interrupt and LSR subsystems.
|
92 |
|
|
// Many bugs hopefully squashed.
|
93 |
|
|
//
|
94 |
|
|
// Revision 1.11 2001/10/29 17:00:46 gorban
|
95 |
|
|
// fixed parity sending and tx_fifo resets over- and underrun
|
96 |
|
|
//
|
97 |
|
|
// Revision 1.10 2001/10/20 09:58:40 gorban
|
98 |
|
|
// Small synopsis fixes
|
99 |
|
|
//
|
100 |
|
|
// Revision 1.9 2001/08/24 21:01:12 mohor
|
101 |
|
|
// Things connected to parity changed.
|
102 |
|
|
// Clock devider changed.
|
103 |
|
|
//
|
104 |
|
|
// Revision 1.8 2001/08/23 16:05:05 mohor
|
105 |
|
|
// Stop bit bug fixed.
|
106 |
|
|
// Parity bug fixed.
|
107 |
|
|
// WISHBONE read cycle bug fixed,
|
108 |
|
|
// OE indicator (Overrun Error) bug fixed.
|
109 |
|
|
// PE indicator (Parity Error) bug fixed.
|
110 |
|
|
// Register read bug fixed.
|
111 |
|
|
//
|
112 |
|
|
// Revision 1.6 2001/06/23 11:21:48 gorban
|
113 |
|
|
// DL made 16-bit long. Fixed transmission/reception bugs.
|
114 |
|
|
//
|
115 |
|
|
// Revision 1.5 2001/06/02 14:28:14 gorban
|
116 |
|
|
// Fixed receiver and transmitter. Major bug fixed.
|
117 |
|
|
//
|
118 |
|
|
// Revision 1.4 2001/05/31 20:08:01 gorban
|
119 |
|
|
// FIFO changes and other corrections.
|
120 |
|
|
//
|
121 |
|
|
// Revision 1.3 2001/05/27 17:37:49 gorban
|
122 |
|
|
// Fixed many bugs. Updated spec. Changed FIFO files structure. See CHANGES.txt file.
|
123 |
|
|
//
|
124 |
|
|
// Revision 1.2 2001/05/21 19:12:02 gorban
|
125 |
|
|
// Corrected some Linter messages.
|
126 |
|
|
//
|
127 |
|
|
// Revision 1.1 2001/05/17 18:34:18 gorban
|
128 |
|
|
// First 'stable' release. Should be sythesizable now. Also added new header.
|
129 |
|
|
//
|
130 |
|
|
// Revision 1.0 2001-05-17 21:27:12+02 jacob
|
131 |
|
|
// Initial revision
|
132 |
|
|
//
|
133 |
|
|
//
|
134 |
|
|
|
135 |
|
|
// synopsys translate_off
|
136 |
|
|
`include "timescale.v"
|
137 |
|
|
// synopsys translate_on
|
138 |
|
|
|
139 |
|
|
`include "uart_defines.v"
|
140 |
|
|
|
141 |
|
|
module uart_transmitter (clk, wb_rst_i, lcr, tf_push, wb_dat_i, enable, stx_pad_o, tstate, tf_count, tx_reset, lsr_mask);
|
142 |
|
|
|
143 |
|
|
input clk;
|
144 |
|
|
input wb_rst_i;
|
145 |
|
|
input [7:0] lcr;
|
146 |
|
|
input tf_push;
|
147 |
|
|
input [7:0] wb_dat_i;
|
148 |
|
|
input enable;
|
149 |
|
|
input tx_reset;
|
150 |
|
|
input lsr_mask; //reset of fifo
|
151 |
|
|
output stx_pad_o;
|
152 |
|
|
output [2:0] tstate;
|
153 |
|
|
output [`UART_FIFO_COUNTER_W-1:0] tf_count;
|
154 |
|
|
|
155 |
|
|
reg [2:0] tstate;
|
156 |
|
|
reg [4:0] counter;
|
157 |
|
|
reg [2:0] bit_counter; // counts the bits to be sent
|
158 |
|
|
reg [6:0] shift_out; // output shift register
|
159 |
|
|
reg stx_o_tmp;
|
160 |
|
|
reg parity_xor; // parity of the word
|
161 |
|
|
reg tf_pop;
|
162 |
|
|
reg bit_out;
|
163 |
|
|
|
164 |
|
|
// TX FIFO instance
|
165 |
|
|
//
|
166 |
|
|
// Transmitter FIFO signals
|
167 |
|
|
wire [`UART_FIFO_WIDTH-1:0] tf_data_in;
|
168 |
|
|
wire [`UART_FIFO_WIDTH-1:0] tf_data_out;
|
169 |
|
|
wire tf_push;
|
170 |
|
|
//wire tf_overrun;
|
171 |
|
|
wire [`UART_FIFO_COUNTER_W-1:0] tf_count;
|
172 |
|
|
|
173 |
|
|
assign tf_data_in = wb_dat_i;
|
174 |
|
|
|
175 |
|
|
uart_fifo fifo_tx( // error bit signal is not used in transmitter FIFO
|
176 |
|
|
.clk( clk ),
|
177 |
|
|
.wb_rst_i( wb_rst_i ),
|
178 |
|
|
.data_in( tf_data_in ),
|
179 |
|
|
.data_out( tf_data_out ),
|
180 |
|
|
.push( tf_push ),
|
181 |
|
|
.pop( tf_pop ),
|
182 |
|
|
.overrun( /*tf_overrun*/ ),
|
183 |
|
|
.count( tf_count ),
|
184 |
|
|
.error_bit(), // Ta ni priklopljen. Prej je manjkal, dodal Igor
|
185 |
|
|
.fifo_reset( tx_reset ),
|
186 |
|
|
.reset_status(lsr_mask)
|
187 |
|
|
);
|
188 |
|
|
|
189 |
|
|
// TRANSMITTER FINAL STATE MACHINE
|
190 |
|
|
|
191 |
|
|
parameter s_idle = 3'd0;
|
192 |
|
|
parameter s_send_start = 3'd1;
|
193 |
|
|
parameter s_send_byte = 3'd2;
|
194 |
|
|
parameter s_send_parity = 3'd3;
|
195 |
|
|
parameter s_send_stop = 3'd4;
|
196 |
|
|
parameter s_pop_byte = 3'd5;
|
197 |
|
|
|
198 |
|
|
always @(posedge clk or posedge wb_rst_i)
|
199 |
|
|
begin
|
200 |
|
|
if (wb_rst_i)
|
201 |
|
|
begin
|
202 |
|
|
tstate <= #1 s_idle;
|
203 |
|
|
stx_o_tmp <= #1 1'b1;
|
204 |
|
|
counter <= #1 5'b0;
|
205 |
|
|
shift_out <= #1 7'b0;
|
206 |
|
|
bit_out <= #1 1'b0;
|
207 |
|
|
parity_xor <= #1 1'b0;
|
208 |
|
|
tf_pop <= #1 1'b0;
|
209 |
|
|
bit_counter <= #1 3'b0;
|
210 |
|
|
end
|
211 |
|
|
else
|
212 |
|
|
if (enable)
|
213 |
|
|
begin
|
214 |
|
|
case (tstate)
|
215 |
|
|
s_idle : if (~|tf_count) // if tf_count==0
|
216 |
|
|
begin
|
217 |
|
|
tstate <= #1 s_idle;
|
218 |
|
|
stx_o_tmp <= #1 1'b1;
|
219 |
|
|
end
|
220 |
|
|
else
|
221 |
|
|
begin
|
222 |
|
|
tf_pop <= #1 1'b0;
|
223 |
|
|
stx_o_tmp <= #1 1'b1;
|
224 |
|
|
tstate <= #1 s_pop_byte;
|
225 |
|
|
end
|
226 |
|
|
s_pop_byte : begin
|
227 |
|
|
tf_pop <= #1 1'b1;
|
228 |
|
|
case (lcr[/*`UART_LC_BITS*/1:0]) // number of bits in a word
|
229 |
|
|
2'b00 : begin
|
230 |
|
|
bit_counter <= #1 3'b100;
|
231 |
|
|
parity_xor <= #1 ^tf_data_out[4:0];
|
232 |
|
|
end
|
233 |
|
|
2'b01 : begin
|
234 |
|
|
bit_counter <= #1 3'b101;
|
235 |
|
|
parity_xor <= #1 ^tf_data_out[5:0];
|
236 |
|
|
end
|
237 |
|
|
2'b10 : begin
|
238 |
|
|
bit_counter <= #1 3'b110;
|
239 |
|
|
parity_xor <= #1 ^tf_data_out[6:0];
|
240 |
|
|
end
|
241 |
|
|
2'b11 : begin
|
242 |
|
|
bit_counter <= #1 3'b111;
|
243 |
|
|
parity_xor <= #1 ^tf_data_out[7:0];
|
244 |
|
|
end
|
245 |
|
|
endcase
|
246 |
|
|
{shift_out[6:0], bit_out} <= #1 tf_data_out;
|
247 |
|
|
tstate <= #1 s_send_start;
|
248 |
|
|
end
|
249 |
|
|
s_send_start : begin
|
250 |
|
|
tf_pop <= #1 1'b0;
|
251 |
|
|
if (~|counter)
|
252 |
|
|
counter <= #1 5'b01111;
|
253 |
|
|
else
|
254 |
|
|
if (counter == 5'b00001)
|
255 |
|
|
begin
|
256 |
|
|
counter <= #1 0;
|
257 |
|
|
tstate <= #1 s_send_byte;
|
258 |
|
|
end
|
259 |
|
|
else
|
260 |
|
|
counter <= #1 counter - 1'b1;
|
261 |
|
|
stx_o_tmp <= #1 1'b0;
|
262 |
|
|
end
|
263 |
|
|
s_send_byte : begin
|
264 |
|
|
if (~|counter)
|
265 |
|
|
counter <= #1 5'b01111;
|
266 |
|
|
else
|
267 |
|
|
if (counter == 5'b00001)
|
268 |
|
|
begin
|
269 |
|
|
if (bit_counter > 3'b0)
|
270 |
|
|
begin
|
271 |
|
|
bit_counter <= #1 bit_counter - 1'b1;
|
272 |
|
|
{shift_out[5:0],bit_out } <= #1 {shift_out[6:1], shift_out[0]};
|
273 |
|
|
tstate <= #1 s_send_byte;
|
274 |
|
|
end
|
275 |
|
|
else // end of byte
|
276 |
|
|
if (~lcr[`UART_LC_PE])
|
277 |
|
|
begin
|
278 |
|
|
tstate <= #1 s_send_stop;
|
279 |
|
|
end
|
280 |
|
|
else
|
281 |
|
|
begin
|
282 |
|
|
case ({lcr[`UART_LC_EP],lcr[`UART_LC_SP]})
|
283 |
|
|
2'b00: bit_out <= #1 ~parity_xor;
|
284 |
|
|
2'b01: bit_out <= #1 1'b1;
|
285 |
|
|
2'b10: bit_out <= #1 parity_xor;
|
286 |
|
|
2'b11: bit_out <= #1 1'b0;
|
287 |
|
|
endcase
|
288 |
|
|
tstate <= #1 s_send_parity;
|
289 |
|
|
end
|
290 |
|
|
counter <= #1 0;
|
291 |
|
|
end
|
292 |
|
|
else
|
293 |
|
|
counter <= #1 counter - 1'b1;
|
294 |
|
|
stx_o_tmp <= #1 bit_out; // set output pin
|
295 |
|
|
end
|
296 |
|
|
s_send_parity : begin
|
297 |
|
|
if (~|counter)
|
298 |
|
|
counter <= #1 5'b01111;
|
299 |
|
|
else
|
300 |
|
|
if (counter == 5'b00001)
|
301 |
|
|
begin
|
302 |
|
|
counter <= #1 4'b0;
|
303 |
|
|
tstate <= #1 s_send_stop;
|
304 |
|
|
end
|
305 |
|
|
else
|
306 |
|
|
counter <= #1 counter - 1'b1;
|
307 |
|
|
stx_o_tmp <= #1 bit_out;
|
308 |
|
|
end
|
309 |
|
|
s_send_stop : begin
|
310 |
|
|
if (~|counter)
|
311 |
|
|
begin
|
312 |
|
|
casex ({lcr[`UART_LC_SB],lcr[`UART_LC_BITS]})
|
313 |
|
|
3'b0xx: counter <= #1 5'b01101; // 1 stop bit ok igor
|
314 |
|
|
3'b100: counter <= #1 5'b10101; // 1.5 stop bit
|
315 |
|
|
default: counter <= #1 5'b11101; // 2 stop bits
|
316 |
|
|
endcase
|
317 |
|
|
end
|
318 |
|
|
else
|
319 |
|
|
if (counter == 5'b00001)
|
320 |
|
|
begin
|
321 |
|
|
counter <= #1 0;
|
322 |
|
|
tstate <= #1 s_idle;
|
323 |
|
|
end
|
324 |
|
|
else
|
325 |
|
|
counter <= #1 counter - 1'b1;
|
326 |
|
|
stx_o_tmp <= #1 1'b1;
|
327 |
|
|
end
|
328 |
|
|
|
329 |
|
|
default : // should never get here
|
330 |
|
|
tstate <= #1 s_idle;
|
331 |
|
|
endcase
|
332 |
|
|
end // end if enable
|
333 |
|
|
else
|
334 |
|
|
tf_pop <= #1 1'b0; // tf_pop must be 1 cycle width
|
335 |
|
|
end // transmitter logic
|
336 |
|
|
|
337 |
|
|
assign stx_pad_o = lcr[`UART_LC_BC] ? 1'b0 : stx_o_tmp; // Break condition
|
338 |
|
|
|
339 |
|
|
endmodule
|