7 input [5:0] more_bytes, /* 0 or 1 or 16 are reasonable */
10 output is_transmitting);
12 parameter CLOCK_DIVIDE = 1;
14 // States for the transmitting state machine.
15 // We transmit a START, then the address (constant 0xE1), then
16 // [tx_byte], then [more_bytes] bytes from [mb_in]
17 parameter TX_IDLE = 0;
18 parameter TX_START = 1;
19 parameter TX_ADDRESS = 2;
20 parameter TX_FIRST_BYTE = 3;
21 parameter TX_MORE_BYTES = 4;
22 parameter TX_STOP = 5;
24 reg [30:0] tx_clk_divider = CLOCK_DIVIDE;
29 reg [3:0] tx_state = TX_IDLE;
30 reg [5:0] tx_countdown;
31 reg [3:0] tx_bits_remaining;
35 reg [5:0] more_bytes_idx = 0;
37 wire [7:0] address = {7'h70, 1'b0}; // address 0x70, write
38 wire [15:0] address_data = {address, tx_data};
40 assign sda = data_out;
42 assign is_transmitting = tx_state != TX_IDLE;
43 assign mb_addr = more_bytes_idx;
45 always @(posedge clk) begin
46 if(tx_clk_divider) begin
47 tx_clk_divider <= tx_clk_divider - 1;
49 tx_clk_divider <= CLOCK_DIVIDE - 1;
56 tx_bits_remaining = 8;
59 tx_clk_divider <= CLOCK_DIVIDE - 1;
64 if(tx_clk_divider) begin end
67 tx_state = TX_ADDRESS;
72 if(tx_clk_divider) begin end
73 else if(step == 0) begin
76 end else if (tx_bits_remaining == 0) begin
78 data_out <= 0; // really should be z, not 0
80 end else if(step == 2)begin
86 tx_state <= TX_FIRST_BYTE;
87 tx_bits_remaining <= 8;
89 end else if(step == 1) begin
90 data_out <= address[tx_bits_remaining - 1];
92 end else if(step == 2) begin
95 end else begin // step == 3
96 tx_bits_remaining = tx_bits_remaining - 1;
99 end // case: TX_ADDRESS
102 if(tx_clk_divider) begin end
103 else if(step == 0) begin
106 end else if (tx_bits_remaining == 0) begin
108 data_out <= 0; // really should be z, not 0
110 end else if(step == 2)begin
117 tx_state <= TX_MORE_BYTES;
120 tx_bits_remaining <= 8;
123 end else if(step == 1) begin
124 data_out <= tx_data[tx_bits_remaining - 1];
126 end else if(step == 2) begin
129 end else begin // step == 3
130 tx_bits_remaining = tx_bits_remaining - 1;
133 end // case: TX_FIRST_BYTE
136 if(tx_clk_divider) begin end
137 else if(step == 0) begin
140 end else if (tx_bits_remaining == 0) begin
142 data_out <= 0; // really should be z, not 0
144 end else if(step == 2)begin
149 tx_bits_remaining <= 8;
151 if(more_bytes_idx == more_bytes)
153 more_bytes_idx <= more_bytes_idx + 1;
155 end else if(step == 1) begin
156 data_out <= mb_in[tx_bits_remaining - 1];
158 end else if(step == 2) begin
161 end else begin // step == 3
162 tx_bits_remaining = tx_bits_remaining - 1;
165 end // case: TX_MORE_BYTES
168 if(tx_clk_divider) begin end
169 else if(step == 0) begin
172 end else if(step == 1) begin
174 end else if(step == 2) begin
185 endmodule // i2c_write