首页 > 系统相关 >Linux环境下I2C应用程序编写

Linux环境下I2C应用程序编写

时间:2023-06-19 22:44:05浏览次数:49  
标签:I2C msgs 应用程序 queue include fd Linux i2c buf

原文:https://blog.csdn.net/propor/article/details/129667596

本文介绍Linux环境下,对I2C设备进行操作。

在对I2C总线进行操作时,可采用i2c-tools对I2C进行查看及操作,待通过工具可对I2C进行操作后,再编写程序进行操作。

1.i2c-tools的使用

1)安装

sudo apt install i2c-tools
2)查询

i2cdetect -l
执行结果如下,可以看到所有的i2c总线。

 

单独查看第1组I2C总线下挂接的设备,可采用:

i2cdetect -y 1
执行结果如下,可以看到i2c-1总线下的设备,如0x36。

 

3)写数据

i2ctransfer -f -y 1 w3@0x36 0x01 0x00 0x01
向0x0100写入0x01,这里总计写入了3个字节的数据(w3)。

4)读数据

i2ctransfer -f -y 1 w2@0x36 0x01 0x00 r1
读取0x0100处数据,按I2C协议,先写入寄存器地址(w2),再进行读取(r1)。

2.用户空间下的读写操作

1)read,write方式

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

int main(int argc, char **argv)
{
unsigned int fd;
unsigned char buf[16];

fd = open("/dev/i2c-1", O_RDWR);
if (!fd) {
printf(“open device failed!\n”);
return -1;
}

ioctl(fd, I2C_SLAVE, 0x36); //slave address
ioctl(fd, I2C_TIMEOUT, 1); //timeout
ioctl(fd, I2C_RETRIES, 1); //try count

//write data to i2c-1
buf[0] = 0x01;
buf[1] = 0x00;
buf[2] = 0x01;
write(fd, &buf[0], 3);

//read data from i2c-1
buf[0] = 0x01;
buf[1] = 0x00;
write(fd, &buf[0], 2); //write register address first
buf[2] = 0x00;
read(fd, &buf[2], 1); //read data

printf("%d\n", buf[2]);

close(fd);

return 0;
}

2)ioctl方式

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <linux/types.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

int main(int argc, char **argv)
{
struct i2c_rdwr_ioctl_data i2c_queue;
unsigned int fd;
unsigned char buf[3];
int ret;

fd = open("/dev/i2c-1", O_RDWR);
if (!fd) {
printf(“open device failed!\n”);
return -1;
}

i2c_queue.nmsgs = 2;
i2c_queue.msgs = (struct i2c_msgs *)malloc(i2c_queue.nmsgs * sizeof(struct i2c_msg));
if (!i2c_queue.msgs) {
printf(“memory alloc error!\n”);
close(fd);
return 1;
}

ioctl(fd, I2C_TIMEOUT, 2); //timeout
ioctl(fd, I2C_RETRIES, 1); //try count

//write data to i2c-1
i2c_queue.nmsgs = 1;
(i2c_queue.msgs[0]).addr = 0x36;
(work_qurue.msgs[0]).len = 3;
(i2c_queue.msgs[1]).flags = 0;
(i2c_queue.msgs[0]).buf = buf; //(unsigned char *)malloc(3)
(i2c_queue.msgs[0]).buf[0] = 0x01;
(i2c_queue.msgs[0]).buf[1] = 0x00;
(i2c_queue.msgs[0]).buf[2] = 0x01;
ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_queue);
if (ret < 0)
printf(“i2c write failed!\n”);

//read data from i2c-1
i2c_queue.nmsgs = 2;
(i2c_queue.msgs[0]).addr = 0x36;
(work_qurue.msgs[0]).len = 2;
(i2c_queue.msgs[0]).flags = 0;
(i2c_queue.msgs[0]).buf = buf;
(i2c_queue.msgs[0]).buf[0] = 0x01;
(i2c_queue.msgs[0]).buf[1] = 0x00;

(i2c_queue.msgs[1]).addr = 0x36;
(work_qurue.msgs[1]).len = 1;
(i2c_queue.msgs[1]).flags = I2C_M_RD;
(i2c_queue.msgs[1]).buf = &buf[2]; //(unsigned char *)malloc(1)
(i2c_queue.msgs[1]).buf[0] = 0;
ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_queue);

if (ret < 0)
printf(“i2c read failed!\n”);
else
printf(“%02x\n”, buf[2]);

close(fd);

return 0;
}
————————————————
版权声明:本文为CSDN博主「propor」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/propor/article/details/129667596

标签:I2C,msgs,应用程序,queue,include,fd,Linux,i2c,buf
From: https://www.cnblogs.com/bruce1992/p/17492436.html

相关文章

  • 《Linux命令行与shell脚本编程大全》研读笔记
    目录命令总结进程相关环境变量相关第六章Linux环境变量命令总结进程相关命令名称作用举例psProcessStatus,用来列出系统中当前运行的那些进程ps-ftop动态地显示进程信息top环境变量相关命令名称作用举例env列出全局环境变量env......
  • linux中进入python交互解释器Tab补全功能
    进入python交互解释器后,按tab键默认是缩进功能,而不是代码补全。为了实现代码补全,可以采用如下操作:1、创建指令补全文件[root@room8pc16 ~]# vim /usr/local/bin/tab.pyfrom rlcompleter import readlinereadline.parse_and_bind('tab: complete')2、配置环境变量,在~/.bashrc......
  • Linux关闭防火墙命令
    1、防火墙的开启、关闭、禁用命令设置开机启用防火墙:systemctlenablefirewalld.service设置开机禁用防火墙:systemctldisablefirewalld.service启动防火墙:systemctlstartfirewalld关闭防火墙:systemctlstopfirewalld检查防火墙状态:systemctlstatusfirewalld2、使用fi......
  • Linux 操作系统及应用考试题
    Linux操作系统及应用考试题要求:操作题截屏到WORD文档,文件名字为学号+姓名。每道题如能显示,应显示你的用户名,必要的步骤需截图,图片不要太大。考试操作题(100分)。......
  • linux 中安装最新版的cmake命令
     001、下载安装包官网:https://cmake.org/ 002、解压安装包tar-xzvfcmake-3.27.0-rc2-linux-x86_64.tar.gzcdcmake-3.27.0-rc2-linux-x86_64/binls 003、测试命令及版本(base)[root@PC1bin]#./cmake--versioncmakeversion3.27.0-rc2CMakesuitemaint......
  • python3 subprocess.getoutput(cmd) 执行linux命令进入交互模式后一直卡住了
    进入交互模式是我们预期之外的,记录一下。进入交互之后linux一直等待你的输入,所有subprocess.getoutput()就一直卡着呢~,我们加入timeout通过学习subprocess中支持timeout有:getoutput并不支持timeout参数尝试了callcheck_allcheck_output这几个方法之后并不能解决Linux交......
  • Linux初始化
    Linux初始化环境说明:VMware®Workstation16Procentos7.6时区设置查看时区timedatectl设置时区timedatectlset-timezoneAsia/Shanghai时间对时使用chrony网络对时安装chronyyum-yinstallchrony配置chrony使用阿里云服务器自动对时删除以下国外的服务器s......
  • linux C语言 使用socket获取本机所有IP地址
    #include<stdio.h>#include<sys/ioctl.h>#include<net/if.h>#include<arpa/inet.h>/******************************************************函数功能:获取本机所有ip地址。*输入参数:*max_ip_num:ip_buf能存的最多ip个数;*输出参数:*ip_b......
  • Linux 常用命令
    一、Linux命令格式command[-options][parameter]说明:command:命令名[-options]:选项,可用来对命令进行控制,也可以省略[parameter]:传给命令的参数,可以是零个、一个或者多个注意:[]代表可选命令名、选项、参数之间有空......
  • 2023年十大最受欢迎的Flutter开源应用程序
    原文出处:https://juejin.cn/post/7245170503798538296在移动应用开发领域,Flutter以其跨平台能力和漂亮的用户界面获得了巨大的人气。随着其开发者社区的不断壮大,Flutter生态系统已经见证了众多开源应用程序的诞生。这些开源应用不仅展示了Flutter的多功能性,而且还为开发者提供......