#include "IIC_Software.h"
#include "ms933x_mpi_dummy.h"

//#define _IIC_MAX_FREQ_ //2Mhz

#ifdef _IIC_MAX_FREQ_
#define IIC_SPEED 0
#endif

////{Freq}: 96Mhz
////#define IIC_SPEED   7    //800khz
////#define IIC_SPEED   10    //600khz
////#define IIC_SPEED   13    //500khz
////#define IIC_SPEED   18    //400khz
////#define IIC_SPEED   25   //300kHz
////#define IIC_SPEED   43   //200khz
//#define IIC_SPEED   80   //110khz
//
//u8 iic_speed_us = 0; //235kHz 默认
//
////修改IIC speed
//void IIC_Set_Speed(u8 i2c_speed)
//{
//    //修改IIC延时时间
//    iic_speed_us = i2c_speed;
//}
//
//void IIC_sf_Init(void)
//{
//    GPIO_InitTypeDef GPIO_InitStructure={0};
//    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
//
//    GPIO_InitStructure.GPIO_Pin = MS933X_SDA | MS933X_SCL;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;   //open drain output
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(GPIOC, &GPIO_InitStructure);
//
//    IIC_SCL(1);
//    IIC_SDA(1);
//    IIC_Set_Speed(IIC_SPEED);
//    //test();
//}
//
//#pragma GCC optimize(1)
//void IIC_Delay(u8 time)
//{
//    u8 i;
//    for (i = 0; i < time; i++)
//    {__NOP();}
//}
//
////产生IIC start 信号
//void IIC_Start(void)
//{
//    SDA_OUT();     //sda output
//    IIC_SDA(1);
//    IIC_SCL(1);
//    IIC_Delay(iic_speed_us);
//    IIC_SDA(0);//START:when CLK is high,DATA change form high to low
//    IIC_Delay(iic_speed_us);
//    IIC_SCL(0);//钳住IIC总线，准备发送或接收data
//}
//
////产生IIC stop信号
//void IIC_Stop(void)
//{
//    SDA_OUT();//sda output
//    IIC_SCL(0);
//    IIC_SDA(0);//STOP:when CLK is high DATA change form low to high
//    IIC_Delay(iic_speed_us); //至少4us 表示空闲
//    IIC_SCL(1);
//    IIC_SDA(1);//send IIC 总线 stop 信号
//    IIC_Delay(iic_speed_us); //至少4us 表示空闲
//}
//
////wait 应答信号到来
////返回值：1，接收应答失败
////       0，接收应答成功
//u8 IIC_Wait_Ack(void)
//{
//    u8 ucErrTime=0;
//    SDA_IN();      //SDA input
//    IIC_SDA(1);IIC_Delay(iic_speed_us);
//    IIC_SCL(1);IIC_Delay(iic_speed_us);
//    while(READ_SDA)
//    {
//        ucErrTime++;
//        if(ucErrTime>250)
//        {
//            IIC_Stop();//等待从机应答超时，send stop
//            return 1;
//        }
//    }
//    IIC_SCL(0);//scl output 0
//    return 0;
//}
//
////产生ACK应答
//void IIC_Ack(void)
//{
//    IIC_SCL(0);
//    SDA_OUT();
//    IIC_SDA(0);
//    IIC_Delay(iic_speed_us);
//    IIC_SCL(1);
//    IIC_Delay(iic_speed_us);
//    IIC_SCL(0);
//}
//
////不产生ACK应答
//void IIC_NAck(void)
//{
//    IIC_SCL(0);
//    SDA_OUT();
//    IIC_SDA(1);
//    IIC_Delay(iic_speed_us);
//    IIC_SCL(1);
//    IIC_Delay(iic_speed_us);
//    IIC_SCL(0);
//}
//
////IIC send one byte
////返回从机有无应答：1，有应答 0，无应答
//#pragma GCC optimize(1)
//void IIC_Send_Byte(u8 txd)
//{
//    u8 t;
//    SDA_OUT();
//    IIC_SCL(0);//拉低scl开始data传输
//    for(t=0;t<8;t++)
//    {
//        if((txd<<t)&0x80)
//            IIC_SDA(1);
//        else
//            IIC_SDA(0);
//        //IIC_Delay(2);   //某些器件延时是必须的
//        #ifdef _IIC_MAX_FREQ_
//            __NOP();
//        #else
//            IIC_Delay(iic_speed_us);
//        #endif
//
//        IIC_SCL(1);
//
//        #ifdef _IIC_MAX_FREQ_
//            __NOP();
//        #else
//            IIC_Delay(iic_speed_us);
//        #endif
//        IIC_SCL(0);
//        //IIC_Delay(2);
//    }
//}
//
////read one byte, ack = 1, send ACK, ack = 0, send NACK
//u8 IIC_Read_Byte(unsigned char ack)
//{
//    unsigned char i,receive=0;
//    SDA_IN();//set SDA input
//    for(i=0;i<8;i++ )
//    {
//        IIC_SCL(0);
//        #ifdef _IIC_MAX_FREQ_
//            __NOP();
//        #else
//            IIC_Delay(iic_speed_us);
//        #endif
//        IIC_SCL(1);
//        receive<<=1;
//        if(READ_SDA)receive++;
//        #ifdef _IIC_MAX_FREQ_
//            __NOP();
//        #else
//            IIC_Delay(iic_speed_us);
//        #endif
//    }
//    if (!ack)
//        IIC_NAck();//send NACK
//    else
//        IIC_Ack(); //send ACK
//    return receive;
//}
//
////check IIC address: 1, 成功 0, 失败
//u8 IIC_Check_Address(u8 Address)
//{
//    IIC_Start();
//    IIC_Send_Byte(Address);
//    if(IIC_Wait_Ack())
//    {
//        IIC_Stop();
//        return (0);
//    }
//    IIC_Stop();
//    return (1);
//}
//
////send many bytes: 8bit address + 8bit register + n bytes data
//u8 IIC_Send_8bitMultiBytes(u8 address, u8 reg, u8 *str, u8 num)
//{
//    u8 i;
//    IIC_Start();
//    IIC_Send_Byte(address);
//    if(IIC_Wait_Ack()) return (0);
//    IIC_Send_Byte(reg);
//    if(IIC_Wait_Ack()) return (0);
//
//    for(i=0; i<num; i++)
//    {
//        IIC_Send_Byte(*str);
//        if(IIC_Wait_Ack()) return (0);
//        str++;
//    }
//    IIC_Stop();
//    return (1);
//}
//
////read many bytes: 8bit address + 8bit register + n bytes data
//u8 IIC_Read_8bitMultiBytes(u8 address, u8 reg, u8 *str, u8 num)
//{
//    u8 i;
//    IIC_Start();
//    IIC_Send_Byte(address);
//    if(IIC_Wait_Ack()) return (0);
//    IIC_Send_Byte(reg);
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Start();
//    IIC_Send_Byte(address+1);
//    if(IIC_Wait_Ack()) return (0);
//    for(i=0; i<num; i++)
//    {
//        *str = IIC_Read_Byte(i == (num-1) ? 0 : 1);
//        str++;
//    }
//
//    IIC_Stop();
//    return (1);
//}
//
////fix read many bytes: 8bit address + 8bit register + n bytes data
//u8 IIC_Read_8bitMultiBytes_sgm832(u8 address, u8 reg, u16 *str, u8 num)
//{
//    IIC_Start();
//    IIC_Send_Byte(address);//8a
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Send_Byte(reg);//fe
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Delay(1);
//    IIC_Start();
//    IIC_Send_Byte(address+1);
//    if(IIC_Wait_Ack()) return (0);
//
//    u8 bit0 = IIC_Read_Byte(1);
//    IIC_Delay(1);
//
//    u8 bit1 = IIC_Read_Byte(0);
//
//    *str = (bit0 << 8) | bit1;
//
//    IIC_Stop();
//    return (1);
//}
//// //fix read many bytes: 8bit address + 8bit register + n bytes data
//// u8 IIC_Read_8bitMultiBytes_sgm832(u8 address, u8 reg, u16 *str, u8 num)
//// {
////     u8 i;
////     IIC_Start();
////     IIC_Send_Byte(address);
////     if(IIC_Wait_Ack()) return (0);
//
////     IIC_Send_Byte(reg >> 8);   // Send the high byte of reg
////     if(IIC_Wait_Ack()) return (0);
////     IIC_Send_Byte(reg & 0xFF); // Send the low byte of reg
////     if(IIC_Wait_Ack()) return (0);
//
////     for(i=0; i<num; i++)
////     {
////         IIC_Send_Byte(*str);
////         if(IIC_Wait_Ack()) return (0);
////         str++;
////     }
////     IIC_Stop();
////     return (1);
//// }
//
////send many bytes(send low byte first): 8bit address + 16bit register + n bytes data
//u8 IIC_Send_16bit_LE_MultiBytes(u8 address, u16 reg, u8 *str, u8 num)
//{
//    u8 i;
//    IIC_Start();
//    IIC_Send_Byte(address);
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Send_Byte(reg & 0xFF); // Send the low byte of reg
//    if(IIC_Wait_Ack()) return (0);
//    IIC_Send_Byte(reg >> 8);   // Send the high byte of reg
//    if(IIC_Wait_Ack()) return (0);
//
//    for(i=0; i<num; i++)
//    {
//        IIC_Send_Byte(*str);
//        if(IIC_Wait_Ack()) return (0);
//        str++;
//    }
//    IIC_Stop();
//    return (1);
//}
//
////read many bytes(send low byte first): 8bit address + 16bit register + n bytes data
//u8 IIC_Read_16bit_LE_MultiBytes(u8 address, u16 reg, u8 *str, u8 num)
//{
//    u8 i;
//    IIC_Start();
//    IIC_Send_Byte(address);
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Send_Byte(reg & 0xFF); // Send the low byte of reg
//    if(IIC_Wait_Ack()) return (0);
//    IIC_Send_Byte(reg >> 8);   // Send the high byte of reg
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Start();
//    IIC_Send_Byte(address+1);
//    if(IIC_Wait_Ack()) return (0);
//    for(i=0; i<num; i++)
//    {
//        *str = IIC_Read_Byte(i == (num-1) ? 0 : 1);
//        str++;
//    }
//
//    IIC_Stop();
//    return (1);
//}
//
////send many bytes(send high byte first): 8bit address + 16bit register + n bytes data
//u8 IIC_Send_16bit_BE_MultiBytes(u8 address, u16 reg, u8 *str, u8 num)
//{
//    u8 i;
//    IIC_Start();
//    IIC_Send_Byte(address);
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Send_Byte(reg >> 8);   // Send the high byte of reg
//    if(IIC_Wait_Ack()) return (0);
//    IIC_Send_Byte(reg & 0xFF); // Send the low byte of reg
//    if(IIC_Wait_Ack()) return (0);
//
//    for(i=0; i<num; i++)
//    {
//        IIC_Send_Byte(*str);
//        if(IIC_Wait_Ack()) return (0);
//        str++;
//    }
//    IIC_Stop();
//    return (1);
//}
//
////read many bytes(send high byte first): 8bit address + 16bit register + n bytes data
//u8 IIC_Read_16bit_BE_MultiBytes(u8 address, u16 reg, u8 *str, u8 num)
//{
//    u8 i;
//    IIC_Start();
//    IIC_Send_Byte(address);
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Send_Byte(reg >> 8);   // Send the high byte of reg
//    if(IIC_Wait_Ack()) return (0);
//    IIC_Send_Byte(reg & 0xFF); // Send the low byte of reg
//    if(IIC_Wait_Ack()) return (0);
//
//    IIC_Start();
//    IIC_Send_Byte(address+1);
//    if(IIC_Wait_Ack()) return (0);
//    for(i=0; i<num; i++)
//    {
//        *str = IIC_Read_Byte(i == (num-1) ? 0 : 1);
//        str++;
//    }
//
//    IIC_Stop();
//    return (1);
//}

#pragma GCC optimize(1)
void IIC_Delay(u8 time)
{
    u8 i;
    for (i = 0; i < time; i++)
    {__NOP();}
}

#define I2C_SDA_HIGH        IIC_SDA(1)
#define I2C_SCL_HIGH        IIC_SCL(1)

#define I2C_SDA_LOW         IIC_SDA(0)
#define I2C_SCL_LOW         IIC_SCL(0)

#define _i2c_read_sda()     GPIO_ReadInputDataBit(GPIOC,MS933X_SDA) //输入SDA



#define _i2c_delay()        mculib_delay_us(g_u8_i2c_delay)

static UINT8 g_u8_i2c_delay = 1;

#define I2C_TIMEOUT         (0x1000) //define I2C wait ack signal time out = I2C_TIMEOUT * _i2c_delay()

VOID _i2c_start(VOID)
{
    /* assume here all lines are HIGH */

    I2C_SDA_HIGH;
    I2C_SCL_HIGH;
    _i2c_delay();

    I2C_SDA_LOW;
    _i2c_delay();

    I2C_SCL_LOW;
}

VOID _i2c_stop(VOID)
{
    I2C_SDA_LOW;
    _i2c_delay();

    I2C_SCL_HIGH;
    _i2c_delay();

    I2C_SDA_HIGH;
    _i2c_delay();
}

BOOL _i2c_write_byte(UINT8 value)
{
    UINT8  index;
    BOOL   result = 0;
    UINT16 u16Wait;

    /* Send 8 bits to the I2C Bus - msb first */
    for (index = 0; index < 8; index++)
    {
        if (value & 0x80)
        {
            I2C_SDA_HIGH;
        }
        else
        {
            I2C_SDA_LOW;
        }
        _i2c_delay();

        I2C_SCL_HIGH;
        _i2c_delay();

        I2C_SCL_LOW;
        _i2c_delay();

        value <<= 1;
    }

    /* wait for ack =  readbit0 */
    I2C_SDA_HIGH;       // avoid  SDA is always Low.
    _i2c_delay();

    I2C_SCL_HIGH;
    _i2c_delay();

    u16Wait = 0;
    while (_i2c_read_sda() && (u16Wait < I2C_TIMEOUT))
        u16Wait ++;

    if (!_i2c_read_sda())
        result = TRUE;

    I2C_SCL_LOW;
    I2C_SDA_HIGH;
    _i2c_delay();

    return result;
}

UINT8 _i2c_read_byte(BOOL lastByte)
{
    UINT8 index;
    UINT8 value = 0x00;

    I2C_SDA_HIGH;
    for (index = 0; index < 8; index++)
    {
        value <<= 1;
        I2C_SCL_HIGH;
        _i2c_delay();
        value |= _i2c_read_sda();
        I2C_SCL_LOW;
        _i2c_delay();
    }

    if (!lastByte)
    {
        I2C_SDA_LOW;
    }
    else
    {
        I2C_SDA_HIGH;
    }
    _i2c_delay();

    I2C_SCL_HIGH;
    _i2c_delay();

    I2C_SCL_LOW;
    I2C_SDA_HIGH;
    _i2c_delay();

    return value;
}

VOID mculib_i2c_init(VOID)
{
        GPIO_InitTypeDef GPIO_InitStructure={0};
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );

        GPIO_InitStructure.GPIO_Pin = MS933X_SDA | MS933X_SCL;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;   //open drain output
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOC, &GPIO_InitStructure);



    _i2c_stop();
}

VOID mculib_i2c_set_speed(UINT8 u8_i2c_speed)
{

}

// 16-bit index for macrosilicon chip
UINT8 mculib_i2c_read_16bidx8bval(UINT8 u8_address, UINT16 u16_index)
{
    UINT8 u8_value = 0;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    _i2c_start();

    if (!_i2c_write_byte(u8_address | 0x01))
        goto STOP;

    u8_value = _i2c_read_byte(1);

STOP:
    _i2c_stop();
    return u8_value;
}

BOOL mculib_i2c_write_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT8 u8_value)
{
    BOOL result = 0;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    if (!_i2c_write_byte(u8_value))
        goto STOP;

    result = TRUE;

STOP:
    _i2c_stop();
    return result;
}

#if 1
VOID mculib_i2c_burstread_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT16 u16_length, UINT8 *pu8_value)
{
    UINT16 i;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    _i2c_start();

    if (!_i2c_write_byte(u8_address | 0x01))
        goto STOP;

    for (i = 0; i < (u16_length-1); i ++)
    {
        *pu8_value++ = _i2c_read_byte(0);
    }

    *pu8_value = _i2c_read_byte(1);

STOP:
    _i2c_stop();
}

VOID mculib_i2c_burstwrite_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT16 u16_length, UINT8 *pu8_value)
{
    UINT16 i;

   _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    for(i = 0; i< (u16_length-1); i ++)
    {
        if (!_i2c_write_byte(*pu8_value++))
            goto STOP;
    }

    if (!_i2c_write_byte(*pu8_value))
        goto STOP;

STOP:
    _i2c_stop();
}
#endif

#if (USER_SAVE == USER_SAVE_EEPROM)
// 8-bit index fro eeprom AT24C02..
UINT8 mculib_i2c_read_8bidx8bval(UINT8 u8_address, UINT8 u8_index)
{
    UINT8 u8_value = 0;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte(u8_index))
        goto STOP;

    _i2c_start();

    if (!_i2c_write_byte(u8_address | 0x01))
        goto STOP;

    u8_value = _i2c_read_byte(1);

STOP:
    _i2c_stop();
    return u8_value;
}

BOOL mculib_i2c_write_8bidx8bval(UINT8 u8_address, UINT8 u8_index, UINT8 u8_value)
{
    BOOL result =0;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte(u8_index))
        goto STOP;

    if (!_i2c_write_byte(u8_value))
        goto STOP;

    result = 1;

STOP:
    _i2c_stop();
    return result;
}

VOID mculib_i2c_burstread_8bidx8bval(UINT8 u8_address, UINT8 u8_index, UINT8 u8_length, UINT8 *pu8_value)
{
    UINT8 i;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte(u8_index))
        goto STOP;

    _i2c_start();

    if (!_i2c_write_byte(u8_address | 0x01))
        goto STOP;

    for (i = 0; i < (u8_length-1); i ++)
    {
        *pu8_value++ = _i2c_read_byte(0);
    }

    *pu8_value = _i2c_read_byte(1);

STOP:
    _i2c_stop();
}

VOID mculib_i2c_burstread_8bidx8bval_ext(UINT8 u8_address, UINT8 u8_index, UINT8 u8_length)
{
    UINT8 i;
    //UINT8 read_falg;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte(u8_index))
        goto STOP;

    _i2c_start();

    if (!_i2c_write_byte(u8_address | 0x01))
        goto STOP;

    for (i = 0; i < (u8_length-1); i ++)
    {
       //read_falg= _i2c_read_byte(0);
      // if(i==0)
       // mculib_uart_log1("read_falg=",read_falg);
    }

    _i2c_read_byte(1);

STOP:
    _i2c_stop();
}

BOOL mculib_i2c_write_blank(UINT8 u8_address, UINT8 u8_index)
{
    BOOL result = 0;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte(u8_index))
        goto STOP;

    result = 1;

STOP:
    ///_i2c_stop();
    return result;
}


VOID mculib_i2c_burstwrite_8bidx8bval(UINT8 u8_address, UINT8 u8_index, UINT8 u8_length, UINT8 *pu8_value)
{
    UINT8 i;

   _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u8_index) ))
        goto STOP;

    for(i = 0; i< (u8_length-1); i ++)
    {
        if (!_i2c_write_byte(*pu8_value++))
            goto STOP;
    }

    if (!_i2c_write_byte(*pu8_value))
        goto STOP;

STOP:
    _i2c_stop();
}
#endif

#if 0
// 16-bit index for eeprom AT24C04..
UINT8 mculib_i2c_read_eeprom_16bidx8bval(UINT8 u8_address, UINT16 u16_index)
{
    UINT8 u8_value = 0;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    _i2c_start();

    if (!_i2c_write_byte(u8_address | 0x01))
        goto STOP;

    u8_value = _i2c_read_byte(TRUE);

STOP:
    _i2c_stop();
    return u8_value;
}

BOOL mculib_i2c_write_eeprom_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT8 u8_value)
{
    BOOL result = FALSE;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    if (!_i2c_write_byte(u8_value))
        goto STOP;

    result = TRUE;

STOP:
    _i2c_stop();
    return result;
}

VOID mculib_i2c_burstread_eeprom_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT16 u16_length, UINT8 *pu8_value)
{
    UINT16 i;

    _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    _i2c_start();

    if (!_i2c_write_byte(u8_address | 0x01))
        goto STOP;

    for (i = 0; i < (u16_length-1); i ++)
    {
        *pu8_value++ = _i2c_read_byte(FALSE);
    }

    *pu8_value = _i2c_read_byte(TRUE);

STOP:
    _i2c_stop();
}

VOID mculib_i2c_burstwrite_eeprom_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT16 u16_length, UINT8 *pu8_value)
{
    UINT16 i;

   _i2c_start();

    if (!_i2c_write_byte(u8_address))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index >> 8) ))
        goto STOP;

    if (!_i2c_write_byte( (UINT8)(u16_index) ))
        goto STOP;

    for(i = 0; i< (u16_length-1); i ++)
    {
        if (!_i2c_write_byte(*pu8_value++))
            goto STOP;
    }

    if (!_i2c_write_byte(*pu8_value))
        goto STOP;

STOP:
    _i2c_stop();
}
#endif

#if 0
//general I2C interfalce
VOID mculib_i2c_rw_register(MS_I2CX_RW_REGISTER_T *pst_i2cx_to_register)
{
    UINT16 u16_i = 0;

    if (pst_i2cx_to_register == NULL || pst_i2cx_to_register->p_u8_addr_buffer == NULL)
    {
        return;
    }

    _i2c_start();

    _i2c_write_byte(*(pst_i2cx_to_register->p_u8_addr_buffer) & 0xfe);

    for (u16_i = 0; u16_i < pst_i2cx_to_register->u8_addr_num - 1; u16_i++)
    {
        _i2c_write_byte(*(pst_i2cx_to_register->p_u8_addr_buffer + u16_i + 1));
    }

    if (pst_i2cx_to_register->u16_i2c_write_bytes_num != 0)
    {
        if ((pst_i2cx_to_register->p_u8_i2c_write_buffer == NULL))
        {
            return;
        }
        for (u16_i = 0; u16_i < pst_i2cx_to_register->u16_i2c_write_bytes_num; u16_i++)
        {
            _i2c_write_byte(*(pst_i2cx_to_register->p_u8_i2c_write_buffer + u16_i));
        }
    }

    if (pst_i2cx_to_register->u16_i2c_read_bytes_num == 0)
    {
        _i2c_stop();
    }
    else
    {
        if ((pst_i2cx_to_register->p_u8_i2c_read_buffer == NULL))
        {
            return;
        }

        _i2c_start();
        _i2c_write_byte(*(pst_i2cx_to_register->p_u8_addr_buffer) | 0x01);

        for (u16_i = 0; u16_i < pst_i2cx_to_register->u16_i2c_read_bytes_num; u16_i++)
        {
            if (u16_i < pst_i2cx_to_register->u16_i2c_read_bytes_num - 1)
            {
                *(pst_i2cx_to_register->p_u8_i2c_read_buffer + u16_i) = _i2c_read_byte(FALSE);
            }
            else
            {
                *(pst_i2cx_to_register->p_u8_i2c_read_buffer + u16_i) = _i2c_read_byte(TRUE);
            }
        }

        _i2c_stop();
    }
}
#endif

