#include "SSD1928_I2C.h"

/*
*************************************************************************************
* Function: Initialization of I2C module for SSD1928
*
* PreCondition: none
*
* Arguments:
*
* Return: none
*
* Note:
*
*************************************************************************************
*/
void ssd1928_I2C_Init(void)
{
	SetRegB(REG_I2C_INTERRUPT, 0x00);				//Reg[235h] ctrl interrupt status, here we disable all relevant I2C interrupts
	SetRegB(REG_I2C_BAUD, SSD1928_I2C_BAUD);		//Reg[233h] defines the I2C baud rate. 
	SetRegB(REG_I2C_EN_OUT, 0x03);					//Reg[232h] controls I2C Bus clear/normal, & I2C enable/disable. By this, we clear I2C bus first & enable I2C module
	SetRegB(REG_I2C_EN_OUT, 0x01);					//Clear I2C Bus clear bit for normal operation
}

/*
*************************************************************************************
* Function: WAIT I2C module ready or Timeout
*
* PreCondition: none
*
* Arguments:	none
*
* Return: 	0 		if I2C ready
*			-1 		if timeout
*
* Note: The timeout counter is hardware dependent, and it subject to variations
*		on processor speed
*
*************************************************************************************
*/
BYTE ssd1928_I2C_Wait(void)
{
	//timeout counter
	WORD timeout=65535;

	//wait for I2C module ready or timeout
	while((!(GetReg(REG_I2C_STATUS)&0x80))&&(timeout--));	

	if(GetReg(REG_I2C_STATUS)&0x80)
		return 0;
	else
		return -1;
}


/*
*************************************************************************************
* Function: Function to send out the I2C Start, Stop conditions, and control
*			I2C command bits for Write, and Read w or wo acknowledgement
*
* PreCondition: none
*
* Arguments:	BYTE code list
*				SSD1928_I2C_START		0x80
*				SSD1928_I2C_STOP		0x10
*				SSD1928_I2C_IDLE  		0x00
*				SSD1928_I2C_WRITE  		0x01
*				SSD1928_I2C_READ_W_ACK 	0x02
*				SSD1928_I2C_READ_WO_ACK 0x03
*
* Return:		none
* Note:
*************************************************************************************
*/
void ssd1928_I2C_Ctl(BYTE code)
{
	if( ssd1928_I2C_Wait()==0)		//if I2C module is ready
		SetRegB(REG_I2C_CTL, code);
	else
		; //debug I2C Control error (optional)
}




/*
*************************************************************************************
* Function: Function to write a single byte to I2C bus of SSD1928 via register
*			REG[230h] of SSD1928
*
* PreCondition: none
*
* Arguments:	BYTE byte to write
*
* Return:		none
*************************************************************************************
*/
void ssd1928_I2C_Write(BYTE byte)
{
	SetRegB(REG_I2C_DATA_OUT, byte);
}




/*
*************************************************************************************
* Function: Function to read a single byte from I2C bus of SSD1928 via register
*			REG[237h] of SSD1928
*
* PreCondition: A timeout value of 0xFFFFFFFF is a hard-code value subject
*				to variation on processor speed
*
* Arguments:	none
*
* Return:		The byte read from I2C
*				or 
*				-1 if timeout
*************************************************************************************
*/
BYTE ssd1928_I2C_Read(void)
{
	DWORD timeout = 0xFFFFFFFF;
	BYTE data;

	SetRegB(REG_I2C_DATA_OUT, 0xFF);
	//wait for I2C data in ready or timeout
	while((!(GetReg(REG_I2C_DATA_READY)&0x01))&&(timeout--));	

	if(GetReg(REG_I2C_DATA_READY)&0x01)
		data = GetReg(REG_I2C_DATA_IN);
	else
		data = -1;

	return data;

}