简体   繁体   English

I2C用户空间读/写问题

[英]I2C user-space read/write issues

I'm writing a user-space program to read and write to/from an EEPROM using open(), ioctl(), read(), and write(), but it doesn't seem to be working the way I expect. 我正在编写一个用户空间程序,使用open(),ioctl(),read()和write()来读取和写入EEPROM,但它似乎没有像我期望的那样工作。

I guess first of all I have to ask, is the entire I2C protocol for read and write handled by calls to read() and write() with an I2C file descriptor? 我想首先我要问的是,读取和写入的整个I2C协议是通过使用I2C文件描述符调用read()和write()来处理的吗? According to the link here , the entire transaction is handled by read() and write(). 根据此处的链接 ,整个事务由read()和write()处理。

If that is so, then how do read() and write() know which register address to read from? 如果是这样,那么read()和write()如何知道从哪个寄存器地址读取? Most places I've read say to use the first byte of the data buffer passed to read() and write() to store the address of the register to read from or write to. 我读过的大多数地方都说使用传递给read()和write()的数据缓冲区的第一个字节来存储要读取或写入的寄存器的地址。 But then what if the device I am communicating with uses 16-bit register addresses and register data? 但是如果我正在与之通信的设备使用16位寄存器地址并注册数据呢? How do read() and write() know if the address is 8 or 16 bits long? read()和write()如何知道地址是8位还是16位?

Thanks and I can provide some code snippets if needed. 谢谢,如果需要,我可以提供一些代码片段。

I think the underlying protocol is implemented with three system calls: read , write , and ioctl , as described in the kernel I²C documentation . 我认为底层协议是通过三个系统调用实现的: readwriteioctl ,如内核I²C文档中所述 However, I recommend using libi2c so you don't have to worry about those details. 但是,我建议使用libi2c这样您就不必担心这些细节了。 I have written about how to use that library here: 我在这里写了关于如何使用该库的文章:

http://blog.davidegrayson.com/2012/06/using-ic-on-raspberry-pi.html http://blog.davidegrayson.com/2012/06/using-ic-on-raspberry-pi.html

I successfully wrapped it in a C++ class, which you can see here: 我成功地将它包装在C ++类中,您可以在此处看到:

https://github.com/DavidEGrayson/minimu9-ahrs/blob/master/I2CBus.cpp https://github.com/DavidEGrayson/minimu9-ahrs/blob/master/I2CBus.cpp

Also, in I²C, device addresses are 7-bit. 此外,在I²C中,器件地址为7位。 If your device implements some kind of 16-bit address, that would probably be implemented inside data transfers to the 7-bit address of a device. 如果您的设备实现某种16位地址,则可能会在数据传输到设备的7位地址内部实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM