1 |
198 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEO430 - Universal Asynchronous Receiver and Transmitter >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
|
|
-- # Fixed frame config: 8-bit, no parity bit, 1 stop bit, variable BAUD rate. #
|
5 |
|
|
-- # Interrupt: UART_RX_available [OR] UART_TX_done #
|
6 |
|
|
-- # ********************************************************************************************* #
|
7 |
|
|
-- # BSD 3-Clause License #
|
8 |
|
|
-- # #
|
9 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
10 |
|
|
-- # #
|
11 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
12 |
|
|
-- # permitted provided that the following conditions are met: #
|
13 |
|
|
-- # #
|
14 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
15 |
|
|
-- # conditions and the following disclaimer. #
|
16 |
|
|
-- # #
|
17 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
18 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
19 |
|
|
-- # provided with the distribution. #
|
20 |
|
|
-- # #
|
21 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
22 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
23 |
|
|
-- # permission. #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
26 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
27 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
28 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
29 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
30 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
31 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
32 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
33 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
34 |
|
|
-- # ********************************************************************************************* #
|
35 |
|
|
-- # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
36 |
|
|
-- #################################################################################################
|
37 |
|
|
|
38 |
|
|
library ieee;
|
39 |
|
|
use ieee.std_logic_1164.all;
|
40 |
|
|
use ieee.numeric_std.all;
|
41 |
|
|
|
42 |
|
|
library neo430;
|
43 |
|
|
use neo430.neo430_package.all;
|
44 |
|
|
|
45 |
|
|
entity neo430_uart is
|
46 |
|
|
port (
|
47 |
|
|
-- host access --
|
48 |
|
|
clk_i : in std_ulogic; -- global clock line
|
49 |
|
|
rden_i : in std_ulogic; -- read enable
|
50 |
|
|
wren_i : in std_ulogic; -- write enable
|
51 |
|
|
addr_i : in std_ulogic_vector(15 downto 0); -- address
|
52 |
|
|
data_i : in std_ulogic_vector(15 downto 0); -- data in
|
53 |
|
|
data_o : out std_ulogic_vector(15 downto 0); -- data out
|
54 |
|
|
-- clock generator --
|
55 |
|
|
clkgen_en_o : out std_ulogic; -- enable clock generator
|
56 |
|
|
clkgen_i : in std_ulogic_vector(07 downto 0);
|
57 |
|
|
-- com lines --
|
58 |
|
|
uart_txd_o : out std_ulogic;
|
59 |
|
|
uart_rxd_i : in std_ulogic;
|
60 |
|
|
-- interrupts --
|
61 |
|
|
uart_irq_o : out std_ulogic -- uart rx/tx interrupt
|
62 |
|
|
);
|
63 |
|
|
end neo430_uart;
|
64 |
|
|
|
65 |
|
|
architecture neo430_uart_rtl of neo430_uart is
|
66 |
|
|
|
67 |
|
|
-- IO space: module base address --
|
68 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
69 |
|
|
constant lo_abb_c : natural := index_size_f(uart_size_c); -- low address boundary bit
|
70 |
|
|
|
71 |
|
|
-- accessible regs --
|
72 |
|
|
signal ctrl : std_ulogic_vector(15 downto 0);
|
73 |
|
|
|
74 |
|
|
-- control reg bits --
|
75 |
|
|
constant ctrl_uart_baud0_c : natural := 0; -- r/w: UART baud config bit 0
|
76 |
|
|
constant ctrl_uart_baud1_c : natural := 1; -- r/w: UART baud config bit 1
|
77 |
|
|
constant ctrl_uart_baud2_c : natural := 2; -- r/w: UART baud config bit 2
|
78 |
|
|
constant ctrl_uart_baud3_c : natural := 3; -- r/w: UART baud config bit 3
|
79 |
|
|
constant ctrl_uart_baud4_c : natural := 4; -- r/w: UART baud config bit 4
|
80 |
|
|
constant ctrl_uart_baud5_c : natural := 5; -- r/w: UART baud config bit 5
|
81 |
|
|
constant ctrl_uart_baud6_c : natural := 6; -- r/w: UART baud config bit 6
|
82 |
|
|
constant ctrl_uart_baud7_c : natural := 7; -- r/w: UART baud config bit 7
|
83 |
|
|
constant ctrl_uart_prsc0_c : natural := 8; -- r/w: UART baud prsc bit 0
|
84 |
|
|
constant ctrl_uart_prsc1_c : natural := 9; -- r/w: UART baud prsc bit 1
|
85 |
|
|
constant ctrl_uart_prsc2_c : natural := 10; -- r/w: UART baud prsc bit 2
|
86 |
|
|
constant ctrl_uart_rxovr_c : natural := 11; -- r/-: UART RX overrun
|
87 |
|
|
constant ctrl_uart_en_c : natural := 12; -- r/w: UART enable
|
88 |
|
|
constant ctrl_uart_rx_irq_c : natural := 13; -- r/w: UART rx done interrupt enable
|
89 |
|
|
constant ctrl_uart_tx_irq_c : natural := 14; -- r/w: UART tx done interrupt enable
|
90 |
|
|
constant ctrl_uart_tx_busy_c : natural := 15; -- r/-: UART transmitter is busy
|
91 |
|
|
|
92 |
|
|
-- data register flags --
|
93 |
|
|
constant data_rx_avail_c : natural := 15; -- r/-: Rx data available/valid
|
94 |
|
|
|
95 |
|
|
-- access control --
|
96 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
97 |
|
|
signal addr : std_ulogic_vector(15 downto 0); -- access address
|
98 |
|
|
signal wr_en : std_ulogic; -- word write enable
|
99 |
|
|
signal rd_en : std_ulogic; -- read enable
|
100 |
|
|
|
101 |
|
|
-- clock generator --
|
102 |
|
|
signal uart_clk : std_ulogic;
|
103 |
|
|
|
104 |
|
|
-- uart tx unit --
|
105 |
|
|
signal uart_tx_busy : std_ulogic;
|
106 |
|
|
signal uart_tx_done : std_ulogic;
|
107 |
|
|
signal uart_tx_bitcnt : std_ulogic_vector(3 downto 0);
|
108 |
|
|
signal uart_tx_sreg : std_ulogic_vector(9 downto 0);
|
109 |
|
|
signal uart_tx_baud_cnt : std_ulogic_vector(7 downto 0);
|
110 |
|
|
|
111 |
|
|
-- uart rx unit --
|
112 |
|
|
signal uart_rx_sync : std_ulogic_vector(4 downto 0);
|
113 |
|
|
signal uart_rx_avail : std_ulogic_vector(1 downto 0);
|
114 |
|
|
signal uart_rx_busy : std_ulogic;
|
115 |
|
|
signal uart_rx_busy_ff : std_ulogic;
|
116 |
|
|
signal uart_rx_bitcnt : std_ulogic_vector(3 downto 0);
|
117 |
|
|
signal uart_rx_sreg : std_ulogic_vector(8 downto 0);
|
118 |
|
|
signal uart_rx_reg : std_ulogic_vector(7 downto 0);
|
119 |
|
|
signal uart_rx_baud_cnt : std_ulogic_vector(7 downto 0);
|
120 |
|
|
|
121 |
|
|
begin
|
122 |
|
|
|
123 |
|
|
-- Access Control -----------------------------------------------------------
|
124 |
|
|
-- -----------------------------------------------------------------------------
|
125 |
|
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = uart_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
126 |
|
|
addr <= uart_base_c(15 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 1) & '0'; -- word aligned
|
127 |
|
|
wr_en <= acc_en and wren_i;
|
128 |
|
|
rd_en <= acc_en and rden_i;
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
-- Write access -------------------------------------------------------------
|
132 |
|
|
-- -----------------------------------------------------------------------------
|
133 |
|
|
wr_access: process(clk_i)
|
134 |
|
|
begin
|
135 |
|
|
if rising_edge(clk_i) then
|
136 |
|
|
if (wr_en = '1') then
|
137 |
|
|
if (addr = uart_ctrl_addr_c) then
|
138 |
|
|
ctrl <= data_i;
|
139 |
|
|
end if;
|
140 |
|
|
end if;
|
141 |
|
|
end if;
|
142 |
|
|
end process wr_access;
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
-- Clock Selection ----------------------------------------------------------
|
146 |
|
|
-- -----------------------------------------------------------------------------
|
147 |
|
|
-- clock enable --
|
148 |
|
|
clkgen_en_o <= ctrl(ctrl_uart_en_c);
|
149 |
|
|
|
150 |
|
|
-- uart clock select --
|
151 |
|
|
uart_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c))));
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
-- UART transmitter ---------------------------------------------------------
|
155 |
|
|
-- -----------------------------------------------------------------------------
|
156 |
|
|
uart_tx_unit: process(clk_i)
|
157 |
|
|
begin
|
158 |
|
|
if rising_edge(clk_i) then
|
159 |
|
|
uart_tx_done <= '0';
|
160 |
|
|
if (uart_tx_busy = '0') or (ctrl(ctrl_uart_en_c) = '0') then -- idle or disabled
|
161 |
|
|
uart_tx_busy <= '0';
|
162 |
|
|
uart_tx_baud_cnt <= ctrl(ctrl_uart_baud7_c downto ctrl_uart_baud0_c);
|
163 |
|
|
uart_tx_bitcnt <= "1010"; -- 10 bit
|
164 |
|
|
if (wr_en = '1') and (ctrl(ctrl_uart_en_c) = '1') and (addr = uart_rtx_addr_c) then
|
165 |
|
|
uart_tx_sreg <= '1' & data_i(7 downto 0) & '0'; -- stopbit & data & startbit
|
166 |
|
|
uart_tx_busy <= '1';
|
167 |
|
|
end if;
|
168 |
|
|
elsif (uart_clk = '1') then
|
169 |
|
|
if (uart_tx_baud_cnt = x"00") then
|
170 |
|
|
uart_tx_baud_cnt <= ctrl(ctrl_uart_baud7_c downto ctrl_uart_baud0_c);
|
171 |
|
|
uart_tx_bitcnt <= std_ulogic_vector(unsigned(uart_tx_bitcnt) - 1);
|
172 |
|
|
uart_tx_sreg <= '1' & uart_tx_sreg(9 downto 1);
|
173 |
|
|
if (uart_tx_bitcnt = "0000") then
|
174 |
|
|
uart_tx_busy <= '0'; -- done
|
175 |
|
|
uart_tx_done <= '1';
|
176 |
|
|
end if;
|
177 |
|
|
else
|
178 |
|
|
uart_tx_baud_cnt <= std_ulogic_vector(unsigned(uart_tx_baud_cnt) - 1);
|
179 |
|
|
end if;
|
180 |
|
|
end if;
|
181 |
|
|
-- transmitter output --
|
182 |
|
|
uart_txd_o <= uart_tx_sreg(0);
|
183 |
|
|
end if;
|
184 |
|
|
end process uart_tx_unit;
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
-- UART receiver ------------------------------------------------------------
|
188 |
|
|
-- -----------------------------------------------------------------------------
|
189 |
|
|
uart_rx_unit: process(clk_i)
|
190 |
|
|
begin
|
191 |
|
|
if rising_edge(clk_i) then
|
192 |
|
|
-- synchronizer --
|
193 |
|
|
uart_rx_sync <= uart_rxd_i & uart_rx_sync(4 downto 1);
|
194 |
|
|
-- arbiter --
|
195 |
|
|
if (uart_rx_busy = '0') or (ctrl(ctrl_uart_en_c) = '0') then -- idle or disabled
|
196 |
|
|
uart_rx_busy <= '0';
|
197 |
|
|
uart_rx_baud_cnt <= '0' & ctrl(ctrl_uart_baud7_c downto ctrl_uart_baud1_c); -- half baud rate to sample in middle of bit
|
198 |
|
|
uart_rx_bitcnt <= "1001"; -- 9 bit (startbit + 8 data bits, ignore stop bit/s)
|
199 |
|
|
if (ctrl(ctrl_uart_en_c) = '0') then
|
200 |
|
|
uart_rx_reg <= (others => '0'); -- to ensure defined state when reading
|
201 |
|
|
elsif (uart_rx_sync(2 downto 0) = "001") then -- start bit? (falling edge)
|
202 |
|
|
uart_rx_busy <= '1';
|
203 |
|
|
end if;
|
204 |
|
|
elsif (uart_clk = '1') then
|
205 |
|
|
if (uart_rx_baud_cnt = x"00") then
|
206 |
|
|
uart_rx_baud_cnt <= ctrl(ctrl_uart_baud7_c downto ctrl_uart_baud0_c);
|
207 |
|
|
uart_rx_bitcnt <= std_ulogic_vector(unsigned(uart_rx_bitcnt) - 1);
|
208 |
|
|
uart_rx_sreg <= uart_rx_sync(0) & uart_rx_sreg(8 downto 1);
|
209 |
|
|
if (uart_rx_bitcnt = "0000") then
|
210 |
|
|
uart_rx_busy <= '0'; -- done
|
211 |
|
|
uart_rx_reg <= uart_rx_sreg(8 downto 1);
|
212 |
|
|
end if;
|
213 |
|
|
else
|
214 |
|
|
uart_rx_baud_cnt <= std_ulogic_vector(unsigned(uart_rx_baud_cnt) - 1);
|
215 |
|
|
end if;
|
216 |
|
|
end if;
|
217 |
|
|
|
218 |
|
|
-- RX available flag --
|
219 |
|
|
uart_rx_busy_ff <= uart_rx_busy;
|
220 |
|
|
if (ctrl(ctrl_uart_en_c) = '0') or (((uart_rx_avail(0) = '1') or (uart_rx_avail(1) = '1')) and (rd_en = '1') and (addr = uart_rtx_addr_c)) then
|
221 |
|
|
uart_rx_avail <= "00";
|
222 |
|
|
elsif (uart_rx_busy_ff = '1') and (uart_rx_busy = '0') then
|
223 |
|
|
uart_rx_avail <= uart_rx_avail(0) & '1';
|
224 |
|
|
end if;
|
225 |
|
|
end if;
|
226 |
|
|
end process uart_rx_unit;
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
-- Interrupt ----------------------------------------------------------------
|
230 |
|
|
-- -----------------------------------------------------------------------------
|
231 |
|
|
-- UART Rx data available [OR] UART Tx complete
|
232 |
|
|
uart_irq_o <= (uart_rx_busy_ff and (not uart_rx_busy) and ctrl(ctrl_uart_rx_irq_c)) or (uart_tx_done and ctrl(ctrl_uart_tx_irq_c));
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
-- Read access --------------------------------------------------------------
|
236 |
|
|
-- -----------------------------------------------------------------------------
|
237 |
|
|
rd_access: process(clk_i)
|
238 |
|
|
begin
|
239 |
|
|
if rising_edge(clk_i) then
|
240 |
|
|
data_o <= (others => '0');
|
241 |
|
|
if (rd_en = '1') then
|
242 |
|
|
if (addr = uart_ctrl_addr_c) then
|
243 |
|
|
data_o(ctrl_uart_baud0_c) <= ctrl(ctrl_uart_baud0_c);
|
244 |
|
|
data_o(ctrl_uart_baud1_c) <= ctrl(ctrl_uart_baud1_c);
|
245 |
|
|
data_o(ctrl_uart_baud2_c) <= ctrl(ctrl_uart_baud2_c);
|
246 |
|
|
data_o(ctrl_uart_baud3_c) <= ctrl(ctrl_uart_baud3_c);
|
247 |
|
|
data_o(ctrl_uart_baud4_c) <= ctrl(ctrl_uart_baud4_c);
|
248 |
|
|
data_o(ctrl_uart_baud5_c) <= ctrl(ctrl_uart_baud5_c);
|
249 |
|
|
data_o(ctrl_uart_baud6_c) <= ctrl(ctrl_uart_baud6_c);
|
250 |
|
|
data_o(ctrl_uart_baud7_c) <= ctrl(ctrl_uart_baud7_c);
|
251 |
|
|
data_o(ctrl_uart_prsc0_c) <= ctrl(ctrl_uart_prsc0_c);
|
252 |
|
|
data_o(ctrl_uart_prsc1_c) <= ctrl(ctrl_uart_prsc1_c);
|
253 |
|
|
data_o(ctrl_uart_prsc2_c) <= ctrl(ctrl_uart_prsc2_c);
|
254 |
|
|
data_o(ctrl_uart_en_c) <= ctrl(ctrl_uart_en_c);
|
255 |
|
|
data_o(ctrl_uart_rx_irq_c) <= ctrl(ctrl_uart_rx_irq_c);
|
256 |
|
|
data_o(ctrl_uart_tx_irq_c) <= ctrl(ctrl_uart_tx_irq_c);
|
257 |
|
|
data_o(ctrl_uart_rxovr_c) <= uart_rx_avail(0) and uart_rx_avail(1);
|
258 |
|
|
data_o(ctrl_uart_tx_busy_c) <= uart_tx_busy;
|
259 |
|
|
else -- uart_rtx_addr_c
|
260 |
|
|
data_o(data_rx_avail_c) <= uart_rx_avail(0);
|
261 |
|
|
data_o(07 downto 0) <= uart_rx_reg;
|
262 |
|
|
end if;
|
263 |
|
|
end if;
|
264 |
|
|
end if;
|
265 |
|
|
end process rd_access;
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
end neo430_uart_rtl;
|