Linux IIC读写

Linux IIC读写


#include <stdio.h>
#include <linux/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_RDWR 0x0707 
/*********参数定义与内核一致,路径include/linux/i2c-dev.h*******/
 
struct i2c_msg
{
	unsigned short addr;
	nsigned short flags;
	unsigned short len;
	unsigned char *buf;
};
 
struct i2c_rdwr_ioctl_data
{
	struct i2c_msg *msgs;
	int nmsgs; 
        /* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/
};
 
int main()
{
	int fd,ret;
	struct i2c_rdwr_ioctl_data e2prom_data;
	fd=open("/dev/i2c-0",O_RDWR);
        /*
        */dev/i2c-0是在注册i2c-dev.c后产生的,代表一个可操作的适配器。如果不使用i2c-dev.c
        *的方式,就没有,也不需要这个节点。
        */
	if(fd<0)
	{
		perror("open error");
	}
	e2prom_data.nmsgs=2; 
        /*
        *因为操作时序中,最多是用到2个开始信号(字节读操作中),所以此将
        *e2prom_data.nmsgs配置为2
        */
	e2prom_data.msgs=(struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));
	if(!e2prom_data.msgs)
	{
		perror("malloc error");
		exit(1);
	}
	ioctl(fd,I2C_TIMEOUT,1);/*超时时间*/
	ioctl(fd,I2C_RETRIES,2);/*重复次数*/
 
	/***write data to e2prom**/
	e2prom_data.nmsgs=1;
	(e2prom_data.msgs[0]).len=2; //1个 e2prom 写入目标的地址和1个数据 
	(e2prom_data.msgs[0]).addr=0x50;//e2prom 设备地址
	(e2prom_data.msgs[0]).flags=0; //write
	(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(2);
	(e2prom_data.msgs[0]).buf[0]=0x10;// e2prom 写入目标的地址
	(e2prom_data.msgs[0]).buf[1]=0x58;//the data to write 
 
        ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
	if(ret<0)
	{
		perror("ioctl error1");
	}
	sleep(1);
 
        /******read data from e2prom*******/
	e2prom_data.nmsgs=2;
	(e2prom_data.msgs[0]).len=1; //e2prom 目标数据的地址
	(e2prom_data.msgs[0]).addr=0x50; // e2prom 设备地址
	(e2prom_data.msgs[0]).flags=0;//write
	(e2prom_data.msgs[0]).buf[0]=0x10;//e2prom数据地址
	(e2prom_data.msgs[1]).len=1;//读出的数据
	(e2prom_data.msgs[1]).addr=0x50;// e2prom 设备地址 
	(e2prom_data.msgs[1]).flags=I2C_M_RD;//read
	(e2prom_data.msgs[1]).buf=(unsigned char*)malloc(1);//存放返回值的地址。
 	(e2prom_data.msgs[1]).buf[0]=0;//初始化读缓冲
 
        ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);
	if(ret<0)
	{
		perror("ioctl error2");
	}
	printf("buff[0]=%x/n",(e2prom_data.msgs[1]).buf[0]);
        /***打印读出的值,没错的话,就应该是前面写的0x58了***/
	close(fd);
	return 0;
}

 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用以下步骤在Linux应用层上进行I2C读写TPA626: 1. 确保您的Linux系统已经加载了I2C设备的驱动程序。您可以使用以下命令来检查: ```shell ls /dev/i2c* ``` 如果您看到/dev/i2c-X(其中X是I2C适配器的编号),则表示驱动程序已加载。 2. 在您的应用程序中,打开I2C适配器。您可以使用open()函数打开适配器设备文件,类似于: ```c #include <fcntl.h> #include <unistd.h> int i2c_fd = open("/dev/i2c-X", O_RDWR); if (i2c_fd < 0) { // 处理打开适配器失败的情况 } ``` 3. 设置从设备的地址。对于TPA626音频放大器,通常使用0x34作为从设备地址。您可以使用ioctl()函数来设置从设备地址,如下所示: ```c #include <linux/i2c-dev.h> int addr = 0x34; if (ioctl(i2c_fd, I2C_SLAVE, addr) < 0) { // 处理设置从设备地址失败的情况 } ``` 4. 发送读写命令和数据。您可以使用write()函数来发送数据,使用read()函数来接收数据。例如,要写入一个寄存器并读取结果,可以执行以下操作: ```c unsigned char reg = 0x01; // 要写入的寄存器地址 unsigned char data = 0x20; // 要写入的数据 if (write(i2c_fd, &reg, sizeof(reg)) != sizeof(reg)) { // 处理写入寄存器地址失败的情况 } if (write(i2c_fd, &data, sizeof(data)) != sizeof(data)) { // 处理写入数据失败的情况 } unsigned char result; if (read(i2c_fd, &result, sizeof(result)) != sizeof(result)) { // 处理读取结果失败的情况 } ``` 请注意,以上代码仅为示例,您需要根据您的实际需求进行修改和适配。 这些是在Linux应用层上进行I2C读写TPA626的基本步骤。希望对您有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值