Arduino I²C 库(Wire)的 Linux 等效项?

发布于 2024-10-17 18:07:28 字数 1527 浏览 6 评论 0原文

我正在尝试将 Arduino 程序移植到 Linux。我陷入困境,因为我似乎找不到 I²C 函数的等效项Arduino 在“Wire.h”中。

连线头:连线库

Linux i2C-dev.h: >在 Linux 中从用户空间使用 I²C

具体来说,我不知道如何才能做到

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”相当于接收,如果连续调用它会增加可用字节。

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)

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

Specifically, 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山有枢 2024-10-24 18:07:28

我认为您可能正在寻找 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文