Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    bno055 linux api

    bno055 linux api

    b0hne
    New Poster

    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, &reg_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;

    }

    2 REPLIES 2

    b0hne
    New Poster

    I think i finally fixed it:

    in s8 BNO055_I2C_bus_read:

    no * in

    if (read(fd, reg_data, cnt) != cnt)

    BSTRobin
    Community Moderator
    Community Moderator

    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.

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist