Browse Source

6400-i2c-start-stop-sync

Prod2U 3 months ago
parent
commit
5c5ae87bd2
1 changed files with 9 additions and 5 deletions
  1. 9 5
      src/transport/host_i2c_register_slave.v

+ 9 - 5
src/transport/host_i2c_register_slave.v

@@ -30,8 +30,10 @@ module host_i2c_register_slave #(
     reg sda_m, sda_s, sda_d;
     wire scl_rise = scl_s && !scl_d;
     wire scl_fall = !scl_s && scl_d;
-    wire start_cond = sda_d && !sda_s && scl_s;
-    wire stop_cond = !sda_d && sda_s && scl_s;
+    // Require SCL to be high for two sampled clocks. This avoids mistaking
+    // the synchronized SDA transition at an ACK boundary for START/STOP.
+    wire start_cond = sda_d && !sda_s && scl_s && scl_d;
+    wire stop_cond = !sda_d && sda_s && scl_s && scl_d;
 
     localparam [3:0] ST_IDLE = 0, ST_ADDR = 1, ST_ADDR_ACK = 2,
         ST_REG_H = 3, ST_REG_H_ACK = 4, ST_REG_L = 5, ST_REG_L_ACK = 6,
@@ -78,9 +80,11 @@ module host_i2c_register_slave #(
                         if (scl_rise) begin
                             shift <= {shift[6:0], sda_s};
                             if (bit_count == 7) begin
-                                address_seen <= shift[7:1];
-                                rw_seen <= shift[0];
-                                drive_low <= (shift[7:1] == SLAVE_ADDRESS);
+                                // At this edge shift[6:0] contains the seven
+                                // address bits; sda_s is the R/W bit.
+                                address_seen <= shift[6:0];
+                                rw_seen <= sda_s;
+                                drive_low <= (shift[6:0] == SLAVE_ADDRESS);
                                 state <= ST_ADDR_ACK;
                                 bit_count <= 0;
                             end else bit_count <= bit_count + 1'b1;