1 //////////////////////////////////////////////////////////////////////
3 //// uart_receiver.v ////
6 //// This file is part of the "UART 16550 compatible" project ////
7 //// http://www.opencores.org/cores/uart16550/ ////
9 //// Documentation related to this project: ////
10 //// - http://www.opencores.org/cores/uart16550/ ////
12 //// Projects compatibility: ////
14 //// RS232 Protocol ////
15 //// 16550D uart (mostly supported) ////
17 //// Overview (main Features): ////
18 //// UART core receiver logic ////
20 //// Known problems (limits): ////
24 //// Thourough testing. ////
27 //// - gorban@opencores.org ////
28 //// - Jacob Gorban ////
29 //// - Igor Mohor (igorm@opencores.org) ////
31 //// Created: 2001/05/12 ////
32 //// Last Updated: 2001/05/17 ////
33 //// (See log for the revision history) ////
36 //////////////////////////////////////////////////////////////////////
38 //// Copyright (C) 2000, 2001 Authors ////
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. ////
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. ////
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 ////
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 ////
61 //////////////////////////////////////////////////////////////////////
63 // CVS Revision History
65 // $Log: not supported by cvs2svn $
66 // Revision 1.29 2002/07/29 21:16:18 gorban
67 // The uart_defines.v file is included again in sources.
69 // Revision 1.28 2002/07/22 23:02:23 gorban
71 // * Possible loss of sync and bad reception of stop bit on slow baud rates fixed.
72 // Problem reported by Kenny.Tung.
73 // * Bad (or lack of ) loopback handling fixed. Reported by Cherry Withers.
76 // * Made FIFO's as general inferrable memory where possible.
77 // So on FPGA they should be inferred as RAM (Distributed RAM on Xilinx).
78 // This saves about 1/3 of the Slice count and reduces P&R and synthesis times.
80 // * Added optional baudrate output (baud_o).
81 // This is identical to BAUDOUT* signal on 16550 chip.
82 // It outputs 16xbit_clock_rate - the divided clock.
83 // It's disabled by default. Define UART_HAS_BAUDRATE_OUTPUT to use.
85 // Revision 1.27 2001/12/30 20:39:13 mohor
86 // More than one character was stored in case of break. End of the break
87 // was not detected correctly.
89 // Revision 1.26 2001/12/20 13:28:27 mohor
90 // Missing declaration of rf_push_q fixed.
92 // Revision 1.25 2001/12/20 13:25:46 mohor
93 // rx push changed to be only one cycle wide.
95 // Revision 1.24 2001/12/19 08:03:34 mohor
98 // Revision 1.23 2001/12/19 07:33:54 mohor
99 // Synplicity was having troubles with the comment.
101 // Revision 1.22 2001/12/17 14:46:48 mohor
102 // overrun signal was moved to separate block because many sequential lsr
103 // reads were preventing data from being written to rx fifo.
104 // underrun signal was not used and was removed from the project.
106 // Revision 1.21 2001/12/13 10:31:16 mohor
107 // timeout irq must be set regardless of the rda irq (rda irq does not reset the
110 // Revision 1.20 2001/12/10 19:52:05 gorban
111 // Igor fixed break condition bugs
113 // Revision 1.19 2001/12/06 14:51:04 gorban
114 // Bug in LSR[0] is fixed.
115 // All WISHBONE signals are now sampled, so another wait-state is introduced on all transfers.
117 // Revision 1.18 2001/12/03 21:44:29 gorban
118 // Updated specification documentation.
119 // Added full 32-bit data bus interface, now as default.
120 // Address is 5-bit wide in 32-bit data bus mode.
121 // Added wb_sel_i input to the core. It's used in the 32-bit mode.
122 // Added debug interface with two 32-bit read-only registers in 32-bit mode.
123 // Bits 5 and 6 of LSR are now only cleared on TX FIFO write.
124 // My small test bench is modified to work with 32-bit mode.
126 // Revision 1.17 2001/11/28 19:36:39 gorban
127 // Fixed: timeout and break didn't pay attention to current data format when counting time
129 // Revision 1.16 2001/11/27 22:17:09 gorban
130 // Fixed bug that prevented synthesis in uart_receiver.v
132 // Revision 1.15 2001/11/26 21:38:54 gorban
134 // Break condition wasn't handled correctly at all.
135 // LSR bits could lose their values.
136 // LSR value after reset was wrong.
137 // Timing of THRE interrupt signal corrected.
138 // LSR bit 0 timing corrected.
140 // Revision 1.14 2001/11/10 12:43:21 gorban
141 // Logic Synthesis bugs fixed. Some other minor changes
143 // Revision 1.13 2001/11/08 14:54:23 mohor
144 // Comments in Slovene language deleted, few small fixes for better work of
145 // old tools. IRQs need to be fix.
147 // Revision 1.12 2001/11/07 17:51:52 gorban
148 // Heavily rewritten interrupt and LSR subsystems.
149 // Many bugs hopefully squashed.
151 // Revision 1.11 2001/10/31 15:19:22 gorban
152 // Fixes to break and timeout conditions
154 // Revision 1.10 2001/10/20 09:58:40 gorban
155 // Small synopsis fixes
157 // Revision 1.9 2001/08/24 21:01:12 mohor
158 // Things connected to parity changed.
159 // Clock devider changed.
161 // Revision 1.8 2001/08/23 16:05:05 mohor
162 // Stop bit bug fixed.
164 // WISHBONE read cycle bug fixed,
165 // OE indicator (Overrun Error) bug fixed.
166 // PE indicator (Parity Error) bug fixed.
167 // Register read bug fixed.
169 // Revision 1.6 2001/06/23 11:21:48 gorban
170 // DL made 16-bit long. Fixed transmission/reception bugs.
172 // Revision 1.5 2001/06/02 14:28:14 gorban
173 // Fixed receiver and transmitter. Major bug fixed.
175 // Revision 1.4 2001/05/31 20:08:01 gorban
176 // FIFO changes and other corrections.
178 // Revision 1.3 2001/05/27 17:37:49 gorban
179 // Fixed many bugs. Updated spec. Changed FIFO files structure. See CHANGES.txt file.
181 // Revision 1.2 2001/05/21 19:12:02 gorban
182 // Corrected some Linter messages.
184 // Revision 1.1 2001/05/17 18:34:18 gorban
185 // First 'stable' release. Should be sythesizable now. Also added new header.
187 // Revision 1.0 2001-05-17 21:27:11+02 jacob
192 `include "uart_defines.v"
194 module uart_receiver (clk, wb_rst_i, lcr, rf_pop, srx_pad_i, enable,
195 counter_t, rf_count, rf_data_out, rf_error_bit, rf_overrun, rx_reset, lsr_mask, rstate, rf_push_pulse);
206 output [9:0] counter_t;
207 output [`UART_FIFO_COUNTER_W-1:0] rf_count;
208 output [`UART_FIFO_REC_WIDTH-1:0] rf_data_out;
212 output rf_push_pulse;
215 reg [3:0] rcounter16;
216 reg [2:0] rbit_counter;
217 reg [7:0] rshift; // receiver shift register
218 reg rparity; // received parity
220 reg rframing_error; // framing error flag
222 reg [7:0] counter_b; // counts the 0 (low) signals
226 reg [`UART_FIFO_REC_WIDTH-1:0] rf_data_in;
227 wire [`UART_FIFO_REC_WIDTH-1:0] rf_data_out;
232 wire [`UART_FIFO_COUNTER_W-1:0] rf_count;
233 wire rf_error_bit; // an error (parity or framing) is inside the fifo
234 wire break_error = (counter_b == 0);
237 uart_rfifo #(`UART_FIFO_REC_WIDTH) fifo_rx(
239 .wb_rst_i( wb_rst_i ),
240 .data_in( rf_data_in ),
241 .data_out( rf_data_out ),
242 .push( rf_push_pulse ),
244 .overrun( rf_overrun ),
246 .error_bit( rf_error_bit ),
247 .fifo_reset( rx_reset ),
248 .reset_status(lsr_mask)
251 wire rcounter16_eq_7 = (rcounter16 == 4'd7);
252 wire rcounter16_eq_0 = (rcounter16 == 4'd0);
254 wire [3:0] rcounter16_minus_1 = rcounter16 - 3'd1;
256 parameter sr_idle = 4'd0;
257 parameter sr_rec_start = 4'd1;
258 parameter sr_rec_bit = 4'd2;
259 parameter sr_rec_parity = 4'd3;
260 parameter sr_rec_stop = 4'd4;
261 parameter sr_check_parity = 4'd5;
262 parameter sr_rec_prepare = 4'd6;
263 parameter sr_end_bit = 4'd7;
264 parameter sr_ca_lc_parity = 4'd8;
265 parameter sr_wait1 = 4'd9;
266 parameter sr_push = 4'd10;
269 always @(posedge clk or posedge wb_rst_i)
277 rframing_error <= 1'b0;
278 rparity_error <= 1'b0;
291 rcounter16 <= 4'b1110;
292 if (srx_pad_i==1'b0 & ~break_error) // detected a pulse (start bit?)
294 rstate <= sr_rec_start;
299 if (rcounter16_eq_7) // check the pulse
300 if (srx_pad_i==1'b1) // no start bit
302 else // start bit detected
303 rstate <= sr_rec_prepare;
304 rcounter16 <= rcounter16_minus_1;
307 case (lcr[/*`UART_LC_BITS*/1:0]) // number of bits in a word
308 2'b00 : rbit_counter <= 3'b100;
309 2'b01 : rbit_counter <= 3'b101;
310 2'b10 : rbit_counter <= 3'b110;
311 2'b11 : rbit_counter <= 3'b111;
315 rstate <= sr_rec_bit;
316 rcounter16 <= 4'b1110;
320 rstate <= sr_rec_prepare;
321 rcounter16 <= rcounter16_minus_1;
325 rstate <= sr_end_bit;
326 if (rcounter16_eq_7) // read the bit
327 case (lcr[/*`UART_LC_BITS*/1:0]) // number of bits in a word
328 2'b00 : rshift[4:0] <= {srx_pad_i, rshift[4:1]};
329 2'b01 : rshift[5:0] <= {srx_pad_i, rshift[5:1]};
330 2'b10 : rshift[6:0] <= {srx_pad_i, rshift[6:1]};
331 2'b11 : rshift[7:0] <= {srx_pad_i, rshift[7:1]};
333 rcounter16 <= rcounter16_minus_1;
336 if (rbit_counter==3'b0) // no more bits in word
337 if (lcr[`UART_LC_PE]) // choose state based on parity
338 rstate <= sr_rec_parity;
341 rstate <= sr_rec_stop;
342 rparity_error <= 1'b0; // no parity - no error :)
344 else // else we have more bits to read
346 rstate <= sr_rec_bit;
347 rbit_counter <= rbit_counter - 3'd1;
349 rcounter16 <= 4'b1110;
352 if (rcounter16_eq_7) // read the parity
354 rparity <= srx_pad_i;
355 rstate <= sr_ca_lc_parity;
357 rcounter16 <= rcounter16_minus_1;
359 sr_ca_lc_parity : begin // rcounter equals 6
360 rcounter16 <= rcounter16_minus_1;
361 rparity_xor <= ^{rshift,rparity}; // calculate parity on all incoming data
362 rstate <= sr_check_parity;
364 sr_check_parity: begin // rcounter equals 5
365 case ({lcr[`UART_LC_EP],lcr[`UART_LC_SP]})
366 2'b00: rparity_error <= rparity_xor == 0; // no error if parity 1
367 2'b01: rparity_error <= ~rparity; // parity should sticked to 1
368 2'b10: rparity_error <= rparity_xor == 1; // error if parity is odd
369 2'b11: rparity_error <= rparity; // parity should be sticked to 0
371 rcounter16 <= rcounter16_minus_1;
374 sr_wait1 : if (rcounter16_eq_0)
376 rstate <= sr_rec_stop;
377 rcounter16 <= 4'b1110;
380 rcounter16 <= rcounter16_minus_1;
382 if (rcounter16_eq_7) // read the parity
384 rframing_error <= !srx_pad_i; // no framing error if input is 1 (stop bit)
387 rcounter16 <= rcounter16_minus_1;
390 ///////////////////////////////////////
391 // $display($time, ": received: %b", rf_data_in);
392 if(srx_pad_i | break_error)
395 rf_data_in <= {8'b0, 3'b100}; // break input (empty character) to receiver FIFO
397 rf_data_in <= {rshift, 1'b0, rparity_error, rframing_error};
401 else if(~rframing_error) // There's always a framing before break_error -> wait for break or srx_pad_i
403 rf_data_in <= {rshift, 1'b0, rparity_error, rframing_error};
405 rcounter16 <= 4'b1110;
406 rstate <= sr_rec_start;
410 default : rstate <= sr_idle;
413 end // always of receiver
415 always @ (posedge clk or posedge wb_rst_i)
420 rf_push_q <= rf_push;
423 assign rf_push_pulse = rf_push & ~rf_push_q;
427 // Break condition detection.
428 // Works in conjuction with the receiver state machine
430 reg [9:0] toc_value; // value to be set to timeout counter
434 4'b0000 : toc_value = 447; // 7 bits
435 4'b0100 : toc_value = 479; // 7.5 bits
436 4'b0001, 4'b1000 : toc_value = 511; // 8 bits
437 4'b1100 : toc_value = 543; // 8.5 bits
438 4'b0010, 4'b0101, 4'b1001 : toc_value = 575; // 9 bits
439 4'b0011, 4'b0110, 4'b1010, 4'b1101 : toc_value = 639; // 10 bits
440 4'b0111, 4'b1011, 4'b1110 : toc_value = 703; // 11 bits
441 4'b1111 : toc_value = 767; // 12 bits
442 endcase // case(lcr[3:0])
444 wire [7:0] brc_value; // value to be set to break counter
445 assign brc_value = toc_value[9:2]; // the same as timeout but 1 insead of 4 character times
447 always @(posedge clk or posedge wb_rst_i)
453 counter_b <= brc_value; // character time length - 1
455 if(enable & counter_b != 8'b0) // only work on enable times break not reached.
456 counter_b <= counter_b - 8'd1; // decrement break counter
457 end // always of break condition detection
460 /// Timeout condition detection
461 reg [9:0] counter_t; // counts the timeout condition clocks
463 always @(posedge clk or posedge wb_rst_i)
466 counter_t <= 10'd639; // 10 bits for the default 8N1
468 if(rf_push_pulse || rf_pop || rf_count == 0) // counter is reset when RX FIFO is empty, accessed or above trigger level
469 counter_t <= toc_value;
471 if (enable && counter_t != 10'b0) // we don't want to underflow
472 counter_t <= counter_t - 10'd1;