Linux等价于ArduinoI²C库(Wire)?

时间:2011-02-17 13:03:31

标签: linux port arduino i2c

我正在尝试将Arduino程序移植到Linux。我被卡住了,因为我似乎无法找到Arduino在“Wire.h”中的I²C函数的等价物。

电汇标头: Wire Library

Linux i2C-dev.h: Using I²C from userspace in Linux

具体来说,我看不出怎么做

Wire.request(address, num_of_bytes); //Request 4 bytes
int a = Wire.receive(); //Receive the four bytes
int b = Wire.receive();
int c = Wire.receive();
int d = Wire.receive();

Linux似乎没有相当于从I²C设备请求特定数量的字节。我想“i2c_smbus_read_byte”相当于receive,并且如果连续调用它会提升可用字节。

Linux中的I²C选项:

i2c_smbus_write_quick( int file, __u8 value)
i2c_smbus_read_byte(int file)
i2c_smbus_write_byte(int file, __u8 value)
i2c_smbus_read_byte_data(int file, __u8 command)
i2c_smbus_write_byte_data(int file, __u8 command, __u8 value)
i2c_smbus_read_word_data(int file, __u8 command)
i2c_smbus_write_word_data(int file, __u8 command, __u16 value)
i2c_smbus_process_call(int file, __u8 command, __u16 value)
i2c_smbus_read_block_data(int file, __u8 command, __u8 *values)
i2c_smbus_write_block_data(int file, __u8 command, __u8 length, __u8 *values)
i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 *values)
i2c_smbus_write_i2c_block_data(int file, __u8 command, __u8 length, __u8 *values)
i2c_smbus_block_process_call(int file, __u8 command, __u8 length, __u8 *values)

1 个答案:

答案 0 :(得分:0)

我认为您可能正在寻找i2c_smbus_read_block_data,它接收文件描述符,要发出的命令以及要从设备读取的字节块。

使用它的代码可能如下所示:

int retval;
uint8_t *block;

block = malloc(32); //i2c_smbus_read_block_data() can return up to 32 bytes
retval = i2c_smbus_read_block_data(fd, req, block);

// check retval.  retval returns bytes read on success, or <0 on error

以下是功能说明的链接:http://ww2.cs.fsu.edu/~rosentha/linux/2.6.26.5/docs/DocBook/kernel-api/re1222.html