#include "mculib_common.h"
#include "ch32v30x.h"
#include "IIC_Software.h"
#include "IIC2_Software.h"

void mculib_chip_reset(void)
{
    MS933x_RST_GPIO_PP();
    GPIO_WriteBit(GPIOC, MS933X_RST,  Bit_RESET);
    Delay_Ms(2);
    GPIO_WriteBit(GPIOC, MS933X_RST,  Bit_SET);
    MS933x_RST_GPIO_IPU();
    Delay_Ms(2);
}

void mculib_delay_ms(UINT8 u8_ms)
{
    Delay_Ms(u8_ms);
}

void mculib_delay_us(UINT8 u8_us)
{
    Delay_Us(u8_us);
}


// 16-bit index for macrosilicon chip
//UINT8 mculib_i2c_read_16bidx8bval(UINT8 u8_address, UINT16 u16_index)
//{
//    UINT8 rBuf = 0;
//
//    IIC_Read_16bit_LE_MultiBytes(u8_address, u16_index, &rBuf, 1);
//
//    return rBuf;
//}

//BOOL mculib_i2c_write_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT8 u8_value)
//{
//  return IIC_Send_16bit_LE_MultiBytes(u8_address, u16_index, &u8_value, 1);
//}

//VOID mculib_i2c_burstread_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT16 u16_length, UINT8 *pu8_value)
//{
//  IIC_Read_16bit_LE_MultiBytes(u8_address, u16_index, pu8_value, u16_length);
//}

//VOID mculib_i2c_burstwrite_16bidx8bval(UINT8 u8_address, UINT16 u16_index, UINT16 u16_length, UINT8 *pu8_value)
//{
//    if(u8_address == IIC_ADDR_MS1826)
//        IIC2_Send_16bit_LE_MultiBytes(u8_address, u16_index, pu8_value, u16_length);
//    else
//        {
//            IIC_Send_16bit_LE_MultiBytes(u8_address, u16_index, pu8_value, u16_length);
//        }
//}

BOOL mculib_i2c_write_8bidx16bval(UINT8 u8_address, UINT8 u8_index, UINT16 u16_value)
{
    UINT8 rBuf = 0;
    if(u8_address == IIC_ADDR_MS1826)
        IIC2_Read_16bit_LE_MultiBytes(u8_address, u8_index, &rBuf, 1);
    else
        IIC_Read_16bit_LE_MultiBytes(u8_address, u8_index, &rBuf, 1);
    return rBuf;
}


//********************************************************************************************
//ms93xxx common function
//********************************************************************************************
// Set I2C Read/Write speed, it is best to set speed of HD DDC at about 20KHz, 否则可能会影响通信质量
// u8_i2c_speed: I2C_SPEED_20K(0), I2C_SPEED_100K(1)
//fix todo: need fix to set i2c speed
//void  mculib_i2c_set_speed(UINT8 u8_i2c_speed)
//{
//  //need to set i2c speed
//  if(u8_i2c_speed == 0)
//  {
//    u8_i2c_speed=0;
//  }
//  else if (u8_i2c_speed == 1)
//  {
//    u8_i2c_speed=1;
//  }
//}
//8-bit index for HD EDID block 2-3 读取接口
//如果用户无需过 HD CTS 认证，此函数实现可为空
//BOOL mculib_i2c_write_blank(UINT8 u8_address, UINT8 u8_index)
//{
//  return 0;
//}

//void mculib_i2c_burstread_8bidx8bval(UINT8 u8_address, UINT8 u8_index, UINT8 u8_length, UINT8 *pu8_value)
//{
//    if(u8_address == IIC_ADDR_MS1826)
//        IIC2_Read_8bitMultiBytes(u8_address, u8_index, pu8_value, u8_length);
//    else
//        IIC_Read_8bitMultiBytes(u8_address, u8_index, pu8_value, u8_length);
//}

//标准字节读写接口
//UINT8 mculib_i2c_read_8bidx8bval(UINT8 u8_address, UINT8 u8_index)
//{
//  UINT8 rBuf = 0;
//  if(u8_address == IIC_ADDR_MS1826)
//    IIC2_Read_8bitMultiBytes(u8_address, u8_index, &rBuf, 1);
//  else
//    IIC_Read_8bitMultiBytes(u8_address, u8_index, &rBuf, 1);
//  return rBuf;
//}

//BOOL  mculib_i2c_write_8bidx8bval(UINT8 u8_address, UINT8 u8_index, UINT8 u8_value)
//{
//  if(u8_address == IIC_ADDR_MS1826)
//    return IIC2_Send_8bitMultiBytes(u8_address, u8_index, &u8_value, 1);
//  else
//    return IIC_Send_8bitMultiBytes(u8_address, u8_index, &u8_value, 1);
//}
