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

Subversion Repositories neorv32

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

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 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 48 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
19 2 zero_gravi
-- #                                                                                               #
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 48 zero_gravi
    irq_rxd_o   : out std_ulogic; -- uart data received interrupt
73
    irq_txd_o   : out std_ulogic  -- uart transmission done interrupt
74 2 zero_gravi
  );
75
end neorv32_uart;
76
 
77
architecture neorv32_uart_rtl of neorv32_uart is
78
 
79 30 zero_gravi
  -- simulation output configuration --
80
  constant sim_screen_output_en_c : boolean := true; -- output lowest byte as char to simulator console when enabled
81
  constant sim_text_output_en_c   : boolean := true; -- output lowest byte as char to text file when enabled
82
  constant sim_data_output_en_c   : boolean := true; -- dump 32-word to file when enabled
83
 
84 2 zero_gravi
  -- IO space: module base address --
85
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
86
  constant lo_abb_c : natural := index_size_f(uart_size_c); -- low address boundary bit
87
 
88
  -- accessible regs --
89
  signal ctrl : std_ulogic_vector(31 downto 0);
90
 
91
  -- control reg bits --
92
  constant ctrl_uart_baud00_c  : natural :=  0; -- r/w: UART baud config bit 0
93
  constant ctrl_uart_baud01_c  : natural :=  1; -- r/w: UART baud config bit 1
94
  constant ctrl_uart_baud02_c  : natural :=  2; -- r/w: UART baud config bit 2
95
  constant ctrl_uart_baud03_c  : natural :=  3; -- r/w: UART baud config bit 3
96
  constant ctrl_uart_baud04_c  : natural :=  4; -- r/w: UART baud config bit 4
97
  constant ctrl_uart_baud05_c  : natural :=  5; -- r/w: UART baud config bit 5
98
  constant ctrl_uart_baud06_c  : natural :=  6; -- r/w: UART baud config bit 6
99
  constant ctrl_uart_baud07_c  : natural :=  7; -- r/w: UART baud config bit 7
100
  --
101
  constant ctrl_uart_baud08_c  : natural :=  8; -- r/w: UART baud config bit 8
102
  constant ctrl_uart_baud09_c  : natural :=  9; -- r/w: UART baud config bit 9
103
  constant ctrl_uart_baud10_c  : natural := 10; -- r/w: UART baud config bit 10
104
  constant ctrl_uart_baud11_c  : natural := 11; -- r/w: UART baud config bit 11
105
  --
106 30 zero_gravi
  constant ctrl_uart_sim_en_c  : natural := 12; -- r/w: UART SIMULATION OUTPUT enable
107
  --
108 42 zero_gravi
  constant ctrl_uart_pmode0_c  : natural := 22; -- r/w: Parity config (0=even; 1=odd)
109
  constant ctrl_uart_pmode1_c  : natural := 23; -- r/w: Enable parity bit
110 2 zero_gravi
  constant ctrl_uart_prsc0_c   : natural := 24; -- r/w: UART baud prsc bit 0
111
  constant ctrl_uart_prsc1_c   : natural := 25; -- r/w: UART baud prsc bit 1
112
  constant ctrl_uart_prsc2_c   : natural := 26; -- r/w: UART baud prsc bit 2
113 42 zero_gravi
  --
114 2 zero_gravi
  constant ctrl_uart_en_c      : natural := 28; -- r/w: UART enable
115
  constant ctrl_uart_tx_busy_c : natural := 31; -- r/-: UART transmitter is busy
116
 
117
  -- data register flags --
118 42 zero_gravi
  constant data_rx_avail_c : natural := 31; -- r/-: Rx data available
119
  constant data_rx_overr_c : natural := 30; -- r/-: Rx data overrun
120
  constant data_rx_ferr_c  : natural := 29; -- r/-: Rx frame error
121
  constant data_rx_perr_c  : natural := 28; -- r/-: Rx parity error
122 2 zero_gravi
 
123
  -- access control --
124
  signal acc_en : std_ulogic; -- module access enable
125
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
126
  signal wr_en  : std_ulogic; -- word write enable
127
  signal rd_en  : std_ulogic; -- read enable
128
 
129
  -- clock generator --
130
  signal uart_clk : std_ulogic;
131
 
132 42 zero_gravi
  -- numbers of bits in transmission frame --
133
  signal num_bits : std_ulogic_vector(03 downto 0);
134
 
135 2 zero_gravi
  -- uart tx unit --
136 42 zero_gravi
  type uart_tx_t is record
137
    busy     : std_ulogic;
138
    done     : std_ulogic;
139
    bitcnt   : std_ulogic_vector(03 downto 0);
140
    sreg     : std_ulogic_vector(10 downto 0);
141
    baud_cnt : std_ulogic_vector(11 downto 0);
142
  end record;
143
  signal uart_tx : uart_tx_t;
144 2 zero_gravi
 
145
  -- uart rx unit --
146 42 zero_gravi
  type uart_rx_t is record
147
    sync     : std_ulogic_vector(04 downto 0);
148
    avail    : std_ulogic_vector(01 downto 0);
149
    busy     : std_ulogic;
150
    busy_ff  : std_ulogic;
151
    bitcnt   : std_ulogic_vector(03 downto 0);
152
    sreg     : std_ulogic_vector(09 downto 0);
153
    data     : std_ulogic_vector(07 downto 0);
154
    baud_cnt : std_ulogic_vector(11 downto 0);
155
    ferr     : std_ulogic; -- frame error (stop bit not set)
156
    perr     : std_ulogic; -- parity error
157
  end record;
158
  signal uart_rx : uart_rx_t;
159 2 zero_gravi
 
160
begin
161
 
162
  -- Access Control -------------------------------------------------------------------------
163
  -- -------------------------------------------------------------------------------------------
164
  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';
165
  addr   <= uart_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
166
  wr_en  <= acc_en and wren_i;
167
  rd_en  <= acc_en and rden_i;
168
 
169
 
170
  -- Read/Write Access ----------------------------------------------------------------------
171
  -- -------------------------------------------------------------------------------------------
172
  rw_access: process(clk_i)
173
  begin
174
    if rising_edge(clk_i) then
175
      ack_o <= acc_en and (rden_i or wren_i);
176
      -- write access --
177
      if (wr_en = '1') then
178
        if (addr = uart_ctrl_addr_c) then
179 42 zero_gravi
          ctrl <= (others => '0');
180
          ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c) <= data_i(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
181
          ctrl(ctrl_uart_sim_en_c)                           <= data_i(ctrl_uart_sim_en_c);
182
          ctrl(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c) <= data_i(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c);
183
          ctrl(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c)  <= data_i(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c);
184
          ctrl(ctrl_uart_en_c)                               <= data_i(ctrl_uart_en_c);
185 2 zero_gravi
        end if;
186
      end if;
187
      -- read access --
188
      data_o <= (others => '0');
189
      if (rd_en = '1') then
190
        if (addr = uart_ctrl_addr_c) then
191 42 zero_gravi
          data_o(ctrl_uart_baud11_c downto ctrl_uart_baud00_c) <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
192
          data_o(ctrl_uart_sim_en_c)                           <= ctrl(ctrl_uart_sim_en_c);
193
          data_o(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c) <= ctrl(ctrl_uart_pmode1_c downto ctrl_uart_pmode0_c);
194
          data_o(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c)  <= ctrl(ctrl_uart_prsc2_c  downto ctrl_uart_prsc0_c);
195
          data_o(ctrl_uart_en_c)                               <= ctrl(ctrl_uart_en_c);
196
          data_o(ctrl_uart_tx_busy_c)                          <= uart_tx.busy;
197 2 zero_gravi
        else -- uart_rtx_addr_c
198 42 zero_gravi
          data_o(data_rx_avail_c) <= uart_rx.avail(0);
199
          data_o(data_rx_overr_c) <= uart_rx.avail(0) and uart_rx.avail(1);
200
          data_o(data_rx_ferr_c)  <= uart_rx.ferr;
201
          data_o(data_rx_perr_c)  <= uart_rx.perr;
202
          data_o(07 downto 0)     <= uart_rx.data;
203 2 zero_gravi
        end if;
204
      end if;
205
    end if;
206
  end process rw_access;
207
 
208 42 zero_gravi
  -- number of bits to be sampled --
209
  -- if parity flag is ENABLED:  11 bit (1 start bit + 8 data bits + 1 parity bit + 1 stop bit)
210
  -- if parity flag is DISABLED: 10 bit (1 start bit + 8 data bits + 1 stop bit)
211
  num_bits <= "1011" when (ctrl(ctrl_uart_pmode1_c) = '1') else "1010";
212 2 zero_gravi
 
213 42 zero_gravi
 
214 2 zero_gravi
  -- Clock Selection ------------------------------------------------------------------------
215
  -- -------------------------------------------------------------------------------------------
216
  -- clock enable --
217
  clkgen_en_o <= ctrl(ctrl_uart_en_c);
218
 
219
  -- uart clock select --
220
  uart_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_uart_prsc2_c downto ctrl_uart_prsc0_c))));
221
 
222
 
223
  -- UART Transmitter -----------------------------------------------------------------------
224
  -- -------------------------------------------------------------------------------------------
225
  uart_tx_unit: process(clk_i)
226
  begin
227
    if rising_edge(clk_i) then
228
      -- serial engine --
229 42 zero_gravi
      uart_tx.done <= '0';
230
      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
231
        uart_tx.busy     <= '0';
232
        uart_tx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
233
        uart_tx.bitcnt   <= num_bits;
234
        uart_tx.sreg(0)  <= '1';
235 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
236 42 zero_gravi
          if (ctrl(ctrl_uart_pmode1_c) = '1') then -- add parity flag
237
            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
238
          else
239
            uart_tx.sreg <= '1' & '1' & data_i(7 downto 0) & '0'; -- (dummy fill-bit &) stopbit & data & startbit
240
          end if;
241
          uart_tx.busy <= '1';
242 2 zero_gravi
        end if;
243
      elsif (uart_clk = '1') then
244 42 zero_gravi
        if (uart_tx.baud_cnt = x"000") then
245
          uart_tx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
246
          uart_tx.bitcnt   <= std_ulogic_vector(unsigned(uart_tx.bitcnt) - 1);
247
          uart_tx.sreg     <= '1' & uart_tx.sreg(uart_tx.sreg'left downto 1);
248 2 zero_gravi
        else
249 42 zero_gravi
          uart_tx.baud_cnt <= std_ulogic_vector(unsigned(uart_tx.baud_cnt) - 1);
250 2 zero_gravi
        end if;
251 42 zero_gravi
        if (uart_tx.bitcnt = "0000") then
252
          uart_tx.busy <= '0'; -- done
253
          uart_tx.done <= '1';
254
        end if;
255 2 zero_gravi
      end if;
256
      -- transmitter output --
257 42 zero_gravi
      uart_txd_o <= uart_tx.sreg(0);
258 2 zero_gravi
    end if;
259
  end process uart_tx_unit;
260
 
261
 
262
  -- UART Receiver --------------------------------------------------------------------------
263
  -- -------------------------------------------------------------------------------------------
264
  uart_rx_unit: process(clk_i)
265
  begin
266
    if rising_edge(clk_i) then
267
      -- input synchronizer --
268 42 zero_gravi
      uart_rx.sync <= uart_rxd_i & uart_rx.sync(4 downto 1);
269 2 zero_gravi
 
270
      -- serial engine --
271 42 zero_gravi
      if (uart_rx.busy = '0') or (ctrl(ctrl_uart_en_c) = '0') then -- idle or disabled
272
        uart_rx.busy     <= '0';
273
        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
274
        uart_rx.bitcnt   <= num_bits;
275
        if (ctrl(ctrl_uart_en_c) = '0') then -- to ensure defined state when reading
276
          uart_rx.perr <= '0';
277
          uart_rx.ferr <= '0';
278
          uart_rx.data <= (others => '0');
279
        elsif (uart_rx.sync(2 downto 0) = "001") then -- start bit? (falling edge)
280
          uart_rx.busy <= '1';
281 2 zero_gravi
        end if;
282
      elsif (uart_clk = '1') then
283 42 zero_gravi
        if (uart_rx.baud_cnt = x"000") then
284
          uart_rx.baud_cnt <= ctrl(ctrl_uart_baud11_c downto ctrl_uart_baud00_c);
285
          uart_rx.bitcnt   <= std_ulogic_vector(unsigned(uart_rx.bitcnt) - 1);
286
          uart_rx.sreg     <= uart_rx.sync(0) & uart_rx.sreg(uart_rx.sreg'left downto 1);
287 2 zero_gravi
        else
288 42 zero_gravi
          uart_rx.baud_cnt <= std_ulogic_vector(unsigned(uart_rx.baud_cnt) - 1);
289 2 zero_gravi
        end if;
290 42 zero_gravi
        if (uart_rx.bitcnt = "0000") then
291
          uart_rx.busy <= '0'; -- done
292
          uart_rx.perr <= ctrl(ctrl_uart_pmode1_c) and (xor_all_f(uart_rx.sreg(8 downto 0)) xor ctrl(ctrl_uart_pmode0_c));
293
          uart_rx.ferr <= not uart_rx.sreg(9); -- check stop bit (error if not set)
294
          if (ctrl(ctrl_uart_pmode1_c) = '1') then -- add parity flag
295
            uart_rx.data <= uart_rx.sreg(7 downto 0);
296
          else
297
            uart_rx.data <= uart_rx.sreg(8 downto 1);
298
          end if;
299
        end if;
300 2 zero_gravi
      end if;
301
 
302
      -- RX available flag --
303 42 zero_gravi
      uart_rx.busy_ff <= uart_rx.busy;
304
      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
305
        uart_rx.avail <= "00";
306
      elsif (uart_rx.busy_ff = '1') and (uart_rx.busy = '0') then -- RX done
307
        uart_rx.avail <= uart_rx.avail(0) & '1';
308 2 zero_gravi
      end if;
309
    end if;
310
  end process uart_rx_unit;
311
 
312
 
313
  -- Interrupt ------------------------------------------------------------------------------
314
  -- -------------------------------------------------------------------------------------------
315 48 zero_gravi
  -- UART Rx data available
316
  irq_rxd_o <= uart_rx.busy_ff and (not uart_rx.busy);
317
  -- UART Tx complete
318
  irq_txd_o <= uart_tx.done;
319 2 zero_gravi
 
320
 
321 30 zero_gravi
  -- SIMULATION Output ----------------------------------------------------------------------
322
  -- -------------------------------------------------------------------------------------------
323
  sim_output: process(clk_i) -- for SIMULATION ONLY!
324
    file file_devnull_text_out : text open write_mode is "neorv32.uart.sim_mode.text.out";
325
    file file_devnull_data_out : text open write_mode is "neorv32.uart.sim_mode.data.out";
326
    variable char_v            : integer;
327
    variable line_screen_v     : line; -- we need several line variables here since "writeline" seems to flush the source variable
328
    variable line_text_v       : line;
329
    variable line_data_v       : line;
330
  begin
331
    if rising_edge(clk_i) then
332
      if (ctrl(ctrl_uart_en_c) = '1') and (ctrl(ctrl_uart_sim_en_c) = '1') then -- UART enabled and simulation output selected?
333
        if (wr_en = '1') and (addr = uart_rtx_addr_c) then -- write access to tx register
334
 
335
          -- print lowest byte to ASCII char --
336
          char_v := to_integer(unsigned(data_i(7 downto 0)));
337
          if (char_v >= 128) then -- out of range?
338
            char_v := 0;
339
          end if;
340
 
341
          if (char_v /= 10) and (char_v /= 13) then -- skip line breaks - they are issued via "writeline"
342
            if (sim_screen_output_en_c = true) then
343
              write(line_screen_v, character'val(char_v));
344
            end if;
345
            if (sim_text_output_en_c = true) then
346
              write(line_text_v, character'val(char_v));
347
            end if;
348
          end if;
349
 
350
          if (char_v = 10) then -- line break: write to screen and text file
351
            if (sim_screen_output_en_c = true) then
352
              writeline(output, line_screen_v);
353
            end if;
354
            if (sim_text_output_en_c = true) then
355
              writeline(file_devnull_text_out, line_text_v);
356
            end if;
357
          end if;
358
 
359
          -- dump raw data as 8 hex char text to file --
360
          if (sim_data_output_en_c = true) then
361
            for x in 7 downto 0 loop
362
              write(line_data_v, to_hexchar_f(data_i(3+x*4 downto 0+x*4))); -- write in hex form
363
            end loop; -- x
364
            writeline(file_devnull_data_out, line_data_v);
365
          end if;
366
 
367
        end if;
368
      end if;
369
    end if;
370
  end process sim_output;
371
 
372
 
373 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.