modify i2c_ctrl.v

v1
lincaigui 7 months ago
parent 5373920978
commit b31bda8983

@ -39,13 +39,14 @@ reg [4: 0] cnt_clk;
// //
reg [9: 0] cnt_wait ;//1000us reg [9: 0] cnt_wait ;//1000us
reg skip_en_1 ;//1, // reg skip_en_1 ;//1,
reg skip_en_2 ;//2,bank0 // reg skip_en_2 ;//2,bank0
reg skip_en_3 ;//3,0x00 // reg skip_en_3 ;//3,0x00
reg skip_en_4 ;//4,0x20 // reg skip_en_4 ;//4,0x20
reg skip_en_5 ;//5,51 // reg skip_en_5 ;//5,51
reg skip_en_6 ;//6,0x43 // reg skip_en_6 ;//6,0x43
reg skip_en_7 ;//7,0x43 // reg skip_en_7 ;//7,0x43
reg skip_en ;
reg [1: 0] cnt_i2c_clk ;//i2c reg [1: 0] cnt_i2c_clk ;//i2c
reg [2: 0] cnt_bit ;//bit reg [2: 0] cnt_bit ;//bit
reg i2c_end ;//i2c reg i2c_end ;//i2c
@ -96,6 +97,84 @@ always @(posedge sys_clk or negedge sys_rst_n) begin
i2c_clk <= i2c_clk; i2c_clk <= i2c_clk;
end end
end end
//step
always @(*)begin
case(step)
3'd0: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr = 8'h0;
wr_data = 8'h0;
end
3'd1: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= {8'hef};
wr_data = {8'h00};
end
3'd2: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= {8'h00};
wr_data = {8'h00};
end
3'd3: begin
slave_addr = {SLAVE_ID, 1'b1};//
device_addr= {8'h00};
wr_data = {8'h00};
end
3'd4: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= cfg_data[15: 8];
wr_data = cfg_data[7: 0];
end
3'd5: begin
slave_addr = {SLAVE_ID, 1'b0};//
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;
wr_data = 8'h0;
end
endcase
end
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cnt_i2c_clk <= 2'd0;
cnt_bit <= 3'd0;
cnt_wait <= 10'd0;
end
else begin
case(state_c)
IDLE, WAIT: if(cnt_wait >= MAX - 1'd1)begin
cnt_wait <= 10'd0;
end
else begin
cnt_wait <= cnt_wait + 1'd1;
end
START, STOP, ACK_1, ACK_2, ACK_3, NACK:
cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
SLAVE_ADDR, DEVICE_ADDR, DATA: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
if((cnt_i2c_clk == 2'd3))begin
cnt_bit <= cnt_bit + 1'd1;
end
else begin
cnt_bit <= cnt_bit;
end
end
default:begin
cnt_i2c_clk <= 2'd0;
cnt_bit <= 3'd0;
cnt_wait <= 10'd0;
end
endcase
end
end
///////// /////////
// //
always @(posedge i2c_clk or negedge sys_rst_n) begin always @(posedge i2c_clk or negedge sys_rst_n) begin
@ -109,55 +188,86 @@ end
// //
always @(*)begin always @(*)begin
case(state_c) 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) || (skip_en_7 == 1'b1))begin IDLE: if(((skip_en == 1'b1) && (step == 3'd0))
|| ((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd2))
|| ((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd4))
|| ((skip_en == 1'b1) && (step == 3'd5))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = START; state_n = START;
end end
else begin else begin
state_n = IDLE; state_n = IDLE;
end 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) || (skip_en_7 == 1'b1))begin START: if(((skip_en == 1'b1) && (step == 3'd0))
|| ((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd2))
|| ((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd4))
|| ((skip_en == 1'b1) && (step == 3'd5))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = SLAVE_ADDR; state_n = SLAVE_ADDR;
end end
else begin else begin
state_n = START; state_n = START;
end end
SLAVE_ADDR: if(skip_en_1 == 1'b1)begin SLAVE_ADDR: if(((skip_en == 1'b1) && (step == 3'd0)))begin
state_n = WAIT; state_n = WAIT;
end 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) || (skip_en_7 == 1'b1))begin else if(((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd2))
|| ((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd4))
|| ((skip_en == 1'b1) && (step == 3'd5))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = ACK_1; state_n = ACK_1;
end end
else begin else begin
state_n = SLAVE_ADDR; state_n = SLAVE_ADDR;
end end
ACK_1: if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin ACK_1: if(((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd2))
|| ((skip_en == 1'b1) && (step == 3'd4))
|| ((skip_en == 1'b1) && (step == 3'd5)))begin
state_n = DEVICE_ADDR; state_n = DEVICE_ADDR;
end end
else if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin else if(((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = DATA; state_n = DATA;
end end
else begin else begin
state_n = ACK_1; state_n = ACK_1;
end end
DEVICE_ADDR:if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin DEVICE_ADDR:if(((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd2))
|| ((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd4))
|| ((skip_en == 1'b1) && (step == 3'd5))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = ACK_2; state_n = ACK_2;
end end
else begin else begin
state_n = DEVICE_ADDR; state_n = DEVICE_ADDR;
end end
ACK_2: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin ACK_2: if(((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd4)))begin
state_n = DATA; state_n = DATA;
end end
else if((skip_en_3 == 1'b1) || (skip_en_6 == 1'b1))begin else if(((skip_en == 1'b1) && (step == 3'd2))
|| ((skip_en == 1'b1) && (step == 3'd5)))begin
state_n = STOP; state_n = STOP;
end end
else begin else begin
state_n = ACK_2; state_n = ACK_2;
end end
DATA: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
DATA: if(((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd4)))begin
state_n = ACK_3; state_n = ACK_3;
end end
else if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin else if(((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = NACK; state_n = NACK;
end end
else if(err_en == 1'b1)begin else if(err_en == 1'b1)begin
@ -166,30 +276,39 @@ always @(*)begin
else begin else begin
state_n = DATA; state_n = DATA;
end end
NACK: if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin NACK: if(((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = STOP; state_n = STOP;
end end
else begin else begin
state_n = NACK; state_n = NACK;
end end
ACK_3: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin ACK_3: if(((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd4)))begin
state_n = STOP; state_n = STOP;
end end
else begin else begin
state_n = ACK_3; state_n = ACK_3;
end end
WAIT: if(skip_en_1 == 1'b1)begin WAIT: if(((skip_en == 1'b1) && (step == 3'd0)))begin
state_n = STOP; state_n = STOP;
end end
else begin else begin
state_n = WAIT; state_n = WAIT;
end 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) || (skip_en_7 == 1'b1))begin STOP: if(((skip_en == 1'b1) && (step == 3'd0))
|| ((skip_en == 1'b1) && (step == 3'd1))
|| ((skip_en == 1'b1) && (step == 3'd2))
|| ((skip_en == 1'b1) && (step == 3'd3))
|| ((skip_en == 1'b1) && (step == 3'd4))
|| ((skip_en == 1'b1) && (step == 3'd5))
|| ((skip_en == 1'b1) && (step == 3'd6)))begin
state_n = IDLE; state_n = IDLE;
end end
else begin else begin
state_n = STOP; state_n = STOP;
end end
default: begin default: begin
state_n = IDLE; state_n = IDLE;
end end
@ -198,307 +317,53 @@ end
// //
always @(posedge i2c_clk or negedge sys_rst_n)begin always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin if(!sys_rst_n)begin
cnt_wait <= 10'd0; skip_en <= 1'b0;
skip_en_1 <= 1'b0;//1
skip_en_2 <= 1'b0;//2
skip_en_3 <= 1'b0;//3
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; step <= 3'd0;
err_en <= 1'b0; err_en <= 1'b0;
cnt_i2c_clk <= 2'd0;
cnt_bit <= 3'd0;
i2c_end <= 1'b0; i2c_end <= 1'b0;
end end
else begin else begin
case(state_c) case(state_c)
IDLE: begin IDLE, WAIT: begin
if(cnt_wait == MAX - 1)begin if((cnt_wait == MAX - 2'd2))begin
cnt_wait <= 10'd0; skip_en <= 1'b1;
end
else begin
cnt_wait <= cnt_wait + 1;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd0))begin
skip_en_1 <= 1'b1;
end
else begin
skip_en_1 <= 1'b0;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end end
else begin else begin
skip_en_5 <= 1'b0; skip_en <= 1'b0;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end
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
end end
START: begin START: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1; if(cnt_i2c_clk == 2'd2)begin
if((cnt_i2c_clk == 2'd2) && (step == 3'd0))begin skip_en <= 1'b1;
skip_en_1 <= 1'b1;
end
else begin
skip_en_1 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end
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;
if((cnt_i2c_clk == 2'd2) && (step == 3'd0) && (cnt_bit == 3'd7))begin
skip_en_1 <= 1'b1;
end
else begin
skip_en_1 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd1) && (cnt_bit == 3'd7))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd2) && (cnt_bit == 3'd7))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd3) && (cnt_bit == 3'd7))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd4) && (cnt_bit == 3'd7))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd5) && (cnt_bit == 3'd7))begin
skip_en_6 <= 1'b1;
end
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
else if(cnt_i2c_clk == 2'd3)begin
cnt_bit <= cnt_bit + 1'd1;
end
else begin
cnt_bit <= cnt_bit;
end
end
ACK_1: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end
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;
if((cnt_i2c_clk == 2'd3) && (cnt_bit == 3'd7))begin
cnt_bit <= 3'd0;
end
else if(cnt_i2c_clk == 2'd3)begin
cnt_bit <= cnt_bit + 1'b1;
end
else begin
cnt_bit <= cnt_bit;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end end
else begin else begin
skip_en_6 <= 1'b0; skip_en <= 1'b0;
end
end end
ACK_2: begin
cnt_i2c_clk <= cnt_i2c_clk + 1;
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end end
else begin SLAVE_ADDR, DEVICE_ADDR:
skip_en_2 <= 1'b0; begin
end if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7))begin
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd2))begin skip_en <= 1'b1;
skip_en_3 <= 1'b1;
end end
else begin else begin
skip_en_3 <= 1'b0; skip_en <= 1'b0;
end end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end end
else begin ACK_1, ACK_2, ACK_3, NACK:
skip_en_5 <= 1'b0; begin
end if((ack == 1'b1) && (cnt_i2c_clk == 2'd2))begin
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd5))begin skip_en <= 1'b1;
skip_en_6 <= 1'b1;
end end
else begin else begin
skip_en_6 <= 1'b0; skip_en <= 1'b0;
end end
end end
DATA: begin DATA: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'b1; if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7))begin
if((cnt_i2c_clk == 2'd3) && (cnt_bit == 3'd7))begin skip_en <= 1'b1;
cnt_bit <= 3'd0;
end
else if(cnt_i2c_clk == 2'd3)begin
cnt_bit <= cnt_bit + 1'b1;
end
else begin
cnt_bit <= cnt_bit;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd3) && (recv_data == 8'h20))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end end
else begin else begin
skip_en_5 <= 1'b0; skip_en <= 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 end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd3) && (recv_data != 8'h20))begin if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd3) && (recv_data != 8'h20))begin
begin begin
@ -513,93 +378,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
end end
end end
end end
NACK: begin
cnt_i2c_clk <= cnt_i2c_clk + 1;
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
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;
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
end
WAIT: begin
if(cnt_wait == MAX - 1'd1)begin
cnt_wait <= 10'd0;
end
else begin
cnt_wait <= cnt_wait + 1'd1;
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd0))begin
skip_en_1 <= 1'b1;
end
else begin
skip_en_1 <= 1'b0;
end
end
STOP: begin STOP: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1; if((cnt_i2c_clk == 2'd2))begin
if((cnt_i2c_clk == 2'd2) && (step == 3'd0))begin skip_en <= 1'b1;
skip_en_1 <= 1'b1;
end
else begin
skip_en_1 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end end
else begin else begin
skip_en_6 <= 1'b0; skip_en <= 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
if(cnt_i2c_clk == 2'd2)begin if(cnt_i2c_clk == 2'd2)begin
i2c_end <= 1'b1; i2c_end <= 1'b1;
@ -619,17 +403,9 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
end end
default: begin default: begin
cnt_wait <= 10'd0; cnt_wait <= 10'd0;
skip_en_1 <= 1'b0; skip_en <= 1'b0;
skip_en_2 <= 1'b0;
skip_en_3 <= 1'b0;
skip_en_4 <= 1'b0;
skip_en_5 <= 1'b0;
skip_en_6 <= 1'b0;
skip_en_7 <= 1'b0;
err_en <= 1'b0; err_en <= 1'b0;
step <= step; step <= step;
cnt_i2c_clk <= 2'd0;
cnt_bit <= 3'd0;
i2c_end <= 1'b0; i2c_end <= 1'b0;
end end
endcase endcase
@ -662,51 +438,7 @@ always @(*)begin
default: ack = 1'b0; default: ack = 1'b0;
endcase endcase
end end
//step
always @(*)begin
case(step)
3'd0: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr = 8'h0;
wr_data = 8'h0;
end
3'd1: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= {8'hef};
wr_data = {8'h00};
end
3'd2: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= {8'h00};
wr_data = {8'h00};
end
3'd3: begin
slave_addr = {SLAVE_ID, 1'b1};//
device_addr= {8'h00};
wr_data = {8'h00};
end
3'd4: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= cfg_data[15: 8];
wr_data = cfg_data[7: 0];
end
3'd5: begin
slave_addr = {SLAVE_ID, 1'b0};//
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;
wr_data = 8'h0;
end
endcase
end
//i2c_scl //i2c_scl
always @(*)begin always @(*)begin
case(state_c) case(state_c)
@ -721,7 +453,7 @@ end
//i2c_sda //i2c_sda
always @(*)begin always @(*)begin
case(state_c) case(state_c)
IDLE: i2c_sda = 1'b1; IDLE, NACK: i2c_sda = 1'b1;
START: i2c_sda = (cnt_i2c_clk > 2'd1) ? 1'b0 : 1'b1; START: i2c_sda = (cnt_i2c_clk > 2'd1) ? 1'b0 : 1'b1;
SLAVE_ADDR: i2c_sda = slave_addr[7 - cnt_bit]; SLAVE_ADDR: i2c_sda = slave_addr[7 - cnt_bit];
DEVICE_ADDR:i2c_sda = device_addr[7 - cnt_bit]; DEVICE_ADDR:i2c_sda = device_addr[7 - cnt_bit];
@ -731,10 +463,8 @@ always @(*)begin
else begin else begin
i2c_sda = wr_data[7 - cnt_bit]; i2c_sda = wr_data[7 - cnt_bit];
end end
ACK_1, ACK_2, ACK_3: ACK_1, ACK_2, ACK_3, WAIT:
i2c_sda = 1'b0; i2c_sda = 1'b0;
WAIT: i2c_sda = 1'b0;
NACK: i2c_sda = 1'b1;//1
STOP: i2c_sda = (cnt_i2c_clk >= 2'd2) ? 1'b1 : 1'b0; STOP: i2c_sda = (cnt_i2c_clk >= 2'd2) ? 1'b1 : 1'b0;
default: i2c_sda = 1'b1; default: i2c_sda = 1'b1;
endcase endcase

Loading…
Cancel
Save