1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - General Purpose Parallel Input/Output Port (GPIO) >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
22 |
zero_gravi |
-- # 32-bit parallel input & output unit. Any pin change (HI->LO or LO->HI) triggers an IRQ. #
|
5 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
6 |
|
|
-- # BSD 3-Clause License #
|
7 |
|
|
-- # #
|
8 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
9 |
|
|
-- # #
|
10 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
11 |
|
|
-- # permitted provided that the following conditions are met: #
|
12 |
|
|
-- # #
|
13 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
14 |
|
|
-- # conditions and the following disclaimer. #
|
15 |
|
|
-- # #
|
16 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
17 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
18 |
|
|
-- # provided with the distribution. #
|
19 |
|
|
-- # #
|
20 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
21 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
22 |
|
|
-- # permission. #
|
23 |
|
|
-- # #
|
24 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
25 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
26 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
27 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
28 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
29 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
30 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
31 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
32 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
33 |
|
|
-- # ********************************************************************************************* #
|
34 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
35 |
|
|
-- #################################################################################################
|
36 |
|
|
|
37 |
|
|
library ieee;
|
38 |
|
|
use ieee.std_logic_1164.all;
|
39 |
|
|
use ieee.numeric_std.all;
|
40 |
|
|
|
41 |
|
|
library neorv32;
|
42 |
|
|
use neorv32.neorv32_package.all;
|
43 |
|
|
|
44 |
|
|
entity neorv32_gpio is
|
45 |
|
|
port (
|
46 |
|
|
-- host access --
|
47 |
|
|
clk_i : in std_ulogic; -- global clock line
|
48 |
|
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
49 |
|
|
rden_i : in std_ulogic; -- read enable
|
50 |
|
|
wren_i : in std_ulogic; -- write enable
|
51 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
52 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
53 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
54 |
|
|
-- parallel io --
|
55 |
22 |
zero_gravi |
gpio_o : out std_ulogic_vector(31 downto 0);
|
56 |
|
|
gpio_i : in std_ulogic_vector(31 downto 0);
|
57 |
2 |
zero_gravi |
-- interrupt --
|
58 |
|
|
irq_o : out std_ulogic
|
59 |
|
|
);
|
60 |
|
|
end neorv32_gpio;
|
61 |
|
|
|
62 |
|
|
architecture neorv32_gpio_rtl of neorv32_gpio is
|
63 |
|
|
|
64 |
|
|
-- IO space: module base address --
|
65 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
66 |
|
|
constant lo_abb_c : natural := index_size_f(gpio_size_c); -- low address boundary bit
|
67 |
|
|
|
68 |
|
|
-- access control --
|
69 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
70 |
|
|
signal addr : std_ulogic_vector(31 downto 0); -- access address
|
71 |
|
|
|
72 |
|
|
-- accessible regs --
|
73 |
23 |
zero_gravi |
signal din : std_ulogic_vector(31 downto 0); -- r/-
|
74 |
|
|
signal dout : std_ulogic_vector(31 downto 0); -- r/w
|
75 |
|
|
signal irq_en : std_ulogic_vector(31 downto 0); -- -/w, uses the same address as data_in
|
76 |
2 |
zero_gravi |
|
77 |
|
|
-- misc --
|
78 |
22 |
zero_gravi |
signal in_buf : std_ulogic_vector(31 downto 0);
|
79 |
2 |
zero_gravi |
|
80 |
|
|
begin
|
81 |
|
|
|
82 |
|
|
-- Access Control -------------------------------------------------------------------------
|
83 |
|
|
-- -------------------------------------------------------------------------------------------
|
84 |
|
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = gpio_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
85 |
|
|
addr <= gpio_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
-- Read/Write Access ----------------------------------------------------------------------
|
89 |
|
|
-- -------------------------------------------------------------------------------------------
|
90 |
|
|
rw_access: process(clk_i)
|
91 |
|
|
begin
|
92 |
|
|
if rising_edge(clk_i) then
|
93 |
|
|
ack_o <= acc_en and (rden_i or wren_i);
|
94 |
|
|
-- write access --
|
95 |
|
|
if ((acc_en and wren_i) = '1') then
|
96 |
23 |
zero_gravi |
if (addr = gpio_in_addr_c) then
|
97 |
|
|
irq_en <= data_i; -- pin change IRQ enable
|
98 |
|
|
else -- gpio_out_addr_c
|
99 |
|
|
dout <= data_i; -- data output port
|
100 |
2 |
zero_gravi |
end if;
|
101 |
|
|
end if;
|
102 |
|
|
-- read access --
|
103 |
|
|
data_o <= (others => '0');
|
104 |
|
|
if ((acc_en and rden_i) = '1') then
|
105 |
|
|
if (addr = gpio_in_addr_c) then
|
106 |
23 |
zero_gravi |
data_o <= din; -- data input port
|
107 |
2 |
zero_gravi |
else -- gpio_out_addr_c
|
108 |
23 |
zero_gravi |
data_o <= dout; -- data output port
|
109 |
2 |
zero_gravi |
end if;
|
110 |
|
|
end if;
|
111 |
|
|
end if;
|
112 |
|
|
end process rw_access;
|
113 |
|
|
|
114 |
|
|
-- output --
|
115 |
|
|
gpio_o <= dout;
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
-- IRQ Detector ------------------------------------------------------------
|
119 |
|
|
-- -----------------------------------------------------------------------------
|
120 |
|
|
irq_detector: process(clk_i)
|
121 |
|
|
begin
|
122 |
|
|
if rising_edge(clk_i) then
|
123 |
|
|
-- input synchronizer --
|
124 |
|
|
in_buf <= gpio_i;
|
125 |
|
|
din <= in_buf;
|
126 |
|
|
-- IRQ --
|
127 |
23 |
zero_gravi |
irq_o <= or_all_f((in_buf xor din) and irq_en); -- any enabled pin transition triggers an interrupt
|
128 |
2 |
zero_gravi |
end if;
|
129 |
|
|
end process irq_detector;
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
end neorv32_gpio_rtl;
|