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

Subversion Repositories wbuart32

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

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

powered by: WebSVN 2.1.0

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