read 0x43

main
lincaigui 7 months ago
parent 940257ecb7
commit bf72032c0c

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,9 +1,10 @@
module ges_recognize(
input wire sys_clk ,
input wire sys_rst_n ,
input wire sys_clk ,
input wire sys_rst_n ,
output wire scl ,
inout wire sda
output wire scl ,
inout wire sda ,
output wire [3: 0] led
);
wire [2: 0] step ;
wire [5: 0] cfg_num ;
@ -11,7 +12,7 @@ wire [15: 0] cfg_data ;
wire cfg_start ;
wire i2c_clk ;
wire i2c_start ;
wire [7: 0] po_data ;
paj7620_cfg paj7620_cfg_inst(
.i2c_clk (i2c_clk ),
.sys_rst_n (sys_rst_n ),
@ -33,8 +34,15 @@ i2c_ctrl i2c_ctrl_inst(
.step (step ),
.i2c_clk (i2c_clk ),
.scl (scl ),
.sda (sda )
.sda (sda ),
.po_data (po_data )
);
led_ctrl led_ctrl_inst(
.sys_clk (sys_clk ),
.sys_rst_n (sys_rst_n ),
.po_data (po_data ),
.led (led )
);
endmodule

@ -9,7 +9,8 @@ module i2c_ctrl(
output reg [2: 0] step ,
output reg i2c_clk ,
output wire scl ,
inout wire sda
inout wire sda ,
output reg [7: 0] po_data
);
@ -44,6 +45,7 @@ reg skip_en_3 ;//步骤3跳转信号,配置0x00
reg skip_en_4 ;//4,0x20
reg skip_en_5 ;//5,51
reg skip_en_6 ;//6,0x43
reg skip_en_7 ;//7,0x43
reg [1: 0] cnt_i2c_clk ;//i2c
reg [2: 0] cnt_bit ;//bit
reg i2c_end ;//i2c
@ -58,7 +60,7 @@ reg [7: 0] recv_data ;//接受0x20寄存器
reg ack ;//
reg err_en ;//0x20
//
assign sda_en = ((state_c == ACK_1) || (state_c == ACK_2) || (state_c == ACK_3) || (state_c == DATA && step == 3'd3)) ? 1'b0: 1'b1;//
assign sda_en = ((state_c == ACK_1) || (state_c == ACK_2) || (state_c == ACK_3) || (state_c == DATA && (step == 3'd3 || step == 3'd6))) ? 1'b0: 1'b1;//
assign sda_in = sda;
assign sda = (sda_en == 1'b1)? i2c_sda : 1'bz;
//cfg_start
@ -107,13 +109,13 @@ end
//
always @(*)begin
case(state_c)
IDLE: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin
IDLE: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1) || (skip_en_7 == 1'b1))begin
state_n = START;
end
else begin
state_n = IDLE;
end
START: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin
START: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1) || (skip_en_7 == 1'b1))begin
state_n = SLAVE_ADDR;
end
else begin
@ -122,7 +124,7 @@ always @(*)begin
SLAVE_ADDR: if(skip_en_1 == 1'b1)begin
state_n = WAIT;
end
else if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin
else if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1) || (skip_en_7 == 1'b1))begin
state_n = ACK_1;
end
else begin
@ -131,7 +133,7 @@ always @(*)begin
ACK_1: if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin
state_n = DEVICE_ADDR;
end
else if(skip_en_4 == 1'b1)begin
else if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin
state_n = DATA;
end
else begin
@ -155,7 +157,7 @@ always @(*)begin
DATA: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = ACK_3;
end
else if(skip_en_4 == 1'b1)begin
else if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin
state_n = NACK;
end
else if(err_en == 1'b1)begin
@ -164,7 +166,7 @@ always @(*)begin
else begin
state_n = DATA;
end
NACK: if(skip_en_4 == 1'b1)begin
NACK: if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin
state_n = STOP;
end
else begin
@ -182,7 +184,7 @@ always @(*)begin
else begin
state_n = WAIT;
end
STOP: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin
STOP: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1) || (skip_en_7 == 1'b1))begin
state_n = IDLE;
end
else begin
@ -203,6 +205,7 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
skip_en_4 <= 1'b0;//4
skip_en_5 <= 1'b0;//5
skip_en_6 <= 1'b0;//6
skip_en_7 <= 1'b0;//7
step <= 3'd0;
err_en <= 1'b0;
cnt_i2c_clk <= 2'd0;
@ -254,6 +257,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
skip_en_6 <= 1'b0;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
end
START: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
@ -293,6 +302,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
skip_en_6 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
end
SLAVE_ADDR: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
@ -332,6 +347,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
skip_en_6 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd6) && (cnt_bit == 3'd7))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
if((cnt_bit == 3'd7) && (cnt_i2c_clk == 2'd3))begin
cnt_bit <= 3'd0;
end
@ -374,6 +395,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
skip_en_6 <= 1'b0;
end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
end
DEVICE_ADDR:begin
cnt_i2c_clk <= cnt_i2c_clk + 1'b1;
@ -467,6 +494,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
skip_en_5 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd3) && (recv_data != 8'h20))begin
begin
err_en <= 1'b1;
@ -488,6 +521,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
skip_en_4 <= 1'b0;
end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
end
ACK_3: begin
cnt_i2c_clk <= cnt_i2c_clk + 1;
@ -556,6 +595,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
skip_en_6 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
if(cnt_i2c_clk == 2'd2)begin
i2c_end <= 1'b1;
end
@ -580,6 +625,7 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
skip_en_4 <= 1'b0;
skip_en_5 <= 1'b0;
skip_en_6 <= 1'b0;
skip_en_7 <= 1'b0;
err_en <= 1'b0;
step <= step;
cnt_i2c_clk <= 2'd0;
@ -597,7 +643,7 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin
case(state_c)
DATA: begin
if((cnt_i2c_clk == 2'd1) && (step == 3'd3))begin
if((cnt_i2c_clk == 2'd1) && ((step == 3'd3) || (step == 3'd6)))begin
recv_data <= {recv_data[6:0], sda_in};
end
else begin
@ -649,6 +695,11 @@ always @(*)begin
device_addr= 8'h43;
wr_data = 8'h00;
end
3'd6: begin
slave_addr = {SLAVE_ID, 1'b1};//
device_addr= 8'h43;
wr_data = 8'h00;
end
default:begin
slave_addr = 8'h0;
device_addr = 8'h0;
@ -674,7 +725,7 @@ always @(*)begin
START: i2c_sda = (cnt_i2c_clk > 2'd1) ? 1'b0 : 1'b1;
SLAVE_ADDR: i2c_sda = slave_addr[7 - cnt_bit];
DEVICE_ADDR:i2c_sda = device_addr[7 - cnt_bit];
DATA: if(step == 3'd3)begin
DATA: if((step == 3'd3) || (step == 3'd6))begin
i2c_sda = sda_in;
end
else begin
@ -689,4 +740,15 @@ always @(*)begin
endcase
end
assign scl = i2c_scl;
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
po_data <= 8'h0;
end
else if((state_c == DATA) && (step == 3'd6) && (cnt_bit == 3'd7) && (cnt_i2c_clk == 2'd3) && (recv_data != 8'h00))begin
po_data <= recv_data;
end
else begin
po_data <= po_data;
end
end
endmodule

@ -0,0 +1,19 @@
module led_ctrl(
input wire sys_clk ,
input wire sys_rst_n ,
input wire [7: 0] po_data ,
output reg [3: 0] led
);
wire [3: 0] data;
assign data = po_data[3: 0];
always @(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
led <= 4'b0000;
end
else begin
led <= data;
end
end
endmodule
Loading…
Cancel
Save