在Linux 应用层 基于i2c-dev.h 实现i2c读写

FT5x06触摸屏升级流程详解
本文详细介绍了如何使用I2C接口对FT5x06触摸屏进行固件升级,包括设置地址、读写寄存器、验证升级前后的固件版本等步骤。
/*
    i2c-dev.h - i2c-bus driver, char device interface

    Copyright (C) 1995-97 Simon G. Vogl
    Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA.
*/

#ifndef _LINUX_I2C_DEV_H
#define _LINUX_I2C_DEV_H

#include <linux/types.h>
#include <sys/ioctl.h>
#include <stddef.h>


/* -- i2c.h -- */


/*
 * I2C Message - used for pure i2c transaction, also from /dev interface
 */
struct i2c_msg {
	__u16 addr;	/* slave address			*/
	unsigned short flags;
#define I2C_M_TEN	0x10	/* we have a ten bit chip address	*/
#define I2C_M_RD	0x01
#define I2C_M_NOSTART	0x4000
#define I2C_M_REV_DIR_ADDR	0x2000
#define I2C_M_IGNORE_NAK	0x1000
#define I2C_M_NO_RD_ACK		0x0800
	short len;		/* msg length				*/
	char *buf;		/* pointer to msg data			*/
};

/* To determine what functionality is present */

#define I2C_FUNC_I2C			0x00000001
#define I2C_FUNC_10BIT_ADDR		0x00000002
#define I2C_FUNC_PROTOCOL_MANGLING	0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
#define I2C_FUNC_SMBUS_PEC		0x00000008
#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL	0x00008000 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_QUICK		0x00010000
#define I2C_FUNC_SMBUS_READ_BYTE	0x00020000
#define I2C_FUNC_SMBUS_WRITE_BYTE	0x00040000
#define I2C_FUNC_SMBUS_READ_BYTE_DATA	0x00080000
#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA	0x00100000
#define I2C_FUNC_SMBUS_READ_WORD_DATA	0x00200000
#define I2C_FUNC_SMBUS_WRITE_WORD_DATA	0x00400000
#define I2C_FUNC_SMBUS_PROC_CALL	0x00800000
#define I2C_FUNC_SMBUS_READ_BLOCK_DATA	0x01000000
#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
#define I2C_FUNC_SMBUS_READ_I2C_BLOCK	0x04000000 /* I2C-like block xfer  */
#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK	0x08000000 /* w/ 1-byte reg. addr. */

#define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
                             I2C_FUNC_SMBUS_WRITE_BYTE)
#define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
                                  I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
#define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
                                  I2C_FUNC_SMBUS_WRITE_WORD_DATA)
#define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
                                   I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
#define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
                                  I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)

/* Old name, for compatibility */
#define I2C_FUNC_SMBUS_HWPEC_CALC	I2C_FUNC_SMBUS_PEC

/*
 * Data for SMBus Messages
 */
#define I2C_SMBUS_BLOCK_MAX	32	/* As specified in SMBus standard */
#define I2C_SMBUS_I2C_BLOCK_MAX	32	/* Not specified but we use same structure */
union i2c_smbus_data {
	__u8 byte;
	__u16 word;
	__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
	                                            /* and one more for PEC */
};

/* smbus_access read or write markers */
#define I2C_SMBUS_READ	1
#define I2C_SMBUS_WRITE	0

/* SMBus transaction types (size parameter in the above functions)
   Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
#define I2C_SMBUS_QUICK		    0
#define I2C_SMBUS_BYTE		    1
#define I2C_SMBUS_BYTE_DATA	    2
#define I2C_SMBUS_WORD_DATA	    3
#define I2C_SMBUS_PROC_CALL	    4
#define I2C_SMBUS_BLOCK_DATA	    5
#define I2C_SMBUS_I2C_BLOCK_BROKEN  6
#define I2C_SMBUS_BLOCK_PROC_CALL   7		/* SMBus 2.0 */
#define I2C_SMBUS_I2C_BLOCK_DATA    8


/* /dev/i2c-X ioctl commands.  The ioctl's parameter is always an
 * unsigned long, except for:
 *	- I2C_FUNCS, takes pointer to an unsigned long
 *	- I2C_RDWR, takes pointer to struct i2c_rdwr_ioctl_data
 *	- I2C_SMBUS, takes pointer to struct i2c_smbus_ioctl_data
 */
#define I2C_RETRIES	0x0701	/* number of times a device address should
				   be polled when not acknowledging */
#define I2C_TIMEOUT	0x0702	/* set timeout in units of 10 ms */

/* NOTE: Slave address is 7 or 10 bits, but 10-bit addresses
 * are NOT supported! (due to code brokenness)
 */
#define I2C_SLAVE	0x0703	/* Use this slave address */
#define I2C_SLAVE_FORCE	0x0706	/* Use this slave address, even if it
				   is already in use by a driver! */
#define I2C_TENBIT	0x0704	/* 0 for 7 bit addrs, != 0 for 10 bit */

#define I2C_FUNCS	0x0705	/* Get the adapter functionality mask */

#define I2C_RDWR	0x0707	/* Combined R/W transfer (one STOP only) */

#define I2C_PEC		0x0708	/* != 0 to use PEC with SMBus */
#define I2C_SMBUS	0x0720	/* SMBus transfer */


/* This is the structure as used in the I2C_SMBUS ioctl call */
struct i2c_smbus_ioctl_data {
	__u8 read_write;
	__u8 command;
	__u32 size;
	union i2c_smbus_data *data;
};

/* This is the structure as used in the I2C_RDWR ioctl call */
struct i2c_rdwr_ioctl_data {
	struct i2c_msg *msgs;	/* pointers to i2c_msgs */
	__u32 nmsgs;			/* number of i2c_msgs */
};

#define  I2C_RDRW_IOCTL_MAX_MSGS	42


static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
                                     int size, union i2c_smbus_data *data)
{
	struct i2c_smbus_ioctl_data args;

	args.read_write = read_write;
	args.command = command;
	args.size = size;
	args.data = data;
	return ioctl(file,I2C_SMBUS,&args);
}


static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
{
	return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
}

static inline __s32 i2c_smbus_read_byte(int file)
{
	union i2c_smbus_data data;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
		return -1;
	else
		return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
{
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
	                        I2C_SMBUS_BYTE,NULL);
}

static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
{
	union i2c_smbus_data data;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                     I2C_SMBUS_BYTE_DATA,&data))
		return -1;
	else
		return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
                                              __u8 value)
{
	union i2c_smbus_data data;
	data.byte = value;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_BYTE_DATA, &data);
}

static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
{
	union i2c_smbus_data data;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                     I2C_SMBUS_WORD_DATA,&data))
		return -1;
	else
		return 0x0FFFF & data.word;
}

static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
                                              __u16 value)
{
	union i2c_smbus_data data;
	data.word = value;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_WORD_DATA, &data);
}

static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
{
	union i2c_smbus_data data;
	data.word = value;
	if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                     I2C_SMBUS_PROC_CALL,&data))
		return -1;
	else
		return 0x0FFFF & data.word;
}


/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
                                              __u8 *values)
{
	union i2c_smbus_data data;
	int i;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                     I2C_SMBUS_BLOCK_DATA,&data))
		return -1;
	else {
		for (i = 1; i <= data.block[0]; i++)
			values[i-1] = data.block[i];
		return data.block[0];
	}
}

static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
                                               __u8 length, const __u8 *values)
{
	union i2c_smbus_data data;
	int i;
	if (length > 32)
		length = 32;
	for (i = 1; i <= length; i++)
		data.block[i] = values[i-1];
	data.block[0] = length;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_BLOCK_DATA, &data);
}

/* Returns the number of read bytes */
/* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you
   ask for less than 32 bytes, your code will only work with kernels
   2.6.23 and later. */
static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
                                                  __u8 length, __u8 *values)
{
	union i2c_smbus_data data;
	int i;

	if (length > 32)
		length = 32;
	data.block[0] = length;
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
	                     length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
	                      I2C_SMBUS_I2C_BLOCK_DATA,&data))
		return -1;
	else {
		for (i = 1; i <= data.block[0]; i++)
			values[i-1] = data.block[i];
		return data.block[0];
	}
}

static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
                                                   __u8 length,
                                                   const __u8 *values)
{
	union i2c_smbus_data data;
	int i;
	if (length > 32)
		length = 32;
	for (i = 1; i <= length; i++)
		data.block[i] = values[i-1];
	data.block[0] = length;
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                        I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
}

/* Returns the number of read bytes */
static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
                                                 __u8 length, __u8 *values)
{
	union i2c_smbus_data data;
	int i;
	if (length > 32)
		length = 32;
	for (i = 1; i <= length; i++)
		data.block[i] = values[i-1];
	data.block[0] = length;
	if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
	                     I2C_SMBUS_BLOCK_PROC_CALL,&data))
		return -1;
	else {
		for (i = 1; i <= data.block[0]; i++)
			values[i-1] = data.block[i];
		return data.block[0];
	}
}


#endif /* _LINUX_I2C_DEV_H */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include "i2c-dev.h"


#define u8 unsigned char
#define u32 unsigned int

#define I2C_DEV "/dev/i2c-1"
#define I2C_ADDR 0x38

#define    FTS_PACKET_LENGTH        128

#define FTS_UPGRADE_LOOP	3

#define FT_UPGRADE_AA	0xAA
#define FT_UPGRADE_55 	0x55
#define FT_UPGRADE_EARSE_DELAY		1500
/*upgrade config of FT5x06(x=2,3,4)*/
#define FT5X06_UPGRADE_AA_DELAY 		50
#define FT5X06_UPGRADE_55_DELAY 		30
#define FT5X06_UPGRADE_ID_1			0x79
#define FT5X06_UPGRADE_ID_2			0x03
#define FT5X06_UPGRADE_READID_DELAY 	1


#define FW_FILE_NAME "ft5406_0X13_app.i"

static unsigned char CTPM_FW[]=
{
	#include FW_FILE_NAME
};



/* 基于ft5x06_ts.c */
/*将读写i2c_msg放在一个ioctl函数来完成,导致读失败*/
/*
int ft5x0x_i2c_Read(int fd, int chip_addr, char *writebuf,
		    int writelen, char *readbuf, int readlen)
{
	int ret;
	struct i2c_rdwr_ioctl_data ioctl_data;
	if (writelen > 0) {//一个i2c_msg对应一个s一个p
		struct i2c_msg msgs[] = {
			{
			 .addr = chip_addr,
			 .flags = 0,//写
			 .len = writelen,
			 .buf = writebuf,
			 },
			{
			 .addr = chip_addr,
			 .flags = I2C_M_RD,//读
			 .len = readlen,
			 .buf = readbuf,
			 },
		};

		ioctl_data.nmsgs = 2;
		ioctl_data.msgs  = msgs;
	//	ret = i2c_transfer(client->adapter, msgs, 2);
		ret = ioctl(fd,I2C_RDWR,&ioctl_data);
		if (ret < 0)
			printf("f%s: i2c read error.\n",__func__);
	} else {
		struct i2c_msg msgs[] = {
			{
			 .addr = chip_addr,
			 .flags = I2C_M_RD,
			 .len = readlen,
			 .buf = readbuf,
			 },
		};

		ioctl_data.nmsgs = 1;
		ioctl_data.msgs  = msgs;
		
	//	ret = i2c_transfer(client->adapter, msgs, 1);
		ret = ioctl(fd,I2C_RDWR,&ioctl_data);
		if (ret < 0)
			printf("f%s: i2c read error.\n",__func__);
		}
	return ret;
}

*/

/* 基于ft5x06_ts.c */
/*
*将读写i2c_msg放在一个ioctl函数来完成,导致读失败
*把两个读写i2c_msg分别ioctl 
*/

int ft5x0x_i2c_Read(int fd, int chip_addr, char *writebuf,
		    int writelen, char *readbuf, int readlen)
{
	int ret;
	struct i2c_rdwr_ioctl_data ioctl_data;
	if (writelen > 0) {//一个i2c_msg对应一个s一个p
		struct i2c_msg msgs[] = {
			{
			 .addr = chip_addr,
			 .flags = 0,//写
			 .len = writelen,
			 .buf = writebuf,
			 },
			{
			 .addr = chip_addr,
			 .flags = I2C_M_RD,//读
			 .len = readlen,
			 .buf = readbuf,
			 },
		};

		ioctl_data.nmsgs = 1;
		ioctl_data.msgs  = &msgs[0];
	//	ret = i2c_transfer(client->adapter, msgs, 2);
		ret = ioctl(fd,I2C_RDWR,&ioctl_data);
		if (ret < 0)
			printf("f%s: i2c read error.\n",__func__);

		ioctl_data.nmsgs = 1;
		ioctl_data.msgs  = &msgs[1];
	//	ret = i2c_transfer(client->adapter, msgs, 2);
		ret = ioctl(fd,I2C_RDWR,&ioctl_data);
		if (ret < 0)
			printf("f%s: i2c read error.\n",__func__);
	} else {
		struct i2c_msg msgs[] = {
			{
			 .addr = chip_addr,
			 .flags = I2C_M_RD,
			 .len = readlen,
			 .buf = readbuf,
			 },
		};

		ioctl_data.nmsgs = 1;
		ioctl_data.msgs  = msgs;
		
	//	ret = i2c_transfer(client->adapter, msgs, 1);
		ret = ioctl(fd,I2C_RDWR,&ioctl_data);
		if (ret < 0)
			printf("f%s: i2c read error.\n",__func__);
		}
	return ret;
}


int ft5x0x_i2c_Write(int fd, int chip_addr, char *writebuf, int writelen)
{
	int ret;

	struct i2c_rdwr_ioctl_data ioctl_data;
	struct i2c_msg msg[] = {
		{
		 .addr = chip_addr,
		 .flags = 0,
		 .len = writelen,
		 .buf = writebuf,
		 },
	};

	ioctl_data.nmsgs = 1;
	ioctl_data.msgs  = msg;

//	ret = i2c_transfer(client->adapter, msgs, 1);
	ret = ioctl(fd,I2C_RDWR,&ioctl_data);

	if (ret < 0)
		printf("f%s: i2c write error.\n",__func__);

	return ret;
}

int ft5x0x_read_reg(int fd,int chip_addr, u8 regaddr, u8 * regvalue)
{
	return ft5x0x_i2c_Read(fd, chip_addr, &regaddr, 1, regvalue, 1);
}

int ft5x0x_write_reg(int fd,int chip_addr, u8 regaddr, u8 regvalue)
{
	unsigned char buf[2] = {0};
	buf[0] = regaddr;
	buf[1] = regvalue;
	
	return ft5x0x_i2c_Write(fd,chip_addr, buf, sizeof(buf));
}


/*
可能是由于FT5x06芯片的电器特性,需要delay一段时间
所以读FT5x06芯片不能使用直接i2c_smbus_read_byte_data函数
而是分别使用i2c_smbus_write_byte和i2c_smbus_read_byte
*/

/*
int ft5x06_read_byte(int fd, __u8 addr)
{
	
	if(i2c_smbus_write_byte(fd, addr) <0 )
	{
		printf("i2c_smbus_write_byte error\n");
		return -1;
	}
	usleep(10); //延时
	
	return i2c_smbus_read_byte(fd);
}


int ft5x06_read_bytes(int fd, __u8 addr , int size , char * buf)
{

	int i;
	char c;
	for(i=0;i < size; ++i, ++addr)
	{
		if((c = ft5x06_read_byte( fd, addr)) < 0 )
		{
			
			return -1;
		} else 
			buf[i] = c;
	}
	
	return 0;
}

int ft5x06_write_bytes(int fd, __u8 addr , int size , const char * buf)
{
	int i;
	for(i=0;i<size;i++,addr ++)
	{
		if(i2c_smbus_write_byte_data(fd, addr, buf[i]) <0 )
		{	
			printf("i2c_smbus_write_byte_data error\n");
			return -1;
		}
	}

	return 0;
}
*/

int  fts_ctpm_fw_upgrade(int fd,int chip_addr, u8* pbt_buf, u32 dw_lenth)
{
	
	
}


/*
upgrade with *.i file
*/
int fts_ctpm_fw_upgrade_with_i_file(int fd,int chip_addr)
{

}


static u8 fts_ctpm_get_i_file_ver(void)
{
    unsigned short ui_sz;
    ui_sz = sizeof(CTPM_FW);
    if (ui_sz > 2)
    {
        return CTPM_FW[ui_sz - 2];
    }
    else
    {
        return 0x00; /*default value*/
    }
}

int main()
{
	int fd;
	unsigned char ver = 0;
	unsigned char fw_file_ver = 0;
	
	fd = open(I2C_DEV,O_RDWR);
	if(fd == -1)
	{
		printf("open error\n");
		return -1;
	}

	if(ioctl(fd,I2C_SLAVE,I2C_ADDR) < 0)
	{
		printf("set addr error\n");
		close(fd);
		return -1;
	}

	fw_file_ver = fts_ctpm_get_i_file_ver();
	printf("[FTS] %s Firmware version = 0x%x\n",FW_FILE_NAME, fw_file_ver);

	if(fw_file_ver == 0)
	{
		printf("Get Firmware version from File %s Error\n",FW_FILE_NAME);
		goto exit;
	}

	ft5x0x_read_reg(fd, I2C_ADDR, 0xa6, &ver);
	printf("[FTS] Firmware version Before Upgrade = 0x%x\n",ver);

	
	/*
	if(ver == fw_file_ver)
	{
		printf("[FTS] Upgrade is not necessary\n");
		goto exit;
	}
	*/

	if(ver != 0x11)
	{
		printf("[FTS] Upgrade is not necessary\n");
		printf("[FTS] Upgrade program is only for 0x11 to 0x13\n");
		goto exit;
	}
	
	//更新
	fts_ctpm_fw_upgrade_with_i_file(fd,I2C_ADDR);

	ft5x0x_read_reg(fd, I2C_ADDR, 0xa6, &ver);
	printf("[FTS] Firmware version After Upgrade = 0x%x\n",ver);


	if(ver == fw_file_ver)
	{
		printf("[FTS] Firmware Update Success!\n");
	} else {
		printf("[FTS] Firmware Update Fail!\n");
	}

exit:	
	close(fd);
	return 0;
}






`i2c-dev.c` 和你自己实现I2C 内核驱动(例如针对某个具体 I2C 设备如温度传感器、EEPROM 等编写的驱动)在 Linux 内核中的角色和用途完全不同,主要区别如下: --- ### 1. **角色与功能不同** | 项目 | `i2c-dev.c` | 自己实现I2C 驱动 | |------|-------------|------------------------| | **类型** | 核心框架提供的标准模块 | 用户自定义设备驱动 | | **作用** | 提供用户空间访问 I2C 总线的能力(通过 `/dev/i2c-X`) | 实现对特定 I2C 设备的控制、数据采集、状态管理等 | | **接口面向** | 用户空间程序(如 `i2cget`, `i2cdetect`) | 内核空间,可能导出接口给其他模块或创建 sysfs 节点 | | **是否绑定具体设备** | 否,它操作的是 I2C 总线本身 | 是,通常绑定到具体的 I2C 设备(通过设备树或板级信息匹配) | --- ### 2. **工作层次不同** - `i2c-dev.c` 属于 **I2C 核心层** 的一部分,位于 `drivers/i2c/i2c-dev.c`。 - 它注册了一个字符设备驱动,为每个 I2C 适配器(如 `i2c-0`, `i2c-1`)创建一个 `/dev/i2c-X` 设备节点。 - 使用 `ioctl` 系统调用让用户空间可以发送读写命令到指定地址的 I2C 从设备。 ```c // 用户空间示例:打开并访问 I2C 总线 int fd = open("/dev/i2c-1", O_RDWR); ioctl(fd, I2C_SLAVE, 0x50); // 设置目标设备地址 write(fd, &reg, 1); // 写寄存器 read(fd, &value, 1); // 读数据 ``` - 自己实现I2C 驱动属于 **设备驱动层**,通常位于 `drivers/i2c/busses/` 或 `drivers/misc/`、`drivers/hwmon/` 等目录下。 - 它会通过 `i2c_client` 和 `i2c_driver` 结构体注册,并与设备树中定义的设备匹配。 - 可以使用 `i2c_transfer()`、`i2c_smbus_read_byte_data()` 等内核 API 与设备通信。 - 常用于构建更高级的功能,比如自动轮询传感器、处理中断、上报 input 事件或 hwmon 数据。 ```c static int my_sensor_probe(struct i2c_client *client, const struct i2c_device_id *id) { dev_info(&client->dev, "My sensor detected at 0x%02x\n", client->addr); // 初始化设备、注册中断、创建 sysfs 属性等 return 0; } ``` --- ### 3. **加载方式与依赖关系** | 方面 | `i2c-dev.c` | 自定义 I2C 驱动 | |------|------------|------------------| | 加载时机 | 通常由内核自动加载(可配置为模块或内置) | 需要显式注册,依赖设备树节点或平台数据 | | 是否需要设备树节点 | 不需要(只要存在 i2c adapter 即可用) | 一般需要设备树中声明设备节点(如 `at24@50`) | | 是否独占设备 | 否(多个进程可通过 `/dev/i2c-X` 访问同一总线) | 是,一旦驱动加载并探测成功,通常禁止其他驱动再访问该设备 | > ⚠️ 注意:如果某个 I2C 设备已被内核驱动占用(如 `at24` EEPROM 驱动),则不能再通过 `/dev/i2c-X` 直接访问它,除非禁用该驱动。 --- ### 4. **应用场景对比** | 场景 | 推荐方式 | |------|----------| | 调试 I2C 设备是否存在、简单读写寄存器 | 使用 `i2c-dev.c` + `i2c-tools` | | 开发嵌入式产品,需稳定采集温湿度、上报事件 | 编写专用 I2C 驱动 | | 需要后台守护、低功耗管理、中断响应 | 必须编写内核驱动 | | 快速原型验证或脚本控制 | 可使用 `/dev/i2c-X` 从用户空间操作 | --- ### 小结 - `i2c-dev.c` 是“通用通道”:让你从用户空间直接操控 I2C 总线上的任意设备。 - 自定义 I2C 驱动是“专用服务”:为特定硬件提供完整、可靠、集成化的支持。 你可以同时使用两者 —— 比如先用 `i2c-dev` 测试设备通信是否正常,再开发正式驱动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值