1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
50 |
zero_gravi |
-- # << NEORV32 - Universal Asynchronous Receiver and Transmitter (UART0/1) >> #
|
3 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
4 |
42 |
zero_gravi |
-- # Frame configuration: 1 start bit, 8 bit data, optional parity bit (even/odd), 1 stop bit, #
|
5 |
|
|
-- # programmable BAUD rate via clock pre-scaler and BAUD value config register. #
|
6 |
2 |
zero_gravi |
-- # Interrupt: UART_RX_available or UART_TX_done #
|
7 |
30 |
zero_gravi |
-- # #
|
8 |
50 |
zero_gravi |
-- # UART0 / UART1: #
|
9 |
|
|
-- # This module is used for implementing UART0 and UART1. The UART_PRIMARY generic configures the #
|
10 |
|
|
-- # interface register addresses and simulation output setting for UART0 (UART_PRIMARY = true) #
|
11 |
|
|
-- # or UART1 (UART_PRIMARY = false). #
|
12 |
|
|
-- # #
|
13 |
30 |
zero_gravi |
-- # SIMULATION: #
|
14 |
|
|
-- # When the simulation mode is enabled (setting the ctrl.ctrl_uart_sim_en_c bit) any write #
|
15 |
|
|
-- # access to the TX register will not trigger any UART activity. Instead, the written data is #
|
16 |
|
|
-- # output to the simulation environment. The lowest 8 bits of the written data are printed as #
|
17 |
50 |
zero_gravi |
-- # ASCII char to the simulator console. #
|
18 |
|
|
-- # This char is also stored to the file "neorv32.uartX.sim_mode.text.out" (where X = 0 for UART0 #
|
19 |
|
|
-- # and X = 1 for UART1). The full 32-bit write data is also stored as 8-digit hexadecimal value #
|
20 |
|
|
-- # to the file "neorv32.uartX.sim_mode.data.out" (where X = 0 for UART0 and X = 1 for UART1). #
|
21 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
22 |
|
|
-- # BSD 3-Clause License #
|
23 |
|
|
-- # #
|
24 |
48 |
zero_gravi |
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
25 |
2 |
zero_gravi |
-- # #
|
26 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
27 |
|
|
-- # permitted provided that the following conditions are met: #
|
28 |
|
|
-- # #
|
29 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
30 |
|
|
-- # conditions and the following disclaimer. #
|
31 |
|
|
-- # #
|
32 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
33 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
34 |
|
|
-- # provided with the distribution. #
|
35 |
|
|
-- # #
|
36 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
37 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
38 |
|
|
-- # permission. #
|
39 |
|
|
-- # #
|
40 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
41 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
42 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
43 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
44 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
45 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
46 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
47 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
48 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
49 |
|
|
-- # ********************************************************************************************* #
|
50 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
51 |
|
|
-- #################################################################################################
|
52 |
|
|
|
53 |
|
|
library ieee;
|
54 |
|
|
use ieee.std_logic_1164.all;
|
55 |
|
|
use ieee.numeric_std.all;
|
56 |
|
|
|
57 |
|
|
library neorv32;
|
58 |
|
|
use neorv32.neorv32_package.all;
|
59 |
30 |
zero_gravi |
use std.textio.all; -- obviously only for simulation
|
60 |
2 |
zero_gravi |
|
61 |
|
|
entity neorv32_uart is
|
62 |
50 |
zero_gravi |
generic (
|
63 |
|
|
UART_PRIMARY : boolean := true -- true = primary UART (UART0), false = secondary UART (UART1)
|
64 |
|
|
);
|
65 |
2 |
zero_gravi |
port (
|
66 |
|
|
-- host access --
|
67 |
|
|
clk_i : in std_ulogic; -- global clock line
|
68 |
|
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
69 |
|
|
rden_i : in std_ulogic; -- read enable
|
70 |
|
|
wren_i : in std_ulogic; -- write enable
|
71 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
72 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
73 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
74 |
|
|
-- clock generator --
|
75 |
|
|
clkgen_en_o : out std_ulogic; -- enable clock generator
|
76 |
|
|
clkgen_i : in std_ulogic_vector(07 downto 0);
|
77 |
|
|
-- com lines --
|
78 |
|
|
uart_txd_o : out std_ulogic;
|
79 |
|
|
uart_rxd_i : in std_ulogic;
|
80 |
|
|
-- interrupts --
|
81 |
48 |
zero_gravi |
irq_rxd_o : out std_ulogic; -- uart data received interrupt
|
82 |
|
|
irq_txd_o : out std_ulogic -- uart transmission done interrupt
|
83 |
2 |
zero_gravi |
);
|
84 |
|
|
end neorv32_uart;
|
85 |
|
|
|
86 |
|
|
architecture neorv32_uart_rtl of neorv32_uart is
|
87 |
|
|
|
88 |
50 |
zero_gravi |
-- interface configuration for UART0 / UART1 --
|
89 |
|
|
constant uart_id_base_c : std_ulogic_vector(data_width_c-1 downto 0) := cond_sel_stdulogicvector_f(UART_PRIMARY, uart0_base_c, uart1_base_c);
|
90 |
|
|
constant uart_id_size_c : natural := cond_sel_natural_f( UART_PRIMARY, uart0_size_c, uart1_size_c);
|
91 |
|
|
constant uart_id_ctrl_addr_c : std_ulogic_vector(data_width_c-1 downto 0) := cond_sel_stdulogicvector_f(UART_PRIMARY, uart0_ctrl_addr_c, uart1_ctrl_addr_c);
|
92 |
|
|
constant uart_id_rtx_addr_c : std_ulogic_vector(data_width_c-1 downto 0) := cond_sel_stdulogicvector_f(UART_PRIMARY, uart0_rtx_addr_c, uart1_rtx_addr_c);
|
93 |
|
|
|
94 |
|
|
-- IO space: module base address --
|
95 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
96 |
|
|
constant lo_abb_c : natural := index_size_f(uart_id_size_c); -- low address boundary bit
|
97 |
|
|
|
98 |
30 |
zero_gravi |
-- simulation output configuration --
|
99 |
|
|
constant sim_screen_output_en_c : boolean := true; -- output lowest byte as char to simulator console when enabled
|
100 |
|
|
constant sim_text_output_en_c : boolean := true; -- output lowest byte as char to text file when enabled
|
101 |
|
|
constant sim_data_output_en_c : boolean := true; -- dump 32-word to file when enabled
|
102 |
|
|
|
103 |
50 |
zero_gravi |
-- simulation output file configuration --
|
104 |
|
|
constant sim_uart_text_file_c : string := cond_sel_string_f(UART_PRIMARY, "neorv32.uart0.sim_mode.text.out", "neorv32.uart1.sim_mode.text.out");
|
105 |
|
|
constant sim_uart_data_file_c : string := cond_sel_string_f(UART_PRIMARY, "neorv32.uart0.sim_mode.data.out", "neorv32.uart1.sim_mode.data.out");
|
106 |
2 |
zero_gravi |
|
107 |
|
|
-- accessible regs --
|
108 |
|
|
signal ctrl : std_ulogic_vector(31 downto 0);
|
109 |
|
|
|
110 |
|
|
-- control reg bits --
|
111 |
|
|
constant ctrl_uart_baud00_c : natural := 0; -- r/w: UART baud config bit 0
|
112 |
|
|
constant ctrl_uart_baud01_c : natural := 1; -- r/w: UART baud config bit 1
|
113 |
|
|
constant ctrl_uart_baud02_c : natural := 2; -- r/w: UART baud config bit 2
|
114 |
|
|
constant ctrl_uart_baud03_c : natural := 3; -- r/w: UART baud config bit 3
|
115 |
|
|
constant ctrl_uart_baud04_c : natural := 4; -- r/w: UART baud config bit 4
|
116 |
|
|
constant ctrl_uart_baud05_c : natural := 5; -- r/w: UART baud config bit 5
|
117 |
|
|
constant ctrl_uart_baud06_c : natural := 6; -- r/w: UART baud config bit 6
|
118 |
|
|
constant ctrl_uart_baud07_c : natural := 7; -- r/w: UART baud config bit 7
|
119 |
|
|
constant ctrl_uart_baud08_c : natural := 8; -- r/w: UART baud config bit 8
|
120 |
|
|
constant ctrl_uart_baud09_c : natural := 9; -- r/w: UART baud config bit 9
|
121 |
|
|
constant ctrl_uart_baud10_c : natural := 10; -- r/w: UART baud config bit 10
|
122 |
|
|
constant ctrl_uart_baud11_c : natural := 11; -- r/w: UART baud config bit 11
|
123 |
|
|
--
|
124 |
30 |
zero_gravi |
constant ctrl_uart_sim_en_c : natural := 12; -- r/w: UART SIMULATION OUTPUT enable
|
125 |
|
|
--
|
126 |
42 |
zero_gravi |
constant ctrl_uart_pmode0_c : natural := 22; -- r/w: Parity config (0=even; 1=odd)
|
127 |
|
|
constant ctrl_uart_pmode1_c : natural := 23; -- r/w: Enable parity bit
|
128 |
2 |
zero_gravi |
constant ctrl_uart_prsc0_c : natural := 24; -- r/w: UART baud prsc bit 0
|
129 |
|
|
constant ctrl_uart_prsc1_c : natural := 25; -- r/w: UART baud prsc bit 1
|
130 |
|
|
constant ctrl_uart_prsc2_c : natural := 26; -- r/w: UART baud prsc bit 2
|
131 |
42 |
zero_gravi |
--
|
132 |
2 |
zero_gravi |
constant ctrl_uart_en_c : natural := 28; -- r/w: UART enable
|
133 |
|
|
constant ctrl_uart_tx_busy_c : natural := 31; -- r/-: UART transmitter is busy
|
134 |
|
|
|
135 |
|
|
-- data register flags --
|
136 |
42 |
zero_gravi |
constant data_rx_avail_c : natural := 31; -- r/-: Rx data available
|
137 |
|
|
constant data_rx_overr_c : natural := 30; -- r/-: Rx data overrun
|
138 |
|
|
constant data_rx_ferr_c : natural := 29; -- r/-: Rx frame error
|
139 |
|
|
constant data_rx_perr_c : natural := 28; -- r/-: Rx parity error
|
140 |
2 |
zero_gravi |
|
141 |
|
|
-- access control --
|
142 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
143 |
|
|
signal addr : std_ulogic_vector(31 downto 0); -- access address
|
144 |
|
|
signal wr_en : std_ulogic; -- word write enable
|
145 |
|
|
signal rd_en : std_ulogic; -- read enable
|
146 |
|
|
|
147 |
|
|
-- clock generator --
|
148 |
|
|
signal uart_clk : std_ulogic;
|
149 |
|
|
|
150 |
42 |
zero_gravi |
-- numbers of bits in transmission frame --
|
151 |
|
|
signal num_bits : std_ulogic_vector(03 downto 0);
|
152 |
|
|
|
153 |
2 |
zero_gravi |
-- uart tx unit --
|
154 |
42 |
zero_gravi |
type uart_tx_t is record
|
155 |
|
|
busy : std_ulogic;
|
156 |
|
|
done : std_ulogic;
|
157 |
|
|
bitcnt : std_ulogic_vector(03 downto 0);
|
158 |
|
|
sreg : std_ulogic_vector(10 downto 0);
|
159 |
|
|
baud_cnt : std_ulogic_vector(11 downto 0);
|
160 |
|
|
end record;
|
161 |
|
|
signal uart_tx : uart_tx_t;
|
162 |
2 |
zero_gravi |
|
163 |
|
|
-- uart rx unit --
|
164 |
42 |
zero_gravi |
type uart_rx_t is record
|
165 |
|
|
sync : std_ulogic_vector(04 downto 0);
|
166 |
|
|
avail : std_ulogic_vector(01 downto 0);
|
167 |
|
|
busy : std_ulogic;
|
168 |
|
|
busy_ff : std_ulogic;
|
169 |
|
|
bitcnt : std_ulogic_vector(03 downto 0);
|
170 |
|
|
sreg : std_ulogic_vector(09 downto 0);
|
171 |
|
|
data : std_ulogic_vector(07 downto 0);
|
172 |
|
|
baud_cnt : std_ulogic_vector(11 downto 0);
|
173 |
|
|
ferr : std_ulogic; -- frame error (stop bit not set)
|
174 |
|
|
perr : std_ulogic; -- parity error
|
175 |
|
|
end record;
|
176 |
|
|
signal uart_rx : uart_rx_t;
|
177 |
2 |
zero_gravi |
|
178 |
|
|
begin
|
179 |
|
|
|
180 |
|
|
-- Access Control -------------------------------------------------------------------------
|
181 |
|
|
-- -------------------------------------------------------------------------------------------
|
182 |
50 |
zero_gravi |
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = uart_id_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
183 |
|
|
addr <= uart_id_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
184 |
2 |
zero_gravi |
wr_en <= acc_en and wren_i;
|
185 |
|
|
rd_en <= acc_en and rden_i;
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
-- Read/Write Access ----------------------------------------------------------------------
|
189 |
|
|
-- -------------------------------------------------------------------------------------------
|
190 |
|
|
rw_access: process(clk_i)
|
191 |
|
|
begin
|
192 |
|
|
if rising_edge(clk_i) then
|
193 |
|
|
ack_o <= acc_en and (rden_i or wren_i);
|
194 |
|
|
-- write access --
|
195 |
|
|
if (wr_en = '1') then
|
196 |
50 |
zero_gravi |
if (addr = uart_id_ctrl_addr_c) then
|
197 |
42 |
zero_gravi |
ctrl <= (others => '0');
|
198 |
|
|
ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c) <= data_i(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
|
199 |
|
|
ctrl(ctrl_uart_sim_en_c) <= data_i(ctrl_uart_sim_en_c);
|
200 |
|
|
ctrl(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c) <= data_i(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c);
|
201 |
|
|
ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c) <= data_i(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c);
|
202 |
|
|
ctrl(ctrl_uart_en_c) <= data_i(ctrl_uart_en_c);
|
203 |
2 |
zero_gravi |
end if;
|
204 |
|
|
end if;
|
205 |
|
|
-- read access --
|
206 |
|
|
data_o <= (others => '0');
|
207 |
|
|
if (rd_en = '1') then
|
208 |
50 |
zero_gravi |
if (addr = uart_id_ctrl_addr_c) then
|
209 |
42 |
zero_gravi |
data_o(ctrl_uart_baud11_c downto ctrl_uart_baud00_c) <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
|
210 |
|
|
data_o(ctrl_uart_sim_en_c) <= ctrl(ctrl_uart_sim_en_c);
|
211 |
|
|
data_o(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c) <= ctrl(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c);
|
212 |
|
|
data_o(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c) <= ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c);
|
213 |
|
|
data_o(ctrl_uart_en_c) <= ctrl(ctrl_uart_en_c);
|
214 |
|
|
data_o(ctrl_uart_tx_busy_c) <= uart_tx.busy;
|
215 |
50 |
zero_gravi |
else -- uart_id_rtx_addr_c
|
216 |
42 |
zero_gravi |
data_o(data_rx_avail_c) <= uart_rx.avail(0);
|
217 |
|
|
data_o(data_rx_overr_c) <= uart_rx.avail(0) and uart_rx.avail(1);
|
218 |
|
|
data_o(data_rx_ferr_c) <= uart_rx.ferr;
|
219 |
|
|
data_o(data_rx_perr_c) <= uart_rx.perr;
|
220 |
|
|
data_o(07 downto 0) <= uart_rx.data;
|
221 |
2 |
zero_gravi |
end if;
|
222 |
|
|
end if;
|
223 |
|
|
end if;
|
224 |
|
|
end process rw_access;
|
225 |
|
|
|
226 |
42 |
zero_gravi |
-- number of bits to be sampled --
|
227 |
|
|
-- if parity flag is ENABLED: 11 bit (1 start bit + 8 data bits + 1 parity bit + 1 stop bit)
|
228 |
|
|
-- if parity flag is DISABLED: 10 bit (1 start bit + 8 data bits + 1 stop bit)
|
229 |
|
|
num_bits <= "1011" when (ctrl(ctrl_uart_pmode1_c) = '1') else "1010";
|
230 |
2 |
zero_gravi |
|
231 |
42 |
zero_gravi |
|
232 |
2 |
zero_gravi |
-- Clock Selection ------------------------------------------------------------------------
|
233 |
|
|
-- -------------------------------------------------------------------------------------------
|
234 |
|
|
-- clock enable --
|
235 |
|
|
clkgen_en_o <= ctrl(ctrl_uart_en_c);
|
236 |
|
|
|
237 |
|
|
-- uart clock select --
|
238 |
|
|
uart_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c))));
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
-- UART Transmitter -----------------------------------------------------------------------
|
242 |
|
|
-- -------------------------------------------------------------------------------------------
|
243 |
|
|
uart_tx_unit: process(clk_i)
|
244 |
|
|
begin
|
245 |
|
|
if rising_edge(clk_i) then
|
246 |
|
|
-- serial engine --
|
247 |
42 |
zero_gravi |
uart_tx.done <= '0';
|
248 |
|
|
if (uart_tx.busy = '0') or (ctrl(ctrl_uart_en_c) = '0') or (ctrl(ctrl_uart_sim_en_c) = '1') then -- idle or disabled or in SIM mode
|
249 |
|
|
uart_tx.busy <= '0';
|
250 |
|
|
uart_tx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
|
251 |
|
|
uart_tx.bitcnt <= num_bits;
|
252 |
|
|
uart_tx.sreg(0) <= '1';
|
253 |
50 |
zero_gravi |
if (wr_en = '1') and (ctrl(ctrl_uart_en_c) = '1') and (addr = uart_id_rtx_addr_c) and (ctrl(ctrl_uart_sim_en_c) = '0') then -- write trigger and not in SIM mode
|
254 |
42 |
zero_gravi |
if (ctrl(ctrl_uart_pmode1_c) = '1') then -- add parity flag
|
255 |
|
|
uart_tx.sreg <= '1' & (xor_all_f(data_i(7 downto 0)) xor ctrl(ctrl_uart_pmode0_c)) & data_i(7 downto 0) & '0'; -- stopbit & parity bit & data & startbit
|
256 |
|
|
else
|
257 |
|
|
uart_tx.sreg <= '1' & '1' & data_i(7 downto 0) & '0'; -- (dummy fill-bit &) stopbit & data & startbit
|
258 |
|
|
end if;
|
259 |
|
|
uart_tx.busy <= '1';
|
260 |
2 |
zero_gravi |
end if;
|
261 |
|
|
elsif (uart_clk = '1') then
|
262 |
42 |
zero_gravi |
if (uart_tx.baud_cnt = x"000") then
|
263 |
|
|
uart_tx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
|
264 |
|
|
uart_tx.bitcnt <= std_ulogic_vector(unsigned(uart_tx.bitcnt) - 1);
|
265 |
|
|
uart_tx.sreg <= '1' & uart_tx.sreg(uart_tx.sreg'left downto 1);
|
266 |
2 |
zero_gravi |
else
|
267 |
42 |
zero_gravi |
uart_tx.baud_cnt <= std_ulogic_vector(unsigned(uart_tx.baud_cnt) - 1);
|
268 |
2 |
zero_gravi |
end if;
|
269 |
42 |
zero_gravi |
if (uart_tx.bitcnt = "0000") then
|
270 |
|
|
uart_tx.busy <= '0'; -- done
|
271 |
|
|
uart_tx.done <= '1';
|
272 |
|
|
end if;
|
273 |
2 |
zero_gravi |
end if;
|
274 |
|
|
-- transmitter output --
|
275 |
42 |
zero_gravi |
uart_txd_o <= uart_tx.sreg(0);
|
276 |
2 |
zero_gravi |
end if;
|
277 |
|
|
end process uart_tx_unit;
|
278 |
|
|
|
279 |
|
|
|
280 |
|
|
-- UART Receiver --------------------------------------------------------------------------
|
281 |
|
|
-- -------------------------------------------------------------------------------------------
|
282 |
|
|
uart_rx_unit: process(clk_i)
|
283 |
|
|
begin
|
284 |
|
|
if rising_edge(clk_i) then
|
285 |
|
|
-- input synchronizer --
|
286 |
42 |
zero_gravi |
uart_rx.sync <= uart_rxd_i & uart_rx.sync(4 downto 1);
|
287 |
2 |
zero_gravi |
|
288 |
|
|
-- serial engine --
|
289 |
42 |
zero_gravi |
if (uart_rx.busy = '0') or (ctrl(ctrl_uart_en_c) = '0') then -- idle or disabled
|
290 |
|
|
uart_rx.busy <= '0';
|
291 |
|
|
uart_rx.baud_cnt <= '0' & ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud01_c); -- half baud delay at the beginning to sample in the middle of each bit
|
292 |
|
|
uart_rx.bitcnt <= num_bits;
|
293 |
|
|
if (ctrl(ctrl_uart_en_c) = '0') then -- to ensure defined state when reading
|
294 |
|
|
uart_rx.perr <= '0';
|
295 |
|
|
uart_rx.ferr <= '0';
|
296 |
|
|
uart_rx.data <= (others => '0');
|
297 |
|
|
elsif (uart_rx.sync(2 downto 0) = "001") then -- start bit? (falling edge)
|
298 |
|
|
uart_rx.busy <= '1';
|
299 |
2 |
zero_gravi |
end if;
|
300 |
|
|
elsif (uart_clk = '1') then
|
301 |
42 |
zero_gravi |
if (uart_rx.baud_cnt = x"000") then
|
302 |
|
|
uart_rx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
|
303 |
|
|
uart_rx.bitcnt <= std_ulogic_vector(unsigned(uart_rx.bitcnt) - 1);
|
304 |
|
|
uart_rx.sreg <= uart_rx.sync(0) & uart_rx.sreg(uart_rx.sreg'left downto 1);
|
305 |
2 |
zero_gravi |
else
|
306 |
42 |
zero_gravi |
uart_rx.baud_cnt <= std_ulogic_vector(unsigned(uart_rx.baud_cnt) - 1);
|
307 |
2 |
zero_gravi |
end if;
|
308 |
42 |
zero_gravi |
if (uart_rx.bitcnt = "0000") then
|
309 |
|
|
uart_rx.busy <= '0'; -- done
|
310 |
|
|
uart_rx.perr <= ctrl(ctrl_uart_pmode1_c) and (xor_all_f(uart_rx.sreg(8 downto 0)) xor ctrl(ctrl_uart_pmode0_c));
|
311 |
|
|
uart_rx.ferr <= not uart_rx.sreg(9); -- check stop bit (error if not set)
|
312 |
|
|
if (ctrl(ctrl_uart_pmode1_c) = '1') then -- add parity flag
|
313 |
|
|
uart_rx.data <= uart_rx.sreg(7 downto 0);
|
314 |
|
|
else
|
315 |
|
|
uart_rx.data <= uart_rx.sreg(8 downto 1);
|
316 |
|
|
end if;
|
317 |
|
|
end if;
|
318 |
2 |
zero_gravi |
end if;
|
319 |
|
|
|
320 |
|
|
-- RX available flag --
|
321 |
42 |
zero_gravi |
uart_rx.busy_ff <= uart_rx.busy;
|
322 |
50 |
zero_gravi |
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_id_rtx_addr_c)) then -- off/RX read access
|
323 |
42 |
zero_gravi |
uart_rx.avail <= "00";
|
324 |
|
|
elsif (uart_rx.busy_ff = '1') and (uart_rx.busy = '0') then -- RX done
|
325 |
|
|
uart_rx.avail <= uart_rx.avail(0) & '1';
|
326 |
2 |
zero_gravi |
end if;
|
327 |
|
|
end if;
|
328 |
|
|
end process uart_rx_unit;
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
-- Interrupt ------------------------------------------------------------------------------
|
332 |
|
|
-- -------------------------------------------------------------------------------------------
|
333 |
48 |
zero_gravi |
-- UART Rx data available
|
334 |
|
|
irq_rxd_o <= uart_rx.busy_ff and (not uart_rx.busy);
|
335 |
|
|
-- UART Tx complete
|
336 |
|
|
irq_txd_o <= uart_tx.done;
|
337 |
2 |
zero_gravi |
|
338 |
|
|
|
339 |
30 |
zero_gravi |
-- SIMULATION Output ----------------------------------------------------------------------
|
340 |
|
|
-- -------------------------------------------------------------------------------------------
|
341 |
|
|
sim_output: process(clk_i) -- for SIMULATION ONLY!
|
342 |
50 |
zero_gravi |
file file_uart_text_out : text open write_mode is sim_uart_text_file_c;
|
343 |
|
|
file file_uart_data_out : text open write_mode is sim_uart_data_file_c;
|
344 |
|
|
variable char_v : integer;
|
345 |
|
|
variable line_screen_v : line; -- we need several line variables here since "writeline" seems to flush the source variable
|
346 |
|
|
variable line_text_v : line;
|
347 |
|
|
variable line_data_v : line;
|
348 |
30 |
zero_gravi |
begin
|
349 |
|
|
if rising_edge(clk_i) then
|
350 |
|
|
if (ctrl(ctrl_uart_en_c) = '1') and (ctrl(ctrl_uart_sim_en_c) = '1') then -- UART enabled and simulation output selected?
|
351 |
50 |
zero_gravi |
if (wr_en = '1') and (addr = uart_id_rtx_addr_c) then -- write access to tx register
|
352 |
30 |
zero_gravi |
|
353 |
|
|
-- print lowest byte to ASCII char --
|
354 |
|
|
char_v := to_integer(unsigned(data_i(7 downto 0)));
|
355 |
|
|
if (char_v >= 128) then -- out of range?
|
356 |
|
|
char_v := 0;
|
357 |
|
|
end if;
|
358 |
|
|
|
359 |
|
|
if (char_v /= 10) and (char_v /= 13) then -- skip line breaks - they are issued via "writeline"
|
360 |
|
|
if (sim_screen_output_en_c = true) then
|
361 |
|
|
write(line_screen_v, character'val(char_v));
|
362 |
|
|
end if;
|
363 |
|
|
if (sim_text_output_en_c = true) then
|
364 |
|
|
write(line_text_v, character'val(char_v));
|
365 |
|
|
end if;
|
366 |
|
|
end if;
|
367 |
|
|
|
368 |
|
|
if (char_v = 10) then -- line break: write to screen and text file
|
369 |
|
|
if (sim_screen_output_en_c = true) then
|
370 |
|
|
writeline(output, line_screen_v);
|
371 |
|
|
end if;
|
372 |
|
|
if (sim_text_output_en_c = true) then
|
373 |
50 |
zero_gravi |
writeline(file_uart_text_out, line_text_v);
|
374 |
30 |
zero_gravi |
end if;
|
375 |
|
|
end if;
|
376 |
|
|
|
377 |
|
|
-- dump raw data as 8 hex char text to file --
|
378 |
|
|
if (sim_data_output_en_c = true) then
|
379 |
|
|
for x in 7 downto 0 loop
|
380 |
|
|
write(line_data_v, to_hexchar_f(data_i(3+x*4 downto 0+x*4))); -- write in hex form
|
381 |
|
|
end loop; -- x
|
382 |
50 |
zero_gravi |
writeline(file_uart_data_out, line_data_v);
|
383 |
30 |
zero_gravi |
end if;
|
384 |
|
|
|
385 |
|
|
end if;
|
386 |
|
|
end if;
|
387 |
|
|
end if;
|
388 |
|
|
end process sim_output;
|
389 |
|
|
|
390 |
|
|
|
391 |
2 |
zero_gravi |
end neorv32_uart_rtl;
|