I'm mainly experienced in C development and I saw in Rust a massive game changer in memory safety & some other things like concurrency, so I started to move to Rust for my developments.
My first program was to develop a device driver using I2C bus, I'm on RPI4, but I'm facing one massive issue, I am not able to write register on the device, but I don't know why.
There is program samples & strace logs:
From my working C program:
#include <stdio.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
int read_status(int fd) {
int rc = 0;
uint8_t buf[3] = {0x6a, 0x0, 0x0};
struct i2c_msg messages[] = {
{.addr = 0x4b, .flags = 0, .buf = buf, .len = 1},
{.addr = 0x4b, .flags = I2C_M_RD, .buf = buf, .len = 3},
};
struct i2c_rdwr_ioctl_data ioctl_data = {
.msgs = messages,
.nmsgs = 2,
};
rc = ioctl(fd, I2C_RDWR, &ioctl_data);
if(rc != 2) {
perror("Unable to read\n");
return -1;
}
printf("Status: [%x, %x, %x]\n", buf[0], buf[1], buf[2]);
return 0;
}
int start_fan(int fd, uint8_t start_stop) {
uint8_t buf[2] = {0x2b, start_stop};
int ret = 0;
ret = i2c_smbus_write_byte_data(fd, buf[0], buf[1]);
if(ret < 0) {
perror("Failed to start fan");
return -1;
}
return 0;
}
int main() {
int ret = 0;
int fd = open("/dev/i2c-1", O_RDWR);
if (fd < 0) {
printf("Failed to open i2c-1\n");
return 1;
}
uint8_t addr = 0x4b;
if (ioctl(fd, I2C_SLAVE, addr) < 0) {
printf("Failed to acquire bus access/talk to slave\n");
return 1;
}
ret = read_status(fd);
if(ret < 0) {
printf("Failed to verify status\n");
return 1;
}
ret = start_fan(fd, 1);
if(ret < 0) {
printf("Failed to start fan\n");
return 1;
}
printf("Fan started\n");
ret = read_status(fd);
if(ret < 0) {
printf("Failed to verify status\n");
return 1;
}
close(fd);
return 0;
}
Status: [0, 78, f0]
Fan started
Status: [1, f1, e1]
Hi,
I'm mainly experienced in C development and I saw in Rust a massive game changer in memory safety & some other things like concurrency, so I started to move to Rust for my developments. My first program was to develop a device driver using I2C bus, I'm on RPI4, but I'm facing one massive issue, I am not able to write register on the device, but I don't know why. There is program samples & strace logs:
From my working C program:
From my non-working rust program :
device.rs
main.rs
What I'm doing wrong ? Can someone help me ?
Thanks in advance, and sorry for this long post.