Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
int main() {
...
int file = open(device, O_RDWR);
if (file < 0) {
perror("Failed to open I2C bus");
return 1;
}
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to set I2C address");
close(file);
return 1;
}
if (write(file, ®, 1) != 1) {
perror("Failed to write register address");
close(file);
return 1;
}
if (read(file, &data, 1) != 1) {
perror("Failed to read from register");
close(file);
return 1;
}
close(file);
return 0;
}
Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
int main() {
...
int file = open(device, O_RDWR);
if (file < 0) {
perror("Failed to open I2C device");
return 1;
}
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];
messages[0].addr = addr;
messages[0].flags = 0;
messages[0].len = 1;
messages[0].buf = ®
messages[1].addr = addr;
messages[1].flags = I2C_M_RD;
messages[1].len = 1;
messages[1].buf = &data;
packets.msgs = messages;
packets.nmsgs = 2;
if (ioctl(file, I2C_RDWR, &packets) < 0) {
perror("I2C_RDWR ioctl failed");
close(file);
return 1;
}
close(file);
return 0;
}
Код: Выделить всё
START -> [ADDR+W] -> [REG] -> STOP
и read () syscall Эта транзакция
Код: Выделить всё
START -> [ADDR+R] -> [DATA] -> STOP
Это означает, что есть условие остановки до следующего начала. Глядя на таблицу устройства, которое он использует, устройство также ожидает повторного начального условия. Итак, я не уверен, есть ли моя интуиция прямо здесь, или это может быть проблемой времени, или эти два метода могут иметь некоторые важные различия?
Подробнее здесь: https://stackoverflow.com/questions/796 ... -returns-0