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

Subversion Repositories socgen

[/] [socgen/] [trunk/] [Projects/] [opencores.org/] [wishbone/] [ip/] [wb_uart16550/] [rtl/] [verilog/] [receiver] - Blame information for rev 131

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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