1 /////////////////////////////////////////////////////////////////////
3 //// WISHBONE rev.B2 compliant I2C Master byte-controller ////
6 //// Author: Richard Herveille ////
7 //// richard@asics.ws ////
10 //// Downloaded from: http://www.opencores.org/projects/i2c/ ////
12 /////////////////////////////////////////////////////////////////////
14 //// Copyright (C) 2001 Richard Herveille ////
15 //// richard@asics.ws ////
17 //// This source file may be used and distributed without ////
18 //// restriction provided that this copyright statement is not ////
19 //// removed from the file and that any derivative work contains ////
20 //// the original copyright notice and the associated disclaimer.////
22 //// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
23 //// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
24 //// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
25 //// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
26 //// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
27 //// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
28 //// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
29 //// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
30 //// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
31 //// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
32 //// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
33 //// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
34 //// POSSIBILITY OF SUCH DAMAGE. ////
36 /////////////////////////////////////////////////////////////////////
40 // $Id: i2c_master_byte_ctrl.v,v 1.7 2004/02/18 11:40:46 rherveille Exp $
42 // $Date: 2004/02/18 11:40:46 $
44 // $Author: rherveille $
49 // $Log: i2c_master_byte_ctrl.v,v $
50 // Revision 1.7 2004/02/18 11:40:46 rherveille
51 // Fixed a potential bug in the statemachine. During a 'stop' 2 cmd_ack signals were generated. Possibly canceling a new start command.
53 // Revision 1.6 2003/08/09 07:01:33 rherveille
54 // Fixed a bug in the Arbitration Lost generation caused by delay on the (external) sda line.
55 // Fixed a potential bug in the byte controller's host-acknowledge generation.
57 // Revision 1.5 2002/12/26 15:02:32 rherveille
58 // Core is now a Multimaster I2C controller
60 // Revision 1.4 2002/11/30 22:24:40 rherveille
63 // Revision 1.3 2001/11/05 11:59:25 rherveille
64 // Fixed wb_ack_o generation bug.
65 // Fixed bug in the byte_controller statemachine.
69 // synopsys translate_off
70 `include "timescale.v"
71 // synopsys translate_on
73 `include "i2c_master_defines.v"
75 module i2c_master_byte_ctrl (
76 clk, rst, nReset, ena, clk_cnt, start, stop, read, write, ack_in, din,
77 cmd_ack, ack_out, dout, i2c_busy, i2c_al, scl_i, scl_o, scl_oen, sda_i, sda_o, sda_oen );
82 input clk; // master clock
83 input rst; // synchronous active high reset
84 input nReset; // asynchronous active low reset
85 input ena; // core enable signal
87 input [15:0] clk_cnt; // 4x SCL
116 // Variable declarations
120 parameter [4:0] ST_IDLE = 5'b0_0000;
121 parameter [4:0] ST_START = 5'b0_0001;
122 parameter [4:0] ST_READ = 5'b0_0010;
123 parameter [4:0] ST_WRITE = 5'b0_0100;
124 parameter [4:0] ST_ACK = 5'b0_1000;
125 parameter [4:0] ST_STOP = 5'b1_0000;
127 // signals for bit_controller
130 wire core_ack, core_rxd;
132 // signals for shift register
133 reg [7:0] sr; //8bit shift register
136 // signals for state machine
145 // hookup bit_controller
146 i2c_master_bit_ctrl bit_controller (
151 .clk_cnt ( clk_cnt ),
153 .cmd_ack ( core_ack ),
160 .scl_oen ( scl_oen ),
166 // generate go-signal
167 assign go = (read | write | stop) & ~cmd_ack;
169 // assign dout output to shift-register
172 // generate shift register
173 always @(posedge clk or negedge nReset)
181 sr <= #1 {sr[6:0], core_rxd};
184 always @(posedge clk or negedge nReset)
192 dcnt <= #1 dcnt - 3'h1;
194 assign cnt_done = ~(|dcnt);
199 reg [4:0] c_state; // synopsis enum_state
201 always @(posedge clk or negedge nReset)
204 core_cmd <= #1 `I2C_CMD_NOP;
209 c_state <= #1 ST_IDLE;
212 else if (rst | i2c_al)
214 core_cmd <= #1 `I2C_CMD_NOP;
219 c_state <= #1 ST_IDLE;
224 // initially reset all signals
225 core_txd <= #1 sr[7];
230 case (c_state) // synopsys full_case parallel_case
236 c_state <= #1 ST_START;
237 core_cmd <= #1 `I2C_CMD_START;
241 c_state <= #1 ST_READ;
242 core_cmd <= #1 `I2C_CMD_READ;
246 c_state <= #1 ST_WRITE;
247 core_cmd <= #1 `I2C_CMD_WRITE;
251 c_state <= #1 ST_STOP;
252 core_cmd <= #1 `I2C_CMD_STOP;
263 c_state <= #1 ST_READ;
264 core_cmd <= #1 `I2C_CMD_READ;
268 c_state <= #1 ST_WRITE;
269 core_cmd <= #1 `I2C_CMD_WRITE;
279 c_state <= #1 ST_ACK;
280 core_cmd <= #1 `I2C_CMD_READ;
284 c_state <= #1 ST_WRITE; // stay in same state
285 core_cmd <= #1 `I2C_CMD_WRITE; // write next bit
294 c_state <= #1 ST_ACK;
295 core_cmd <= #1 `I2C_CMD_WRITE;
299 c_state <= #1 ST_READ; // stay in same state
300 core_cmd <= #1 `I2C_CMD_READ; // read next bit
304 core_txd <= #1 ack_in;
312 c_state <= #1 ST_STOP;
313 core_cmd <= #1 `I2C_CMD_STOP;
317 c_state <= #1 ST_IDLE;
318 core_cmd <= #1 `I2C_CMD_NOP;
320 // generate command acknowledge signal
324 // assign ack_out output to bit_controller_rxd (contains last received bit)
325 ack_out <= #1 core_rxd;
330 core_txd <= #1 ack_in;
335 c_state <= #1 ST_IDLE;
336 core_cmd <= #1 `I2C_CMD_NOP;
338 // generate command acknowledge signal