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

Subversion Repositories neorv32

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

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
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
54
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
55
    ack_o       : out std_ulogic; -- transfer acknowledge
56
    -- clock generator --
57
    clkgen_en_o : out std_ulogic; -- enable clock generator
58
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
59
    -- com lines --
60
    uart_txd_o  : out std_ulogic;
61
    uart_rxd_i  : in  std_ulogic;
62
    -- interrupts --
63
    uart_irq_o  : out std_ulogic  -- uart rx/tx interrupt
64
  );
65
end neorv32_uart;
66
 
67
architecture neorv32_uart_rtl of neorv32_uart is
68
 
69
  -- IO space: module base address --
70
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
71
  constant lo_abb_c : natural := index_size_f(uart_size_c); -- low address boundary bit
72
 
73
  -- accessible regs --
74
  signal ctrl : std_ulogic_vector(31 downto 0);
75
 
76
  -- control reg bits --
77
  constant ctrl_uart_baud00_c  : natural :=  0; -- r/w: UART baud config bit 0
78
  constant ctrl_uart_baud01_c  : natural :=  1; -- r/w: UART baud config bit 1
79
  constant ctrl_uart_baud02_c  : natural :=  2; -- r/w: UART baud config bit 2
80
  constant ctrl_uart_baud03_c  : natural :=  3; -- r/w: UART baud config bit 3
81
  constant ctrl_uart_baud04_c  : natural :=  4; -- r/w: UART baud config bit 4
82
  constant ctrl_uart_baud05_c  : natural :=  5; -- r/w: UART baud config bit 5
83
  constant ctrl_uart_baud06_c  : natural :=  6; -- r/w: UART baud config bit 6
84
  constant ctrl_uart_baud07_c  : natural :=  7; -- r/w: UART baud config bit 7
85
  --
86
  constant ctrl_uart_baud08_c  : natural :=  8; -- r/w: UART baud config bit 8
87
  constant ctrl_uart_baud09_c  : natural :=  9; -- r/w: UART baud config bit 9
88
  constant ctrl_uart_baud10_c  : natural := 10; -- r/w: UART baud config bit 10
89
  constant ctrl_uart_baud11_c  : natural := 11; -- r/w: UART baud config bit 11
90
  --
91
  constant ctrl_uart_prsc0_c   : natural := 24; -- r/w: UART baud prsc bit 0
92
  constant ctrl_uart_prsc1_c   : natural := 25; -- r/w: UART baud prsc bit 1
93
  constant ctrl_uart_prsc2_c   : natural := 26; -- r/w: UART baud prsc bit 2
94
  constant ctrl_uart_rxovr_c   : natural := 27; -- r/-: UART RX overrun
95
  constant ctrl_uart_en_c      : natural := 28; -- r/w: UART enable
96
  constant ctrl_uart_rx_irq_c  : natural := 29; -- r/w: UART rx done interrupt enable
97
  constant ctrl_uart_tx_irq_c  : natural := 30; -- r/w: UART tx done interrupt enable
98
  constant ctrl_uart_tx_busy_c : natural := 31; -- r/-: UART transmitter is busy
99
 
100
  -- data register flags --
101
  constant data_rx_avail_c : natural := 31; -- r/-: Rx data available/valid
102
 
103
  -- access control --
104
  signal acc_en : std_ulogic; -- module access enable
105
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
106
  signal wr_en  : std_ulogic; -- word write enable
107
  signal rd_en  : std_ulogic; -- read enable
108
 
109
  -- clock generator --
110
  signal uart_clk : std_ulogic;
111
 
112
  -- uart tx unit --
113
  signal uart_tx_busy     : std_ulogic;
114
  signal uart_tx_done     : std_ulogic;
115
  signal uart_tx_bitcnt   : std_ulogic_vector(03 downto 0);
116
  signal uart_tx_sreg     : std_ulogic_vector(09 downto 0) := (others => '1'); -- just for simulation
117
  signal uart_tx_baud_cnt : std_ulogic_vector(11 downto 0);
118
 
119
  -- uart rx unit --
120
  signal uart_rx_sync     : std_ulogic_vector(04 downto 0);
121
  signal uart_rx_avail    : std_ulogic_vector(01 downto 0);
122
  signal uart_rx_busy     : std_ulogic;
123
  signal uart_rx_busy_ff  : std_ulogic;
124
  signal uart_rx_bitcnt   : std_ulogic_vector(03 downto 0);
125
  signal uart_rx_sreg     : std_ulogic_vector(08 downto 0);
126
  signal uart_rx_reg      : std_ulogic_vector(07 downto 0);
127
  signal uart_rx_baud_cnt : std_ulogic_vector(11 downto 0);
128
 
129
begin
130
 
131
  -- Access Control -------------------------------------------------------------------------
132
  -- -------------------------------------------------------------------------------------------
133
  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';
134
  addr   <= uart_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
135
  wr_en  <= acc_en and wren_i;
136
  rd_en  <= acc_en and rden_i;
137
 
138
 
139
  -- Read/Write Access ----------------------------------------------------------------------
140
  -- -------------------------------------------------------------------------------------------
141
  rw_access: process(clk_i)
142
  begin
143
    if rising_edge(clk_i) then
144
      ack_o <= acc_en and (rden_i or wren_i);
145
      -- write access --
146
      if (wr_en = '1') then
147
        if (addr = uart_ctrl_addr_c) then
148 22 zero_gravi
          ctrl <= data_i;
149 2 zero_gravi
        end if;
150
      end if;
151
      -- read access --
152
      data_o <= (others => '0');
153
      if (rd_en = '1') then
154
        if (addr = uart_ctrl_addr_c) then
155
          data_o(ctrl_uart_baud00_c)  <= ctrl(ctrl_uart_baud00_c);
156
          data_o(ctrl_uart_baud01_c)  <= ctrl(ctrl_uart_baud01_c);
157
          data_o(ctrl_uart_baud02_c)  <= ctrl(ctrl_uart_baud02_c);
158
          data_o(ctrl_uart_baud03_c)  <= ctrl(ctrl_uart_baud03_c);
159
          data_o(ctrl_uart_baud04_c)  <= ctrl(ctrl_uart_baud04_c);
160
          data_o(ctrl_uart_baud05_c)  <= ctrl(ctrl_uart_baud05_c);
161
          data_o(ctrl_uart_baud06_c)  <= ctrl(ctrl_uart_baud06_c);
162
          data_o(ctrl_uart_baud07_c)  <= ctrl(ctrl_uart_baud07_c);
163
          --
164
          data_o(ctrl_uart_baud08_c)  <= ctrl(ctrl_uart_baud08_c);
165
          data_o(ctrl_uart_baud09_c)  <= ctrl(ctrl_uart_baud09_c);
166
          data_o(ctrl_uart_baud10_c)  <= ctrl(ctrl_uart_baud10_c);
167
          data_o(ctrl_uart_baud11_c)  <= ctrl(ctrl_uart_baud11_c);
168
          --
169
          data_o(ctrl_uart_prsc0_c)   <= ctrl(ctrl_uart_prsc0_c);
170
          data_o(ctrl_uart_prsc1_c)   <= ctrl(ctrl_uart_prsc1_c);
171
          data_o(ctrl_uart_prsc2_c)   <= ctrl(ctrl_uart_prsc2_c);
172
          data_o(ctrl_uart_rxovr_c)   <= uart_rx_avail(0) and uart_rx_avail(1);
173
          data_o(ctrl_uart_en_c)      <= ctrl(ctrl_uart_en_c);
174
          data_o(ctrl_uart_rx_irq_c)  <= ctrl(ctrl_uart_rx_irq_c);
175
          data_o(ctrl_uart_tx_irq_c)  <= ctrl(ctrl_uart_tx_irq_c);
176
          data_o(ctrl_uart_tx_busy_c) <= uart_tx_busy;
177
        else -- uart_rtx_addr_c
178
          data_o(data_rx_avail_c) <= uart_rx_avail(0);
179
          data_o(07 downto 0)     <= uart_rx_reg;
180
        end if;
181
      end if;
182
    end if;
183
  end process rw_access;
184
 
185
 
186
  -- Clock Selection ------------------------------------------------------------------------
187
  -- -------------------------------------------------------------------------------------------
188
  -- clock enable --
189
  clkgen_en_o <= ctrl(ctrl_uart_en_c);
190
 
191
  -- uart clock select --
192
  uart_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c))));
193
 
194
 
195
  -- UART Transmitter -----------------------------------------------------------------------
196
  -- -------------------------------------------------------------------------------------------
197
  uart_tx_unit: process(clk_i)
198
  begin
199
    if rising_edge(clk_i) then
200
      -- serial engine --
201
      uart_tx_done <= '0';
202
      if (uart_tx_busy = '0') or (ctrl(ctrl_uart_en_c) = '0') then -- idle or disabled
203
        uart_tx_busy     <= '0';
204
        uart_tx_baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
205
        uart_tx_bitcnt   <= "1010"; -- 10 bit
206
        if (wr_en = '1') and (ctrl(ctrl_uart_en_c) = '1') and (addr = uart_rtx_addr_c) then
207
          uart_tx_sreg <= '1' & data_i(7 downto 0) & '0'; -- stopbit & data & startbit
208
          uart_tx_busy <= '1';
209
        end if;
210
      elsif (uart_clk = '1') then
211
        if (uart_tx_baud_cnt = x"000") then
212
          uart_tx_baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
213
          uart_tx_bitcnt   <= std_ulogic_vector(unsigned(uart_tx_bitcnt) - 1);
214
          uart_tx_sreg     <= '1' & uart_tx_sreg(9 downto 1);
215
          if (uart_tx_bitcnt = "0000") then
216
            uart_tx_busy <= '0'; -- done
217
            uart_tx_done <= '1';
218
          end if;
219
        else
220
          uart_tx_baud_cnt <= std_ulogic_vector(unsigned(uart_tx_baud_cnt) - 1);
221
        end if;
222
      end if;
223
      -- transmitter output --
224
      uart_txd_o <= uart_tx_sreg(0);
225
    end if;
226
  end process uart_tx_unit;
227
 
228
 
229
  -- UART Receiver --------------------------------------------------------------------------
230
  -- -------------------------------------------------------------------------------------------
231
  uart_rx_unit: process(clk_i)
232
  begin
233
    if rising_edge(clk_i) then
234
      -- input synchronizer --
235
      uart_rx_sync <= uart_rxd_i & uart_rx_sync(4 downto 1);
236
 
237
      -- serial engine --
238
      if (uart_rx_busy = '0') or (ctrl(ctrl_uart_en_c) = '0') then -- idle or disabled
239
        uart_rx_busy     <= '0';
240
        uart_rx_baud_cnt <= '0' & ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud01_c); -- half baud rate to sample in middle of bit
241
        uart_rx_bitcnt   <= "1001"; -- 9 bit (startbit + 8 data bits, ignore stop bit/s)
242
        if (ctrl(ctrl_uart_en_c) = '0') then
243
          uart_rx_reg <= (others => '0'); -- to ensure defined state when reading
244
        elsif (uart_rx_sync(2 downto 0) = "001") then -- start bit? (falling edge)
245
          uart_rx_busy <= '1';
246
        end if;
247
      elsif (uart_clk = '1') then
248
        if (uart_rx_baud_cnt = x"000") then
249
          uart_rx_baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
250
          uart_rx_bitcnt   <= std_ulogic_vector(unsigned(uart_rx_bitcnt) - 1);
251
          uart_rx_sreg     <= uart_rx_sync(0) & uart_rx_sreg(8 downto 1);
252
          if (uart_rx_bitcnt = "0000") then
253
            uart_rx_busy <= '0'; -- done
254
            uart_rx_reg  <= uart_rx_sreg(8 downto 1);
255
          end if;
256
        else
257
          uart_rx_baud_cnt <= std_ulogic_vector(unsigned(uart_rx_baud_cnt) - 1);
258
        end if;
259
      end if;
260
 
261
      -- RX available flag --
262
      uart_rx_busy_ff <= uart_rx_busy;
263
      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
264
        uart_rx_avail <= "00";
265
      elsif (uart_rx_busy_ff = '1') and (uart_rx_busy = '0') then
266
        uart_rx_avail <= uart_rx_avail(0) & '1';
267
      end if;
268
    end if;
269
  end process uart_rx_unit;
270
 
271
 
272
  -- Interrupt ------------------------------------------------------------------------------
273
  -- -------------------------------------------------------------------------------------------
274
  -- UART Rx data available [OR] UART Tx complete
275
  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));
276
 
277
 
278
end neorv32_uart_rtl;

powered by: WebSVN 2.1.0

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