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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [rtl/] [wbuart.v] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    wbuart.v
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     Unlilke wbuart-insert.v, this is a full blown wishbone core
8
//              with integrated FIFO support to support the UART transmitter
9
//      and receiver found within here.  As a result, it's usage may be
10
//      heavier on the bus than the insert, but it may also be more useful.
11
//
12
// Creator:     Dan Gisselquist, Ph.D.
13
//              Gisselquist Technology, LLC
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
//
17 26 dgisselq
// Copyright (C) 2015-2019, Gisselquist Technology, LLC
18 5 dgisselq
//
19
// This program is free software (firmware): you can redistribute it and/or
20
// modify it under the terms of  the GNU General Public License as published
21
// by the Free Software Foundation, either version 3 of the License, or (at
22
// your option) any later version.
23
//
24
// This program is distributed in the hope that it will be useful, but WITHOUT
25
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
26
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
// for more details.
28
//
29
// You should have received a copy of the GNU General Public License along
30 9 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
31 5 dgisselq
// target there if the PDF file isn't present.)  If not, see
32
// <http://www.gnu.org/licenses/> for a copy.
33
//
34
// License:     GPL, v3, as defined and found on www.gnu.org,
35
//              http://www.gnu.org/licenses/gpl.html
36
//
37
//
38
////////////////////////////////////////////////////////////////////////////////
39
//
40
//
41 17 dgisselq
`default_nettype        none
42
//
43 5 dgisselq
`define UART_SETUP      2'b00
44
`define UART_FIFO       2'b01
45
`define UART_RXREG      2'b10
46
`define UART_TXREG      2'b11
47 26 dgisselq
//
48
// `define      USE_LITE_UART
49 5 dgisselq
module  wbuart(i_clk, i_rst,
50
                //
51
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
52 9 dgisselq
                        o_wb_ack, o_wb_stall, o_wb_data,
53 5 dgisselq
                //
54 15 dgisselq
                i_uart_rx, o_uart_tx, i_cts_n, o_rts_n,
55 5 dgisselq
                //
56
                o_uart_rx_int, o_uart_tx_int,
57
                o_uart_rxfifo_int, o_uart_txfifo_int);
58 9 dgisselq
        parameter [30:0] INITIAL_SETUP = 31'd25; // 4MB 8N1, when using 100MHz clock
59
        parameter [3:0]  LGFLEN = 4;
60
        parameter [0:0]   HARDWARE_FLOW_CONTROL_PRESENT = 1'b1;
61
        // Perform a simple/quick bounds check on the log FIFO length, to make
62
        // sure its within the bounds we can support with our current
63
        // interface.
64
        localparam [3:0] LCLLGFLEN = (LGFLEN > 4'ha)? 4'ha
65
                                        : ((LGFLEN < 4'h2) ? 4'h2 : LGFLEN);
66 5 dgisselq
        //
67 17 dgisselq
        input   wire            i_clk, i_rst;
68 5 dgisselq
        // Wishbone inputs
69 18 dgisselq
        input   wire            i_wb_cyc;       // We ignore CYC for efficiency
70
        input   wire            i_wb_stb, i_wb_we;
71 17 dgisselq
        input   wire    [1:0]    i_wb_addr;
72 18 dgisselq
        input   wire    [31:0]   i_wb_data;      // and only use 30 lines here
73 9 dgisselq
        output  reg             o_wb_ack;
74 5 dgisselq
        output  wire            o_wb_stall;
75
        output  reg     [31:0]   o_wb_data;
76
        //
77 17 dgisselq
        input   wire            i_uart_rx;
78 5 dgisselq
        output  wire            o_uart_tx;
79 9 dgisselq
        // RTS is used for hardware flow control.  According to Wikipedia, it
80
        // should probably be renamed RTR for "ready to receive".  It tell us
81
        // whether or not the receiving hardware is ready to accept another
82
        // byte.  If low, the transmitter will pause.
83
        //
84 15 dgisselq
        // If you don't wish to use hardware flow control, just set i_cts_n to
85
        // 1'b0 and let the optimizer simply remove this logic.
86 17 dgisselq
        input   wire            i_cts_n;
87 9 dgisselq
        // CTS is the "Clear-to-send" signal.  We set it anytime our FIFO
88
        // isn't full.  Feel free to ignore this output if you do not wish to
89
        // use flow control.
90 15 dgisselq
        output  reg             o_rts_n;
91 5 dgisselq
        output  wire            o_uart_rx_int, o_uart_tx_int,
92
                                o_uart_rxfifo_int, o_uart_txfifo_int;
93
 
94
        wire    tx_busy;
95
 
96
        //
97
        // The UART setup parameters: bits per byte, stop bits, parity, and
98
        // baud rate are all captured within this uart_setup register.
99
        //
100 9 dgisselq
        reg     [30:0]   uart_setup;
101 18 dgisselq
        initial uart_setup = INITIAL_SETUP
102
                | ((HARDWARE_FLOW_CONTROL_PRESENT==1'b0)? 31'h40000000 : 0);
103 5 dgisselq
        always @(posedge i_clk)
104
                // Under wishbone rules, a write takes place any time i_wb_stb
105
                // is high.  If that's the case, and if the write was to the
106
                // setup address, then set us up for the new parameters.
107
                if ((i_wb_stb)&&(i_wb_addr == `UART_SETUP)&&(i_wb_we))
108 9 dgisselq
                        uart_setup <= {
109
                                (i_wb_data[30])
110
                                        ||(!HARDWARE_FLOW_CONTROL_PRESENT),
111
                                i_wb_data[29:0] };
112 5 dgisselq
 
113 9 dgisselq
        /////////////////////////////////////////
114 5 dgisselq
        //
115
        //
116 9 dgisselq
        // First, the UART receiver
117
        //
118
        //
119
        /////////////////////////////////////////
120 5 dgisselq
 
121
        // First the wires/registers this receiver depends upon
122
        wire            rx_stb, rx_break, rx_perr, rx_ferr, ck_uart;
123
        wire    [7:0]    rx_uart_data;
124
        reg             rx_uart_reset;
125
 
126
        // Here's our UART receiver.  Basically, it accepts our setup wires, 
127
        // the UART input, a clock, and a reset line, and produces outputs:
128 15 dgisselq
        // a stb (true when new data is ready), and an 8-bit data out value
129
        // valid when stb is high.
130
`ifdef  USE_LITE_UART
131
        rxuartlite      #(INITIAL_SETUP[23:0])
132 26 dgisselq
                rx(i_clk, i_uart_rx, rx_stb, rx_uart_data);
133 15 dgisselq
        assign  rx_break = 1'b0;
134
        assign  rx_perr  = 1'b0;
135
        assign  rx_ferr  = 1'b0;
136
        assign  ck_uart  = 1'b0;
137
`else
138
        // The full receiver also produces a break value (true during a break
139
        // cond.), and parity/framing error flags--also valid when stb is true.
140 5 dgisselq
        rxuart  #(INITIAL_SETUP) rx(i_clk, (i_rst)||(rx_uart_reset),
141
                        uart_setup, i_uart_rx,
142
                        rx_stb, rx_uart_data, rx_break,
143
                        rx_perr, rx_ferr, ck_uart);
144 15 dgisselq
        // The real trick is ... now that we have this extra data, what do we do
145 5 dgisselq
        // with it?
146 15 dgisselq
`endif
147 5 dgisselq
 
148
 
149
        // We place it into a receiver FIFO.
150
        //
151
        // Here's the declarations for the wires it needs.
152
        wire            rx_empty_n, rx_fifo_err;
153
        wire    [7:0]    rxf_wb_data;
154
        wire    [15:0]   rxf_status;
155
        reg             rxf_wb_read;
156
        //
157
        // And here's the FIFO proper.
158
        //
159
        // Note that the FIFO will be cleared upon any reset: either if there's
160
        // a UART break condition on the line, the receiver is in reset, or an
161
        // external reset is issued.
162
        //
163
        // The FIFO accepts strobe and data from the receiver.
164
        // We issue another wire to it (rxf_wb_read), true when we wish to read
165
        // from the FIFO, and we get our data in rxf_wb_data.  The FIFO outputs
166
        // four status-type values: 1) is it non-empty, 2) is the FIFO over half
167
        // full, 3) a 16-bit status register, containing info regarding how full
168
        // the FIFO truly is, and 4) an error indicator.
169 9 dgisselq
        ufifo   #(.LGFLEN(LCLLGFLEN), .RXFIFO(1))
170 5 dgisselq
                rxfifo(i_clk, (i_rst)||(rx_break)||(rx_uart_reset),
171
                        rx_stb, rx_uart_data,
172 9 dgisselq
                        rx_empty_n,
173 5 dgisselq
                        rxf_wb_read, rxf_wb_data,
174
                        rxf_status, rx_fifo_err);
175 9 dgisselq
        assign  o_uart_rxfifo_int = rxf_status[1];
176 5 dgisselq
 
177
        // We produce four interrupts.  One of the receive interrupts indicates
178
        // whether or not the receive FIFO is non-empty.  This should wake up
179
        // the CPU.
180 9 dgisselq
        assign  o_uart_rx_int = rxf_status[0];
181 5 dgisselq
 
182 9 dgisselq
        // The clear to send line, which may be ignored, but which we set here
183
        // to be true any time the FIFO has fewer than N-2 items in it.
184 21 dgisselq
        // Why not N-1?  Because at N-1 we are totally full, but already so full
185 9 dgisselq
        // that if the transmit end starts sending we won't have a location to
186
        // receive it.  (Transmit might've started on the next character by the
187 18 dgisselq
        // time we set this--thus we need to set it to one, one character before
188
        // necessary).
189
        wire    [(LCLLGFLEN-1):0]        check_cutoff;
190
        assign  check_cutoff = -3;
191 9 dgisselq
        always @(posedge i_clk)
192 18 dgisselq
                o_rts_n <= ((HARDWARE_FLOW_CONTROL_PRESENT)
193 15 dgisselq
                        &&(!uart_setup[30])
194 18 dgisselq
                        &&(rxf_status[(LCLLGFLEN+1):2] > check_cutoff));
195 9 dgisselq
 
196 5 dgisselq
        // If the bus requests that we read from the receive FIFO, we need to
197
        // tell this to the receive FIFO.  Note that because we are using a 
198
        // clock here, the output from the receive FIFO will necessarily be
199
        // delayed by an extra clock.
200
        initial rxf_wb_read = 1'b0;
201
        always @(posedge i_clk)
202
                rxf_wb_read <= (i_wb_stb)&&(i_wb_addr[1:0]==`UART_RXREG)
203
                                &&(!i_wb_we);
204
 
205
        // Now, let's deal with those RX UART errors: both the parity and frame
206
        // errors.  As you may recall, these are valid only when rx_stb is
207
        // valid, so we need to hold on to them until the user reads them via
208
        // a UART read request..
209
        reg     r_rx_perr, r_rx_ferr;
210
        initial r_rx_perr = 1'b0;
211
        initial r_rx_ferr = 1'b0;
212
        always @(posedge i_clk)
213
                if ((rx_uart_reset)||(rx_break))
214
                begin
215
                        // Clear the error
216
                        r_rx_perr <= 1'b0;
217
                        r_rx_ferr <= 1'b0;
218
                end else if ((i_wb_stb)
219
                                &&(i_wb_addr[1:0]==`UART_RXREG)&&(i_wb_we))
220
                begin
221
                        // Reset the error lines if a '1' is ever written to
222
                        // them, otherwise leave them alone.
223
                        //
224
                        r_rx_perr <= (r_rx_perr)&&(~i_wb_data[9]);
225
                        r_rx_ferr <= (r_rx_ferr)&&(~i_wb_data[10]);
226
                end else if (rx_stb)
227
                begin
228
                        // On an rx_stb, capture any parity or framing error
229
                        // indications.  These aren't kept with the data rcvd,
230
                        // but rather kept external to the FIFO.  As a result,
231
                        // if you get a parity or framing error, you will never
232
                        // know which data byte it was associated with.
233
                        // For now ... that'll work.
234
                        r_rx_perr <= (r_rx_perr)||(rx_perr);
235
                        r_rx_ferr <= (r_rx_ferr)||(rx_ferr);
236
                end
237
 
238
        initial rx_uart_reset = 1'b1;
239
        always @(posedge i_clk)
240
                if ((i_rst)||((i_wb_stb)&&(i_wb_addr[1:0]==`UART_SETUP)&&(i_wb_we)))
241
                        // The receiver reset, always set on a master reset
242
                        // request.
243
                        rx_uart_reset <= 1'b1;
244
                else if ((i_wb_stb)&&(i_wb_addr[1:0]==`UART_RXREG)&&(i_wb_we))
245
                        // Writes to the receive register will command a receive
246
                        // reset anytime bit[12] is set.
247
                        rx_uart_reset <= i_wb_data[12];
248
                else
249
                        rx_uart_reset <= 1'b0;
250
 
251
        // Finally, we'll construct a 32-bit value from these various wires,
252
        // to be returned over the bus on any read.  These include the data
253
        // that would be read from the FIFO, an error indicator set upon
254
        // reading from an empty FIFO, a break indicator, and the frame and
255
        // parity error signals.
256
        wire    [31:0]   wb_rx_data;
257
        assign  wb_rx_data = { 16'h00,
258
                                3'h0, rx_fifo_err,
259
                                rx_break, rx_ferr, r_rx_perr, !rx_empty_n,
260
                                rxf_wb_data};
261
 
262 9 dgisselq
        /////////////////////////////////////////
263 5 dgisselq
        //
264 9 dgisselq
        //
265 5 dgisselq
        // Then the UART transmitter
266
        //
267 9 dgisselq
        //
268
        /////////////////////////////////////////
269 15 dgisselq
        wire            tx_empty_n, txf_err, tx_break;
270 5 dgisselq
        wire    [7:0]    tx_data;
271
        wire    [15:0]   txf_status;
272 15 dgisselq
        reg             txf_wb_write, tx_uart_reset;
273 5 dgisselq
        reg     [7:0]    txf_wb_data;
274
 
275
        // Unlike the receiver which goes from RXUART -> UFIFO -> WB, the
276
        // transmitter basically goes WB -> UFIFO -> TXUART.  Hence, to build
277
        // support for the transmitter, we start with the command to write data
278
        // into the FIFO.  In this case, we use the act of writing to the 
279
        // UART_TXREG address as our indication that we wish to write to the 
280
        // FIFO.  Here, we create a write command line, and latch the data for
281
        // the extra clock that it'll take so that the command and data can be
282
        // both true on the same clock.
283
        initial txf_wb_write = 1'b0;
284
        always @(posedge i_clk)
285
        begin
286
                txf_wb_write <= (i_wb_stb)&&(i_wb_addr == `UART_TXREG)
287
                                        &&(i_wb_we);
288
                txf_wb_data  <= i_wb_data[7:0];
289
        end
290
 
291
        // Transmit FIFO
292
        //
293
        // Most of this is just wire management.  The TX FIFO is identical in
294
        // implementation to the RX FIFO (theyre both UFIFOs), but the TX
295
        // FIFO is fed from the WB and read by the transmitter.  Some key
296
        // differences to note: we reset the transmitter on any request for a
297
        // break.  We read from the FIFO any time the UART transmitter is idle.
298
        // and ... we just set the values (above) for controlling writing into
299
        // this.
300 9 dgisselq
        ufifo   #(.LGFLEN(LGFLEN), .RXFIFO(0))
301 15 dgisselq
                txfifo(i_clk, (tx_break)||(tx_uart_reset),
302 5 dgisselq
                        txf_wb_write, txf_wb_data,
303 9 dgisselq
                        tx_empty_n,
304
                        (!tx_busy)&&(tx_empty_n), tx_data,
305
                        txf_status, txf_err);
306 6 dgisselq
        // Let's create two transmit based interrupts from the FIFO for the CPU.
307 9 dgisselq
        //      The first will be true any time the FIFO has at least one open
308
        //      position within it.
309
        assign  o_uart_tx_int = txf_status[0];
310 5 dgisselq
        //      The second will be true any time the FIFO is less than half
311
        //      full, allowing us a change to always keep it (near) fully 
312
        //      charged.
313 9 dgisselq
        assign  o_uart_txfifo_int = txf_status[1];
314 5 dgisselq
 
315 15 dgisselq
`ifndef USE_LITE_UART
316 5 dgisselq
        // Break logic
317
        //
318
        // A break in a UART controller is any time the UART holds the line
319
        // low for an extended period of time.  Here, we capture the wb_data[9]
320
        // wire, on writes, as an indication we wish to break.  As long as you
321
        // write unsigned characters to the interface, this will never be true
322
        // unless you wish it to be true.  Be aware, though, writing a valid
323
        // value to the interface will bring it out of the break condition.
324 15 dgisselq
        reg     r_tx_break;
325 5 dgisselq
        initial r_tx_break = 1'b0;
326
        always @(posedge i_clk)
327
                if (i_rst)
328
                        r_tx_break <= 1'b0;
329
                else if ((i_wb_stb)&&(i_wb_addr[1:0]==`UART_TXREG)&&(i_wb_we))
330
                        r_tx_break <= i_wb_data[9];
331 15 dgisselq
        assign  tx_break = r_tx_break;
332
`else
333
        assign  tx_break = 1'b0;
334
`endif
335 5 dgisselq
 
336
        // TX-Reset logic
337
        //
338
        // This is nearly identical to the RX reset logic above.  Basically,
339
        // any time someone writes to bit [12] the transmitter will go through
340
        // a reset cycle.  Keep bit [12] low, and everything will proceed as
341
        // normal.
342
        initial tx_uart_reset = 1'b1;
343
        always @(posedge i_clk)
344
                if((i_rst)||((i_wb_stb)&&(i_wb_addr == `UART_SETUP)&&(i_wb_we)))
345
                        tx_uart_reset <= 1'b1;
346
                else if ((i_wb_stb)&&(i_wb_addr[1:0]==`UART_TXREG)&&(i_wb_we))
347
                        tx_uart_reset <= i_wb_data[12];
348
                else
349
                        tx_uart_reset <= 1'b0;
350
 
351 15 dgisselq
`ifdef  USE_LITE_UART
352 17 dgisselq
        txuartlite #(INITIAL_SETUP[23:0]) tx(i_clk, (tx_empty_n), tx_data,
353 15 dgisselq
                        o_uart_tx, tx_busy);
354
`else
355
        wire    cts_n;
356
        assign  cts_n = (HARDWARE_FLOW_CONTROL_PRESENT)&&(i_cts_n);
357 5 dgisselq
        // Finally, the UART transmitter module itself.  Note that we haven't
358
        // connected the reset wire.  Transmitting is as simple as setting
359
        // the stb value (here set to tx_empty_n) and the data.  When these
360
        // are both set on the same clock that tx_busy is low, the transmitter
361
        // will move on to the next data byte.  Really, the only thing magical
362
        // here is that tx_empty_n wire--thus, if there's anything in the FIFO,
363
        // we read it here.  (You might notice above, we register a read any
364
        // time (tx_empty_n) and (!tx_busy) are both true---the condition for
365
        // starting to transmit a new byte.)
366
        txuart  #(INITIAL_SETUP) tx(i_clk, 1'b0, uart_setup,
367
                        r_tx_break, (tx_empty_n), tx_data,
368 15 dgisselq
                        cts_n, o_uart_tx, tx_busy);
369
`endif
370 5 dgisselq
 
371
        // Now that we are done with the chain, pick some wires for the user
372
        // to read on any read of the transmit port.
373
        //
374
        // This port is different from reading from the receive port, since
375
        // there are no side effects.  (Reading from the receive port advances
376
        // the receive FIFO, here only writing to the transmit port advances the
377
        // transmit FIFO--hence the read values are free for ... whatever.)  
378
        // We choose here to provide information about the transmit FIFO
379 9 dgisselq
        // (txf_err, txf_half_full, txf_full_n), information about the current
380 5 dgisselq
        // voltage on the line (o_uart_tx)--and even the voltage on the receive
381
        // line (ck_uart), as well as our current setting of the break and
382
        // whether or not we are actively transmitting.
383
        wire    [31:0]   wb_tx_data;
384
        assign  wb_tx_data = { 16'h00,
385 15 dgisselq
                                i_cts_n, txf_status[1:0], txf_err,
386
                                ck_uart, o_uart_tx, tx_break, (tx_busy|txf_status[0]),
387 9 dgisselq
                                (tx_busy|txf_status[0])?txf_wb_data:8'b00};
388 5 dgisselq
 
389
        // Each of the FIFO's returns a 16 bit status value.  This value tells
390
        // us both how big the FIFO is, as well as how much of the FIFO is in 
391
        // use.  Let's merge those two status words together into a word we
392
        // can use when reading about the FIFO.
393
        wire    [31:0]   wb_fifo_data;
394
        assign  wb_fifo_data = { txf_status, rxf_status };
395
 
396
        // You may recall from above that reads take two clocks.  Hence, we
397
        // need to delay the address decoding for a clock until the data is 
398
        // ready.  We do that here.
399
        reg     [1:0]    r_wb_addr;
400
        always @(posedge i_clk)
401
                r_wb_addr <= i_wb_addr;
402
 
403
        // Likewise, the acknowledgement is delayed by one clock.
404
        reg     r_wb_ack;
405
        always @(posedge i_clk) // We'll ACK in two clocks
406
                r_wb_ack <= i_wb_stb;
407
        always @(posedge i_clk) // Okay, time to set the ACK
408
                o_wb_ack <= r_wb_ack;
409
 
410
        // Finally, set the return data.  This data must be valid on the same
411
        // clock o_wb_ack is high.  On all other clocks, it is irrelelant--since
412
        // no one cares, no one is reading it, it gets lost in the mux in the
413
        // interconnect, etc.  For this reason, we can just simplify our logic.
414
        always @(posedge i_clk)
415
                casez(r_wb_addr)
416 9 dgisselq
                `UART_SETUP: o_wb_data <= { 1'b0, uart_setup };
417 5 dgisselq
                `UART_FIFO:  o_wb_data <= wb_fifo_data;
418
                `UART_RXREG: o_wb_data <= wb_rx_data;
419
                `UART_TXREG: o_wb_data <= wb_tx_data;
420
                endcase
421
 
422
        // This device never stalls.  Sure, it takes two clocks, but they are
423
        // pipelined, and nothing stalls that pipeline.  (Creates FIFO errors,
424
        // perhaps, but doesn't stall the pipeline.)  Hence, we can just
425
        // set this value to zero.
426
        assign  o_wb_stall = 1'b0;
427
 
428 18 dgisselq
        // Make verilator happy
429
        // verilator lint_off UNUSED
430
        wire    [33:0] unused;
431
        assign  unused = { i_rst, i_wb_cyc, i_wb_data };
432
        // verilator lint_on UNUSED
433
 
434 5 dgisselq
endmodule

powered by: WebSVN 2.1.0

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