OpenCores
URL https://opencores.org/ocsvn/uart16550/uart16550/trunk

Subversion Repositories uart16550

[/] [uart16550/] [trunk/] [rtl/] [verilog/] [uart_receiver.v] - Blame information for rev 67

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  uart_receiver.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 receiver 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 29 mohor
////      - Igor Mohor (igorm@opencores.org)                      ////
30 27 mohor
////                                                              ////
31
////  Created:        2001/05/12                                  ////
32
////  Last Updated:   2001/05/17                                  ////
33
////                  (See log for the revision history)          ////
34
////                                                              ////
35
////                                                              ////
36
//////////////////////////////////////////////////////////////////////
37
////                                                              ////
38 29 mohor
//// Copyright (C) 2000, 2001 Authors                             ////
39 27 mohor
////                                                              ////
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 67 mohor
// Revision 1.25  2001/12/20 13:25:46  mohor
67
// rx push changed to be only one cycle wide.
68
//
69 66 mohor
// Revision 1.24  2001/12/19 08:03:34  mohor
70
// Warnings cleared.
71
//
72 64 mohor
// Revision 1.23  2001/12/19 07:33:54  mohor
73
// Synplicity was having troubles with the comment.
74
//
75 63 mohor
// Revision 1.22  2001/12/17 14:46:48  mohor
76
// overrun signal was moved to separate block because many sequential lsr
77
// reads were preventing data from being written to rx fifo.
78
// underrun signal was not used and was removed from the project.
79
//
80 61 mohor
// Revision 1.21  2001/12/13 10:31:16  mohor
81
// timeout irq must be set regardless of the rda irq (rda irq does not reset the
82
// timeout counter).
83
//
84 57 mohor
// Revision 1.20  2001/12/10 19:52:05  gorban
85
// Igor fixed break condition bugs
86
//
87 51 gorban
// Revision 1.19  2001/12/06 14:51:04  gorban
88
// Bug in LSR[0] is fixed.
89
// All WISHBONE signals are now sampled, so another wait-state is introduced on all transfers.
90
//
91 50 gorban
// Revision 1.18  2001/12/03 21:44:29  gorban
92
// Updated specification documentation.
93
// Added full 32-bit data bus interface, now as default.
94
// Address is 5-bit wide in 32-bit data bus mode.
95
// Added wb_sel_i input to the core. It's used in the 32-bit mode.
96
// Added debug interface with two 32-bit read-only registers in 32-bit mode.
97
// Bits 5 and 6 of LSR are now only cleared on TX FIFO write.
98
// My small test bench is modified to work with 32-bit mode.
99
//
100 48 gorban
// Revision 1.17  2001/11/28 19:36:39  gorban
101
// Fixed: timeout and break didn't pay attention to current data format when counting time
102
//
103 47 gorban
// Revision 1.16  2001/11/27 22:17:09  gorban
104
// Fixed bug that prevented synthesis in uart_receiver.v
105
//
106 46 gorban
// Revision 1.15  2001/11/26 21:38:54  gorban
107
// Lots of fixes:
108
// Break condition wasn't handled correctly at all.
109
// LSR bits could lose their values.
110
// LSR value after reset was wrong.
111
// Timing of THRE interrupt signal corrected.
112
// LSR bit 0 timing corrected.
113
//
114 45 gorban
// Revision 1.14  2001/11/10 12:43:21  gorban
115 63 mohor
// Logic Synthesis bugs fixed. Some other minor changes
116 45 gorban
//
117 40 gorban
// Revision 1.13  2001/11/08 14:54:23  mohor
118
// Comments in Slovene language deleted, few small fixes for better work of
119
// old tools. IRQs need to be fix.
120
//
121 39 mohor
// Revision 1.12  2001/11/07 17:51:52  gorban
122
// Heavily rewritten interrupt and LSR subsystems.
123
// Many bugs hopefully squashed.
124
//
125 37 gorban
// Revision 1.11  2001/10/31 15:19:22  gorban
126
// Fixes to break and timeout conditions
127
//
128 35 gorban
// Revision 1.10  2001/10/20 09:58:40  gorban
129
// Small synopsis fixes
130
//
131 33 gorban
// Revision 1.9  2001/08/24 21:01:12  mohor
132
// Things connected to parity changed.
133
// Clock devider changed.
134
//
135 29 mohor
// Revision 1.8  2001/08/23 16:05:05  mohor
136
// Stop bit bug fixed.
137
// Parity bug fixed.
138
// WISHBONE read cycle bug fixed,
139
// OE indicator (Overrun Error) bug fixed.
140
// PE indicator (Parity Error) bug fixed.
141
// Register read bug fixed.
142
//
143 27 mohor
// Revision 1.6  2001/06/23 11:21:48  gorban
144
// DL made 16-bit long. Fixed transmission/reception bugs.
145
//
146
// Revision 1.5  2001/06/02 14:28:14  gorban
147
// Fixed receiver and transmitter. Major bug fixed.
148
//
149
// Revision 1.4  2001/05/31 20:08:01  gorban
150
// FIFO changes and other corrections.
151
//
152
// Revision 1.3  2001/05/27 17:37:49  gorban
153
// Fixed many bugs. Updated spec. Changed FIFO files structure. See CHANGES.txt file.
154
//
155
// Revision 1.2  2001/05/21 19:12:02  gorban
156
// Corrected some Linter messages.
157
//
158
// Revision 1.1  2001/05/17 18:34:18  gorban
159
// First 'stable' release. Should be sythesizable now. Also added new header.
160
//
161
// Revision 1.0  2001-05-17 21:27:11+02  jacob
162
// Initial revision
163
//
164
//
165
 
166 33 gorban
// synopsys translate_off
167 27 mohor
`include "timescale.v"
168 33 gorban
// synopsys translate_on
169
 
170 27 mohor
`include "uart_defines.v"
171
 
172 64 mohor
module uart_receiver (clk, wb_rst_i, lcr, rf_pop, srx_pad_i, enable,
173 66 mohor
        counter_t, rf_count, rf_data_out, rf_error_bit, rf_overrun, rx_reset, lsr_mask, rstate, rf_push_pulse);
174 27 mohor
 
175
input                           clk;
176
input                           wb_rst_i;
177 39 mohor
input   [7:0]    lcr;
178 27 mohor
input                           rf_pop;
179
input                           srx_pad_i;
180
input                           enable;
181
input                           rx_reset;
182 37 gorban
input       lsr_mask;
183 27 mohor
 
184 35 gorban
output  [9:0]                    counter_t;
185 27 mohor
output  [`UART_FIFO_COUNTER_W-1:0]       rf_count;
186
output  [`UART_FIFO_REC_WIDTH-1:0]       rf_data_out;
187
output                          rf_overrun;
188
output                          rf_error_bit;
189 48 gorban
output [3:0]             rstate;
190 66 mohor
output                          rf_push_pulse;
191 27 mohor
 
192
reg     [3:0]    rstate;
193
reg     [3:0]    rcounter16;
194
reg     [2:0]    rbit_counter;
195
reg     [7:0]    rshift;                 // receiver shift register
196
reg             rparity;                // received parity
197
reg             rparity_error;
198
reg             rframing_error;         // framing error flag
199
reg             rbit_in;
200
reg             rparity_xor;
201 45 gorban
reg     [7:0]    counter_b;      // counts the 0 (low) signals
202 67 mohor
reg   rf_push_q;
203 27 mohor
 
204
// RX FIFO signals
205
reg     [`UART_FIFO_REC_WIDTH-1:0]       rf_data_in;
206
wire    [`UART_FIFO_REC_WIDTH-1:0]       rf_data_out;
207 66 mohor
wire      rf_push_pulse;
208 27 mohor
reg                             rf_push;
209
wire                            rf_pop;
210
wire                            rf_overrun;
211
wire    [`UART_FIFO_COUNTER_W-1:0]       rf_count;
212
wire                            rf_error_bit; // an error (parity or framing) is inside the fifo
213 45 gorban
wire                            break_error = (counter_b == 0);
214 27 mohor
 
215
// RX FIFO instance
216
uart_fifo #(`UART_FIFO_REC_WIDTH) fifo_rx(
217
        .clk(           clk             ),
218
        .wb_rst_i(      wb_rst_i        ),
219
        .data_in(       rf_data_in      ),
220
        .data_out(      rf_data_out     ),
221 66 mohor
        .push(          rf_push_pulse           ),
222 27 mohor
        .pop(           rf_pop          ),
223
        .overrun(       rf_overrun      ),
224
        .count(         rf_count        ),
225
        .error_bit(     rf_error_bit    ),
226
        .fifo_reset(    rx_reset        ),
227 37 gorban
        .reset_status(lsr_mask)
228 27 mohor
);
229
 
230
wire            rcounter16_eq_7 = (rcounter16 == 4'd7);
231
wire            rcounter16_eq_0 = (rcounter16 == 4'd0);
232
wire            rcounter16_eq_1 = (rcounter16 == 4'd1);
233
 
234 39 mohor
wire [3:0] rcounter16_minus_1 = rcounter16 - 1'b1;
235 27 mohor
 
236
parameter  sr_idle                                      = 4'd0;
237
parameter  sr_rec_start                         = 4'd1;
238
parameter  sr_rec_bit                           = 4'd2;
239
parameter  sr_rec_parity                        = 4'd3;
240
parameter  sr_rec_stop                          = 4'd4;
241
parameter  sr_check_parity              = 4'd5;
242
parameter  sr_rec_prepare                       = 4'd6;
243
parameter  sr_end_bit                           = 4'd7;
244
parameter  sr_ca_lc_parity            = 4'd8;
245
parameter  sr_wait1                                     = 4'd9;
246
parameter  sr_push                                      = 4'd10;
247
parameter  sr_last                                      = 4'd11;
248
 
249 51 gorban
 
250 27 mohor
always @(posedge clk or posedge wb_rst_i)
251
begin
252
  if (wb_rst_i)
253
  begin
254
     rstate                     <= #1 sr_idle;
255
          rbit_in                               <= #1 1'b0;
256
          rcounter16                    <= #1 0;
257
          rbit_counter          <= #1 0;
258
          rparity_xor           <= #1 1'b0;
259
          rframing_error        <= #1 1'b0;
260
          rparity_error                 <= #1 1'b0;
261
          rparity                               <= #1 1'b0;
262
          rshift                                <= #1 0;
263
          rf_push                               <= #1 1'b0;
264
          rf_data_in                    <= #1 0;
265
  end
266
  else
267 51 gorban
//        if (break_error && rstate != sr_idle) // break condition met while receiver is not idle
268
//        begin
269
//                rstate                 <= #1 sr_idle;
270
//                rf_data_in     <= #1 {8'b0, 3'b100}; // break input (empty character) to receiver FIFO
271
//                rf_push                <= #1 1'b1;
272
//        end
273
//  else
274 27 mohor
  if (enable)
275
  begin
276
        case (rstate)
277 45 gorban
        sr_idle : begin
278
                        rf_push                           <= #1 1'b0;
279
                        rf_data_in        <= #1 0;
280
                        if (srx_pad_i==1'b0)   // detected a pulse (start bit?)
281 27 mohor
                        begin
282 45 gorban
                                rstate            <= #1 sr_rec_start;
283
                                rcounter16        <= #1 4'b1110;
284 27 mohor
                        end
285 45 gorban
                end
286 27 mohor
        sr_rec_start :  begin
287
                                if (rcounter16_eq_7)    // check the pulse
288
                                        if (srx_pad_i==1'b1)   // no start bit
289
                                                rstate <= #1 sr_idle;
290
                                        else            // start bit detected
291
                                                rstate <= #1 sr_rec_prepare;
292
                                rcounter16 <= #1 rcounter16_minus_1;
293
                        end
294
        sr_rec_prepare:begin
295
                                case (lcr[/*`UART_LC_BITS*/1:0])  // number of bits in a word
296
                                2'b00 : rbit_counter <= #1 3'b100;
297
                                2'b01 : rbit_counter <= #1 3'b101;
298
                                2'b10 : rbit_counter <= #1 3'b110;
299
                                2'b11 : rbit_counter <= #1 3'b111;
300
                                endcase
301
                                if (rcounter16_eq_0)
302
                                begin
303
                                        rstate          <= #1 sr_rec_bit;
304
                                        rcounter16      <= #1 4'b1110;
305
                                        rshift          <= #1 0;
306
                                end
307
                                else
308
                                        rstate <= #1 sr_rec_prepare;
309
                                rcounter16 <= #1 rcounter16_minus_1;
310
                        end
311
        sr_rec_bit :    begin
312
                                if (rcounter16_eq_0)
313
                                        rstate <= #1 sr_end_bit;
314
                                if (rcounter16_eq_7) // read the bit
315
                                        case (lcr[/*`UART_LC_BITS*/1:0])  // number of bits in a word
316
                                        2'b00 : rshift[4:0]  <= #1 {srx_pad_i, rshift[4:1]};
317
                                        2'b01 : rshift[5:0]  <= #1 {srx_pad_i, rshift[5:1]};
318
                                        2'b10 : rshift[6:0]  <= #1 {srx_pad_i, rshift[6:1]};
319
                                        2'b11 : rshift[7:0]  <= #1 {srx_pad_i, rshift[7:1]};
320
                                        endcase
321
                                rcounter16 <= #1 rcounter16_minus_1;
322
                        end
323
        sr_end_bit :   begin
324
                                if (rbit_counter==3'b0) // no more bits in word
325
                                        if (lcr[`UART_LC_PE]) // choose state based on parity
326
                                                rstate <= #1 sr_rec_parity;
327
                                        else
328
                                        begin
329
                                                rstate <= #1 sr_rec_stop;
330
                                                rparity_error <= #1 1'b0;  // no parity - no error :)
331
                                        end
332
                                else            // else we have more bits to read
333
                                begin
334
                                        rstate <= #1 sr_rec_bit;
335 39 mohor
                                        rbit_counter <= #1 rbit_counter - 1'b1;
336 27 mohor
                                end
337
                                rcounter16 <= #1 4'b1110;
338
                        end
339
        sr_rec_parity: begin
340
                                if (rcounter16_eq_7)    // read the parity
341
                                begin
342
                                        rparity <= #1 srx_pad_i;
343
                                        rstate <= #1 sr_ca_lc_parity;
344
                                end
345
                                rcounter16 <= #1 rcounter16_minus_1;
346
                        end
347
        sr_ca_lc_parity : begin    // rcounter equals 6
348
                                rcounter16  <= #1 rcounter16_minus_1;
349 37 gorban
                                rparity_xor <= #1 ^{rshift,rparity}; // calculate parity on all incoming data
350 27 mohor
                                rstate      <= #1 sr_check_parity;
351
                          end
352
        sr_check_parity: begin    // rcounter equals 5
353
                                case ({lcr[`UART_LC_EP],lcr[`UART_LC_SP]})
354 37 gorban
                                        2'b00: rparity_error <= #1  rparity_xor == 0;  // no error if parity 1
355
                                        2'b01: rparity_error <= #1 ~rparity;      // parity should sticked to 1
356
                                        2'b10: rparity_error <= #1  rparity_xor == 1;   // error if parity is odd
357
                                        2'b11: rparity_error <= #1  rparity;      // parity should be sticked to 0
358 27 mohor
                                endcase
359
                                rcounter16 <= #1 rcounter16_minus_1;
360
                                rstate <= #1 sr_wait1;
361
                          end
362
        sr_wait1 :      if (rcounter16_eq_0)
363
                        begin
364
                                rstate <= #1 sr_rec_stop;
365
                                rcounter16 <= #1 4'b1110;
366
                        end
367
                        else
368
                                rcounter16 <= #1 rcounter16_minus_1;
369
        sr_rec_stop :   begin
370
                                if (rcounter16_eq_7)    // read the parity
371
                                begin
372
                                        rframing_error <= #1 !srx_pad_i; // no framing error if input is 1 (stop bit)
373
                                        rstate <= #1 sr_push;
374
                                end
375
                                rcounter16 <= #1 rcounter16_minus_1;
376
                        end
377
        sr_push :       begin
378
///////////////////////////////////////
379
//                              $display($time, ": received: %b", rf_data_in);
380 51 gorban
        if(srx_pad_i | break_error)
381
          begin
382
            if(break_error)
383
                          rf_data_in    <= #1 {8'b0, 3'b100}; // break input (empty character) to receiver FIFO
384
            else
385
                                rf_data_in  <= #1 {rshift, 1'b0, rparity_error, rframing_error};
386
                  rf_push                 <= #1 1'b1;
387
                                rstate        <= #1 sr_last;
388
          end
389
 
390 27 mohor
                        end
391
        sr_last :       begin
392 51 gorban
//                              if (rcounter16_eq_1)
393
                                if (rcounter16_eq_1 & srx_pad_i)    // igor
394 27 mohor
                                        rstate <= #1 sr_idle;
395
                                rcounter16 <= #1 rcounter16_minus_1;
396
                                rf_push    <= #1 1'b0;
397
                        end
398
        default : rstate <= #1 sr_idle;
399
        endcase
400
  end  // if (enable)
401
end // always of receiver
402
 
403 66 mohor
always @ (posedge clk or posedge wb_rst_i)
404
begin
405
  if(wb_rst_i)
406
    rf_push_q <= 0;
407
  else
408
    rf_push_q <= #1 rf_push;
409
end
410
 
411
assign rf_push_pulse = rf_push & ~rf_push_q;
412
 
413
 
414 27 mohor
//
415
// Break condition detection.
416
// Works in conjuction with the receiver state machine
417
 
418 47 gorban
reg     [9:0]    toc_value; // value to be set to timeout counter
419
 
420
always @(lcr)
421
        case (lcr[3:0])
422
                4'b0000                                                                         : toc_value = 447; // 7 bits
423
                4'b0100                                                                         : toc_value = 479; // 7.5 bits
424
                4'b0001,        4'b1000                                                 : toc_value = 511; // 8 bits
425
                4'b1100                                                                         : toc_value = 543; // 8.5 bits
426
                4'b0010, 4'b0101, 4'b1001                               : toc_value = 575; // 9 bits
427
                4'b0011, 4'b0110, 4'b1010, 4'b1101      : toc_value = 639; // 10 bits
428
                4'b0111, 4'b1011, 4'b1110                               : toc_value = 703; // 11 bits
429
                4'b1111                                                                         : toc_value = 767; // 12 bits
430
        endcase // case(lcr[3:0])
431
 
432
wire [7:0]       brc_value; // value to be set to break counter
433
assign          brc_value = toc_value[9:2]; // the same as timeout but 1 insead of 4 character times
434
 
435 27 mohor
always @(posedge clk or posedge wb_rst_i)
436
begin
437
        if (wb_rst_i)
438 47 gorban
                counter_b <= #1 8'd159;
439 27 mohor
        else
440 51 gorban
//  if(lsr_mask)                          igor
441
//              counter_b <= #1 brc_value;
442
//  else
443 27 mohor
        if (enable)  // only work on enable times
444 39 mohor
                if (srx_pad_i)
445 47 gorban
                        counter_b <= #1 brc_value; // character time length - 1
446 27 mohor
                else
447 40 gorban
                        if (counter_b != 8'b0)            // break not reached it
448
                                counter_b <= #1 counter_b - 1;  // decrement break counter
449 27 mohor
end // always of break condition detection
450
 
451
///
452
/// Timeout condition detection
453 35 gorban
reg     [9:0]    counter_t;      // counts the timeout condition clocks
454 27 mohor
 
455
always @(posedge clk or posedge wb_rst_i)
456
begin
457
        if (wb_rst_i)
458 47 gorban
                counter_t <= #1 10'd639; // 10 bits for the default 8N1
459 27 mohor
        else
460 66 mohor
                if(rf_push_pulse || rf_pop || rf_count == 0) // counter is reset when RX FIFO is empty, accessed or above trigger level
461 47 gorban
                        counter_t <= #1 toc_value;
462 27 mohor
                else
463 39 mohor
                if (enable && counter_t != 10'b0)  // we don't want to underflow
464
                        counter_t <= #1 counter_t - 1;
465 27 mohor
end
466
 
467
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.