方法一:
在學習IIC的時候我們知道這么設計inout
inout scl ;
reg scl_reg , scl_en ;
scl = scl_en ? scl_reg : 1'dz ;
當scl_en 有效輸出scl_reg 的波形,就是output,否則就是input。
方法二:
后來在工作中遇到了一個IIC的IP核 ,這個IIC的IP核接口是這樣子的
1 mi2c U_mi2c( 2 .CLK (clock), 3 .NRST (reset), 4 .A (a), 5 .DI (di), 6 .WR (wr), 7 .SEL (sel), 8 .ISCL (iscl), 9 .ISDA (isda), 10 .DA (da), 11 .NOE (noe), 12 .INTR (intr), 13 .OSCL (oscl), 14 .OSDA (osda) 15 );
這個IP中,將IIC的input 和output分開了。並且沒有上面說的使能scl_en。這下子我就懵了,經過一番苦心尋找,終於知道如何合並了,廢話不多說,直接上代碼,最后變成這個樣子。
1 module mi2c_top ( 2 clock , 3 reset, 4 a, 5 di, 6 wr, 7 sel, 8 scl, 9 sda, 10 da, 11 noe, 12 intr 13 ); 14 input clock ,reset ; 15 input [2:0] a ; 16 input [7:0] di ; 17 input wr ,sel ; 18 19 output [7:0] da ; 20 output noe ,intr ; 21 22 inout tri1 scl ; 23 inout tri1 sda ; 24 25 wire isda,iscl,osda,oscl ; 26 27 mi2c U_mi2c( 28 .CLK (clock), 29 .NRST (reset), 30 .A (a), 31 .DI (di), 32 .WR (wr), 33 .SEL (sel), 34 .ISCL (iscl), 35 .ISDA (isda), 36 .DA (da), 37 .NOE (noe), 38 .INTR (intr), 39 .OSCL (oscl), 40 .OSDA (osda) 41 ); 42 43 assign iscl = scl ; 44 assign isda = sda ; 45 assign scl = (oscl == 1'd0) ? 1'd0 : 1'dz ; 46 assign sda = (osda == 1'd0) ? 1'd0 : 1'dz ; 47 48 49 endmodule
在modelsim中仿真可以看到,如果是高電平,會顯示虛線,也就是弱上拉的意思。
方法三:
還有一種情況就是,如果是多bit線呢。我這里就有一個例子
wire [7:0] IPMC_CPLD_D_O; wire [7:0] IPMC_CPLD_D_I; wire DATA_OUT_ENA ; wire DATA_INP_ENA ; wire DATA_IOP_ENA ;
wire [7:0] IPMC_CPLD_D_O; 輸出
wire [7:0] IPMC_CPLD_D_I; 輸入
wire DATA_OUT_ENA ; 輸出使能
wire DATA_INP_ENA ; 輸入使能
wire DATA_IOP_ENA ; 片選使能
以上使能都是1有效
那么就需要這么干:
1 reg [7:0] IPMC_CPLD_D_I_reg; 2 assign IPMC_CPLD_D = (DATA_IOP_ENA&DATA_OUT_ENA)?IPMC_CPLD_D_O:8'dz ; 3 assign IPMC_CPLD_D_I = IPMC_CPLD_D_I_reg; 4 always @ (posedge CLOCK or negedge RST_N) 5 if (!RST_N) 6 IPMC_CPLD_D_I_reg <= 8'd0; 7 else if(DATA_IOP_ENA&DATA_INP_ENA) 8 IPMC_CPLD_D_I_reg <= IPMC_CPLD_D;
IPMC_CPLD_D 就是inout 口
小伙伴兒們,以后再也不用擔心inout口了。
歡迎加入: FPGA廣東交流群:162664354
FPGA開發者聯盟: 485678884