您的位置 首页 软件

用户态应用程序直接拜访I2C驱动

includestdiohincludestdlibhincludestringhincludesystypeshincludesysstathincludefcntlhinclud

#include “stdio.h”

#include “stdlib.h”
#include “string.h”
#include “sys/types.h”
#include “sys/stat.h”
#include “fcntl.h”
#include “i2c-dev.h” //调用来自“i2c-tools-3.1.0.tar.bz2”,包括操作I2C函数的声明
// i2c_usr_test r addr
* i2c_usr_test w addr val
//
void print_usage(char *file)
{
printf(“%s r addr\n”, file);
printf(“%s w addr val\n”, file);
}
int main(int argc, char **argv)
{
int fd;
unsigned char addr, data;
int dev_addr;
if ((argc != 5) && (argc != 6))
{
print_usage(argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR);
if (fd < 0)
{
printf(“cant open %s\n”, argv[1]);
return -1;
}
dev_addr = strtoul(argv[2], NULL, 0);
if (ioctl(fd, I2C_SLAVE, dev_addr) < 0)
{
// ERROR HANDLING; you can check errno to see what went wrong //
printf(“set addr error!\n”);
return -1;
}
if (strcmp(argv[3], “r”) == 0)
{
addr = strtoul(argv[4], NULL, 0);
data = i2c_smbus_read_word_data(fd, addr);
printf(“data: %c, %d, 0x%2x\n”, data, data, data);
}
else if ((strcmp(argv[3], “w”) == 0) && (argc == 6))
{
addr = strtoul(argv[4], NULL, 0);
data = strtoul(argv[5], NULL, 0);
i2c_smbus_write_byte_data(fd, addr, data);
}
else
{
print_usage(argv[0]);
return -1;
}
return 0;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/ruanjian/263386.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部