Q群:
电话:
邮箱:
地址:
函数 | 说明 |
---|---|
os_device_find() | 根据I2C总线设备名称查找设备 |
os_i2c_transfer() | I2C数据传输 |
os_i2c_client_write() | I2C主设备数据发送 |
os_i2c_client_read() | I2C主设备数据接收 |
os_i2c_client_write_byte() | I2C主设备发送一个字节数据 |
os_i2c_client_read_byte() | I2C主设备接收一个字节数据 |
os_i2c_master_send() | I2C总线设备数据发送 |
os_i2c_master_recv() | I2C总线设备数据接收 |
该函数根据 I2C 总线设备名称查找对应的I2C设备,函数原型如下:
os_device_t *os_device_find(const char *name);
参数 | 说明 |
---|---|
name | I2C总线设备名称 |
返回 | 说明 |
设备指针 | 查找到对应设备将返回相应的设备指针 |
OS_NULL | 没有找到设备 |
该函数用于 I2C 数据传输,函数原型如下:
os_size_t os_i2c_transfer(struct os_i2c_bus_device *bus, struct os_i2c_msg msgs[], os_uint32_t num);
参数 | 说明 |
---|---|
bus | I2C 总线设备句柄 |
msgs[] | 待传输的消息数组指针 |
num | 消息数组的元素个数 |
返回 | 说明 |
消息数组的元素个数 | 成功 |
<=0 | 失败 |
I2C 消息数据结构原型如下:
struct os_i2c_msg
{
os_uint16_t addr;
os_uint16_t flags;
os_uint16_t len;
os_uint8_t *buf;
};
标志 flags 可取值为以下宏定义,根据需要可以与其他宏使用位运算 “|” 组合起来使用:
#define OS_I2C_WR 0x0000
#define OS_I2C_RD (1u << 0)
#define OS_I2C_ADDR_10BIT (1u << 2)
#define OS_I2C_NO_START (1u << 4)
#define OS_I2C_IGNORE_NACK (1u << 5)
#define OS_I2C_NO_READ_ACK (1u << 6)
该函数用于I2C主设备发送数据,函数原型如下:
os_err_t os_i2c_client_write(struct os_i2c_client *client,
os_uint32_t cmd,
os_uint8_t cmd_len,
os_uint8_t *buff,
os_uint32_t len)
参数 | 说明 |
---|---|
client | 设备指针 |
cmd | 命令字(1-4字节) |
cmd_len | 命令字长度(取值1-4) |
buff | 数据缓存指针 |
len | 数据长度 |
返回 | 说明 |
OS_EOK | 发送成功 |
OS_ERROR | 发送失败 |
该函数用于I2C主设备接收数据,函数原型如下:
os_err_t os_i2c_client_read(struct os_i2c_client *client,
os_uint32_t cmd,
os_uint8_t cmd_len,
os_uint8_t *buff,
os_uint32_t len)
参数 | 说明 |
---|---|
client | 设备指针 |
cmd | 命令字(1-4字节) |
cmd_len | 命令字长度(取值1-4) |
buff | 数据缓存指针 |
len | 数据长度 |
返回 | 说明 |
OS_EOK | 接收成功 |
OS_ERROR | 接收失败 |
该函数用于I2C主设备发送一个字节,函数原型如下:
os_err_t os_i2c_client_write_byte(struct os_i2c_client *client, os_uint32_t cmd, os_uint8_t cmd_len, os_uint8_t data)
参数 | 说明 |
---|---|
client | 设备指针 |
cmd | 命令字(1-4字节) |
cmd_len | 命令字长度(取值1-4) |
data | 待发送的数据(1字节) |
返回 | 说明 |
OS_EOK | 发送成功 |
OS_ERROR | 发送失败 |
该函数用于I2C主设备接收一个字节,函数原型如下:
os_uint8_t os_i2c_client_read_byte(struct os_i2c_client *client, os_uint32_t cmd, os_uint8_t cmd_len);
参数 | 说明 |
---|---|
client | 设备指针 |
cmd | 命令字(1-4字节) |
cmd_len | 命令字长度(取值1-4) |
返回 | 说明 |
接收到的数据 | 接收到的数据(1字节) |
该函数用于I2C总线设备发送数据,函数原型如下:
os_size_t os_i2c_master_send(struct os_i2c_bus_device *bus,
os_uint16_t addr,
os_uint16_t flags,
const os_uint8_t *buf,
os_uint32_t count)
参数 | 说明 |
---|---|
bus | I2C总线指针 |
addr | 传输地址 |
flags | 传输标志 |
buf | 数据缓存指针 |
count | 数据长度 |
返回 | 说明 |
成功发送的数据量 | 以字节为单位 |
<= 0 | 发送失败 |
该函数用于I2C总线设备接收数据,函数原型如下:
os_size_t os_i2c_master_recv(struct os_i2c_bus_device *bus,
os_uint16_t addr,
os_uint16_t flags,
os_uint8_t *buf,
os_uint32_t count)
参数 | 说明 |
---|---|
bus | I2C总线指针 |
addr | 传输地址 |
flags | 传输标志 |
buf | 数据缓存指针 |
count | 数据长度 |
返回 | 说明 |
成功接收的数据量 | 以字节为单位 |
<= 0 | 接收失败 |
示例中通过I2C控制AT24CXX设备:
#include <board.h>
#include <drv_cfg.h>
#include <os_hw.h>
#include <os_memory.h>
#include <sensors/sensor.h>
#include <drv_log.h>
#include <string.h>
#ifdef OS_USING_SHELL
#include <shell.h>
#endif
#define OS_AT24CXX_I2C_ADDR 0xA0 >> 1
#define OS_AT24CXX_PAGE 0x02
#define I2C_PAGESIZE 16
static struct os_i2c_bus_device* at24cxx_i2c_bus;
static os_err_t write_regs(struct os_i2c_bus_device *bus, os_uint16_t reg, os_uint8_t *buf, os_uint8_t len)
{
struct os_i2c_msg msgs;
os_uint8_t databuf[9];
os_uint8_t device_addr = 0;
os_uint8_t reg_addr = 0;
OS_ASSERT(len <= 8);
if (reg > 255)
{
device_addr = OS_AT24CXX_I2C_ADDR | 0x01;
}
else
{
device_addr = OS_AT24CXX_I2C_ADDR;
}
reg_addr = reg & 0xFF;
databuf[0] = reg_addr;
memcpy(&databuf[1], buf, len);
msgs.addr = device_addr;
msgs.flags = OS_I2C_WR;
msgs.buf = databuf;
msgs.len = len + 1;
if (os_i2c_transfer(bus, &msgs, 1) == 1)
{
return OS_EOK;
}
else
{
LOG_EXT_E("Writing command error");
return -OS_ERROR;
}
}
static os_err_t read_regs(struct os_i2c_bus_device *bus, os_uint16_t reg, os_uint8_t *buf, os_uint8_t len)
{
struct os_i2c_msg msgs[2];
os_uint8_t device_addr = 0;
os_uint8_t reg_addr = 0;
if (reg > 255)
{
device_addr = OS_AT24CXX_I2C_ADDR | 0x01;
}
else
{
device_addr = OS_AT24CXX_I2C_ADDR;
}
reg_addr = reg & 0xFF;
msgs[0].addr = device_addr;
msgs[0].flags = OS_I2C_WR;
msgs[0].buf = ®_addr;
msgs[0].len = 1;
msgs[1].addr = device_addr;
msgs[1].flags = OS_I2C_RD;
msgs[1].buf = buf;
msgs[1].len = len;
if (os_i2c_transfer(bus, msgs, 2) == 2)
{
return OS_EOK;
}
else
{
LOG_EXT_E("Reading command error");
return -OS_ERROR;
}
}
void __at24cxx_test(const char* i2c_bus)
{
int i;
os_uint8_t read_buff[8];
os_uint8_t write_buff[8];
/* find i2c device */
at24cxx_i2c_bus = (struct os_i2c_bus_device *)os_device_find(i2c_bus);
if (at24cxx_i2c_bus == NULL)
{
LOG_EXT_E("AT24CXX i2c invalid.");
return;
}
for (i = 0; i < 8; i++)
{
write_buff[i] = (os_uint8_t)rand();
}
write_regs(at24cxx_i2c_bus, 0, write_buff, sizeof(write_buff));
os_task_mdelay(5);
memset(read_buff, 0, sizeof(read_buff));
read_regs(at24cxx_i2c_bus, 0, read_buff, sizeof(read_buff));
for (i = 0; i < 8; i++)
{
os_kprintf("write: 0x%02x, read: 0x%02x\r\n", write_buff[i], read_buff[i]);
if (read_buff[i] != write_buff[i])
{
LOG_EXT_E("at24xx test failed index:%d, write:0x%02x, read:0x%02x",
i, write_buff[i], read_buff[i]);
return;
}
}
os_kprintf("at24xx test success.\r\n");
}
static void at24cxx_test(int argc, char** argv)
{
if (argc != 2)
{
LOG_EXT_E("at24cxx_test <i2c bus name>");
LOG_EXT_E("eg: at24cxx_test i2c2");
return;
}
/* at24cxx test */
__at24cxx_test(argv[1]);
}
SH_CMD_EXPORT(at24cxx_test, at24cxx_test, "test_at24cxx!");
运行结果如下:
sh />at24cxx_test i2c2
write: 0xd4, read: 0xd4
write: 0x97, read: 0x97
write: 0x67, read: 0x67
write: 0x2e, read: 0x2e
write: 0xb2, read: 0xb2
write: 0x1d, read: 0x1d
write: 0x75, read: 0x75
write: 0x24, read: 0x24
at24xx test success.