linux上层应用程序,linux 上层应用 i2c读写demo程序

#include #define I2C_FILE_NAME "/dev/i2c-2"

#define USAGE_MESSAGE \

"Usage:\n" \

" %s i2c_ch r [addr] [register] " \

"to read value from [register]\n" \

" %s i2c_ch w [addr] [register] [value] " \

"to write a value [value] to register [register]\n" \

""

static int set_i2c_register(int file,

unsigned char addr,

unsigned char reg,

unsigned char value) {

unsigned char outbuf[2];

struct i2c_rdwr_ioctl_data packets;

struct i2c_msg messages[1];

messages[0].addr = addr;

messages[0].flags = 0;

messages[0].len = sizeof(outbuf);

messages[0].buf = outbuf;

/* The first byte indicates which register we'll write */

outbuf[0] = reg;

/*

* The second byte indicates the value to write. Note that for many

* devices, we can write multiple, sequential registers at once by

* simply making outbuf bigger.

*/

outbuf[1] = value;

/* Transfer the i2c packets to the kernel and verify it worked */

packets.msgs = messages;

packets.nmsgs = 1;

if(ioctl(file, I2C_RDWR, &packets) < 0) {

perror("Unable to send data");

return 1;

}

return 0;

}

static int get_i2c_register(int file,

unsigned char addr,

unsigned char reg,

unsigned char *val) {

unsigned char inbuf, outbuf;

struct i2c_rdwr_ioctl_data packets;

struct i2c_msg messages[2];

/*

* In order to read a register, we first do a "dummy write" by writing

* 0 bytes to the register we want to read from. This is similar to

* the packet in set_i2c_register, except it's 1 byte rather than 2.

*/

outbuf = reg;

messages[0].addr = addr;

messages[0].flags = 0;

messages[0].len = sizeof(outbuf);

messages[0].buf = &outbuf;

/* The data will get returned in this structure */

messages[1].addr = addr;

messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;

messages[1].len = sizeof(inbuf);

messages[1].buf = &inbuf;

/* Send the request to the kernel and get the result back */

packets.msgs = messages;

packets.nmsgs = 2;

if(ioctl(file, I2C_RDWR, &packets) < 0) {

perror("Unable to send data");

return 1;

}

*val = inbuf;

return 0;

}

int main(int argc, char **argv)

{

int i2c_file;

char i2cDevName[15] = {0};

if(argc > 4 && !strcmp(argv[2], "r")) {

int i2c_ch = strtol(argv[1], NULL, 0);

if( i2c_ch > 10 || i2c_ch < 0 ){

perror("input argv[1] i2c ch error\n");

exit(1);

}

sprintf(i2cDevName,"/dev/i2c-%d",i2c_ch);

// Open a connection to the I2C userspace control file.

if ((i2c_file = open(i2cDevName, O_RDWR)) < 0) {

perror("Unable to open i2c control file");

exit(1);

}

int addr = strtol(argv[3], NULL, 0);

int reg = strtol(argv[4], NULL, 0);

unsigned char value;

if(get_i2c_register(i2c_file, addr, reg, &value)) {

printf("Unable to get register!\n");

}

else {

printf("Register %d: %d (%x)\n", reg, (int)value, (int)value);

}

}

else if(argc > 5 && !strcmp(argv[2], "w")) {

int i2c_ch = strtol(argv[1], NULL, 0);

if( i2c_ch > 10 || i2c_ch < 0 ){

perror("input argv[1] i2c ch error\n");

exit(1);

}

sprintf(i2cDevName,"/dev/i2c-%d",i2c_ch);

// Open a connection to the I2C userspace control file.

if ((i2c_file = open(i2cDevName, O_RDWR)) < 0) {

perror("Unable to open i2c control file");

exit(1);

}

int addr = strtol(argv[3], NULL, 0);

int reg = strtol(argv[4], NULL, 0);

int value = strtol(argv[5], NULL, 0);

if(set_i2c_register(i2c_file, addr, reg, value)) {

printf("Unable to get register!\n");

}

else {

printf("Set register %x: %d (%x)\n", reg, value, value);

}

}

else {

fprintf(stderr, USAGE_MESSAGE, argv[0], argv[0]);

}

close(i2c_file);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值