|
|
@@ -0,0 +1,437 @@
|
|
|
+`timescale 1ns/1ps
|
|
|
+
|
|
|
+module prod2u_p4_i2c_stop_start_read #(
|
|
|
+ parameter integer HALF_PERIOD_CLKS = 125,
|
|
|
+ parameter integer TIMEOUT_CLKS = 25000
|
|
|
+) (
|
|
|
+ input wire clk,
|
|
|
+ input wire rst_n,
|
|
|
+ input wire abort,
|
|
|
+ output reg abort_done,
|
|
|
+ input wire request_valid,
|
|
|
+ output wire request_ready,
|
|
|
+ input wire [6:0] request_device_addr,
|
|
|
+ input wire [7:0] request_register,
|
|
|
+ output reg response_valid,
|
|
|
+ input wire response_ready,
|
|
|
+ output reg [15:0] response_data,
|
|
|
+ output reg [3:0] response_error,
|
|
|
+ output wire busy,
|
|
|
+ input wire scl_i,
|
|
|
+ input wire sda_i,
|
|
|
+ output reg scl_drive_low,
|
|
|
+ output reg sda_drive_low
|
|
|
+);
|
|
|
+ localparam [3:0] ERROR_NONE = 4'd0;
|
|
|
+ localparam [3:0] ERROR_ADDRESS_NACK = 4'd1;
|
|
|
+ localparam [3:0] ERROR_REGISTER_NACK = 4'd2;
|
|
|
+ localparam [3:0] ERROR_READ_ADDRESS_NACK = 4'd3;
|
|
|
+ localparam [3:0] ERROR_TIMEOUT = 4'd4;
|
|
|
+ localparam [3:0] ERROR_STUCK_LOW = 4'd5;
|
|
|
+ localparam [3:0] ERROR_ARBITRATION_LOST = 4'd6;
|
|
|
+
|
|
|
+ localparam [1:0] TX_WRITE_ADDRESS = 2'd0;
|
|
|
+ localparam [1:0] TX_REGISTER = 2'd1;
|
|
|
+ localparam [1:0] TX_READ_ADDRESS = 2'd2;
|
|
|
+
|
|
|
+ localparam [4:0] ST_IDLE = 5'd0;
|
|
|
+ localparam [4:0] ST_BUS_WAIT_WRITE = 5'd1;
|
|
|
+ localparam [4:0] ST_START_HOLD = 5'd2;
|
|
|
+ localparam [4:0] ST_TX_BIT_LOW = 5'd3;
|
|
|
+ localparam [4:0] ST_TX_BIT_HIGH = 5'd4;
|
|
|
+ localparam [4:0] ST_TX_ACK_LOW = 5'd5;
|
|
|
+ localparam [4:0] ST_TX_ACK_HIGH = 5'd6;
|
|
|
+ localparam [4:0] ST_STOP_LOW = 5'd7;
|
|
|
+ localparam [4:0] ST_STOP_SCL_HIGH = 5'd8;
|
|
|
+ localparam [4:0] ST_STOP_RELEASE = 5'd9;
|
|
|
+ localparam [4:0] ST_BUS_WAIT_READ = 5'd10;
|
|
|
+ localparam [4:0] ST_RX_BIT_LOW = 5'd11;
|
|
|
+ localparam [4:0] ST_RX_BIT_HIGH = 5'd12;
|
|
|
+ localparam [4:0] ST_RX_ACK_LOW = 5'd13;
|
|
|
+ localparam [4:0] ST_RX_ACK_HIGH = 5'd14;
|
|
|
+ localparam [4:0] ST_RESPONSE = 5'd15;
|
|
|
+ localparam [4:0] ST_ABORT_SCL_HIGH = 5'd16;
|
|
|
+ localparam [4:0] ST_ABORT_RELEASE = 5'd17;
|
|
|
+
|
|
|
+ (* ASYNC_REG = "TRUE" *) reg [1:0] scl_sync;
|
|
|
+ (* ASYNC_REG = "TRUE" *) reg [1:0] sda_sync;
|
|
|
+ reg [4:0] state;
|
|
|
+ reg [31:0] phase_count;
|
|
|
+ reg [31:0] timeout_count;
|
|
|
+ reg [6:0] device_addr;
|
|
|
+ reg [7:0] register_addr;
|
|
|
+ reg [7:0] tx_byte;
|
|
|
+ reg [1:0] tx_stage;
|
|
|
+ reg [2:0] bit_index;
|
|
|
+ reg [15:0] rx_shift;
|
|
|
+ reg rx_byte_index;
|
|
|
+ reg [3:0] pending_error;
|
|
|
+ reg after_stop_read;
|
|
|
+
|
|
|
+ wire scl_sample;
|
|
|
+ wire sda_sample;
|
|
|
+
|
|
|
+ assign scl_sample = scl_sync[1];
|
|
|
+ assign sda_sample = sda_sync[1];
|
|
|
+ assign request_ready = (state == ST_IDLE) && !response_valid;
|
|
|
+ assign busy = (state != ST_IDLE) || response_valid;
|
|
|
+
|
|
|
+ generate
|
|
|
+ if (HALF_PERIOD_CLKS < 1) begin : g_half_period_must_be_positive
|
|
|
+ prod2u_p4_i2c_half_period_must_be_positive u_half_period_error();
|
|
|
+ end
|
|
|
+ if (TIMEOUT_CLKS < 1) begin : g_timeout_must_be_positive
|
|
|
+ prod2u_p4_i2c_timeout_must_be_positive u_timeout_error();
|
|
|
+ end
|
|
|
+ endgenerate
|
|
|
+
|
|
|
+ always @(posedge clk or negedge rst_n) begin
|
|
|
+ if (!rst_n) begin
|
|
|
+ scl_sync <= 2'b11;
|
|
|
+ sda_sync <= 2'b11;
|
|
|
+ end else begin
|
|
|
+ scl_sync <= {scl_sync[0], scl_i};
|
|
|
+ sda_sync <= {sda_sync[0], sda_i};
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ always @(posedge clk or negedge rst_n) begin
|
|
|
+ if (!rst_n) begin
|
|
|
+ state <= ST_IDLE;
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ device_addr <= 7'd0;
|
|
|
+ register_addr <= 8'd0;
|
|
|
+ tx_byte <= 8'd0;
|
|
|
+ tx_stage <= TX_WRITE_ADDRESS;
|
|
|
+ bit_index <= 3'd0;
|
|
|
+ rx_shift <= 16'd0;
|
|
|
+ rx_byte_index <= 1'b0;
|
|
|
+ pending_error <= ERROR_NONE;
|
|
|
+ after_stop_read <= 1'b0;
|
|
|
+ response_valid <= 1'b0;
|
|
|
+ abort_done <= 1'b0;
|
|
|
+ response_data <= 16'd0;
|
|
|
+ response_error <= ERROR_NONE;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ end else begin
|
|
|
+ abort_done <= 1'b0;
|
|
|
+ if (abort && state != ST_ABORT_SCL_HIGH && state != ST_ABORT_RELEASE) begin
|
|
|
+ response_valid <= 1'b0;
|
|
|
+ response_data <= 16'd0;
|
|
|
+ response_error <= ERROR_NONE;
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ if (state == ST_IDLE) begin
|
|
|
+ state <= ST_IDLE;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ abort_done <= 1'b1;
|
|
|
+ end else begin
|
|
|
+ state <= ST_ABORT_SCL_HIGH;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b1;
|
|
|
+ end
|
|
|
+ end else if (state != ST_IDLE && state != ST_RESPONSE &&
|
|
|
+ state != ST_ABORT_SCL_HIGH && state != ST_ABORT_RELEASE &&
|
|
|
+ timeout_count >= TIMEOUT_CLKS - 1) begin
|
|
|
+ state <= ST_RESPONSE;
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ response_valid <= 1'b1;
|
|
|
+ response_data <= 16'd0;
|
|
|
+ response_error <= ((state == ST_BUS_WAIT_WRITE) || (state == ST_BUS_WAIT_READ)) ?
|
|
|
+ ERROR_STUCK_LOW : ERROR_TIMEOUT;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ end else begin
|
|
|
+ if (state != ST_IDLE && state != ST_RESPONSE) begin
|
|
|
+ timeout_count <= timeout_count + 32'd1;
|
|
|
+ end
|
|
|
+
|
|
|
+ case (state)
|
|
|
+ ST_IDLE: begin
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ if (request_valid) begin
|
|
|
+ device_addr <= request_device_addr;
|
|
|
+ register_addr <= request_register;
|
|
|
+ response_data <= 16'd0;
|
|
|
+ response_error <= ERROR_NONE;
|
|
|
+ pending_error <= ERROR_NONE;
|
|
|
+ after_stop_read <= 1'b0;
|
|
|
+ rx_shift <= 16'd0;
|
|
|
+ state <= ST_BUS_WAIT_WRITE;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_BUS_WAIT_WRITE,
|
|
|
+ ST_BUS_WAIT_READ: begin
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ if (scl_sample && sda_sample) begin
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ sda_drive_low <= 1'b1;
|
|
|
+ tx_stage <= (state == ST_BUS_WAIT_WRITE) ?
|
|
|
+ TX_WRITE_ADDRESS : TX_READ_ADDRESS;
|
|
|
+ state <= ST_START_HOLD;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end else begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_START_HOLD: begin
|
|
|
+ if (scl_sample) begin
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ tx_byte <= (tx_stage == TX_READ_ADDRESS) ?
|
|
|
+ {device_addr, 1'b1} : {device_addr, 1'b0};
|
|
|
+ bit_index <= 3'd7;
|
|
|
+ scl_drive_low <= 1'b1;
|
|
|
+ state <= ST_TX_BIT_LOW;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_TX_BIT_LOW: begin
|
|
|
+ if (phase_count == 32'd0) begin
|
|
|
+ sda_drive_low <= ~tx_byte[bit_index];
|
|
|
+ end
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ state <= ST_TX_BIT_HIGH;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_TX_BIT_HIGH: begin
|
|
|
+ if (scl_sample) begin
|
|
|
+ if (tx_byte[bit_index] && !sda_sample) begin
|
|
|
+ state <= ST_RESPONSE;
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ response_valid <= 1'b1;
|
|
|
+ response_data <= 16'd0;
|
|
|
+ response_error <= ERROR_ARBITRATION_LOST;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ end else if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b1;
|
|
|
+ if (bit_index == 3'd0) begin
|
|
|
+ state <= ST_TX_ACK_LOW;
|
|
|
+ end else begin
|
|
|
+ bit_index <= bit_index - 3'd1;
|
|
|
+ state <= ST_TX_BIT_LOW;
|
|
|
+ end
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_TX_ACK_LOW: begin
|
|
|
+ if (phase_count == 32'd0) begin
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ end
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ state <= ST_TX_ACK_HIGH;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_TX_ACK_HIGH: begin
|
|
|
+ if (scl_sample) begin
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b1;
|
|
|
+ if (sda_sample) begin
|
|
|
+ case (tx_stage)
|
|
|
+ TX_WRITE_ADDRESS: pending_error <= ERROR_ADDRESS_NACK;
|
|
|
+ TX_REGISTER: pending_error <= ERROR_REGISTER_NACK;
|
|
|
+ default: pending_error <= ERROR_READ_ADDRESS_NACK;
|
|
|
+ endcase
|
|
|
+ after_stop_read <= 1'b0;
|
|
|
+ state <= ST_STOP_LOW;
|
|
|
+ end else if (tx_stage == TX_WRITE_ADDRESS) begin
|
|
|
+ tx_byte <= register_addr;
|
|
|
+ tx_stage <= TX_REGISTER;
|
|
|
+ bit_index <= 3'd7;
|
|
|
+ state <= ST_TX_BIT_LOW;
|
|
|
+ end else if (tx_stage == TX_REGISTER) begin
|
|
|
+ pending_error <= ERROR_NONE;
|
|
|
+ after_stop_read <= 1'b1;
|
|
|
+ state <= ST_STOP_LOW;
|
|
|
+ end else begin
|
|
|
+ rx_shift <= 16'd0;
|
|
|
+ rx_byte_index <= 1'b0;
|
|
|
+ bit_index <= 3'd7;
|
|
|
+ state <= ST_RX_BIT_LOW;
|
|
|
+ end
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_RX_BIT_LOW: begin
|
|
|
+ if (phase_count == 32'd0) begin
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ end
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ state <= ST_RX_BIT_HIGH;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_RX_BIT_HIGH: begin
|
|
|
+ if (scl_sample) begin
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ rx_shift <= {rx_shift[14:0], sda_sample};
|
|
|
+ scl_drive_low <= 1'b1;
|
|
|
+ if (bit_index == 3'd0) begin
|
|
|
+ state <= ST_RX_ACK_LOW;
|
|
|
+ end else begin
|
|
|
+ bit_index <= bit_index - 3'd1;
|
|
|
+ state <= ST_RX_BIT_LOW;
|
|
|
+ end
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_RX_ACK_LOW: begin
|
|
|
+ if (phase_count == 32'd0) begin
|
|
|
+ sda_drive_low <= (rx_byte_index == 1'b0);
|
|
|
+ end
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ state <= ST_RX_ACK_HIGH;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_RX_ACK_HIGH: begin
|
|
|
+ if (scl_sample) begin
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b1;
|
|
|
+ if (rx_byte_index == 1'b0) begin
|
|
|
+ rx_byte_index <= 1'b1;
|
|
|
+ bit_index <= 3'd7;
|
|
|
+ state <= ST_RX_BIT_LOW;
|
|
|
+ end else begin
|
|
|
+ pending_error <= ERROR_NONE;
|
|
|
+ after_stop_read <= 1'b0;
|
|
|
+ state <= ST_STOP_LOW;
|
|
|
+ end
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_STOP_LOW: begin
|
|
|
+ scl_drive_low <= 1'b1;
|
|
|
+ sda_drive_low <= 1'b1;
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ state <= ST_STOP_SCL_HIGH;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_STOP_SCL_HIGH: begin
|
|
|
+ if (scl_sample) begin
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ state <= ST_STOP_RELEASE;
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_STOP_RELEASE: begin
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ if (scl_sample && sda_sample) begin
|
|
|
+ if (phase_count >= HALF_PERIOD_CLKS - 1) begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ if (after_stop_read && pending_error == ERROR_NONE) begin
|
|
|
+ after_stop_read <= 1'b0;
|
|
|
+ state <= ST_BUS_WAIT_READ;
|
|
|
+ end else begin
|
|
|
+ state <= ST_RESPONSE;
|
|
|
+ response_valid <= 1'b1;
|
|
|
+ response_data <= (pending_error == ERROR_NONE) ? rx_shift : 16'd0;
|
|
|
+ response_error <= pending_error;
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ end
|
|
|
+ end else begin
|
|
|
+ phase_count <= phase_count + 32'd1;
|
|
|
+ end
|
|
|
+ end else begin
|
|
|
+ phase_count <= 32'd0;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_RESPONSE: begin
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ if (response_valid && response_ready) begin
|
|
|
+ response_valid <= 1'b0;
|
|
|
+ state <= ST_IDLE;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_ABORT_SCL_HIGH: begin
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b1;
|
|
|
+ if (scl_sample || timeout_count >= TIMEOUT_CLKS - 1) begin
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ state <= ST_ABORT_RELEASE;
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ ST_ABORT_RELEASE: begin
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ timeout_count <= 32'd0;
|
|
|
+ state <= ST_IDLE;
|
|
|
+ abort_done <= 1'b1;
|
|
|
+ end
|
|
|
+
|
|
|
+ default: begin
|
|
|
+ state <= ST_IDLE;
|
|
|
+ scl_drive_low <= 1'b0;
|
|
|
+ sda_drive_low <= 1'b0;
|
|
|
+ end
|
|
|
+ endcase
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+endmodule
|