驱动I2C操作研究

typedef struct
{
    uint32 id;                设备逻辑ID
    uint32 freq;            频率
    uint32 bus;                总线,这里不用理会,自动分配
    uint8 slave_addr;        从设备地址
    uint8 reg_addr_num;        IIC内部寄存器长度
    uint8 check_ack;        应答信号,1检查 0不检查
    uint8 no_stop;            停止位
} I2C_DEV;


API
I2C_HAL_OPEN  :初始化I2C设备控制器,并返回从设备句柄,必须要调用I2C_HAL_Close来释放。
    参数:dev: I2C从设备指针
    返回值:大于等于0 成功返回句柄
            小于0        出错
            
I2C_HAL_CLOSE  :关闭I2C控制器
    参数:Handle: 已初始化的I2C handle
    返回值:成功 0
            失败 -1
            
I2C_HAL_Read  :I2C控制器读数据
    参数:Handle    : 已初始化的I2C handle
          reg_addr    :从设备寄存器地址
          buffer    :从从设备里读取的数据保存在buffer里
          bytes        :读多少位
    返回值:成功 返回读取的数量
            失败 0
            
            
I2C_HAL_Write    :I2C控制器写数据
    参数:Handle    : 已初始化的I2C handle
          reg_addr    :从设备寄存器地址
          buffer    :往从设备里写保存在buffer里的数据
          bytes        :写多少位
    返回值:成功 返回写的数量
            失败 0

I2C_HAL_Ioctl   :设备读取I2C控制器状态
    参数:Handle    : 已初始化的I2C handle
          cmd        :i2c_drvapi.h里定义的命令
          arg        :命令参数
    返回值:没有返回值
    
    
    
    
API 例子
操作一个8位寄存器设备
1写
[开始][从设备地址][写][应答][设备寄存器地址7:0][应答][数据][应答][停止]
2读
[开始][从设备地址][写][应答][设备寄存器地址7:0][应答][重复起始][设备寄存器地址7:0][读][读到的数据][非应答][停止]


sample code
#include "i2c_drvapi.h"
       
/*define the i2c slave device handle*/
uint32 i2c_handle_device;

void i2c_device_init(void)
{
    ……
    /*init i2c*/
    I2C_DEV dev;
    dev.id = 0; //this i2c slave device is connected to i2c bus 0.
    dev.freq = 100*1000; //100KHz
    dev.slave_addr = 0x60; //8-bit write address
    dev.reg_addr_num = 1; // i2c slave device register’s length is 8-bits
    dev.check_ack = 1; //check ack signal
    dev.no_stop = 0; //send stop
    i2c_handle_device  = I2C_HAL_Open(&dev);

    /*other init process*/
        ……
}

uint32 device_set_some_para (void)
{
    ……
    uint8 addr = 0x30; //register address
    uint8 buffer = 0x10; //data buffer
    I2C_HAL_Write(i2c_handle_device, &addr, &buffer, 1);
    ……
}

uint32 device_get_some_para (void)
{
    ……
    uint8 addr = 0x30; //register address
    uint8 buffer = 0; //data buffer
    I2C_HAL_Read(i2c_handle_device, &addr, &buffer, 1);
    ……
}

void i2c_device_exit(void)
{
    ……
    I2C_HAL_Close(i2c_handle_device);
    ……

}


操作一个16位寄存器设备
1写
[开始][从设备地址][写][应答][从设备寄存器(15:8)][应答][设备寄存器地址7:0][应答][数据][应答][停止]
2读
[开始][从设备地址][写][应答][从设备寄存器(15:8)][应答][设备寄存器地址7:0][应答][重复起始][从设备地址][读][读到的数据][非应答][停止]

sample code
#include "i2c_drvapi.h"
       
/*define the i2c slave device handle*/
uint32 i2c_handle_device;

void i2c_device_init(void)
{
    ……
    /*init i2c*/
    I2C_DEV dev;
    dev.id = 0; //this i2c slave device is connected to i2c bus 0.
    dev.freq = 100*1000; //100KHz
    dev.slave_addr = 0x30; //8-bit write address
    dev.reg_addr_num = 2; // i2c slave device register’s length is 16-bits
    dev.check_ack = 1; //check ack signal
    dev.no_stop = 0; //send stop
    i2c_handle_device  = I2C_HAL_Open(&dev);

    /*other init process*/
    ……
}

uint32 device_set_some_para (void)
{
    ……
    uint8 addr[2] = {0}; //register address
    uint8 buffer[2] = {0}; //data buffer
    addr[0] = 0x30; //high 8 bits of the register address
    addr[1] = 0x90; //low 8 bits of the register address
    buffer[0] = 0x10; //high 8 bits of the data value
    buffer[1] = 0x20; //low 8 bits of the data value
    I2C_HAL_Write(i2c_handle_device, addr, buffer, 2);
    ……
}

uint32 device_get_some_para (void)
{
    ……
    uint8 addr[2] = {0}; //register address
    uint8 buffer[2] = {0}; //data buffer
    addr[0] = 0x30; //high 8 bits of the register address
    addr[1] = 0x90; //low 8 bits of the register address
    I2C_HAL_Read(i2c_handle_device, addr, buffer, 2);
    ……
}

void i2c_device_exit(void)
{
    ……
    I2C_HAL_Close(i2c_handle_device);    
    ……

}

-----------------------------多设备
        #include "i2c_drvapi.h"
       
       /*define the i2c slave device handle*/
uint32 i2c_handle_device;

void i2c_device_init(void)
{
    ……
    /*init i2c*/
    I2C_DEV dev;
    dev.id = 1; //this i2c slave device is connected to i2c bus 1.
    dev.freq = 100*1000; //100KHz
    dev.slave_addr = 0x2c; //8-bit write address
    dev.reg_addr_num = 0; // i2c slave device is simple
    dev.check_ack = 1; //check ack signal
    dev.no_stop = 0; //send stop
    i2c_handle_device  = I2C_HAL_Open(&dev);

    /*other init process*/
        ……
}

uint32 device_set_some_para (void)
{
    ……
    uint8 buffer[2] = {0}; //data buffer
    buffer[0] = 0x10; //high 8 bits of the data value
    buffer[1] = 0x20; //low 8 bits of the data value
    I2C_HAL_Write(i2c_handle_device, NULL, buffer, 2);
    ……
}

uint32 device_get_some_para (void)
{
    ……
    uint8 buffer[2] = {0}; //data buffer
    I2C_HAL_Read(i2c_handle_device, NULL, buffer, 2);
    ……
}

void i2c_device_exit(void)
{
    ……
    I2C_HAL_Close(i2c_handle_device);    
    ……

}

----------------------------------------------
APPLICATION EXAMPLE
PUBLIC ERR_I2C_E PCM1774_WriteRegister(uint8 reg_addr,uint8 data)
{
    uint8 cmd[2];
    cmd[0]    =    reg_addr;
    cmd[1]    =    data;

    return I2C_WriteCmdArr(PCM1774_ADDRESS_WR, cmd, 2, SCI_TRUE);
}



原文地址:https://www.cnblogs.com/baoshulin/p/6308847.html