p1-imu-i2c-read-7.ino
Low-level C example to read raw registers from the ICM20602 sensor connected via I2C on the Allwinner V40.
The code uses standard Linux I2C interfaces. Verify that the I2C-2 bus is enabled in the kernel configuration (device tree). The physical bus corresponds to the GPIO port of the BPI-M2 Ultra. Compile with gcc -O2 -o imu_read imu_read.c.
p1-imu-i2c-read-7.ino
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdint.h>
#define I2C_BUS "/dev/i2c-2"
#define ICM_ADDR 0x68
int main() {
int file;
uint8_t buf[14];
if ((file = open(I2C_BUS, O_RDWR)) < 0) {
perror("Failed to open I2C bus");
return 1;
}
if (ioctl(file, I2C_SLAVE, ICM_ADDR) < 0) {
perror("Failed to acquire bus access and/or talk to slave");
return 1;
}
// Lecture registre ACC_X_H (0x3B) à GYRO_Z_L (0x44)
if (read(file, &buf[0], 14) != 14) {
perror("Failed to read from I2C");
close(file);
return 1;
}
int16_t acc_x = (int16_t)((buf[0] << 8) | buf[1]);
int16_t gyro_x = (int16_t)((buf[8] << 8) | buf[9]);
printf("Accel X: %d, Gyro X: %d\n", acc_x, gyro_x);
close(file);
return 0;
}