#include "drv_master_iic.h"

#define I2C_TIMEOUT                         (0x1800U)
#define I2C_BUFFER_LENGTH                   (8U)

static uint32_t s_timeout = I2C_TIMEOUT;
static status_t status = STATUS_SUCCESS;
static uint32_t s_byteRemaining = 8U;
static bool ACK;

void bsp_drv_master_iic_init(void)
{
    /* Configure I2C  */
	I2C_MasterInit(&flexio_instance, &i2c_pal_flexio_MasterConfig0);
}

void bsp_drv_master_iic_deinit(void)
{
	I2C_MasterDeinit(&flexio_instance);
}

int bsp_drv_maste_iic_set_slave_address(uint16_t address)
{
	return I2C_MasterSetSlaveAddress(&flexio_instance, address, false);
}

bool bsp_drv_master_iic_read(uint8_t *pBuf,int len ,bool sendstop)
{
	 s_timeout = I2C_TIMEOUT;
	 status = I2C_MasterReceiveData(&flexio_instance, pBuf, len, sendstop);
	 if (status == STATUS_SUCCESS)
	 {
	     do
	     {
	         s_timeout--;
	         status = I2C_MasterGetTransferStatus(&flexio_instance, &s_byteRemaining);
	     } while ((s_timeout != 0U) && (status != STATUS_SUCCESS));
	     if ((s_byteRemaining != 0U) || (s_timeout == 0U))
	     {
	         return false;
	     }
	     else
	     {
	         return true;
	     }
	 }
	 else
	 {
	     return false;
	 }
}
bool bsp_drv_master_iic_write(const uint8_t *pBuf,int len, bool sendstop)
{
    s_timeout = I2C_TIMEOUT;
    status = I2C_MasterSendData(&flexio_instance, pBuf, len, sendstop);
    if (status == STATUS_SUCCESS)
    {
        do
        {
            s_timeout--;
            status = I2C_MasterGetTransferStatus(&flexio_instance, &s_byteRemaining);
        } while ((s_timeout != 0U) && (status != STATUS_SUCCESS));
        if ((s_byteRemaining != 0U) || (s_timeout == 0U))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}
bool bsp_ctrl_write_register(uint16_t address, uint8_t *buf,  uint8_t len)
{
	bsp_drv_maste_iic_set_slave_address(address);
    ACK =  bsp_drv_master_iic_write(buf,len,false);
    return ACK;
}
bool bsp_ctrl_read_register(uint16_t address,uint8_t pos, uint8_t *buf,  uint8_t len)
{
	bsp_drv_maste_iic_set_slave_address(address);
    ACK =  bsp_drv_master_iic_write(&pos,1,false);
    if(ACK == true)
        ACK =  bsp_drv_master_iic_read(buf,len,true);
     return ACK;
}

bool bsp_ctrl_read_pair_register(uint16_t address,short pos, uint8_t *buf,  uint8_t len)
{
	uint8_t array[2] = {(pos>>8)&0xFF,(pos)&0xFF};
    ACK =  bsp_drv_master_iic_write(array,2,false);
    if(ACK)
    {
    	bsp_drv_maste_iic_set_slave_address(address);
        ACK =  bsp_drv_master_iic_read(buf,len,true);
    }
     return ACK;
}

void bsp_ctrl_wirte_polling(uint16_t address,uint8_t* s_transmitBuffer,uint32_t I2C_TX_BUFFER_LENGTH)
{
	bsp_drv_maste_iic_set_slave_address(address);
	bsp_drv_master_iic_write(s_transmitBuffer,I2C_BUFFER_LENGTH,true);
}
void bsp_ctrl_read_polling(uint16_t address, uint8_t* s_receiveBuffer,uint32_t I2C_RX_BUFFER_LENGTH)
{
	bsp_drv_maste_iic_set_slave_address(address);

	bsp_drv_master_iic_read(s_receiveBuffer,I2C_BUFFER_LENGTH,true);

}
