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

Subversion Repositories neorv32

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

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 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 2 zero_gravi
  constant ctrl_uart_prsc0_c   : natural := 24; -- r/w: UART baud prsc bit 0
108
  constant ctrl_uart_prsc1_c   : natural := 25; -- r/w: UART baud prsc bit 1
109
  constant ctrl_uart_prsc2_c   : natural := 26; -- r/w: UART baud prsc bit 2
110
  constant ctrl_uart_rxovr_c   : natural := 27; -- r/-: UART RX overrun
111
  constant ctrl_uart_en_c      : natural := 28; -- r/w: UART enable
112
  constant ctrl_uart_rx_irq_c  : natural := 29; -- r/w: UART rx done interrupt enable
113
  constant ctrl_uart_tx_irq_c  : natural := 30; -- r/w: UART tx done interrupt enable
114
  constant ctrl_uart_tx_busy_c : natural := 31; -- r/-: UART transmitter is busy
115
 
116
  -- data register flags --
117
  constant data_rx_avail_c : natural := 31; -- r/-: Rx data available/valid
118
 
119
  -- access control --
120
  signal acc_en : std_ulogic; -- module access enable
121
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
122
  signal wr_en  : std_ulogic; -- word write enable
123
  signal rd_en  : std_ulogic; -- read enable
124
 
125
  -- clock generator --
126
  signal uart_clk : std_ulogic;
127
 
128
  -- uart tx unit --
129
  signal uart_tx_busy     : std_ulogic;
130
  signal uart_tx_done     : std_ulogic;
131
  signal uart_tx_bitcnt   : std_ulogic_vector(03 downto 0);
132
  signal uart_tx_sreg     : std_ulogic_vector(09 downto 0) := (others => '1'); -- just for simulation
133
  signal uart_tx_baud_cnt : std_ulogic_vector(11 downto 0);
134
 
135
  -- uart rx unit --
136
  signal uart_rx_sync     : std_ulogic_vector(04 downto 0);
137
  signal uart_rx_avail    : std_ulogic_vector(01 downto 0);
138
  signal uart_rx_busy     : std_ulogic;
139
  signal uart_rx_busy_ff  : std_ulogic;
140
  signal uart_rx_bitcnt   : std_ulogic_vector(03 downto 0);
141
  signal uart_rx_sreg     : std_ulogic_vector(08 downto 0);
142
  signal uart_rx_reg      : std_ulogic_vector(07 downto 0);
143
  signal uart_rx_baud_cnt : std_ulogic_vector(11 downto 0);
144
 
145
begin
146
 
147
  -- Access Control -------------------------------------------------------------------------
148
  -- -------------------------------------------------------------------------------------------
149
  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';
150
  addr   <= uart_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
151
  wr_en  <= acc_en and wren_i;
152
  rd_en  <= acc_en and rden_i;
153
 
154
 
155
  -- Read/Write Access ----------------------------------------------------------------------
156
  -- -------------------------------------------------------------------------------------------
157
  rw_access: process(clk_i)
158
  begin
159
    if rising_edge(clk_i) then
160
      ack_o <= acc_en and (rden_i or wren_i);
161
      -- write access --
162
      if (wr_en = '1') then
163
        if (addr = uart_ctrl_addr_c) then
164 22 zero_gravi
          ctrl <= data_i;
165 2 zero_gravi
        end if;
166
      end if;
167
      -- read access --
168
      data_o <= (others => '0');
169
      if (rd_en = '1') then
170
        if (addr = uart_ctrl_addr_c) then
171 23 zero_gravi
          data_o <= ctrl; -- default
172 2 zero_gravi
          data_o(ctrl_uart_rxovr_c)   <= uart_rx_avail(0) and uart_rx_avail(1);
173
          data_o(ctrl_uart_tx_busy_c) <= uart_tx_busy;
174
        else -- uart_rtx_addr_c
175
          data_o(data_rx_avail_c) <= uart_rx_avail(0);
176
          data_o(07 downto 0)     <= uart_rx_reg;
177
        end if;
178
      end if;
179
    end if;
180
  end process rw_access;
181
 
182
 
183
  -- Clock Selection ------------------------------------------------------------------------
184
  -- -------------------------------------------------------------------------------------------
185
  -- clock enable --
186
  clkgen_en_o <= ctrl(ctrl_uart_en_c);
187
 
188
  -- uart clock select --
189
  uart_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c))));
190
 
191
 
192
  -- UART Transmitter -----------------------------------------------------------------------
193
  -- -------------------------------------------------------------------------------------------
194
  uart_tx_unit: process(clk_i)
195
  begin
196
    if rising_edge(clk_i) then
197
      -- serial engine --
198
      uart_tx_done <= '0';
199 30 zero_gravi
      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
200 2 zero_gravi
        uart_tx_busy     <= '0';
201
        uart_tx_baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
202
        uart_tx_bitcnt   <= "1010"; -- 10 bit
203 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
204 2 zero_gravi
          uart_tx_sreg <= '1' & data_i(7 downto 0) & '0'; -- stopbit & data & startbit
205
          uart_tx_busy <= '1';
206
        end if;
207
      elsif (uart_clk = '1') then
208
        if (uart_tx_baud_cnt = x"000") then
209
          uart_tx_baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
210
          uart_tx_bitcnt   <= std_ulogic_vector(unsigned(uart_tx_bitcnt) - 1);
211
          uart_tx_sreg     <= '1' & uart_tx_sreg(9 downto 1);
212
          if (uart_tx_bitcnt = "0000") then
213
            uart_tx_busy <= '0'; -- done
214
            uart_tx_done <= '1';
215
          end if;
216
        else
217
          uart_tx_baud_cnt <= std_ulogic_vector(unsigned(uart_tx_baud_cnt) - 1);
218
        end if;
219
      end if;
220
      -- transmitter output --
221
      uart_txd_o <= uart_tx_sreg(0);
222
    end if;
223
  end process uart_tx_unit;
224
 
225
 
226
  -- UART Receiver --------------------------------------------------------------------------
227
  -- -------------------------------------------------------------------------------------------
228
  uart_rx_unit: process(clk_i)
229
  begin
230
    if rising_edge(clk_i) then
231
      -- input synchronizer --
232
      uart_rx_sync <= uart_rxd_i & uart_rx_sync(4 downto 1);
233
 
234
      -- serial engine --
235
      if (uart_rx_busy = '0') or (ctrl(ctrl_uart_en_c) = '0') then -- idle or disabled
236
        uart_rx_busy     <= '0';
237
        uart_rx_baud_cnt <= '0' & ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud01_c); -- half baud rate to sample in middle of bit
238
        uart_rx_bitcnt   <= "1001"; -- 9 bit (startbit + 8 data bits, ignore stop bit/s)
239
        if (ctrl(ctrl_uart_en_c) = '0') then
240
          uart_rx_reg <= (others => '0'); -- to ensure defined state when reading
241
        elsif (uart_rx_sync(2 downto 0) = "001") then -- start bit? (falling edge)
242
          uart_rx_busy <= '1';
243
        end if;
244
      elsif (uart_clk = '1') then
245
        if (uart_rx_baud_cnt = x"000") then
246
          uart_rx_baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
247
          uart_rx_bitcnt   <= std_ulogic_vector(unsigned(uart_rx_bitcnt) - 1);
248
          uart_rx_sreg     <= uart_rx_sync(0) & uart_rx_sreg(8 downto 1);
249
          if (uart_rx_bitcnt = "0000") then
250
            uart_rx_busy <= '0'; -- done
251
            uart_rx_reg  <= uart_rx_sreg(8 downto 1);
252
          end if;
253
        else
254
          uart_rx_baud_cnt <= std_ulogic_vector(unsigned(uart_rx_baud_cnt) - 1);
255
        end if;
256
      end if;
257
 
258
      -- RX available flag --
259
      uart_rx_busy_ff <= uart_rx_busy;
260
      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
261
        uart_rx_avail <= "00";
262
      elsif (uart_rx_busy_ff = '1') and (uart_rx_busy = '0') then
263
        uart_rx_avail <= uart_rx_avail(0) & '1';
264
      end if;
265
    end if;
266
  end process uart_rx_unit;
267
 
268
 
269
  -- Interrupt ------------------------------------------------------------------------------
270
  -- -------------------------------------------------------------------------------------------
271
  -- UART Rx data available [OR] UART Tx complete
272
  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));
273
 
274
 
275 30 zero_gravi
  -- SIMULATION Output ----------------------------------------------------------------------
276
  -- -------------------------------------------------------------------------------------------
277
  sim_output: process(clk_i) -- for SIMULATION ONLY!
278
    file file_devnull_text_out : text open write_mode is "neorv32.uart.sim_mode.text.out";
279
    file file_devnull_data_out : text open write_mode is "neorv32.uart.sim_mode.data.out";
280
    variable char_v            : integer;
281
    variable line_screen_v     : line; -- we need several line variables here since "writeline" seems to flush the source variable
282
    variable line_text_v       : line;
283
    variable line_data_v       : line;
284
  begin
285
    if rising_edge(clk_i) then
286
      if (ctrl(ctrl_uart_en_c) = '1') and (ctrl(ctrl_uart_sim_en_c) = '1') then -- UART enabled and simulation output selected?
287
        if (wr_en = '1') and (addr = uart_rtx_addr_c) then -- write access to tx register
288
 
289
          -- print lowest byte to ASCII char --
290
          char_v := to_integer(unsigned(data_i(7 downto 0)));
291
          if (char_v >= 128) then -- out of range?
292
            char_v := 0;
293
          end if;
294
 
295
          if (char_v /= 10) and (char_v /= 13) then -- skip line breaks - they are issued via "writeline"
296
            if (sim_screen_output_en_c = true) then
297
              write(line_screen_v, character'val(char_v));
298
            end if;
299
            if (sim_text_output_en_c = true) then
300
              write(line_text_v, character'val(char_v));
301
            end if;
302
          end if;
303
 
304
          if (char_v = 10) then -- line break: write to screen and text file
305
            if (sim_screen_output_en_c = true) then
306
              writeline(output, line_screen_v);
307
            end if;
308
            if (sim_text_output_en_c = true) then
309
              writeline(file_devnull_text_out, line_text_v);
310
            end if;
311
          end if;
312
 
313
          -- dump raw data as 8 hex char text to file --
314
          if (sim_data_output_en_c = true) then
315
            for x in 7 downto 0 loop
316
              write(line_data_v, to_hexchar_f(data_i(3+x*4 downto 0+x*4))); -- write in hex form
317
            end loop; -- x
318
            writeline(file_devnull_data_out, line_data_v);
319
          end if;
320
 
321
        end if;
322
      end if;
323
    end if;
324
  end process sim_output;
325
 
326
 
327 2 zero_gravi
end neorv32_uart_rtl;

powered by: WebSVN 2.1.0

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