linux 应用层读写I2C设备

28次阅读
没有评论

I2C 简介

I2C(Inter-Integrated Circuit)是一种广泛应用于嵌入式系统的串行通信协议,由飞利浦(Philips)在 1980 年代开发。I2C 允许多个设备通过两根信号线(数据线 SDA 和时钟线 SCL)进行通信。与其他串行总线(如 SPI)相比,I2C 的最大特点是它支持多主机和多从设备,且仅需要两根线就能连接多个设备,这使得 I2C 成为嵌入式系统中广泛应用的通信协议。

I2C 协议采用主从架构,在通信时通过地址唯一标识每个设备。主设备通过 SCL 时钟线同步数据传输,数据通过 SDA 数据线传输。I2C 通信的传输速率通常为 100kHz 或 400kHz,但也可以通过配置提高至 1MHz 或更高。

Linux 下的 I2C 设备

在 Linux 系统中,I2C 设备通过 /dev 目录下的设备文件呈现,通常命名为 /dev/i2c-X,其中 X 是设备编号。例如,/dev/i2c-0 表示第一个 I2C 总线设备,/dev/i2c-1 则表示第二个 I2C 总线设备。

要使用这些设备文件,开发者需要通过相应的系统调用来与 I2C 设备进行通信。通常,I2C 设备的操作包括设备的初始化、数据的读写、设备的控制等。

I2C应用编程

关键结构体

struct i2c_msg

i2c_msg 是 I2C 消息的描述结构体,用于定义一次 I2C 通信中的单个传输消息。它可以是读或写操作,并包含目标设备的地址、数据缓冲区和数据长度。

struct i2c_msg {
    __u16 addr;    // 从设备的地址
    __u16 flags;   // 操作标志位
    __u16 len;     // 要发送/接收的数据长度
    __u8 *buf;     // 数据缓冲区指针
};

主要标志位:

  • I2C_M_RD: 表示读操作。如果未设置此标志,则表示写操作。
  • I2C_M_TEN: 表示使用 10 位地址模式。
  • I2C_M_STOP: 表示在消息结束时生成 STOP 信号。

struct i2c_rdwr_ioctl_data

i2c_rdwr_ioctl_data 结构体用于通过 ioctl 系统调用执行 I2C 读写操作。它包含一个 i2c_msg 数组和消息的数量。

struct i2c_rdwr_ioctl_data {
    struct i2c_msg *msgs;  // 指向消息数组
    __u32 nmsgs;           // 消息的数量
};

I2C 应用编程概述

在用户空间中,I2C 编程的核心操作是:

  1. 打开 I2C 设备:通过 open() 函数打开 /dev/i2c-X 设备文件。
  2. 设置从设备地址:使用 ioctl() 系统调用设置目标从设备的地址。
  3. 数据读写:通过 read() 和 write() 进行简单的读写操作,或使用 ioctl() 进行复杂的多字节传输。

打开 I2C 设备

使用 open() 函数打开 I2C 设备文件,例如 /dev/i2c-1

int file;
file = open("/dev/i2c-1", O_RDWR);
if (file < 0) {
    perror("Failed to open the i2c bus");
    exit(1);
}

设置从设备地址

通过 ioctl() 设置与哪个从设备进行通信:

int addr = 0x50; // 从设备地址
if (ioctl(file, I2C_SLAVE, addr) < 0) {
    perror("Failed to acquire bus access and/or talk to slave");
    exit(1);
}

I2C_SLAVE 是常用的 ioctl 命令,用于设置 I2C 从设备地址。

读写 I2C 数据

写入数据

使用 write() 函数将数据写入 I2C 设备:

unsigned char buffer[1] = { 0xA0 };
if (write(file, buffer, 1) != 1) {
    perror("Failed to write to the i2c bus");
}

读取数据

使用 read() 函数从 I2C 设备读取数据:

unsigned char buffer[1];
if (read(file, buffer, 1) != 1) {
    perror("Failed to read from the i2c bus");
} else {
    printf("Data read: 0x%02x\n", buffer[0]);
}

使用 ioctl 进行复杂读写操作

对于更复杂的读写操作(如多字节传输),可以使用 I2C_RDWR 命令结合 i2c_rdwr_ioctl_data 来实现。

写入多个字节

假设要向寄存器 0x10 写入两个字节的数据:

unsigned char outbuf[3] = { 0x10, 0x01, 0x02 };
struct i2c_msg messages[1];
messages[0].addr  = addr;
messages[0].flags = 0;  // 写操作
messages[0].len   = 3;
messages[0].buf   = outbuf;

struct i2c_rdwr_ioctl_data ioctl_data;
ioctl_data.msgs  = messages;
ioctl_data.nmsgs = 1;

if (ioctl(file, I2C_RDWR, &ioctl_data) < 0) {
    perror("Failed to write to the i2c bus");
}

读取寄存器数据

通常,读取寄存器数据需要先写入寄存器地址,再读取其数据。例如,读取寄存器 0x10:

unsigned char reg = 0x10;
unsigned char inbuf[1];

struct i2c_msg messages[2];
messages[0].addr  = addr;
messages[0].flags = 0;  // 写操作
messages[0].len   = 1;
messages[0].buf   = &reg;

messages[1].addr  = addr;
messages[1].flags = I2C_M_RD;  // 读操作
messages[1].len   = 1;
messages[1].buf   = inbuf;

struct i2c_rdwr_ioctl_data ioctl_data;
ioctl_data.msgs  = messages;
ioctl_data.nmsgs = 2;

if (ioctl(file, I2C_RDWR, &ioctl_data) < 0) {
    perror("Failed to read from the i2c bus");
} else {
    printf("Register 0x10 read: 0x%02x\n", inbuf[0]);
}

常用的 ioctl 命令

  • I2C_SLAVE: 设置从设备地址。
  • I2C_RDWR: 执行多条 I2C 消息(读或写),对应 i2c_rdwr_ioctl_data

完整的 I2C 应用层示例

以下是一个完整的示例程序,展示如何通过 I2C 接口读取和写入数据:

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <unistd.h>

int main() {
    int file;
    int addr = 0x50; // I2C 从设备地址

    // 打开 I2C 设备
    if ((file = open("/dev/i2c-1", O_RDWR)) < 0) {
        perror("Failed to open the i2c bus");
        return 1;
    }

    // 设置从设备地址
    if (ioctl(file, I2C_SLAVE, addr) < 0) {
        perror("Failed to acquire bus access and/or talk to slave");
        return 1;
    }

    // 写入数据到寄存器 0x10
    unsigned char outbuf[3] = { 0x10, 0x01, 0x02 };

    if (write(file, outbuf, 3) != 3) {
        perror("Failed to write to the i2c bus");
        return 1;
    }

    // 读取寄存器 0x10 的值
    unsigned char reg = 0x10;
    unsigned char inbuf[1];

    struct i2c_msg messages[2];
    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = 1;
    messages[0].buf   = &reg;

    messages[1].addr  = addr;
    messages[1].flags = I2C_M_RD;
    messages[1].len   = 1;
    messages[1].buf   = inbuf;

    struct i2c_rdwr_ioctl_data ioctl_data;
    ioctl_data.msgs  = messages;
    ioctl_data.nmsgs = 2;

    if (ioctl(file, I2C_RDWR, &ioctl_data) < 0) {
        perror("Failed to read from the i2c bus");
        return 1;
    } else {
        printf("Register 0x10 read: 0x%02x\n", inbuf[0]);
    }

    close(file);
    return 0;
}

使用 i2c-tools 进行调试

i2c-tools 是一组用于调试 I2C 设备的工具集,其中包括 i2cdetecti2cgeti2cset 和 i2cdump 等常用工具。以下是这些工具的使用介绍。

i2cdetect

i2cdetect 用于扫描 I2C 总线,查看哪些设备在响应。

i2cdetect -y 1

i2cget

i2cget 用于读取 I2C 设备寄存器的数据。

i2cget -y 1 0x50 0x00

i2cset

i2cset 用于向 I2C 设备写入数据。

i2cset -y 1 0x50 0x00 0xFF

i2cdump

i2cdump 用于读取 I2C 设备的所有寄存器,并以十六进制输出。

i2cdump -y 1 0x50

其它

i2c-tools 官方源码仓库:https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git

正文完
 0
评论(没有评论)
验证码