cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 interface with BMI270

Dvan .11
Associate

I have verified in my code that writing and reading with I2C works as expected, and registers are configured seemingly correctly. I believe there must be a problem with how I write the configuration file to the BMI270, but I am at a loss as to what error exists in my code.

Here I present the initialization function and starting function for the BMI270:

--------------------------------------------

uint8_t BMI270_init(void)
{
uint8_t address;
uint8_t init_addr0 = 0x5B;
uint8_t init_addr1 = 0x5C;
uint16_t init_val = 0;
uint8_t value, val_lsb, val_msb;
uint8_t buffer[2] = {0};
uint8_t err = 0;

//disable PWR_CONF.adv_power_save (addr=0x7C,val=0x00)
address = 0x7C;
value = 0x00;

//Read address before
err = I2C_read(&address, buffer, 1);
if (err != HAL_OK)
{return err;}

//Print result
UART_print_char("test pwc 1: ", 12);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

err = I2C_write(&address, &value, 1);

if (err != HAL_OK)

{return err;}

HAL_Delay(1);

//Read address after

err = I2C_read(&address, buffer, 1);

if (err != HAL_OK)

{return err;}

//Print result

UART_print_char("test pwc 2: ", 12);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

//wait for 450 us

HAL_Delay(1);

//set INIT_CTRL to zero (addr=0x59,val=0x00)

address = 0x59;

value = 0x00;

err = I2C_write(&address, &value, 1);

if (err != HAL_OK)

{return err;}

//Read address after

err = I2C_read(&address, buffer, 1);

if (err != HAL_OK)

{return err;}

//Print result

UART_print_char("test set 1: ", 12);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

//write bmi270_config_file (addr=0x5E,val=bmi270_config_file)

address = 0x5E;

//Continuously write parts of the configuration file <--- error??

for(uint8_t i = 0; i<32 ; i++)

{

err = I2C_write(&address, &bmi270_config_file[i*255], 255);

if (err != HAL_OK)

{return err;}

init_val=128;

val_msb = (uint8_t)(init_val >> 8);

val_lsb = (uint8_t)init_val;

//Update init_addr0 and init_addr1 (increment with bytes/2)

err = I2C_write(&init_addr0, &val_lsb, 1);

if (err != HAL_OK)

{return err;}

err = I2C_write(&init_addr1, &val_msb, 1);

if (err != HAL_OK)

{return err;}

address+= 256;

}



//set INIT_CTRL to one (addr=0x59,val=0x01)

address = 0x59;

value = 0x01;

err = I2C_write(&address, &value, 1);

if (err != HAL_OK)

{return err;}

//Read address after

err = I2C_read(&address, buffer, 1);

if (err != HAL_OK)

{return err;}

//Print result

UART_print_char("test set 2: ", 12);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

return 0;

}

//end BMI270_initialize



uint8_t BMI270_start(void)

{

uint8_t address;

uint8_t value;

uint8_t err = 0;

uint8_t buffer[2] = {0};

//Write to PWR_CTRL (addr=0x7D,val=0x0E) (enable acc. & gyr.)

address = 0x7D;

value = 0x0E;

//Read address before

err = I2C_read(&address, buffer, 1);

if (err != HAL_OK)

{return err;}

//Print result

UART_print_char("test pwr 1: ", 12);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

//Write value to register

err = I2C_write(&address, &value, 1);

if (err != HAL_OK)

{return err;}

//Read address after

err = I2C_read(&address, buffer, 1);

if (err != HAL_OK)

{return err;}

//Print result

UART_print_char("test pwr 2: ", 12);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

//Write to ACC_CONF (addr=0x40,val=0xA8)

address = 0x40;

value = 0xA3;

err = I2C_write(&address, &value, 1);

if (err != HAL_OK)

{return err;}



err = I2C_read(&address, buffer, 1);

if (err != HAL_OK)

{return err;}

UART_print_char("test acc: ", 10);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

//Write to GYR_CONF (addr=0x42,val=0xA9)

address = 0x42;

value = 0xA9;

err = I2C_write(&address, &value, 1);

if (err != HAL_OK)

{return err;}

err = I2C_read(&address, buffer, 1);

if (err != HAL_OK)

{return err;}

UART_print_char("test gyr: ", 10);

UART_print(buffer, 2);

UART_print_char("\r\n", 2);

//Write to PWR_CONF (addr=0x7C,val=0x02) (disable adv_power_save)

address = 0x7C;

value = 0x02;

err = I2C_write(&address, &value, 1);

if (err != HAL_OK)

{return err;}

return 0;

}

//end BMI270_start

--------------------------------------------

 

If the initialization was successful, the INTERNAL_STATUS register should contain the value 0x01, but in my case keeps containing the value 0x02 (init. error). The strangest thing is that at one point this code did work (internal_status = 0x01, gyroscope and accelerometer outputted values), however I can no longer replicate this response.

I included a zip folder containing two screenshots of both the error response and the working response in my serial monitor.

 

I have been breaking my head over the initialization proces for a while now, and I have a feeling there's just something I am overlooking. Help would be immensely appreciated. I would be eager to provide any further information if desired.

 

Kind regards,

Daan


Edited to apply source code formatting - please see How to insert source code for future reference.

10 REPLIES 10

Now it works for me .. no idea what I did wrong .... foudn differences in the Datasheet compared to the Reference implementation (ADDR_0/1 was explicitly set and PowerConf was not set to 0x00 but 0x02) but both did not seam to be the reason ...

Thanks for your kind support!

BR, Bastel