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

Subversion Repositories wbuart32

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

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

powered by: WebSVN 2.1.0

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