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

Subversion Repositories wbuart32

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

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

powered by: WebSVN 2.1.0

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