1 |
12 |
ste.fis |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// This sourcecode is released under BSD license.
|
3 |
|
|
// Please see http://www.opensource.org/licenses/bsd-license.php for details!
|
4 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
5 |
|
|
//
|
6 |
|
|
// Copyright (c) 2010, Stefan Fischer <Ste.Fis@OpenCores.org>
|
7 |
|
|
// All rights reserved.
|
8 |
|
|
//
|
9 |
|
|
// Redistribution and use in source and binary forms, with or without
|
10 |
|
|
// modification, are permitted provided that the following conditions are met:
|
11 |
|
|
//
|
12 |
|
|
// * Redistributions of source code must retain the above copyright notice,
|
13 |
|
|
// this list of conditions and the following disclaimer.
|
14 |
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
15 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
16 |
|
|
// and/or other materials provided with the distribution.
|
17 |
|
|
// * Neither the name of the author nor the names of his contributors may be
|
18 |
|
|
// used to endorse or promote products derived from this software without
|
19 |
|
|
// specific prior written permission.
|
20 |
|
|
//
|
21 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
24 |
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
25 |
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
26 |
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
27 |
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
28 |
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
29 |
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
30 |
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
31 |
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
32 |
|
|
//
|
33 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
34 |
|
|
// filename: wbs_uart.v
|
35 |
|
|
// description: synthesizable wishbone slave uart sio module using Xilinx (R)
|
36 |
|
|
// macros and adding some functionality like a configurable
|
37 |
|
|
// baud rate and buffer level checking
|
38 |
|
|
// todo4user: add other uart functionality as needed, i. e. interrupt logic or
|
39 |
|
|
// modem control signals
|
40 |
|
|
// version: 0.0.0
|
41 |
|
|
// changelog: - 0.0.0, initial release
|
42 |
|
|
// - ...
|
43 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
module wbs_uart (
|
47 |
|
|
rst,
|
48 |
|
|
clk,
|
49 |
|
|
|
50 |
|
|
wbs_cyc_i,
|
51 |
|
|
wbs_stb_i,
|
52 |
|
|
wbs_we_i,
|
53 |
|
|
wbs_adr_i,
|
54 |
|
|
wbs_dat_m2s_i,
|
55 |
|
|
wbs_dat_s2m_o,
|
56 |
|
|
wbs_ack_o,
|
57 |
|
|
|
58 |
|
|
uart_rx_si_i,
|
59 |
|
|
uart_tx_so_o
|
60 |
|
|
);
|
61 |
|
|
|
62 |
|
|
input rst;
|
63 |
|
|
wire rst;
|
64 |
|
|
input clk;
|
65 |
|
|
wire clk;
|
66 |
|
|
|
67 |
|
|
input wbs_cyc_i;
|
68 |
|
|
wire wbs_cyc_i;
|
69 |
|
|
input wbs_stb_i;
|
70 |
|
|
wire wbs_stb_i;
|
71 |
|
|
input wbs_we_i;
|
72 |
|
|
wire wbs_we_i;
|
73 |
|
|
input[7:0] wbs_adr_i;
|
74 |
|
|
wire [7:0] wbs_adr_i;
|
75 |
|
|
input[7:0] wbs_dat_m2s_i;
|
76 |
|
|
wire [7:0] wbs_dat_m2s_i;
|
77 |
|
|
output[7:0] wbs_dat_s2m_o;
|
78 |
|
|
reg [7:0] wbs_dat_s2m_o;
|
79 |
|
|
output wbs_ack_o;
|
80 |
|
|
reg wbs_ack_o;
|
81 |
|
|
|
82 |
|
|
input uart_rx_si_i;
|
83 |
|
|
wire uart_rx_si_i;
|
84 |
|
|
output uart_tx_so_o;
|
85 |
|
|
wire uart_tx_so_o;
|
86 |
|
|
|
87 |
|
|
wire wb_reg_we;
|
88 |
|
|
|
89 |
|
|
parameter ADDR_MSB = 1;
|
90 |
|
|
parameter[7:0] UART_RXTX_ADDR = 8'h00;
|
91 |
|
|
parameter[7:0] UART_SR_ADDR = 8'h01;
|
92 |
|
|
parameter UART_SR_RX_F_FLAG = 0;
|
93 |
|
|
parameter UART_SR_RX_HF_FLAG = 1;
|
94 |
|
|
parameter UART_SR_RX_DP_FLAG = 2;
|
95 |
|
|
parameter UART_SR_TX_F_FLAG = 4;
|
96 |
|
|
parameter UART_SR_TX_HF_FLAG = 5;
|
97 |
|
|
parameter[7:0] UART_BAUD_LO_ADDR = 8'h02;
|
98 |
|
|
parameter[7:0] UART_BAUD_HI_ADDR = 8'h03;
|
99 |
|
|
|
100 |
|
|
reg[15:0] baud_count;
|
101 |
|
|
reg[15:0] baud_limit;
|
102 |
|
|
|
103 |
|
|
reg en_16_x_baud;
|
104 |
|
|
|
105 |
|
|
reg rx_read_buffer;
|
106 |
|
|
wire rx_buffer_full;
|
107 |
|
|
wire rx_buffer_half_full;
|
108 |
|
|
wire rx_buffer_data_present;
|
109 |
|
|
wire[7:0] rx_data_out;
|
110 |
|
|
|
111 |
|
|
reg tx_write_buffer;
|
112 |
|
|
wire tx_buffer_full;
|
113 |
|
|
wire tx_buffer_half_full;
|
114 |
|
|
|
115 |
|
|
// internal register write enable signal
|
116 |
|
|
assign wb_reg_we = wbs_cyc_i && wbs_stb_i && wbs_we_i;
|
117 |
|
|
|
118 |
|
|
always@(posedge clk) begin
|
119 |
|
|
|
120 |
|
|
// baud rate configuration:
|
121 |
|
|
// baud_limit = round( system clock frequency / (16 * baud rate) ) - 1
|
122 |
|
|
// i. e. 9600 baud at 50 MHz system clock =>
|
123 |
|
|
// baud_limit = round( 50.0E6 / (16 * 9600) ) - 1 = 325 = 0x0145
|
124 |
|
|
|
125 |
|
|
// baud timer
|
126 |
|
|
if (baud_count == baud_limit) begin
|
127 |
|
|
baud_count <= 16'h0000;
|
128 |
|
|
en_16_x_baud <= 1'b1;
|
129 |
|
|
end else begin
|
130 |
|
|
baud_count <= baud_count + 1;
|
131 |
|
|
en_16_x_baud <= 1'b0;
|
132 |
|
|
end
|
133 |
|
|
|
134 |
|
|
rx_read_buffer <= 1'b0;
|
135 |
|
|
tx_write_buffer <= 1'b0;
|
136 |
|
|
|
137 |
|
|
wbs_dat_s2m_o <= 8'h00;
|
138 |
|
|
// registered wishbone slave handshake (default)
|
139 |
|
|
wbs_ack_o <= wbs_cyc_i && wbs_stb_i && (! wbs_ack_o);
|
140 |
|
|
|
141 |
|
|
case(wbs_adr_i[ADDR_MSB:0])
|
142 |
|
|
// receive/transmit buffer access
|
143 |
|
|
UART_RXTX_ADDR[ADDR_MSB:0]: begin
|
144 |
|
|
if (wbs_cyc_i && wbs_stb_i)
|
145 |
|
|
// overwriting wishbone slave handshake for blocking transactions
|
146 |
|
|
// to rx/tx fifos by using buffer status flags
|
147 |
|
|
if (wbs_we_i) begin
|
148 |
|
|
tx_write_buffer <= (! tx_buffer_full) && (! wbs_ack_o);
|
149 |
|
|
wbs_ack_o <= (! tx_buffer_full) && (! wbs_ack_o);
|
150 |
|
|
end else begin
|
151 |
|
|
rx_read_buffer <= rx_buffer_data_present && (! wbs_ack_o);
|
152 |
|
|
wbs_ack_o <= rx_buffer_data_present && (! wbs_ack_o);
|
153 |
|
|
end
|
154 |
|
|
wbs_dat_s2m_o <= rx_data_out;
|
155 |
|
|
end
|
156 |
|
|
// status register access
|
157 |
|
|
UART_SR_ADDR[ADDR_MSB:0]: begin
|
158 |
|
|
wbs_dat_s2m_o[UART_SR_RX_F_FLAG] <= rx_buffer_full;
|
159 |
|
|
wbs_dat_s2m_o[UART_SR_RX_HF_FLAG] <= rx_buffer_half_full;
|
160 |
|
|
wbs_dat_s2m_o[UART_SR_RX_DP_FLAG] <= rx_buffer_data_present;
|
161 |
|
|
wbs_dat_s2m_o[UART_SR_TX_F_FLAG] <= tx_buffer_full;
|
162 |
|
|
wbs_dat_s2m_o[UART_SR_TX_HF_FLAG] <= tx_buffer_half_full;
|
163 |
|
|
end
|
164 |
|
|
// baud rate register access / low byte
|
165 |
|
|
UART_BAUD_LO_ADDR[ADDR_MSB:0]: begin
|
166 |
|
|
if (wb_reg_we) begin
|
167 |
|
|
baud_limit[7:0] <= wbs_dat_m2s_i;
|
168 |
|
|
baud_count <= 16'h0000;
|
169 |
|
|
end
|
170 |
|
|
wbs_dat_s2m_o <= baud_limit[7:0];
|
171 |
|
|
end
|
172 |
|
|
// baud rate register access / high byte
|
173 |
|
|
UART_BAUD_HI_ADDR[ADDR_MSB:0]: begin
|
174 |
|
|
if (wb_reg_we) begin
|
175 |
|
|
baud_limit[15:8] <= wbs_dat_m2s_i;
|
176 |
|
|
baud_count <= 16'h0000;
|
177 |
|
|
end
|
178 |
|
|
wbs_dat_s2m_o <= baud_limit[15:8];
|
179 |
|
|
end
|
180 |
|
|
default: ;
|
181 |
|
|
endcase
|
182 |
|
|
|
183 |
17 |
ste.fis |
if (rst)
|
184 |
12 |
ste.fis |
wbs_ack_o <= 1'b0;
|
185 |
|
|
|
186 |
|
|
end
|
187 |
|
|
|
188 |
|
|
// Xilinx (R) uart macro instances
|
189 |
|
|
//////////////////////////////////
|
190 |
|
|
|
191 |
|
|
uart_rx inst_uart_rx (
|
192 |
|
|
.serial_in(uart_rx_si_i),
|
193 |
|
|
.data_out(rx_data_out),
|
194 |
|
|
.read_buffer(rx_read_buffer),
|
195 |
|
|
.reset_buffer(rst),
|
196 |
|
|
.en_16_x_baud(en_16_x_baud),
|
197 |
|
|
.buffer_data_present(rx_buffer_data_present),
|
198 |
|
|
.buffer_full(rx_buffer_full),
|
199 |
|
|
.buffer_half_full(rx_buffer_half_full),
|
200 |
|
|
.clk(clk)
|
201 |
|
|
);
|
202 |
|
|
|
203 |
|
|
uart_tx inst_uart_tx (
|
204 |
|
|
.data_in(wbs_dat_m2s_i),
|
205 |
|
|
.write_buffer(tx_write_buffer),
|
206 |
|
|
.reset_buffer(rst),
|
207 |
|
|
.en_16_x_baud(en_16_x_baud),
|
208 |
|
|
.serial_out(uart_tx_so_o),
|
209 |
|
|
.buffer_full(tx_buffer_full),
|
210 |
|
|
.buffer_half_full(tx_buffer_half_full),
|
211 |
|
|
.clk(clk)
|
212 |
|
|
);
|
213 |
|
|
|
214 |
|
|
endmodule
|