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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [rtl/] [rxuart.v] - Blame information for rev 17

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

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    rxuart.v
4
//
5 5 dgisselq
// Project:     wbuart32, a full featured UART with simulator
6 2 dgisselq
//
7
// Purpose:     Receive and decode inputs from a single UART line.
8
//
9
//
10
//      To interface with this module, connect it to your system clock,
11
//      pass it the 32 bit setup register (defined below) and the UART
12
//      input.  When data becomes available, the o_wr line will be asserted
13
//      for one clock cycle.  On parity or frame errors, the o_parity_err
14
//      or o_frame_err lines will be asserted.  Likewise, on a break 
15
//      condition, o_break will be asserted.  These lines are self clearing.
16
//
17
//      There is a synchronous reset line, logic high.
18
//
19
//      Now for the setup register.  The register is 32 bits, so that this
20
//      UART may be set up over a 32-bit bus.
21
//
22 14 dgisselq
//      i_setup[30]     True if we are not using hardware flow control.  This bit
23 9 dgisselq
//              is ignored within this module, as any receive hardware flow
24
//              control will need to be implemented elsewhere.
25
//
26 2 dgisselq
//      i_setup[29:28]  Indicates the number of data bits per word.  This will
27 9 dgisselq
//              either be 2'b00 for an 8-bit word, 2'b01 for a 7-bit word, 2'b10
28
//              for a six bit word, or 2'b11 for a five bit word.
29 2 dgisselq
//
30
//      i_setup[27]     Indicates whether or not to use one or two stop bits.
31
//              Set this to one to expect two stop bits, zero for one.
32
//
33
//      i_setup[26]     Indicates whether or not a parity bit exists.  Set this
34
//              to 1'b1 to include parity.
35
//
36
//      i_setup[25]     Indicates whether or not the parity bit is fixed.  Set
37
//              to 1'b1 to include a fixed bit of parity, 1'b0 to allow the
38
//              parity to be set based upon data.  (Both assume the parity
39
//              enable value is set.)
40
//
41
//      i_setup[24]     This bit is ignored if parity is not used.  Otherwise,
42
//              in the case of a fixed parity bit, this bit indicates whether
43
//              mark (1'b1) or space (1'b0) parity is used.  Likewise if the
44
//              parity is not fixed, a 1'b1 selects even parity, and 1'b0
45
//              selects odd.
46
//
47
//      i_setup[23:0]   Indicates the speed of the UART in terms of clocks.
48
//              So, for example, if you have a 200 MHz clock and wish to
49
//              run your UART at 9600 baud, you would take 200 MHz and divide
50
//              by 9600 to set this value to 24'd20834.  Likewise if you wished
51
//              to run this serial port at 115200 baud from a 200 MHz clock,
52
//              you would set the value to 24'd1736
53
//
54
//      Thus, to set the UART for the common setting of an 8-bit word, 
55
//      one stop bit, no parity, and 115200 baud over a 200 MHz clock, you
56
//      would want to set the setup value to:
57
//
58
//      32'h0006c8              // For 115,200 baud, 8 bit, no parity
59
//      32'h005161              // For 9600 baud, 8 bit, no parity
60
//      
61
//
62
//
63
// Creator:     Dan Gisselquist, Ph.D.
64
//              Gisselquist Technology, LLC
65
//
66
////////////////////////////////////////////////////////////////////////////////
67
//
68
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
69
//
70
// This program is free software (firmware): you can redistribute it and/or
71
// modify it under the terms of  the GNU General Public License as published
72
// by the Free Software Foundation, either version 3 of the License, or (at
73
// your option) any later version.
74
//
75
// This program is distributed in the hope that it will be useful, but WITHOUT
76
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
77
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
78
// for more details.
79
//
80
// You should have received a copy of the GNU General Public License along
81 9 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
82 2 dgisselq
// target there if the PDF file isn't present.)  If not, see
83
// <http://www.gnu.org/licenses/> for a copy.
84
//
85
// License:     GPL, v3, as defined and found on www.gnu.org,
86
//              http://www.gnu.org/licenses/gpl.html
87
//
88
//
89
////////////////////////////////////////////////////////////////////////////////
90
//
91
//
92 17 dgisselq
`default_nettype        none
93
//
94 2 dgisselq
// States: (@ baud counter == 0)
95
//      0        First bit arrives
96
//      ..7     Bits arrive
97
//      8       Stop bit (x1)
98
//      9       Stop bit (x2)
99 9 dgisselq
//      c       break condition
100 2 dgisselq
//      d       Waiting for the channel to go high
101
//      e       Waiting for the reset to complete
102
//      f       Idle state
103
`define RXU_BIT_ZERO            4'h0
104
`define RXU_BIT_ONE             4'h1
105
`define RXU_BIT_TWO             4'h2
106
`define RXU_BIT_THREE           4'h3
107
`define RXU_BIT_FOUR            4'h4
108
`define RXU_BIT_FIVE            4'h5
109
`define RXU_BIT_SIX             4'h6
110
`define RXU_BIT_SEVEN           4'h7
111
`define RXU_PARITY              4'h8
112
`define RXU_STOP                4'h9
113
`define RXU_SECOND_STOP         4'ha
114
// Unused 4'hb
115
// Unused 4'hc
116
`define RXU_BREAK               4'hd
117
`define RXU_RESET_IDLE          4'he
118
`define RXU_IDLE                4'hf
119
 
120 5 dgisselq
module rxuart(i_clk, i_reset, i_setup, i_uart_rx, o_wr, o_data, o_break,
121 2 dgisselq
                        o_parity_err, o_frame_err, o_ck_uart);
122 9 dgisselq
        parameter [30:0] INITIAL_SETUP = 31'd868;
123 2 dgisselq
        // 8 data bits, no parity, (at least 1) stop bit
124 17 dgisselq
        input   wire            i_clk, i_reset;
125
        input   wire    [30:0]   i_setup;
126
        input   wire            i_uart_rx;
127 2 dgisselq
        output  reg             o_wr;
128
        output  reg     [7:0]    o_data;
129
        output  reg             o_break;
130
        output  reg             o_parity_err, o_frame_err;
131
        output  wire            o_ck_uart;
132
 
133
 
134
        wire    [27:0]   clocks_per_baud, break_condition, half_baud;
135
        wire    [1:0]    data_bits;
136
        wire            use_parity, parity_even, dblstop, fixd_parity;
137
        reg     [29:0]   r_setup;
138 7 dgisselq
        reg     [3:0]    state;
139 5 dgisselq
 
140 2 dgisselq
        assign  clocks_per_baud = { 4'h0, r_setup[23:0] };
141 9 dgisselq
        // assign hw_flow_control = !r_setup[30];
142 2 dgisselq
        assign  data_bits   = r_setup[29:28];
143
        assign  dblstop     = r_setup[27];
144
        assign  use_parity  = r_setup[26];
145
        assign  fixd_parity = r_setup[25];
146
        assign  parity_even = r_setup[24];
147
        assign  break_condition = { r_setup[23:0], 4'h0 };
148 5 dgisselq
        assign  half_baud = { 5'h00, r_setup[23:1] }-28'h1;
149
        reg     [27:0]   baud_counter;
150
        reg             zero_baud_counter;
151 2 dgisselq
 
152 5 dgisselq
 
153
        // Since this is an asynchronous receiver, we need to register our
154
        // input a couple of clocks over to avoid any problems with 
155
        // metastability.  We do that here, and then ignore all but the
156
        // ck_uart wire.
157 2 dgisselq
        reg     q_uart, qq_uart, ck_uart;
158
        initial q_uart  = 1'b0;
159
        initial qq_uart = 1'b0;
160
        initial ck_uart = 1'b0;
161
        always @(posedge i_clk)
162
        begin
163 5 dgisselq
                q_uart <= i_uart_rx;
164 2 dgisselq
                qq_uart <= q_uart;
165
                ck_uart <= qq_uart;
166
        end
167 5 dgisselq
 
168
        // In case anyone else wants this clocked, stabilized value, we
169
        // offer it on our output.
170 2 dgisselq
        assign  o_ck_uart = ck_uart;
171
 
172 5 dgisselq
        // Keep track of the number of clocks since the last change.
173
        //
174
        // This is used to determine if we are in either a break or an idle
175
        // condition, as discussed further below.
176 2 dgisselq
        reg     [27:0]   chg_counter;
177
        initial chg_counter = 28'h00;
178
        always @(posedge i_clk)
179
                if (i_reset)
180
                        chg_counter <= 28'h00;
181
                else if (qq_uart != ck_uart)
182
                        chg_counter <= 28'h00;
183
                else if (chg_counter < break_condition)
184
                        chg_counter <= chg_counter + 1;
185
 
186 5 dgisselq
        // Are we in a break condition?
187
        //
188
        // A break condition exists if the line is held low for longer than
189
        // a data word.  Hence, we keep track of when the last change occurred.
190
        // If it was more than break_condition clocks ago, and the current input
191
        // value is a 0, then we're in a break--and nothing can be read until
192
        // the line idles again.
193 2 dgisselq
        initial o_break    = 1'b0;
194
        always @(posedge i_clk)
195
                o_break <= ((chg_counter >= break_condition)&&(~ck_uart))? 1'b1:1'b0;
196 5 dgisselq
 
197
        // Are we between characters?
198
        //
199
        // The opposite of a break condition is where the line is held high
200
        // for more clocks than would be in a character.  When this happens,
201
        // we know we have synchronization--otherwise, we might be sampling
202
        // from within a data word.
203
        //
204
        // This logic is used later to hold the RXUART in a reset condition
205
        // until we know we are between data words.  At that point, we should
206
        // be able to hold on to our synchronization.
207
        reg     line_synch;
208
        initial line_synch = 1'b0;
209 2 dgisselq
        always @(posedge i_clk)
210
                line_synch <= ((chg_counter >= break_condition)&&(ck_uart));
211
 
212 5 dgisselq
        // Are we in the middle of a baud iterval?  Specifically, are we
213
        // in the middle of a start bit?  Set this to high if so.  We'll use
214
        // this within our state machine to transition out of the IDLE
215
        // state.
216
        reg     half_baud_time;
217
        initial half_baud_time = 0;
218
        always @(posedge i_clk)
219
                half_baud_time <= (~ck_uart)&&(chg_counter >= half_baud);
220
 
221
 
222
        // Allow our controlling processor to change our setup at any time
223
        // outside of receiving/processing a character.
224 9 dgisselq
        initial r_setup     = INITIAL_SETUP[29:0];
225 5 dgisselq
        always @(posedge i_clk)
226
                if (state >= `RXU_RESET_IDLE)
227 9 dgisselq
                        r_setup <= i_setup[29:0];
228 5 dgisselq
 
229
 
230
        // Our monster state machine.  YIKES!
231
        //
232
        // Yeah, this may be more complicated than it needs to be.  The basic
233
        // progression is:
234
        //      RESET -> RESET_IDLE -> (when line is idle) -> IDLE
235
        //      IDLE -> bit 0 -> bit 1 -> bit_{ndatabits} -> 
236
        //              (optional) PARITY -> STOP -> (optional) SECOND_STOP
237
        //              -> IDLE
238
        //      ANY -> (on break) BREAK -> IDLE
239
        //
240
        // There are 16 states, although all are not used.  These are listed
241
        // at the top of this file.
242
        //
243
        //      Logic inputs (12):      (I've tried to minimize this number)
244
        //              state   (4)
245
        //              i_reset
246
        //              line_synch
247
        //              o_break
248
        //              ckuart
249
        //              half_baud_time
250
        //              zero_baud_counter
251
        //              use_parity
252
        //              dblstop
253
        //      Logic outputs (4):
254
        //              state
255
        //
256 2 dgisselq
        initial state = `RXU_RESET_IDLE;
257
        always @(posedge i_clk)
258
        begin
259
                if (i_reset)
260
                        state <= `RXU_RESET_IDLE;
261 5 dgisselq
                else if (state == `RXU_RESET_IDLE)
262 2 dgisselq
                begin
263
                        if (line_synch)
264
                                // Goto idle state from a reset
265
                                state <= `RXU_IDLE;
266
                        else // Otherwise, stay in this condition 'til reset
267
                                state <= `RXU_RESET_IDLE;
268
                end else if (o_break)
269
                begin // We are in a break condition
270
                        state <= `RXU_BREAK;
271
                end else if (state == `RXU_BREAK)
272
                begin // Goto idle state following return ck_uart going high
273
                        if (ck_uart)
274
                                state <= `RXU_IDLE;
275
                        else
276
                                state <= `RXU_BREAK;
277
                end else if (state == `RXU_IDLE)
278
                begin // Idle state, independent of baud counter
279
                        if ((~ck_uart)&&(half_baud_time))
280
                        begin
281
                                // We are in the center of a valid start bit
282
                                case (data_bits)
283
                                2'b00: state <= `RXU_BIT_ZERO;
284
                                2'b01: state <= `RXU_BIT_ONE;
285
                                2'b10: state <= `RXU_BIT_TWO;
286
                                2'b11: state <= `RXU_BIT_THREE;
287
                                endcase
288
                        end else // Otherwise, just stay here in idle
289
                                state <= `RXU_IDLE;
290
                end else if (zero_baud_counter)
291
                begin
292
                        if (state < `RXU_BIT_SEVEN)
293
                                // Data arrives least significant bit first.
294
                                // By the time this is clocked in, it's what
295
                                // you'll have.
296
                                state <= state + 1;
297 5 dgisselq
                        else if (state == `RXU_BIT_SEVEN)
298 2 dgisselq
                                state <= (use_parity) ? `RXU_PARITY:`RXU_STOP;
299 5 dgisselq
                        else if (state == `RXU_PARITY)
300 2 dgisselq
                                state <= `RXU_STOP;
301 5 dgisselq
                        else if (state == `RXU_STOP)
302 2 dgisselq
                        begin // Stop (or parity) bit(s)
303 5 dgisselq
                                if (~ck_uart) // On frame error, wait 4 ch idle
304 2 dgisselq
                                        state <= `RXU_RESET_IDLE;
305
                                else if (dblstop)
306
                                        state <= `RXU_SECOND_STOP;
307
                                else
308
                                        state <= `RXU_IDLE;
309
                        end else // state must equal RX_SECOND_STOP
310
                        begin
311 5 dgisselq
                                if (~ck_uart) // On frame error, wait 4 ch idle
312 2 dgisselq
                                        state <= `RXU_RESET_IDLE;
313 5 dgisselq
                                else
314 2 dgisselq
                                        state <= `RXU_IDLE;
315
                        end
316
                end
317
        end
318
 
319 5 dgisselq
        // Data bit capture logic.
320
        //
321
        // This is drastically simplified from the state machine above, based
322
        // upon: 1) it doesn't matter what it is until the end of a captured
323
        // byte, and 2) the data register will flush itself of any invalid
324
        // data in all other cases.  Hence, let's keep it real simple.
325
        // The only trick, though, is that if we have parity, then the data
326
        // register needs to be held through that state without getting
327
        // updated.
328
        reg     [7:0]    data_reg;
329
        always @(posedge i_clk)
330
                if ((zero_baud_counter)&&(state != `RXU_PARITY))
331
                        data_reg <= { ck_uart, data_reg[7:1] };
332
 
333
        // Parity calculation logic
334
        //
335
        // As with the data capture logic, all that must be known about this
336
        // bit is that it is the exclusive-OR of all bits prior.  The first
337
        // of those will follow idle, so we set ourselves to zero on idle.
338
        // Then, as we walk through the states of a bit, all will adjust this
339
        // value up until the parity bit, where the value will be read.  Setting
340
        // it then or after will be irrelevant, so ... this should be good
341
        // and simplified.  Note--we don't need to adjust this on reset either,
342
        // since the reset state will lead to the idle state where we'll be
343
        // reset before any transmission takes place.
344
        reg             calc_parity;
345
        always @(posedge i_clk)
346
                if (state == `RXU_IDLE)
347
                        calc_parity <= 0;
348
                else if (zero_baud_counter)
349
                        calc_parity <= calc_parity ^ ck_uart;
350
 
351
        // Parity error logic
352
        //
353
        // Set during the parity bit interval, read during the last stop bit
354
        // interval, cleared on BREAK, RESET_IDLE, or IDLE states.
355
        initial o_parity_err = 1'b0;
356
        always @(posedge i_clk)
357
                if ((zero_baud_counter)&&(state == `RXU_PARITY))
358
                begin
359
                        if (fixd_parity)
360
                                // Fixed parity bit--independent of any dat
361
                                // value.
362
                                o_parity_err <= (ck_uart ^ parity_even);
363
                        else if (parity_even)
364
                                // Parity even: The XOR of all bits including
365
                                // the parity bit must be zero.
366
                                o_parity_err <= (calc_parity != ck_uart);
367
                        else
368
                                // Parity odd: the parity bit must equal the
369
                                // XOR of all the data bits.
370
                                o_parity_err <= (calc_parity == ck_uart);
371
                end else if (state >= `RXU_BREAK)
372
                        o_parity_err <= 1'b0;
373
 
374
        // Frame error determination
375
        //
376
        // For the purpose of this controller, a frame error is defined as a
377
        // stop bit (or second stop bit, if so enabled) not being high midway
378
        // through the stop baud interval.   The frame error value is
379
        // immediately read, so we can clear it under all other circumstances.
380
        // Specifically, we want it clear in RXU_BREAK, RXU_RESET_IDLE, and
381
        // most importantly in RXU_IDLE.
382
        initial o_frame_err  = 1'b0;
383
        always @(posedge i_clk)
384
                if ((zero_baud_counter)&&((state == `RXU_STOP)
385
                                                ||(state == `RXU_SECOND_STOP)))
386
                        o_frame_err <= (o_frame_err)||(~ck_uart);
387
                else if ((zero_baud_counter)||(state >= `RXU_BREAK))
388
                        o_frame_err <= 1'b0;
389
 
390
        // Our data bit logic doesn't need nearly the complexity of all that
391
        // work above.  Indeed, we only need to know if we are at the end of
392
        // a stop bit, in which case we copy the data_reg into our output
393
        // data register, o_data.
394
        //
395
        // We would also set o_wr to be true when this is the case, but ... we
396
        // won't know if there is a frame error on the second stop bit for 
397
        // another baud interval yet.  So, instead, we set up the logic so that
398
        // we know on the next zero baud counter that we can write out.  That's
399
        // the purpose of pre_wr.
400
        initial o_data = 8'h00;
401
        reg     pre_wr;
402
        initial pre_wr = 1'b0;
403
        always @(posedge i_clk)
404
                if (i_reset)
405
                begin
406
                        pre_wr <= 1'b0;
407
                        o_data <= 8'h00;
408
                end else if ((zero_baud_counter)&&(state == `RXU_STOP))
409
                begin
410
                        pre_wr <= 1'b1;
411
                        case (data_bits)
412
                        2'b00: o_data <= data_reg;
413
                        2'b01: o_data <= { 1'b0, data_reg[7:1] };
414
                        2'b10: o_data <= { 2'b0, data_reg[7:2] };
415
                        2'b11: o_data <= { 3'b0, data_reg[7:3] };
416
                        endcase
417
                end else if ((zero_baud_counter)||(state == `RXU_IDLE))
418
                        pre_wr <= 1'b0;
419
 
420
        // Create an output strobe, true for one clock only, once we know
421
        // all we need to know.  o_data will be set on the last baud interval,
422
        // o_parity_err on the last parity baud interval (if it existed,
423
        // cleared otherwise, so ... we should be good to go here.)
424
        initial o_wr   = 1'b0;
425
        always @(posedge i_clk)
426
                if ((zero_baud_counter)||(state == `RXU_IDLE))
427
                        o_wr <= (pre_wr)&&(!i_reset);
428
                else
429
                        o_wr <= 1'b0;
430
 
431
        // The baud counter
432
        //
433
        // This is used as a "clock divider" if you will, but the clock needs
434
        // to be reset before any byte can be decoded.  In all other respects,
435
        // we set ourselves up for clocks_per_baud counts between baud
436
        // intervals.
437
        always @(posedge i_clk)
438
                if (i_reset)
439
                        baud_counter <= clocks_per_baud-28'h01;
440
                else if (zero_baud_counter)
441
                        baud_counter <= clocks_per_baud-28'h01;
442
                else case(state)
443
                        `RXU_RESET_IDLE:baud_counter <= clocks_per_baud-28'h01;
444
                        `RXU_BREAK:     baud_counter <= clocks_per_baud-28'h01;
445
                        `RXU_IDLE:      baud_counter <= clocks_per_baud-28'h01;
446
                        default:        baud_counter <= baud_counter-28'h01;
447
                endcase
448
 
449
        // zero_baud_counter
450
        //
451
        // Rather than testing whether or not (baud_counter == 0) within our
452
        // (already too complicated) state transition tables, we use
453
        // zero_baud_counter to pre-charge that test on the clock
454
        // before--cleaning up some otherwise difficult timing dependencies.
455 2 dgisselq
        initial zero_baud_counter = 1'b0;
456
        always @(posedge i_clk)
457 5 dgisselq
                if (state == `RXU_IDLE)
458
                        zero_baud_counter <= 1'b0;
459
                else
460 2 dgisselq
                zero_baud_counter <= (baud_counter == 28'h01);
461
 
462
 
463
endmodule
464
 
465
 

powered by: WebSVN 2.1.0

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