/********************************************************************************************
Debug Switch Definition Section
********************************************************************************************/


/********************************************************************************************
Include File Section
********************************************************************************************/

#include "i2c.h"
/********************************************************************************************
Macro Definition Section
********************************************************************************************/


/********************************************************************************************
Prototype Declaration Section
********************************************************************************************/


/********************************************************************************************
Structure Type Definition Section
********************************************************************************************/


/********************************************************************************************
Const Global Variable Definition Section
********************************************************************************************/


/********************************************************************************************
Global Variable Definition Section
********************************************************************************************/

/********************************************************************************************
Static Variable Definition Section
********************************************************************************************/
i2c_Ip_MasterCallbackType lpI2cCallback[LPI2C_INSTANCE_COUNT];
i2c_Ip_MasterCallbackType FlexioI2cCallback[FEATURE_FLEXIO_MAX_CHANNEL_COUNT];
/********************************************************************************************
Function Definition Section
********************************************************************************************/
void Lpi2c_MasterInit(uint8 channelID,const Lpi2c_Ip_MasterConfigType * ConfigPtr)
{
	Lpi2c_Ip_MasterInit(channelID,ConfigPtr);
}
void Lpi2c_MasterDeinit(uint8 channelID)
{
	Lpi2c_Ip_MasterDeinit(channelID);
}
void Lpi2c_SlaveInit(uint8 channelID,const Lpi2c_Ip_SlaveConfigType * ConfigPtr)
{
	Lpi2c_Ip_SlaveInit(channelID,ConfigPtr);
}
void Lpi2c_SlaveDeInit(uint8 channelID)
{
	Lpi2c_Ip_SlaveDeinit(channelID);
}
void Lpi2c_StartListening(uint8 channelID)
{
	Lpi2c_Ip_StartListening(channelID);
}


void LPI2C_Synchronization_Write(I2c_WriteInfo_t* writeData)
{

	lpI2cCallback[writeData->channelID] = writeData->Callback;
	Lpi2c_Ip_MasterSetSlaveAddr(writeData->channelID, writeData->Address, writeData->Is10bitAddr);
	Lpi2c_Ip_MasterSendDataBlocking(writeData->channelID, writeData->TxBuff, writeData->TxSize, TRUE,writeData->Timerout);
}

void LPI2C_Synchronization_Read(I2c_ReadInfo_t* readData)
{

	lpI2cCallback[readData->channelID] = readData->Callback;
	Lpi2c_Ip_MasterSetSlaveAddr(readData->channelID, readData->Address, readData->Is10bitAddr);
	Lpi2c_Ip_MasterSendDataBlocking(readData->channelID, readData->Reg_Addr, readData->Reg_AddrSize, FALSE,readData->Timerout);


	Lpi2c_Ip_MasterReceiveDataBlocking(readData->channelID, readData->RxBuff, readData->RxSize, TRUE,readData->Timerout);
	

}
void LPI2C_Asynchronous_Write(I2c_WriteInfo_t* writeData)
{

	lpI2cCallback[writeData->channelID] = writeData->Callback;
	Lpi2c_Ip_MasterSetSlaveAddr(writeData->channelID, writeData->Address, writeData->Is10bitAddr);
	Lpi2c_Ip_MasterSendData(writeData->channelID, writeData->TxBuff, writeData->TxSize, TRUE);
}

void LPI2C_Asynchronous_Read(I2c_ReadInfo_t* readData)
{
	uint32 timeout = readData->Timerout;

	lpI2cCallback[readData->channelID] = readData->Callback;
	Lpi2c_Ip_MasterSetSlaveAddr(readData->channelID, readData->Address, readData->Is10bitAddr);
	Lpi2c_Ip_MasterSendData(readData->channelID, readData->Reg_Addr, readData->Reg_AddrSize, FALSE);

	while(((Lpi2c_Ip_MasterGetTransferStatus(readData->channelID, NULL_PTR)) == LPI2C_IP_BUSY_STATUS) && (timeout > 0))
	{
		timeout--;
	}

	Lpi2c_Ip_MasterReceiveData(readData->channelID, readData->RxBuff, readData->RxSize, TRUE);
	

}



void Flexio_I2c_MasterInit( uint8 Channel,const Flexio_I2c_Ip_MasterConfigType * ConfigPtr)
{

	Flexio_I2c_Ip_MasterInit(0U, Channel, ConfigPtr);

}
void Flexio_I2c_MasterDeInit( uint8 Channel)
{

	Flexio_I2c_Ip_MasterDeinit(0,Channel);
}

void FlexioI2C_Synchronization_WriteData(I2c_WriteInfo_t* writeData)
{

	FlexioI2cCallback[writeData->channelID] = writeData->Callback;
	Flexio_I2c_Ip_MasterSetSlaveAddr(0, writeData->channelID, writeData->Address);
	Flexio_I2c_Ip_MasterSendDataBlocking(0, writeData->channelID, writeData->TxBuff, writeData->TxSize, TRUE,writeData->Timerout);
}

void FlexioI2C_Synchronization_ReadData(I2c_ReadInfo_t* readData)
{
	uint32 timeout = readData->Timerout;

	FlexioI2cCallback[readData->channelID] = readData->Callback;
	Flexio_I2c_Ip_MasterSetSlaveAddr(0, readData->channelID, readData->Address);
	Flexio_I2c_Ip_MasterSendDataBlocking(0, readData->channelID, readData->Reg_Addr, readData->Reg_AddrSize, FALSE,readData->Timerout);


	Flexio_I2c_Ip_MasterReceiveDataBlocking(0, readData->channelID, readData->RxBuff, readData->RxSize, TRUE,readData->Timerout);

}


void FlexioI2C_Asynchronous_WriteData(I2c_WriteInfo_t* writeData)
{

	FlexioI2cCallback[writeData->channelID] = writeData->Callback;
	Flexio_I2c_Ip_MasterSetSlaveAddr(0, writeData->channelID, writeData->Address);
	Flexio_I2c_Ip_MasterSendData(0, writeData->channelID, writeData->TxBuff, writeData->TxSize, TRUE);
}

void FlexioI2C_Asynchronous_ReadData(I2c_ReadInfo_t* readData)
{
	uint32 timeout = readData->Timerout;

	FlexioI2cCallback[readData->channelID] = readData->Callback;
	Flexio_I2c_Ip_MasterSetSlaveAddr(0, readData->channelID, readData->Address);
	Flexio_I2c_Ip_MasterSendData(0, readData->channelID, readData->Reg_Addr, readData->Reg_AddrSize, FALSE);

	while(((Flexio_I2c_Ip_MasterGetStatus(0, readData->channelID, NULL_PTR)) == FLEXIO_I2C_IP_BUSY_STATUS) && (timeout > 0))
	{
		timeout--;
	}

	
	Flexio_I2c_Ip_MasterReceiveData(0, readData->channelID, readData->RxBuff, readData->RxSize, TRUE);

}
i2c_ReturnType returnevent = I2C_NULL;
void I2c_Lpi2cMasterChannel0_Callback(Lpi2c_Ip_MasterEventType event, uint8 userData)
{
	i2c_ReturnType returnevent = I2C_NULL;
	switch(event)
	{
		case I2C_MASTER_EVENT_NACK:
			returnevent = I2C_NACK;
			break;
		case I2C_MASTER_EVENT_ARBITRATION_LOST:
			returnevent = I2C_ARBITRATION_LOST;
			break;
		case I2C_MASTER_EVENT_ERROR_FIFO:
			returnevent = I2C_ERROR_FIFO;
			break;
		case I2C_MASTER_EVENT_END_TRANSFER:
			returnevent = I2C_END_TRANSFER;
			break;
		case I2C_MASTER_EVENT_PIN_LOW_TIMEOUT:
			returnevent = I2C_PIN_LOW_TIMEOUT;
			break;
		default:
			returnevent = I2C_TRAN_OK;
			break;
	}
	lpI2cCallback[userData](returnevent);
}

void I2c_Flexioi2cMasterChannel_Callback(Flexio_I2c_Ip_MasterEventType event, uint8 userData)
{
	i2c_ReturnType returnevent = I2C_NULL;
	switch(event)
	{
		case FLEXIO_I2C_MASTER_EVENT_NACK:
			returnevent = I2C_NACK;
			break;
		case FLEXIO_I2C_MASTER_EVENT_ARBITRATION_LOST:
			returnevent = I2C_ARBITRATION_LOST;
			break;
		case FLEXIO_I2C_MASTER_EVENT_ERROR_FIFO:
			returnevent = I2C_ERROR_FIFO;
			break;
		case FLEXIO_I2C_MASTER_EVENT_END_TRANSFER:
			returnevent = I2C_END_TRANSFER;
			break;
		case FLEXIO_I2C_MASTER_EVENT_PIN_LOW_TIMEOUT:
			returnevent = I2C_PIN_LOW_TIMEOUT;
			break;
		default:
			returnevent = I2C_TRAN_OK;
			break;
	}
	FlexioI2cCallback[userData](returnevent);
}

