09-16-2021 01:23 PM
Hi,
I am trying to use the bno055 files on GitHub on a nanopi board running armbian.
I have implemented read write and delay functions to the best of my knowledge, but every readout returns a 0.
Could anyone tell me where I went wrong?
The code is:
#include <iostream>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <syslog.h>
#include <inttypes.h>
#include <cstring>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
extern "C"
{
#include "BNO055_driver/bno055.c"
#include "BNO055_driver/bno055.h"
}
#define BUFFER_SIZE 0x01
using namespace std;
s8 BNO055_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
const char *path = "/dev/i2c-0";
int fd;
if (fd != -1)
{
if ((fd = open(path, O_RDWR)) < 0)
{
return (1);
}
}
if (ioctl(fd, I2C_SLAVE, dev_addr) < 0)
{
close(fd);
return (1);
}
char buff[BUFFER_SIZE];
buff[0] = reg_addr;
if (write(fd, buff, BUFFER_SIZE) != BUFFER_SIZE)
{
close(fd);
return (1);
}
if (read(fd, ®_data, cnt) != cnt)
{
close(fd);
return (1);
}
close(fd);
return 0;
}
s8 BNO055_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
const char *path = "/dev/i2c-0";
int fd;
if (fd != -1)
{
if ((fd = open(path, O_RDWR)) < 0)
{
return 1;
}
}
if (ioctl(fd, I2C_SLAVE, dev_addr) < 0)
{
close(fd);
return 1;
}
char buff[cnt + 1];
buff[0] = reg_addr;
memcpy(&buff[1], reg_data, cnt);
if (write(fd, buff, cnt + 1) != cnt + 1)
{
close(fd);
return 1;
}
close(fd);
return 0;
}
void sleep_ms(unsigned int milliseconds)
{
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
}
s8 I2C_routine(bno055_t *bno055)
{
bno055->bus_write = BNO055_I2C_bus_write;
bno055->bus_read = BNO055_I2C_bus_read;
bno055->delay_msec = sleep_ms;
bno055->dev_addr = BNO055_I2C_ADDR1;
return BNO055_INIT_VALUE;
}
int main()
{
struct bno055_t myBNO;
I2C_routine(&myBNO);
cout << "bno055_init " << endl;
s8 ans = bno055_init(&myBNO);
cout << "true " << (((s8)0) == ans) << endl;
u8 chip_id_u8;
bno055_read_chip_id(&chip_id_u8);
cout << "chip_id_u8 " << (int)chip_id_u8 << endl;
}
09-16-2021 02:14 PM
I think i finally fixed it:
in s8 BNO055_I2C_bus_read:
no * in
if (read(fd, reg_data, cnt) != cnt)
09-16-2021 05:22 PM
Hello b0hne,
You should adapt BNO055_I2C_bus_read() function implement into your platform when you migrate BNO055 driver code to your platform.
After migration, you could read chip ID to verify read funtion is correct or not.