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_ADDRESS = 1;
19 parameter TX_FIRST_BYTE = 2;
20 parameter TX_MORE_BYTES = 3;
21 parameter TX_STOP = 4;
23 reg [10:0] tx_clk_divider = CLOCK_DIVIDE;
28 reg [3:0] tx_state = TX_IDLE;
29 reg [5:0] tx_countdown;
30 reg [3:0] tx_bits_remaining;
35 wire [7:0] address = {7'h70, 1'b0}; // address 0x70, write
36 wire [15:0] address_data = {address, tx_data};
38 assign sda = data_out;
40 assign is_transmitting = tx_state != TX_IDLE;
42 always @(posedge clk) begin
43 // The clk_divider counter counts down from
44 // the CLOCK_DIVIDE constant. Whenever it
45 // reaches 0, 1/16 of the bit period has elapsed.
46 // Countdown timers for the receiving and transmitting
47 // state machines are decremented.
48 tx_clk_divider = tx_clk_divider - 1;
49 if (!tx_clk_divider) begin
50 tx_clk_divider = CLOCK_DIVIDE;
51 tx_countdown = tx_countdown - 1;
54 // Transmit state machine
58 // If the transmit flag is raised in the idle
59 // state, save tx_byte for transmission
61 // Send the initial, low pulse of 1 bit period
62 // to signal the start, followed by the data
64 tx_state = TX_ADDRESS;
65 tx_bits_remaining = 8;
74 end else if (tx_bits_remaining == 0) begin
76 data_out <= 0; // really should be z, not 0
78 end else if(step == 2)begin
84 tx_state <= TX_FIRST_BYTE;
85 tx_bits_remaining <= 8;
87 end else if(step == 1) begin
88 data_out <= address[tx_bits_remaining - 1];
90 end else if(step == 2) begin
93 end else begin // step == 3
94 tx_bits_remaining = tx_bits_remaining - 1;
97 end // case: TX_ADDRESS
103 end else if (tx_bits_remaining == 0) begin
105 data_out <= 0; // really should be z, not 0
107 end else if(step == 2)begin
115 end else if(step == 1) begin
116 data_out <= tx_data[tx_bits_remaining - 1];
118 end else if(step == 2) begin
121 end else begin // step == 3
122 tx_bits_remaining = tx_bits_remaining - 1;
125 end // case: TX_FIRST_BYTE
131 end else if(step == 1) begin
133 end else if(step == 2) begin
144 endmodule // i2c_write