RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
+ I2C 的协作图:

结构体

struct  rt_i2c_priv_data
 
struct  rt_i2c_msg
 
struct  rt_i2c_bus_device_ops
 
struct  rt_i2c_bus_device
 
struct  rt_i2c_client
 

宏定义

#define RT_I2C_WR   0x0000
 
#define RT_I2C_RD   (1u << 0)
 
#define RT_I2C_ADDR_10BIT   (1u << 2)
 
#define RT_I2C_NO_START   (1u << 4)
 
#define RT_I2C_IGNORE_NACK   (1u << 5)
 
#define RT_I2C_NO_READ_ACK   (1u << 6) /* when I2C reading, we do not ACK */
 
#define RT_I2C_NO_STOP   (1u << 7)
 
#define RT_I2C_DEV_CTRL_10BIT   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x01)
 
#define RT_I2C_DEV_CTRL_ADDR   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x02)
 
#define RT_I2C_DEV_CTRL_TIMEOUT   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x03)
 
#define RT_I2C_DEV_CTRL_RW   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x04)
 
#define RT_I2C_DEV_CTRL_CLK   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x05)
 
#define RT_I2C_DEV_CTRL_UNLOCK   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x06)
 
#define RT_I2C_DEV_CTRL_GET_STATE   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x07)
 
#define RT_I2C_DEV_CTRL_GET_MODE   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x08)
 
#define RT_I2C_DEV_CTRL_GET_ERROR   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x09)
 

函数

rt_err_t rt_i2c_bus_device_device_init (struct rt_i2c_bus_device *bus, const char *name)
 
rt_err_t rt_i2c_bus_device_register (struct rt_i2c_bus_device *bus, const char *bus_name)
 
struct rt_i2c_bus_devicert_i2c_bus_device_find (const char *bus_name)
 
rt_ssize_t rt_i2c_transfer (struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
 
rt_err_t rt_i2c_control (struct rt_i2c_bus_device *bus, int cmd, void *args)
 
rt_ssize_t rt_i2c_master_send (struct rt_i2c_bus_device *bus, rt_uint16_t addr, rt_uint16_t flags, const rt_uint8_t *buf, rt_uint32_t count)
 
rt_ssize_t rt_i2c_master_recv (struct rt_i2c_bus_device *bus, rt_uint16_t addr, rt_uint16_t flags, rt_uint8_t *buf, rt_uint32_t count)
 
rt_inline rt_err_t rt_i2c_bus_lock (struct rt_i2c_bus_device *bus, rt_tick_t timeout)
 
rt_inline rt_err_t rt_i2c_bus_unlock (struct rt_i2c_bus_device *bus)
 

详细描述

I2C driver api

Example

#include <rtthread.h>
#include <rtdevice.h>
#define AHT10_I2C_BUS_NAME "i2c1" // 传感器连接的I2C总线设备名称
#define AHT10_ADDR 0x38 // 从机地址
#define AHT10_CALIBRATION_CMD 0xE1 // 校准命令
#define AHT10_NORMAL_CMD 0xA8 // 一般命令
#define AHT10_GET_DATA 0xAC // 获取数据命令
static struct rt_i2c_bus_device *i2c_bus = RT_NULL; // I2C总线设备句柄
static rt_bool_t initialized = RT_FALSE; // 传感器初始化状态
// 写传感器寄存器
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
{
rt_uint8_t buf[3];
struct rt_i2c_msg msgs;
rt_uint32_t buf_size = 1;
buf[0] = reg; //cmd
if (data != RT_NULL)
{
buf[1] = data[0];
buf[2] = data[1];
buf_size = 3;
}
msgs.addr = AHT10_ADDR;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = buf_size;
// 调用I2C设备接口传输数据
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
// 读传感器寄存器数据
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
{
struct rt_i2c_msg msgs;
msgs.addr = AHT10_ADDR;
msgs.flags = RT_I2C_RD;
msgs.buf = buf;
msgs.len = len;
// 调用I2C设备接口传输数据
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
static void read_temp_humi(float *cur_temp, float *cur_humi)
{
rt_uint8_t temp[6];
write_reg(i2c_bus, AHT10_GET_DATA, RT_NULL); // 发送命令
read_regs(i2c_bus, 6, temp); // 获取传感器数据
// 湿度数据转换
*cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
// 温度数据转换
*cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
}
static void aht10_init(const char *name)
{
rt_uint8_t temp[2] = {0, 0};
// 查找I2C总线设备,获取I2C总线设备句柄
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
if (i2c_bus == RT_NULL)
{
rt_kprintf("can't find %s device!\n", name);
}
else
{
write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
temp[0] = 0x08;
temp[1] = 0x00;
write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp);
initialized = RT_TRUE;
}
}
static void i2c_aht10_sample(int argc, char *argv[])
{
float humidity, temperature;
char name[RT_NAME_MAX];
humidity = 0.0;
temperature = 0.0;
if (argc == 2)
{
rt_strncpy(name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
}
if (!initialized)
{
// 传感器初始化
aht10_init(name);
}
if (initialized)
{
// 读取温湿度数据
read_temp_humi(&temperature, &humidity);
rt_kprintf("read aht10 sensor humidity : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
if( temperature >= 0 )
{
rt_kprintf("read aht10 sensor temperature: %d.%d°C\n", (int)temperature, (int)(temperature * 10) % 10);
}
else
{
rt_kprintf("read aht10 sensor temperature: %d.%d°C\n", (int)temperature, (int)(-temperature * 10) % 10);
}
}
else
{
rt_kprintf("initialize sensor failed!\n");
}
}
// 导出到 msh 命令列表中
MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);
rt_device_t rt_device_find(const char *name)
#define RT_I2C_RD
#define RT_I2C_WR
rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
I2C data transmission.
#define rt_kprintf(...)
rt_err_t rt_thread_mdelay(rt_int32_t ms)
This function will let current thread delay for some milliseconds.
#define MSH_CMD_EXPORT(...)
int rt_bool_t
rt_base_t rt_err_t
unsigned char rt_uint8_t
#define RT_TRUE
unsigned int rt_uint32_t
#define RT_FALSE
#define RT_NULL
I2C Bus Device
I2C Message
rt_uint16_t addr
rt_uint16_t len
rt_uint8_t * buf

宏定义说明

◆ RT_I2C_WR

#define RT_I2C_WR   0x0000

i2c wirte flag

在文件 dev_i2c.h183 行定义.

◆ RT_I2C_RD

#define RT_I2C_RD   (1u << 0)

i2c read flag

在文件 dev_i2c.h184 行定义.

◆ RT_I2C_ADDR_10BIT

#define RT_I2C_ADDR_10BIT   (1u << 2)

this is a ten bit chip address

在文件 dev_i2c.h185 行定义.

◆ RT_I2C_NO_START

#define RT_I2C_NO_START   (1u << 4)

do not generate START condition

在文件 dev_i2c.h186 行定义.

◆ RT_I2C_IGNORE_NACK

#define RT_I2C_IGNORE_NACK   (1u << 5)

ignore NACK from slave

在文件 dev_i2c.h187 行定义.

◆ RT_I2C_NO_READ_ACK

#define RT_I2C_NO_READ_ACK   (1u << 6) /* when I2C reading, we do not ACK */

在文件 dev_i2c.h188 行定义.

◆ RT_I2C_NO_STOP

#define RT_I2C_NO_STOP   (1u << 7)

do not generate STOP condition

在文件 dev_i2c.h189 行定义.

◆ RT_I2C_DEV_CTRL_10BIT

#define RT_I2C_DEV_CTRL_10BIT   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x01)

在文件 dev_i2c.h191 行定义.

◆ RT_I2C_DEV_CTRL_ADDR

#define RT_I2C_DEV_CTRL_ADDR   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x02)

在文件 dev_i2c.h192 行定义.

◆ RT_I2C_DEV_CTRL_TIMEOUT

#define RT_I2C_DEV_CTRL_TIMEOUT   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x03)

在文件 dev_i2c.h193 行定义.

◆ RT_I2C_DEV_CTRL_RW

#define RT_I2C_DEV_CTRL_RW   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x04)

在文件 dev_i2c.h194 行定义.

◆ RT_I2C_DEV_CTRL_CLK

#define RT_I2C_DEV_CTRL_CLK   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x05)

在文件 dev_i2c.h195 行定义.

◆ RT_I2C_DEV_CTRL_UNLOCK

#define RT_I2C_DEV_CTRL_UNLOCK   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x06)

在文件 dev_i2c.h196 行定义.

◆ RT_I2C_DEV_CTRL_GET_STATE

#define RT_I2C_DEV_CTRL_GET_STATE   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x07)

在文件 dev_i2c.h197 行定义.

◆ RT_I2C_DEV_CTRL_GET_MODE

#define RT_I2C_DEV_CTRL_GET_MODE   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x08)

在文件 dev_i2c.h198 行定义.

◆ RT_I2C_DEV_CTRL_GET_ERROR

#define RT_I2C_DEV_CTRL_GET_ERROR   (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x09)

在文件 dev_i2c.h199 行定义.

函数说明

◆ rt_i2c_bus_device_device_init()

rt_err_t rt_i2c_bus_device_device_init ( struct rt_i2c_bus_device * bus,
const char * name )

I2C Bus Device Initialization

参数
busthe I2C bus device
namethe name of I2C bus device
返回
rt_err_t error code

◆ rt_i2c_bus_device_register()

rt_err_t rt_i2c_bus_device_register ( struct rt_i2c_bus_device * bus,
const char * bus_name )

I2C Bus Device Register

参数
busthe I2C bus device
bus_namethe name of I2C bus device
返回
rt_err_t error code

◆ rt_i2c_bus_device_find()

struct rt_i2c_bus_device * rt_i2c_bus_device_find ( const char * bus_name)

I2C Bus Device Find

参数
bus_namethe name of I2C bus device
返回
rt_i2c_bus_device the I2C bus device

◆ rt_i2c_transfer()

rt_ssize_t rt_i2c_transfer ( struct rt_i2c_bus_device * bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num )

I2C data transmission.

参数
busthe I2C bus device
msgsthe I2C message list
numthe number of I2C message
返回
rt_ssize_t the actual length of transmitted

◆ rt_i2c_control()

rt_err_t rt_i2c_control ( struct rt_i2c_bus_device * bus,
int cmd,
void * args )

I2C Control

参数
busthe I2C bus device
cmdthe I2C control command
argsthe I2C control arguments
返回
rt_err_t error code

◆ rt_i2c_master_send()

rt_ssize_t rt_i2c_master_send ( struct rt_i2c_bus_device * bus,
rt_uint16_t addr,
rt_uint16_t flags,
const rt_uint8_t * buf,
rt_uint32_t count )

I2C Master Send

参数
busthe I2C bus device
addrthe I2C slave address
flagsthe I2C flags
bufthe I2C send buffer
countthe I2C send buffer length
返回
rt_ssize_t the actual length of transmitted

◆ rt_i2c_master_recv()

rt_ssize_t rt_i2c_master_recv ( struct rt_i2c_bus_device * bus,
rt_uint16_t addr,
rt_uint16_t flags,
rt_uint8_t * buf,
rt_uint32_t count )

I2C Master Receive

参数
busthe I2C bus device
addrthe I2C slave address
flagsthe I2C flags
bufthe I2C receive buffer
countthe I2C receive buffer length
返回
rt_ssize_t the actual length of received

◆ rt_i2c_bus_lock()

rt_inline rt_err_t rt_i2c_bus_lock ( struct rt_i2c_bus_device * bus,
rt_tick_t timeout )

在文件 dev_i2c.h385 行定义.

386{
387 return rt_mutex_take(&bus->lock, timeout);
388}
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
定义 ipc.c:1537
struct rt_mutex lock

引用了 rt_i2c_bus_device::lock, rt_mutex_take() , 以及 rt_i2c_bus_device::timeout.

+ 函数调用图:

◆ rt_i2c_bus_unlock()

rt_inline rt_err_t rt_i2c_bus_unlock ( struct rt_i2c_bus_device * bus)

在文件 dev_i2c.h390 行定义.

391{
392 return rt_mutex_release(&bus->lock);
393}
rt_err_t rt_mutex_release(rt_mutex_t mutex)
This function will release a mutex. If there is thread suspended on the mutex, the thread will be res...
定义 ipc.c:1589

引用了 rt_i2c_bus_device::lock , 以及 rt_mutex_release().

+ 函数调用图: