#ifdef I2C_USE_GPIO
#define GPIO_I2C_DEBUG
#define GPIO_I2C_SDA_PIN GPIO_I2C1_SCA_PIN
#define GPIO_I2C_SCA_PIN GPIO_I2C1_SDA_PIN
#define GPIO_SDA_OUTP mt_set_gpio_dir(GPIO_I2C_SDA_PIN,GPIO_DIR_OUT) //设定SDA输出
#define GPIO_SDA_INP mt_set_gpio_dir(GPIO_I2C_SDA_PIN,GPIO_DIR_IN) //设定SDA输入
#define GPIO_SCL_OUTP mt_set_gpio_dir(GPIO_I2C_SCA_PIN,GPIO_DIR_OUT) //设定SCL输出
#define I2C_SDA_Output(value) mt_set_gpio_out(GPIO_I2C_SDA_PIN,value)
#define I2C_SCL_Output(value) mt_set_gpio_out(GPIO_I2C_SCA_PIN,value)
#define DELAY_TIME 0xc0
u8 I2C_SDA_Read(void) //SDA读数据
{
return mt_get_gpio_in(GPIO_I2C_SDA_PIN);
}
void I2C_Init(void)
{
mt_set_gpio_mode(GPIO_I2C_SDA_PIN,GPIO_MODE_00);
mt_set_gpio_mode(GPIO_I2C_SCA_PIN,GPIO_MODE_00);
GPIO_SDA_OUTP;
GPIO_SCL_OUTP;
I2C_SDA_Output(1);
I2C_SCL_Output(1); //默认拉高
}
#define I2C_START_TRANSMISSION
{
volatile u8 idx;
GPIO_SCL_OUTP;
GPIO_SDA_OUTP;
I2C_SDA_Output(1);
for (idx = 0; idx
I2C_SCL_Output(1);
for (idx = 0; idx
I2C_SDA_Output(0);
for (idx = 0; idx
I2C_SCL_Output(0);
}
#define I2C_STOP_TRANSMISSION
{
volatile u8 idx;
GPIO_SCL_OUTP;
GPIO_SDA_OUTP;
I2C_SCL_Output(0);
I2C_SDA_Output(0);
for (idx = 0; idx
I2C_SCL_Output(1);
for (idx = 0; idx
I2C_SDA_Output(1);
}
static kal_uint8 I2C_Send_Byte(kal_uint8 send_byte)
{
volatile signed char i = 0;
volatile kal_uint16 j = 0;
kal_uint8 ack = 0;
for (i = 7; i >= 0; i--)
{
if (send_byte&(1 <
{
I2C_SDA_Output(1);
}
else
{
I2C_SDA_Output(0);
}
for (j = 0; j
I2C_SCL_Output(1);
for (j = 0; j
GPIO_SDA_OUTP; /* only for delay */
for (j = 0; j
I2C_SCL_Output(0);
for (j = 0; j
}
GPIO_SDA_INP;
for (j = 0; j
I2C_SCL_Output(1);
for (j = 0; j
ack = I2C_SDA_Read();
for (j = 0; j
I2C_SCL_Output(0);
for (j = 0; j
GPIO_SDA_OUTP;
return ack;
}
static kal_uint8 I2C_Receive_Byte(void)
{
volatile signed char i = 0;
volatile kal_uint16 j = 0;
kal_uint8 get_byte = 0;
GPIO_SDA_INP;
for (j = 0; j
for (i = 7; i >= 0; i--) { // data bit 7~0
I2C_SCL_Output(1);
for (j = 0; j
if (I2C_SDA_Read()) {
get_byte |= (1 <
}
for (j = 0; j
I2C_SCL_Output(0);
for (j = 0; j
}
// don't care bit, 9th bit
GPIO_SDA_OUTP;
I2C_SDA_Output(1);
for (j = 0; j
I2C_SCL_Output(1);
for (j = 0; j
I2C_SCL_Output(0);
return get_byte;
}
static kal_uint16 I2C_Receive_word(void)
{
volatile signed char i = 0;
volatile kal_uint32 j = 0;
kal_uint16 get_byte = 0;
for (i = 15; i >= 8; i--)
{
GPIO_SDA_INP;
for (j = 0; j
I2C_SCL_Output(1);
for (j = 0; j
if (I2C_SDA_Read()) get_byte |= (1 <
for (j = 0; j
I2C_SCL_Output(0);
for (j = 0; j
}
I2C_SDA_Output(0);
GPIO_SDA_OUTP;
for (j = 0;j
I2C_SCL_Output(1);
for (j = 0;j
GPIO_SDA_OUTP; /* just for delay */
for (j = 0;j
I2C_SCL_Output(0);
for (j = 0;j
for (; i >= 0; i--)
{
GPIO_SDA_INP;
for (j = 0; j
I2C_SCL_Output(1);
for (j = 0; j
if (I2C_SDA_Read()) get_byte |= (1 <
for (j = 0; j
I2C_SCL_Output(0);
for (j = 0; j
}
I2C_SDA_Output(1);
GPIO_SDA_OUTP;
for (j = 0;j
I2C_SCL_Output(1);
for (j = 0;j
GPIO_SDA_OUTP; /* just for delay */
for (j = 0;j
I2C_SCL_Output(0);
for (j = 0;j
return get_byte;
}
u8 I2C_Byte_Write(u8 * a_puBuff , u8 len , u8 i2c_addr)
{
kal_uint8 fail_try_no = 4;
volatile signed char i = 0 ;
u8 ack_flag = 0;
while (--fail_try_no > 0)
{
ack_flag = 0;
I2C_START_TRANSMISSION;
if (I2C_Send_Byte(i2c_addr)) continue;
for(i = 0 ;i
if (I2C_Send_Byte(a_puBuff[i])){
ack_flag = 1;
break;
}
}
if(ack_flag)
continue;
break;
}
I2C_STOP_TRANSMISSION;
return 0;
}
u8 I2C_Bytes_Read(u8 *a_puBuff, u8 reglen ,u8 *byteget,u8 bytelen,u8 i2c_addr)
{
kal_uint16 get_byte = 0xFFFF;
kal_uint8 fail_try_no = 4;
volatile kal_uint32 i = 0;
u8 ack_flag = 0;
while (--fail_try_no > 0)
{
ack_flag = 0;
I2C_START_TRANSMISSION;
if (I2C_Send_Byte(i2c_addr)) continue;
//send reg
for(i = 0 ;i
if (I2C_Send_Byte(a_puBuff[i])){
ack_flag = 1;
break;
}
}
if(ack_flag)
continue;
I2C_START_TRANSMISSION;
if (I2C_Send_Byte(i2c_addr | 1)) continue;
if(bytelen == 1)
byteget[0] = get_byte = I2C_Receive_Byte(); //读单个字节,不需要再发应答
else if(bytelen > 1)
{
get_byte=I2C_Receive_word();
byteget[1] = get_byte & 0x00ff;
byteget[0] = (get_byte & 0xff00) >> 8;
}
break;
}
I2C_STOP_TRANSMISSION;
printk("----%s---read data %x---n",__func__,get_byte);
return get_byte;
}
int iWriteRegI2C(u8 *a_pSendData , u16 a_sizeSendData, u16 i2cId)
{
I2C_Byte_Write(a_pSendData,a_sizeSendData,i2cId);
return 0;
}
int iReadRegI2C(u8 *a_pSendData , u16 a_sizeSendData, u8 * a_pRecvData, u16 a_sizeRecvData, u16 i2cId)
{