/******************************************************************************
* Copyright (C), 2007-2018, Sannuo R&D CENTER
* FileName: software_I2C_driver
* Author: ZHIYU.RONG
* Date: 2018-06-21
* Description: Software I2C for ATS282x
*
* Function List:
* 1.I2C_Recev_Bytes
* 2.Software_I2C_Read
* 3.Software_I2C_Write
*****************************************************************************/
#include "TWI.h"
#include "software_I2C_driver.h"
#define WRITE_MAX 3
#define READ_MAX 3
/*----------------------------------------------------------------------------*
* Function: I2C_Recev_Bytes
*
* Input:
* 1.gpio: Used pins
* 2.device_addr: Device address
* 3.reg_addr: Register address
* 4.write_data: Data to be written
* 5.delay: Delay Time
* 6.read_len: The length of the read data
*
* Return:
* 0:FALSE
* 1:TRUE
*
* Description: Software I2C reads data to a register of the device
*---------------------------------------------------------------------------*/
uint8 I2C_Recev_Bytes (gpio_init_cfg_t* gpio, uint8 device_addr, uint8 reg_addr, uint8* read_data, uint8 read_len, uint8* delay)
{
uint8* p_get = read_data;
uint8 ret = 1;
TWI_Init(gpio, delay);
TWI_Start (gpio, delay);
TWI_WriteByte (device_addr, gpio, delay);
if (TWI_GetAck (gpio, delay) != 0)
{
//读寄存器地址,地址在高位,需左移1位,低位为1表示读,从0x0a寄存器读数据
TWI_WriteByte (reg_addr, gpio, delay);
if (TWI_GetAck (gpio, delay) != 0)
{
TWI_Start (gpio, delay);
TWI_WriteByte ( (device_addr | 0x01), gpio, delay);
if (TWI_GetAck (gpio, delay) != 0)
{
while (read_len != 0)
{
//receive data
*p_get = TWI_ReadByte (gpio, delay);
if (read_len == 1)
{
//最后一字节发送NACK
TWI_SendAck (1, gpio, delay);
}
else
{
//前面字节发送ACK
TWI_SendAck (0, gpio, delay);
}
p_get++;
read_len--;
}
}
else
{
ret = 0;
}
}
else
{
ret = 0;
}
}
else
{
ret = 0;
}
TWI_Stop (gpio, delay);
return ret;
}
/*----------------------------------------------------------------------------*
* Function: Software_I2C_Read
*
* Input:
* 1.gpio: Used pins
* 2.device_addr: Device address
* 3.reg_addr: Register address
* 4.write_data: Data to be written
* 5.delay: Delay Time
* 6.read_len: The length of the read data
*
* Return:
* 0:FALSE
* 1:TRUE
*
* Description: Software I2C reads data to a register of the device
*---------------------------------------------------------------------------*/
uint8 Software_I2C_Read (gpio_init_cfg_t* gpio, uint8 device_addr, uint8 reg_addr, uint8* read_data, uint8 read_len, uint8* delay)
{
uint8 ret = FALSE;
uint8 i;
for (i=0; i<READ_MAX; i++)
{
if (TRUE == I2C_Recev_Bytes (gpio, device_addr, reg_addr, read_data, read_len, delay))
{
ret = TRUE;
break;
}
}
return ret;
}
/*----------------------------------------------------------------------------*
* Function: Software_I2C_Write
*
* Input:
* 1.gpio: Used pins
* 2.device_addr: Device address
* 3.reg_addr: Register address
* 4.write_data: Data to be written
* 5.delay: Delay Time
*
* Return:
* 0:FALSE
* 1:TRUE
*
* Description: Software I2C writes data to a register of the device
*---------------------------------------------------------------------------*/
uint8 Software_I2C_Write (gpio_init_cfg_t* gpio, uint8 device_addr, uint8 reg_addr, uint8 write_data, uint8* delay)
{
uint8 set_buf[2];
uint8 ret = FALSE;
uint8 i;
set_buf[0] = reg_addr;
set_buf[1] = write_data;
for (i=0; i<WRITE_MAX; i++)
{
if (TRUE == TWI_Trans_Bytes (set_buf, device_addr, 0x2, gpio, delay))
{
ret = TRUE;
break;
}
}
return ret;
}