/**
******************************************************************************
* @file    ms933x_comm.c
* @author
* @version V1.0.0
* @date    15-Nov-2014
* @brief   This file contains all c files
* @history
*
* Copyright (c) 2009-2014, MacroSilicon Technology Co.,Ltd.
******************************************************************************/
#include "BoardConfig.h"
#include "ms933x_comm.h"
#include "ms933x.h"
#include "ms933x_app_config.h"
#include "IIC_Software.h"
//#include "IIC_Hardware.h"

//无需非常精确， 误差不要大于 30%.
void _mculib_delay_us_(UINT16 u8_us)
{
  Delay_Us(u8_us);
}

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

#if 0

//复位引脚至少有 1ms 时间的低电平，芯片才会复位。
//拉高后，建议再 delay 1ms，让芯片 stable。
void mculib_chip_reset()
{
  MS1826a_RST_GPIO_PP();

  GPIO_WriteBit(GPIOA, MS1826_RST, Bit_RESET);
  Delay_Ms(2);//RST
  GPIO_WriteBit(GPIOA, MS1826_RST, Bit_SET);
  //for test
  Delay_Ms(2);//RST
  GPIO_WriteBit(GPIOA, MS1826_RST, Bit_RESET);
  Delay_Ms(2);//RST
  GPIO_WriteBit(GPIOA, MS1826_RST, Bit_SET);

  MS1826a_RST_GPIO_IPU();
  Delay_Ms(2);
}

//MS933X 的寄存器索引为 16 位地址； I2C 在写入索引值时，先写入索引地址的低 8 位，然后写入索引地址的高 8 位。
//I2C 的速度建议在： 100KHz~400KHz 之间，越大软件执行效率越高
//GT_CMD_WR = 0xB2;
//GT_CMD_RD = 0xB3;
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)
{
  IIC_Send_16bit_LE_MultiBytes(u8_address, u16_index, pu8_value, u16_length);
}


// I2C 读写速度设置， HD DDC 最好工作在较低速度 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;
  }
}

//标准字节读写接口
UINT8 mculib_i2c_read_8bidx8bval(UINT8 u8_address, UINT8 u8_index)
{
  UINT8 rBuf = 0;
  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)
{
  return IIC_Send_8bitMultiBytes(u8_address, u8_index, &u8_value, 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)
{
  IIC_Read_8bitMultiBytes(u8_address, u8_index, pu8_value, u8_length);
}

void _mculib_uart_log_(UINT8 *u8_string)
{
  if(_LOG_ENABLE_)
    printf("%s\n",u8_string);
}

void _mculib_uart_log1_(UINT8 *u8_string, UINT16 u16_hex)
{
  if(_LOG_ENABLE_)
    printf("%s: 0x%04X\n", u8_string, u16_hex);
}

void _mculib_uart_log2_(UINT8 *u8_string, UINT16 u16_dec)
{
  if(_LOG_ENABLE_)
    printf("%s: %d\n", u8_string, u16_dec);
}

#endif