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

Subversion Repositories uart16550

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

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

powered by: WebSVN 2.1.0

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