Okay, so it seems that by default i2c interface is on, on the latest build.

you do:
i2cdetect -y 0
find the connected external device (not forgetting to do pull up with 10k resistors of the nanopi's SCL/SDA lines).


then you can use the following command to write a double byte:
i2cset -y 0x30 0x0B 0x01

It uses an external device's address 0x30
then accesses its register at 0x0B
and then write into it 0x01 (I'm guessing LSB is set to 1, correct?)

Now, how do I do it in a C file, as a script.c?
I looked at this example:
here's full code:

Code: Select all

#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <errno.h>

#define I2C_ADDR 0x30

int main (void) {
    int fd;
    fd = open("/dev/i2c-0", O_RDWR);

    if (fd < 0) {
        printf("Error opening file: %s\n", strerror(errno));
        return 1;
    }

    if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) {
        printf("ioctl error: %s\n", strerror(errno));
        return 1;
    }
   __u8 reg = 0x0B;
   char buf[1];
   buf[0] = reg;
   buf[1] = 0x01;
   write(fd, buf, 2);
   
return 0;
}


However, upon compilation I get warning about "warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("ioctl error: %s\n", strerror(errno));"

but looking at websites, it seems this can be ignored.

When I try to run the script:
./script
I get "Segmentation fault", which is something about address memory incorrectly. I followed the example from a reputable website, what's wrong?

Also, doing consecutive "write" commands wouldn't work, as i2c of NanoPI will have start/stop at each "write" command, so it's not what I need.

Also also, in case i2c gets frozen/locked, how do I unlock it? By resetting the i2c module somehow? Other than shutting down the board, what commands can I use?