#include "bsp_i2c.h"


I2C_HELPER_TypeDef * I2C_HELPER_Init( )
{
        I2C_HELPER_TypeDef *Handle = (I2C_HELPER_TypeDef *)malloc(sizeof(I2C_HELPER_TypeDef));
    if (Handle == NULL)
    {
        return NULL;
    }
	Handle->hi2c = NULL;
	Handle->flag_busy = 0;
	Handle->flag_send_or_rcv = 0;

	Handle->mem_addr_size = I2C_MEMADD_SIZE_8BIT;
	Handle->dev_addr = 0;

	Handle->size = 0;
	Handle->write_delay_ms = 0;
	Handle->buf = NULL;

	Handle->trans_mode = I2C_Trans_Polling;
	Handle->rcv_mode = I2C_Trans_Polling;
 
	Handle->obj =NULL;
	Handle->callback =NULL;
    return Handle;
}

void I2C_HELPER_Set_Hi2c(I2C_HELPER_TypeDef * i2chelper, I2C_HandleTypeDef *hi2c)
{
    i2chelper->hi2c = hi2c;
}

void I2C_HELPER_Set_dev_addr(I2C_HELPER_TypeDef * i2chelper, uint8_t dev_addr)
{
    i2chelper->dev_addr = dev_addr;

}
void I2C_HELPER_Set_mem_addr_size(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr_size)
{
    i2chelper->mem_addr_size = mem_addr_size;

}
void I2C_HELPER_Set_mem_addr(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr)
{
    // i2chelper->mem_addr[0] = mem_addr>>8;
    // i2chelper->mem_addr[1] = mem_addr&0xFF;
    
    i2chelper->mem_addr =mem_addr;
    if (i2chelper->mem_addr_size == I2C_MEMADD_SIZE_16BIT)
    {
        i2chelper->mem_addr =mem_addr;
    }
}

int I2C_HELPER_Setup_Trans_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t trans_mode )
{
    i2chelper->trans_mode = trans_mode;
    i2chelper->rcv_mode = trans_mode;
}
int I2C_HELPER_Setup_Rcv_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t rcv_mode )
{
    i2chelper->rcv_mode = rcv_mode;
}

int I2C_HELPER_Set_Callback( I2C_HELPER_TypeDef * i2chelper,  void *obj, i2c_callback_fun func)
{
	i2chelper->obj = obj;
	i2chelper->callback = func;
	return 0;
}

//   TODO  添加地址 
int I2C_HELPER_Trans(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size)
{
	int st;
    i2chelper->flag_busy = 1;
 

    switch ( i2chelper->trans_mode)
    {
        case 0:
            // st = HAL_UART_Transmit(i2chelper->huart,  buf,  size  , 0xFFFF);
            st = HAL_I2C_Mem_Write(i2chelper->hi2c, i2chelper->dev_addr, 
                i2chelper->mem_addr,i2chelper->mem_addr_size ,
                 buf,size,0xFFFF);
            return st;
            break;
        case 1:
            st = HAL_I2C_Mem_Write_IT(i2chelper->hi2c, i2chelper->dev_addr, 
                i2chelper->mem_addr,i2chelper->mem_addr_size ,
                 buf,size );
            return st;
            break;
        case 2:
            /* 会出现 st ==2  huart->gState != HAL_UART_STATE_READY */
             st = HAL_I2C_Mem_Write_DMA(i2chelper->hi2c, i2chelper->dev_addr, 
                i2chelper->mem_addr,i2chelper->mem_addr_size ,
                 buf,size );
            return st;
            break;    
        default:
            break;
    }
    return st;

}

int I2C_HELPER_Start_Rcv(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size)
{
	int st;
    switch ( i2chelper->rcv_mode)
    {
        case 0:
            // st = HAL_UART_Receive(uarthelper->huart,  buf,  size  , 0xFFFF);
            st =HAL_I2C_Mem_Read(i2chelper->hi2c, i2chelper->dev_addr, 
                i2chelper->mem_addr,i2chelper->mem_addr_size ,
                 buf,size,0xFFFF);

            return st;
            break;
        case 1:
            st =HAL_I2C_Mem_Read_IT(i2chelper->hi2c, i2chelper->dev_addr, 
                i2chelper->mem_addr,i2chelper->mem_addr_size ,
                 buf,size );
            return st;
            break;
        case 2:
            /* 会出现 st ==2  huart->gState != HAL_UART_STATE_READY */
			// if (uarthelper->enable_idle_it == 1) 
			// {
            // // log_i("dma rcv idle en... ");
			// 	__HAL_UART_ENABLE_IT(uarthelper->huart, UART_IT_IDLE);
			// }
            // st = HAL_UART_Receive_DMA(uarthelper->huart,  buf,  size);
            // log_i("dma rcv ... ");
            st =HAL_I2C_Mem_Read_DMA(i2chelper->hi2c, i2chelper->dev_addr, 
                i2chelper->mem_addr,i2chelper->mem_addr_size ,
                 buf,size );
            return st;
            break;    
        default:
            break;
    }
    return st;
}


int I2C_HELPER_Trans_TxCplt_Callback( I2C_HELPER_TypeDef * i2chelper )
{
    // log_i( "I2C_HELPER_Trans_TxCplt_Callback  ... ");
    i2chelper->callback( i2chelper->obj, NULL, 0);
}



// int I2C_HELPER_read_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size)
// {
// 	return 0;

// }
// int I2C_HELPER_write_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size)
// {
// 	return 0;

// }

// int I2C_HELPER_read_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size)
// {
// 	return 0;

// }
// int I2C_HELPER_write_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size)
// {
// 	return 0;

// }
