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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_uart.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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