module i2c_touch_config	 (	
            // Host Side
						iCLK,
						iRSTN,
						iTRIG,
						oREADY,
						oREG_X1,								
						oREG_Y1,								
						oREG_X2,								
						oREG_Y2,								
						oREG_TOUCH_COUNT,								
						oREG_GESTURE,								
						// I2C Side
						I2C_SCLK,
						I2C_SDAT
);

`include "i2c_touch_param.h"

//=======================================================
//  Port declarations
//=======================================================					
//  Host Side
input		           iCLK;
input		           iRSTN;
input              iTRIG;
output reg         oREADY;
output	   [9:0]	 oREG_X1;				
output	   [8:0]	 oREG_Y1;				
output	   [9:0]	 oREG_X2;				
output	   [8:0]	 oREG_Y2;				
output	   [1:0]	 oREG_TOUCH_COUNT;	
output	   [7:0]	 oREG_GESTURE;			
//  I2C Side
output		         I2C_SCLK;
inout		           I2C_SDAT;


//=======================================================
//  REG/WIRE declarations
//=======================================================
reg	         [9:0] trig_count;
wire               i2c_ack, i2c_end;
wire               i2c_clk_out, i2c_clk;
reg		             i2c_state, i2c_go;
wire         [7:0] reg_control, reg_touch_count; 
wire        [15:0] reg_x1, reg_y1, reg_x2, reg_y2; 
wire               valid_control;    


//=======================================================
//  Sub-modules
//=======================================================
i2c_touch_controller u_i2c_touch_controller (		
				  .iRSTN(iRSTN),
				  .iI2C_CLK(i2c_clk),
				  .iI2C_CLK_OUT(i2c_clk_out),							
				  .iI2C_GO(i2c_go),
				  .oI2C_END(i2c_end),					
				  .oI2C_ACK(i2c_ack),					
				  .oREG_CONTROL(reg_control),								
				  .oREG_X1_MB(reg_x1[15:8]),								
				  .oREG_X1_LB(reg_x1[7:0]),								
				  .oREG_Y1_MB(reg_y1[15:8]),								
				  .oREG_Y1_LB(reg_y1[7:0]),								
				  .oREG_X2_MB(reg_x2[15:8]),									
				  .oREG_X2_LB(reg_x2[7:0]),									
				  .oREG_Y2_MB(reg_y2[15:8]),									
				  .oREG_Y2_LB(reg_y2[7:0]),									
				  .oREG_TOUCH_COUNT(reg_touch_count),								
				  .oREG_GESTURE(oREG_GESTURE),								
				  .I2C_SDAT(I2C_SDAT),
				  .I2C_SCLK(I2C_SCLK) );

//pll u_pll (
//	        .inclk0(iCLK),
//	        .c0(i2c_clk),
//	        .c1(i2c_clk_out));
			  
parameter DIV = 50000000/70000/4/2;			  
reg	[15:0]	cnt;			  
always @ (posedge iCLK or negedge iRSTN)			  
begin
	if (~iRSTN)
		cnt <= 0;
	else if (cnt < DIV)
		cnt <= cnt + 1;
	else
		cnt <= 0;
end

reg [1:0] step;
reg step_i2c_clk;
reg step_i2c_clk_out;
always @ (posedge iCLK or negedge iRSTN)			  
begin
	if (~iRSTN)
	begin
		step <= 0;
		step_i2c_clk <= 0;
		step_i2c_clk_out <= 0;
	end
	else if (cnt == DIV)
	begin
		step <= step + 1;
		if (step == 0)
			step_i2c_clk = ~step_i2c_clk;
		else if (step == 1)
			step_i2c_clk_out = step_i2c_clk;
	end
end

assign i2c_clk = step_i2c_clk;
assign i2c_clk_out = step_i2c_clk_out;

//=======================================================
//  Structural coding
//=======================================================
assign oREG_X1 = reg_x1[9:0];				
assign oREG_Y1 = reg_y1[8:0];				
assign oREG_X2 = reg_x2[9:0];				
assign oREG_Y2 = reg_y2[8:0];				
assign oREG_TOUCH_COUNT = reg_touch_count[1:0];
assign valid_control = reg_control == 8'h2;


always@(posedge iCLK or negedge iRSTN)
	if(!iRSTN)
		trig_count <= 10'h200;
  else if (iTRIG) 
		trig_count <= 10'h0;
	else if (!trig_count[9])
		trig_count <= trig_count + 10'h1;

//	Config Control
always@(posedge i2c_clk or negedge iRSTN)
	if (!iRSTN)
	begin
		i2c_go		<= 1'b0;
		i2c_state	<= IDLE;
		oREADY    <= 1'b0;
	end
  else
  begin 
		case (i2c_state)
			IDLE : begin
				  if (!trig_count[9])
          begin
					  i2c_go		<= 1'b1;
					  i2c_state	<= TRANSFER;
					  oREADY <= 1'b0;
					end
			end
			TRANSFER : begin
					if (i2c_end)
					begin
						i2c_go		<= 1'b0;
						i2c_state	<= IDLE;
						if (!i2c_ack && valid_control)
					    oREADY <= 1'b1;
					end
			end
		endcase
  end
endmodule
