Linux equivalents for Arduino I²C libraries (Wire)?
I am trying to port an Arduino program to Linux. I'm stuck because I can't seem to find equivalents to the I²C functions that the Arduino has in "Wire.h".
Wire header: Wire Library
Linux i2C-dev.h: Using I²C from userspace in Linux
Specific开发者_Python百科ally, I can't see how I can do a
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 does not appear to have the equivalent of requesting a specific number of bytes from a I²C device. I imagine that "i2c_smbus_read_byte" is the equivalent of receive, and that it would ascend the available bytes if called in succession.
I²C options in Linux:
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)
I think you're probably looking for i2c_smbus_read_block_data
which takes in a file descriptor, the command to be issued, and a block of bytes to be read from the device.
The code for using it would probably look something like this:
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
Here's a link for the function description: http://ww2.cs.fsu.edu/~rosentha/linux/2.6.26.5/docs/DocBook/kernel-api/re1222.html
精彩评论