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