使用Linux上的I2C读/写 [英] Reading / writing from using I2C on Linux
问题描述
我试图读取/写入到连接在一个I2C总线上地址 FM24CL64-GTR FRAM
片 0B 1010 011
。
I'm trying to read/write to a FM24CL64-GTR FRAM
chip that is connected over a I2C bus on address 0b 1010 011
.
当我试着写3个字节(数据地址2个字节,+数据一个字节),我得到一个内核消息( [12406.360000] I2C适配器I2C-0:sendbytes:NAK救助
),以及写收益= 3,见code如下:
When I'm trying to write 3 bytes (data address 2 bytes, + data one byte), I get a kernel message ([12406.360000] i2c-adapter i2c-0: sendbytes: NAK bailout.
), as well as the write returns != 3. See code below:
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
int file;
char filename[20];
int addr = 0x53; // 0b1010011; /* The I2C address */
uint16_t dataAddr = 0x1234;
uint8_t val = 0x5c;
uint8_t buf[3];
sprintf(filename,"/dev/i2c-%d",0);
if ((file = open(filename,O_RDWR)) < 0)
exit(1);
if (ioctl(file,I2C_SLAVE,addr) < 0)
exit(2);
buf[0] = dataAddr >> 8;
buf[1] = dataAddr & 0xff;
buf[2] = val;
if (write(file, buf, 3) != 3)
exit(3);
...
然而,当我写2个字节,然后写另一个字节,我没有得到任何内核错误,而是试图从FRAM读的时候,我总是回来0.下面是code从FRAM阅读:
However when I write 2 bytes, then write another byte, I get no kernel error, but when trying to read from the FRAM, I always get back 0. Here is the code to read from the FRAM:
uint8_t val;
if ((file = open(filename,O_RDWR)) < 0)
exit(1);
if (ioctl(file,I2C_SLAVE,addr) < 0)
exit(2);
if (write(file, &dataAddr, 2) != 2) {
exit(3);
if (read(file, &val, 1) != 1) {
exit(3);
中的所有功能都返回错误值,而我也试了一下:
None of the functions return an error value, and I have also tried it with:
#include <linux/i2c.h>
struct i2c_rdwr_ioctl_data work_queue;
struct i2c_msg msg[2];
uint8_t ret;
work_queue.nmsgs = 2;
work_queue.msgs = msg;
work_queue.msgs[0].addr = addr;
work_queue.msgs[0].len = 2;
work_queue.msgs[0].flags = 0;
work_queue.msgs[0].buf = &dataAddr;
work_queue.msgs[1].addr = addr;
work_queue.msgs[1].len = 1;
work_queue.msgs[1].flags = I2C_M_RD;
work_queue.msgs[1].buf = &ret;
if (ioctl(file,I2C_RDWR,&work_queue) < 0)
exit(3);
这也是成功,但总是返回0。这是否表明一个硬件问题,还是我做错了什么?
Which also succeeds, but always returns 0. Does this indicate a hardware issue, or am I doing something wrong?
是否有FM24CL64-GTR任何FRAM驱动程序通过I2C在Linux上,并且将这个API是什么?任何一个环节将是有益的。
Are there any FRAM drivers for FM24CL64-GTR over I2C on Linux, and what would the API be? Any link would be helpful.
推荐答案
在NAK是一个很大的提示:WriteProtect引脚被外部上拉,不得不被驱赶到地面后,该地址进行一次写其次是数据字节是成功的(第一个code段)。
The NAK was a big hint: the WriteProtect pin was externally pulled up, and had to be driven to ground, after that a single write of the address followed by data-bytes is successful (first code segment).
有关读出可以写出的地址的第一(使用写入()),然后顺序数据可以被读取从该地址开始
For reading the address can be written out first (using write()), and then sequential data can be read starting from that address.
这篇关于使用Linux上的I2C读/写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!