1 `timescale 1 ns / 100 ps
2 module iic_init( Clk, //I
4 Pixel_clk_greater_than_65Mhz, //I
12 input Pixel_clk_greater_than_65Mhz;
17 parameter CLK_RATE_MHZ = 200,
19 TRANSITION_CYCLE = (CLK_RATE_MHZ * SCK_PERIOD_US) / 2,
20 TRANSITION_CYCLE_MSB = 11;
23 localparam IDLE = 3'd0,
31 SLAVE_ADDR= 7'b1110110,
52 reg [TRANSITION_CYCLE_MSB:0] cycle_count;
56 reg [2:0] write_count;
58 reg [SDA_BUFFER_MSB:0] SDA_BUFFER;
61 always @ (posedge Clk) begin
62 if (~Reset_n||c_state==IDLE ) begin
66 else if (c_state==INIT && transition) begin
69 else if (c_state==SETUP) begin
70 SDA_out <=SDA_BUFFER[SDA_BUFFER_MSB];
72 else if (c_state==CLK_RISE && cycle_count==TRANSITION_CYCLE/2 && bit_count==SDA_BUFFER_MSB) begin
75 else if (c_state==CLK_FALL) begin
79 else if (c_state==CLK_RISE) begin
84 //OBUFT_LVCMOS33 sda0(.O(SDA), .I(1'b0), .T(SDA_out));
85 //OBUFT_LVCMOS33 scl0(.O(SCL), .I(1'b0), .T(SCL_out));
89 always @ (posedge Clk) begin
90 //reset or end condition
92 SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR0,ACK,DATA0,ACK,STOP_BIT};
96 //setup sda for sck rise
97 else if ( c_state==SETUP && cycle_count==TRANSITION_CYCLE)begin
98 SDA_BUFFER <= {SDA_BUFFER[SDA_BUFFER_MSB-1:0],1'b0};
101 //reset count at end of state
102 else if ( cycle_count==TRANSITION_CYCLE)
105 else if (c_state==WAIT && Pixel_clk_greater_than_65Mhz )begin
107 0:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR1,ACK,DATA1,ACK,STOP_BIT};
108 1:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR2,ACK,DATA2a,ACK,STOP_BIT};
109 2:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR3,ACK,DATA3a,ACK,STOP_BIT};
110 3:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR4,ACK,DATA4a,ACK,STOP_BIT};
111 default: SDA_BUFFER <=28'dx;
113 cycle_count<=cycle_count+1;
116 else if (c_state==WAIT && ~Pixel_clk_greater_than_65Mhz )begin
118 0:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR1,ACK,DATA1,ACK,STOP_BIT};
119 1:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR2,ACK,DATA2b,ACK,STOP_BIT};
120 2:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR3,ACK,DATA3b,ACK,STOP_BIT};
121 3:SDA_BUFFER <= {SLAVE_ADDR,WRITE,ACK,REG_ADDR4,ACK,DATA4b,ACK,STOP_BIT};
122 default: SDA_BUFFER <=28'dx;
124 cycle_count<=cycle_count+1;
127 cycle_count<=cycle_count+1;
130 always @ (posedge Clk) begin
133 else if (c_state==WAIT && cycle_count==TRANSITION_CYCLE)
134 write_count<=write_count+1;
138 always @ (posedge Clk) begin
141 else if (c_state==IDLE)
148 always @ (posedge Clk) begin
149 if(~Reset_n||(c_state==WAIT))
151 else if ( c_state==CLK_RISE && cycle_count==TRANSITION_CYCLE)
152 bit_count<=bit_count+1;
157 always @ (posedge Clk) begin
166 assign transition = (cycle_count==TRANSITION_CYCLE);
172 if(~Reset_n) n_state = INIT;
176 if (transition) n_state = START;
180 if(~Reset_n) n_state = INIT;
181 else if( transition) n_state = CLK_FALL;
182 else n_state = START;
185 if(~Reset_n) n_state = INIT;
186 else if( transition) n_state = SETUP;
187 else n_state = CLK_FALL;
190 if(~Reset_n) n_state = INIT;
191 else if( transition) n_state = CLK_RISE;
192 else n_state = SETUP;
197 else if( transition && bit_count==SDA_BUFFER_MSB)
199 else if (transition )
201 else n_state = CLK_RISE;
204 if(~Reset_n|(transition && write_count!=3'd4))
206 else if (transition )
210 default: n_state = IDLE;