Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BMI270 Initialization error with STM32 | possible error with writing config file

    BMI270 Initialization error with STM32 | possible error with writing config file

    Dnacious
    New Poster

    Good day,

    I am using an STM32L4 microcontroller to operate a BMI270 IMU. By the use of HAL_I2C libraries I made seperate functions to initialize the BMI270 and start the imu in normal mode, according to the flow charts (chapter 3: quick start) and instructions (chapter 4.4: POR and Device init.) in the BMI270 datasheet.

    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

     

     

    3 REPLIES 3

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Dnacious,

    Suggest you use BMI270 sensor API(https://github.com/boschsensortec/BMI270-Sensor-API) on github which can fastly deploy your software.
    I upload BMI270 example code based on sensor API v2.71.8 on STM32F4 for your reference.

    Hi BSTRobin, A big thank for the code example you provided, I learned a lot from it.

    I tried to run you example along with the BMI270-Sensor-API, I built everyting using STM32CUBEIDE 1.13.0, and could not make it run with a BMI270 sensor on a flight controller.

    The configs of my flight controller can be found here .

    The logs I get from UART :

    SPI Interface

    Error [-3] : Device not found error. It occurs when the device chip id is incorrectly read

    bmi270_wh_init() failed, error code: -3

    I hope you can enlighten me about what I did wrong, I spent a long time trying to develop a custom driver following the BMI270 datasheet, and gave up.

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Mattieu,

    Some suggestion for you:
    1.Ensure that your host and BMI270 have the correct hardware connection;
    2. SPI waveforms can be captured through a logic analyzer to see what happens during SPI communication?

    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