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

Subversion Repositories uart16550

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

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

powered by: WebSVN 2.1.0

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