You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

754 lines
32 KiB
Coq

8 months ago
module i2c_ctrl(
8 months ago
input wire sys_clk ,
input wire sys_rst_n ,
input wire i2c_start ,
input wire [5: 0] cfg_num ,
input wire [15: 0] cfg_data ,
output reg cfg_start ,
output reg [2: 0] step ,
output reg i2c_clk ,
output wire scl ,
8 months ago
inout wire sda ,
output reg [7: 0] po_data
8 months ago
8 months ago
);
parameter I2C_CLK_DIV = 5'd24,
MAX = 10'd1000,
SLAVE_ID = 7'h73;
//
8 months ago
parameter IDLE = 4'd0,
START = 4'd1,
SLAVE_ADDR = 4'd2,
ACK_1 = 4'd3,
ACK_2 = 4'd4,
ACK_3 = 4'd5,
DEVICE_ADDR = 4'd6,
DATA = 4'd7,
WAIT = 4'd8,
8 months ago
NACK = 4'd9,
STOP = 4'd10;
8 months ago
reg [3: 0] state_c;
reg [3: 0] state_n;
8 months ago
////
//i2c
reg [4: 0] cnt_clk;
/////
//
reg [9: 0] cnt_wait ;//1000us
8 months ago
reg skip_en_1 ;//1,
reg skip_en_2 ;//2,bank0
reg skip_en_3 ;//3,0x00
reg skip_en_4 ;//4,0x20
reg skip_en_5 ;//5,51
reg skip_en_6 ;//6,0x43
8 months ago
reg skip_en_7 ;//7,0x43
8 months ago
reg [1: 0] cnt_i2c_clk ;//i2c
reg [2: 0] cnt_bit ;//bit
reg i2c_end ;//i2c
wire sda_en ;
wire sda_in ;
reg i2c_sda ;
reg i2c_scl ;
reg [7: 0] slave_addr ;
reg [7: 0] device_addr ;
reg [7: 0] wr_data ;
8 months ago
reg [7: 0] recv_data ;//0x20
8 months ago
reg ack ;//
8 months ago
reg err_en ;//0x20
8 months ago
//
8 months ago
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;//
8 months ago
assign sda_in = sda;
assign sda = (sda_en == 1'b1)? i2c_sda : 1'bz;
8 months ago
//cfg_start
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cfg_start <= 1'b0;
end
else begin
cfg_start <= i2c_end;//
end
end
8 months ago
//i2c
always @(posedge sys_clk or negedge sys_rst_n) begin
if(!sys_rst_n)begin
cnt_clk <= 5'd0;
end
else if(cnt_clk == I2C_CLK_DIV)begin
cnt_clk <= 5'd0;
end
else begin
cnt_clk <= cnt_clk + 1;
end
end
always @(posedge sys_clk or negedge sys_rst_n) begin
if(!sys_rst_n)begin
i2c_clk <= 1'b1;
end
else if(cnt_clk == I2C_CLK_DIV)begin
i2c_clk <= ~i2c_clk;
end
else begin
i2c_clk <= i2c_clk;
end
end
/////////
//
always @(posedge i2c_clk or negedge sys_rst_n) begin
if(!sys_rst_n)begin
state_c <= IDLE;
end
else begin
state_c <= state_n;
end
end
//
always @(*)begin
case(state_c)
8 months ago
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
8 months ago
state_n = START;
end
else begin
state_n = IDLE;
end
8 months ago
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
8 months ago
state_n = SLAVE_ADDR;
end
else begin
state_n = START;
end
SLAVE_ADDR: if(skip_en_1 == 1'b1)begin
state_n = WAIT;
end
8 months ago
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
8 months ago
state_n = ACK_1;
end
8 months ago
else begin
state_n = SLAVE_ADDR;
end
8 months ago
ACK_1: if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin
8 months ago
state_n = DEVICE_ADDR;
end
8 months ago
else if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin
8 months ago
state_n = DATA;
end
8 months ago
else begin
state_n = ACK_1;
end
8 months ago
DEVICE_ADDR:if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_5 == 1'b1) || (skip_en_6 == 1'b1))begin
8 months ago
state_n = ACK_2;
end
else begin
state_n = DEVICE_ADDR;
end
8 months ago
ACK_2: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
8 months ago
state_n = DATA;
end
8 months ago
else if((skip_en_3 == 1'b1) || (skip_en_6 == 1'b1))begin
8 months ago
state_n = STOP;
end
8 months ago
else begin
state_n = ACK_2;
end
8 months ago
DATA: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
8 months ago
state_n = ACK_3;
end
8 months ago
else if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin
8 months ago
state_n = NACK;
end
else if(err_en == 1'b1)begin
state_n = IDLE;
end
8 months ago
else begin
state_n = DATA;
end
8 months ago
NACK: if((skip_en_4 == 1'b1) || (skip_en_7 == 1'b1))begin
8 months ago
state_n = STOP;
end
else begin
state_n = NACK;
end
8 months ago
ACK_3: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
8 months ago
state_n = STOP;
end
else begin
state_n = ACK_3;
end
8 months ago
WAIT: if(skip_en_1 == 1'b1)begin
state_n = STOP;
end
else begin
state_n = WAIT;
end
8 months ago
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
8 months ago
state_n = IDLE;
end
else begin
state_n = STOP;
end
default: begin
state_n = IDLE;
end
endcase
end
//
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cnt_wait <= 10'd0;
8 months ago
skip_en_1 <= 1'b0;//1
8 months ago
skip_en_2 <= 1'b0;//2
8 months ago
skip_en_3 <= 1'b0;//3
8 months ago
skip_en_4 <= 1'b0;//4
8 months ago
skip_en_5 <= 1'b0;//5
8 months ago
skip_en_6 <= 1'b0;//6
8 months ago
skip_en_7 <= 1'b0;//7
8 months ago
step <= 3'd0;
8 months ago
err_en <= 1'b0;
8 months ago
cnt_i2c_clk <= 2'd0;
cnt_bit <= 3'd0;
i2c_end <= 1'b0;
end
else begin
case(state_c)
IDLE: begin
8 months ago
if(cnt_wait == MAX - 1)begin
8 months ago
cnt_wait <= 10'd0;
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
8 months ago
if((cnt_wait == MAX - 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
8 months ago
if((cnt_wait == MAX - 2'd2) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
8 months ago
if((cnt_wait == MAX - 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
8 months ago
if((cnt_wait == MAX - 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
8 months ago
end
if((cnt_wait == MAX - 2'd2) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
8 months ago
end
8 months ago
else begin
skip_en_6 <= 1'b0;
end
8 months ago
if((cnt_wait == MAX - 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
8 months ago
end
START: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
if((cnt_i2c_clk == 2'd2) && (step == 3'd0))begin
skip_en_1 <= 1'b1;
end
else begin
skip_en_1 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end
else begin
skip_en_6 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end
else begin
skip_en_6 <= 1'b0;
end
8 months ago
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
else begin
skip_en_2 <= 1'b0;
end
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
end
DATA: 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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd3) && (recv_data != 8'h20))begin
begin
err_en <= 1'b1;
step <= 3'd0;
end
end
else begin
begin
err_en <= 1'b0;
step <= step;
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
8 months ago
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
8 months ago
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
8 months ago
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
8 months ago
end
8 months ago
WAIT: begin
if(cnt_wait == MAX - 1'd1)begin
8 months ago
cnt_wait <= 10'd0;
8 months ago
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
cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
if((cnt_i2c_clk == 2'd2) && (step == 3'd0))begin
skip_en_1 <= 1'b1;
end
else begin
skip_en_1 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd1))begin
skip_en_2 <= 1'b1;
end
else begin
skip_en_2 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd2))begin
skip_en_3 <= 1'b1;
end
else begin
skip_en_3 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd3))begin
skip_en_4 <= 1'b1;
end
else begin
skip_en_4 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd5))begin
skip_en_6 <= 1'b1;
end
else begin
skip_en_6 <= 1'b0;
end
8 months ago
if((cnt_i2c_clk == 2'd2) && (step == 3'd6))begin
skip_en_7 <= 1'b1;
end
else begin
skip_en_7 <= 1'b0;
end
8 months ago
if(cnt_i2c_clk == 2'd2)begin
i2c_end <= 1'b1;
end
else begin
i2c_end <= 1'b0;
end
8 months ago
if((i2c_end == 1'b1) && ((step <= 3'd3) || step == 3'd5))begin
8 months ago
step <= step + 1'd1;
end
else if((i2c_end == 1'b1) && (step == 3'd4) && (cfg_num == 6'd51))begin
8 months ago
step <= step + 1'd1;
end
else begin
step <= step;
end
end
default: begin
cnt_wait <= 10'd0;
skip_en_1 <= 1'b0;
8 months ago
skip_en_2 <= 1'b0;
8 months ago
skip_en_3 <= 1'b0;
8 months ago
skip_en_4 <= 1'b0;
8 months ago
skip_en_5 <= 1'b0;
8 months ago
skip_en_6 <= 1'b0;
8 months ago
skip_en_7 <= 1'b0;
8 months ago
err_en <= 1'b0;
8 months ago
step <= step;
cnt_i2c_clk <= 2'd0;
cnt_bit <= 3'd0;
i2c_end <= 1'b0;
end
endcase
end
end
8 months ago
//recv_data
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
recv_data <= 8'h0;
end
else begin
case(state_c)
DATA: begin
8 months ago
if((cnt_i2c_clk == 2'd1) && ((step == 3'd3) || (step == 3'd6)))begin
8 months ago
recv_data <= {recv_data[6:0], sda_in};
end
else begin
recv_data <= recv_data;
end
end
default: recv_data <= recv_data;
endcase
end
end
8 months ago
//ack
always @(*)begin
case(state_c)
ACK_1, ACK_2, ACK_3: ack = ~sda_in;
8 months ago
NACK: ack = i2c_sda;//NACK
8 months ago
default: ack = 1'b0;
endcase
end
8 months ago
//step
always @(*)begin
case(step)
3'd0: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr = 8'h0;
wr_data = 8'h0;
end
8 months ago
3'd1: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= {8'hef};
wr_data = {8'h00};
end
8 months ago
3'd2: begin
slave_addr = {SLAVE_ID, 1'b0};
8 months ago
device_addr= {8'h00};
wr_data = {8'h00};
end
3'd3: begin
slave_addr = {SLAVE_ID, 1'b1};//
device_addr= {8'h00};
8 months ago
wr_data = {8'h00};
end
8 months ago
3'd4: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= cfg_data[15: 8];
wr_data = cfg_data[7: 0];
end
8 months ago
3'd5: begin
slave_addr = {SLAVE_ID, 1'b0};//
device_addr= 8'h43;
wr_data = 8'h00;
end
8 months ago
3'd6: begin
slave_addr = {SLAVE_ID, 1'b1};//
device_addr= 8'h43;
wr_data = 8'h00;
end
8 months ago
default:begin
slave_addr = 8'h0;
device_addr = 8'h0;
wr_data = 8'h0;
end
endcase
end
//i2c_scl
always @(*)begin
case(state_c)
IDLE: i2c_scl = 1'b1;
START: i2c_scl = (cnt_i2c_clk <= 2'd2) ? 1'b1 : 1'b0;
8 months ago
SLAVE_ADDR, DEVICE_ADDR, DATA, ACK_1, ACK_2, ACK_3, NACK:
8 months ago
i2c_scl = ((cnt_i2c_clk == 2'd1) || (cnt_i2c_clk == 2'd2)) ? 1'b1 : 1'b0;
8 months ago
WAIT: i2c_scl = 1'b0;
STOP: i2c_scl = (cnt_i2c_clk >= 2'd1) ? 1'b1 : 1'b0;
endcase
end
//i2c_sda
always @(*)begin
case(state_c)
IDLE: i2c_sda = 1'b1;
START: i2c_sda = (cnt_i2c_clk > 2'd1) ? 1'b0 : 1'b1;
SLAVE_ADDR: i2c_sda = slave_addr[7 - cnt_bit];
8 months ago
DEVICE_ADDR:i2c_sda = device_addr[7 - cnt_bit];
8 months ago
DATA: if((step == 3'd3) || (step == 3'd6))begin
8 months ago
i2c_sda = sda_in;
end
else begin
i2c_sda = wr_data[7 - cnt_bit];
end
8 months ago
ACK_1, ACK_2, ACK_3:
i2c_sda = 1'b0;
8 months ago
WAIT: i2c_sda = 1'b0;
8 months ago
NACK: i2c_sda = 1'b1;//1
8 months ago
STOP: i2c_sda = (cnt_i2c_clk >= 2'd2) ? 1'b1 : 1'b0;
default: i2c_sda = 1'b1;
endcase
end
assign scl = i2c_scl;
8 months ago
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
7 months ago
po_data <= 8'h0;
8 months ago
end
end
8 months ago
endmodule