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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [wbuart.v] - Blame information for rev 50

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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