|
|
@@ -48,6 +48,7 @@ module host_i2c_register_slave #(
|
|
|
reg [3:0] tx_bit;
|
|
|
reg drive_low;
|
|
|
reg read_phase;
|
|
|
+ reg ack_high_seen;
|
|
|
wire write_allowed = (reg_ptr >= 16'h0100 && reg_ptr <= 16'h011F) || (reg_ptr == 16'h0010);
|
|
|
|
|
|
assign sda_drive_low = drive_low;
|
|
|
@@ -61,6 +62,7 @@ module host_i2c_register_slave #(
|
|
|
state <= ST_IDLE; shift <= 0; bit_count <= 0;
|
|
|
address_seen <= 0; rw_seen <= 0; reg_ptr <= 0;
|
|
|
tx_byte <= 0; tx_bit <= 0; drive_low <= 0; read_phase <= 0;
|
|
|
+ ack_high_seen <= 0;
|
|
|
write_strobe <= 0; write_address <= 0; write_data <= 0;
|
|
|
command_strobe <= 0; command_data <= 0; read_address <= 0;
|
|
|
end else begin
|
|
|
@@ -69,10 +71,10 @@ module host_i2c_register_slave #(
|
|
|
write_strobe <= 1'b0;
|
|
|
command_strobe <= 1'b0;
|
|
|
|
|
|
- if (start_cond) begin
|
|
|
- state <= ST_ADDR; bit_count <= 0; shift <= 0; drive_low <= 0;
|
|
|
+ if (start_cond && (state == ST_IDLE || state == ST_WRITE)) begin
|
|
|
+ state <= ST_ADDR; bit_count <= 0; shift <= 0; drive_low <= 0; ack_high_seen <= 0;
|
|
|
end else if (stop_cond) begin
|
|
|
- state <= ST_IDLE; bit_count <= 0; drive_low <= 0;
|
|
|
+ state <= ST_IDLE; bit_count <= 0; drive_low <= 0; ack_high_seen <= 0;
|
|
|
end else begin
|
|
|
case (state)
|
|
|
ST_IDLE: begin drive_low <= 0; end
|
|
|
@@ -87,11 +89,13 @@ module host_i2c_register_slave #(
|
|
|
drive_low <= (shift[6:0] == SLAVE_ADDRESS);
|
|
|
state <= ST_ADDR_ACK;
|
|
|
bit_count <= 0;
|
|
|
+ ack_high_seen <= 0;
|
|
|
end else bit_count <= bit_count + 1'b1;
|
|
|
end
|
|
|
end
|
|
|
ST_ADDR_ACK: begin
|
|
|
- if (scl_fall) begin
|
|
|
+ if (scl_rise) ack_high_seen <= 1'b1;
|
|
|
+ if (scl_fall && ack_high_seen) begin
|
|
|
drive_low <= 0;
|
|
|
if (address_seen != SLAVE_ADDRESS) state <= ST_IDLE;
|
|
|
else if (rw_seen) begin
|
|
|
@@ -107,24 +111,26 @@ module host_i2c_register_slave #(
|
|
|
shift <= {shift[6:0], sda_s};
|
|
|
if (bit_count == 7) begin
|
|
|
reg_ptr[15:8] <= {shift[6:0], sda_s};
|
|
|
- bit_count <= 0; drive_low <= 1; state <= ST_REG_H_ACK;
|
|
|
+ bit_count <= 0; drive_low <= 1; ack_high_seen <= 0; state <= ST_REG_H_ACK;
|
|
|
end else bit_count <= bit_count + 1'b1;
|
|
|
end
|
|
|
end
|
|
|
ST_REG_H_ACK: begin
|
|
|
- if (scl_fall) begin drive_low <= 0; state <= ST_REG_L; end
|
|
|
+ if (scl_rise) ack_high_seen <= 1'b1;
|
|
|
+ if (scl_fall && ack_high_seen) begin drive_low <= 0; state <= ST_REG_L; end
|
|
|
end
|
|
|
ST_REG_L: begin
|
|
|
if (scl_rise) begin
|
|
|
shift <= {shift[6:0], sda_s};
|
|
|
if (bit_count == 7) begin
|
|
|
reg_ptr[7:0] <= {shift[6:0], sda_s};
|
|
|
- bit_count <= 0; drive_low <= 1; state <= ST_REG_L_ACK;
|
|
|
+ bit_count <= 0; drive_low <= 1; ack_high_seen <= 0; state <= ST_REG_L_ACK;
|
|
|
end else bit_count <= bit_count + 1'b1;
|
|
|
end
|
|
|
end
|
|
|
ST_REG_L_ACK: begin
|
|
|
- if (scl_fall) begin drive_low <= 0; state <= ST_WRITE; end
|
|
|
+ if (scl_rise) ack_high_seen <= 1'b1;
|
|
|
+ if (scl_fall && ack_high_seen) begin drive_low <= 0; state <= ST_WRITE; end
|
|
|
end
|
|
|
ST_WRITE: begin
|
|
|
if (scl_rise) begin
|
|
|
@@ -138,12 +144,13 @@ module host_i2c_register_slave #(
|
|
|
command_strobe <= 1'b1;
|
|
|
end
|
|
|
reg_ptr <= reg_ptr + 1'b1;
|
|
|
- bit_count <= 0; drive_low <= write_allowed; state <= ST_WRITE_ACK;
|
|
|
+ bit_count <= 0; drive_low <= write_allowed; ack_high_seen <= 0; state <= ST_WRITE_ACK;
|
|
|
end else bit_count <= bit_count + 1'b1;
|
|
|
end
|
|
|
end
|
|
|
ST_WRITE_ACK: begin
|
|
|
- if (scl_fall) begin drive_low <= 0; state <= ST_WRITE; end
|
|
|
+ if (scl_rise) ack_high_seen <= 1'b1;
|
|
|
+ if (scl_fall && ack_high_seen) begin drive_low <= 0; state <= ST_WRITE; end
|
|
|
end
|
|
|
ST_READ: begin
|
|
|
if (scl_fall) begin
|