| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- `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;
- (* keep = "true", syn_encoding = "sequential", fsm_encoding = "none" *)
- 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
|