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

Subversion Repositories wbuart32

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

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

powered by: WebSVN 2.1.0

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