【VHDL】inout双向端口的使用
世界Skill
在FPGA设计中常常会遇到和外设的IO口相连的情况,这时候就要求我们必须使用它的双向端口,但这个端口的使用是由一定要求的,尤其是在Altera的器件中尚未发现特例。
inout端口的使用
1.结构
双向端口是一种特殊的三态资源,尤其是Altera的器件中基本是只存在于IO口附近的,在器件内部一般不会有三态门的存在。
双向端口可以看作一个选通器,这就决定了它的使用形式。
2.使用方式
双向端口有三种状态,即“高电平1”、“低电平0”、“高阻态Z”。其中“Z”态用于控制传输方向,操作如下:
entity TEST is
port
(
clk:in std_logic;
iotest:inout std_logic
);
end entity;
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
outp:process(clk)
begin
if clk'event and clk='1' then
iotest<='1';
end if;
end process;
inp:process(clk)
begin
if clk'event and clk='1' then
iotest<='Z';
insignal<=iotest;
end if;
end process;
以上,线程outp用于输出,inp用于输入。可以看到,在每一次输入前要先将端口置为高阻态。
3.非顶层模块实现io口
在模块化设计中经常会出现模块端口需要双向的情况,比如在调用一个RAM的控制器时,一般做法是将模块的端口映射到顶层,然后顶层与外部相连。
这时,将模块和顶层的inout端口直接映射、三态操作放在模块内虽然可以使用(实测,隐患未知),但会产生警告,告诉你三态端口没有被正常使用,这里建议不要这么做,而是换一种更为标准的做法,如下:
在非顶层模块中,将一个定义为inout类型的端口拆分为三个端口:
iotest_in:in std_logic;
iotest_out:out std_logic;
iotest_en:out std_logic;
也就是拆分为输入、输出和输出使能三个信号,然后再顶层中进行对应操作即可:
with iotest_en select
iotest<=iotest_out when '1';
'Z' when '0';
null when others;
iotest_in<=iotest;
点击查看评论