Page 1 of 1

BNO055 Bosch sensortec library integration

Posted: Tue Apr 22, 2025 10:49 am
by Adns9800
Hey everyone,

I am trying to use the BNO055 sensor (a custom breakout) with my project. As I am not using Arduino, I can't use adafruit's library. I sought to use the original library provided by Bosch, but I am not confident my read and write function implementations are correct. Forgive me if there are any silly mistakes as I am still new to this. Any help is greatly appreciated.

Code:

8 bno055_read(u8 addr, u8 reg_addr, u8* reg_data, u8 cnt){

// need to get bus handle + device handle
i2c_master_bus_handle_t bno055_bus_handle;
ESP_ERROR_CHECK(i2c_master_get_bus_handle(BNO055_PORT, &bno055_bus_handle));

// first, send the device address and then the register address
esp_err_t ret = 0;
if(cnt > 1){
// if count is greater than one...
// magic
// unfortunately ESP32 new api doesnt have any read functions that acn do what is specified in the bno055 datasheet
for(int i = 0; i < cnt; i++){
// increment register address and the array value to be able to read more than one byte
// sum to ret because ESP_OK is basically just 0, any other value is an error
// similarly to the BNO055 expected return code
ret += i2c_master_transmit_receive(BNO055_handle, &reg_addr, sizeof(reg_addr), reg_data + i, sizeof(*(reg_data + i)), -1);
}
}else{
// else just read once
ret = i2c_master_transmit_receive(BNO055_handle, &reg_addr, sizeof(reg_addr), reg_data, sizeof(*reg_data), -1);
}

if(ret != ESP_OK){
ESP_ERROR_CHECK(i2c_master_bus_reset(bno055_bus_handle));
return ret; // anything but 0x00 indicates an error
}
return 0; // if no issues, return 0
}

s8 bno055_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt){
// need to get bus handle + device handle
i2c_master_bus_handle_t bno055_bus_handle;
ESP_ERROR_CHECK(i2c_master_get_bus_handle(BNO055_PORT, &bno055_bus_handle));
// need to write the register address + the actual data...
// size should be cnt + 1 (in bytes)
u8 data_buffer[2];
memcpy(&data_buffer[0], &reg_addr, sizeof(reg_addr));
memcpy(&data_buffer[1], reg_data, sizeof(*reg_data));

esp_err_t ret = 0;
for(int i = 0 ; i < (cnt + 1); i++){
ret += i2c_master_transmit(BNO055_handle, &(data_buffer), sizeof(data_buffer), -1);
}
if(ret != ESP_OK){
ESP_ERROR_CHECK(i2c_master_bus_reset(bno055_bus_handle));
return ret;
}
return 0;
}

void bno055_delay_ms(u32 tmsec){
vTaskDelay(pdMS_TO_TICKS(tmsec));
}