Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    Loading of config file with SPI

    Loading of config file with SPI

    chuannes
    New Poster

    Hi there,

    I am really likeing the library for the BMI270 from github, but i am having one issue.

    When loading the config file over SPI to the BMI270 the program seems to stop with the error code -9 (=BMI2_E_CONFIG_LOAD). I checked BMI2_OK wich is 0 as it should so it must be something with "load_status" but i cant figure out whats wrong.

    Here the errorcode gets thrown:

    * Write the configuration file */
    rslt = write_config_file(dev);
    if (rslt == BMI2_OK)
    {
    /* Check the configuration load status */
    rslt = bmi2_get_internal_status(&load_status, dev);

    /* Return error if loading not successful */
    if ((rslt == BMI2_OK) && (!(load_status & BMI2_CONFIG_LOAD_SUCCESS)))
    {
    rslt = BMI2_E_CONFIG_LOAD;
    }
    }

    So i was thinking that the problem must be within bmi2_get_internal_status.

    I should also mention, that the programm seems to have issues with the function upload file:

    static int8_t write_config_file(struct bmi2_dev *dev)
    {
    /* Variable to define error */
    int8_t rslt;

    /* Variable to update the configuration file index */
    uint16_t index = 0;

    /* config file size */
    uint16_t config_size = dev->config_size;

    /* Variable to get the remainder */
    uint8_t remain = (uint8_t)(config_size % dev->read_write_len);

    /* Variable to get the balance bytes */
    uint16_t bal_byte = 0;

    /* Variable to define temporary read/write length */
    uint16_t read_write_len = 0;

    /* Disable advanced power save mode */
    rslt = bmi2_set_adv_power_save(BMI2_DISABLE, dev);
    if (rslt == BMI2_OK)
    {
    /* Disable loading of the configuration */
    rslt = set_config_load(BMI2_DISABLE, dev);
    if (rslt == BMI2_OK)
    {
    if (!remain)
    {
    /* Write the configuration file */
    for (index = 0; (index < config_size) && (rslt == BMI2_OK); index += dev->read_write_len)
    {
    rslt = upload_file((dev->config_file_ptr + index), index, dev->read_write_len, dev);
    }
    }
    else ...

    It sends out the config file (checked with osciloscope), but doesnt proceed in the code after that.

     

    Does anyone see the issue at hand? I would be imennsly gearfull.

    Best regards Hannes

     

    3 REPLIES 3

    BSTRobin
    Community Moderator
    Community Moderator

    Hello chuannes,

    You could refer BMI270 sensor API and example from github: https://github.com/BoschSensortec/BMI270-Sensor-API/tree/master/examples/bmi270

    And following SPI read and write code on STM32 for your reference.
    uint8_t GTXBuffer[512], GRXBuffer[2048];
    int8_t SensorAPI_SPIx_Read(uint8_t subaddress, uint8_t *pBuffer, uint16_t ReadNumbr, void *intf_ptr)
    {
    GTXBuffer[0] = subaddress | 0x80;

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // NSS low

    //HAL_SPI_TransmitReceive(&hspi2, pTxData, pRxData, ReadNumbr+1, BUS_TIMEOUT); // timeout 1000msec;
    HAL_SPI_TransmitReceive(&SPI_HANDLE, GTXBuffer, GRXBuffer, ReadNumbr+1, BUS_TIMEOUT); // timeout 1000msec;
    while(SPI_HANDLE.State == HAL_SPI_STATE_BUSY); // wait for xmission complete

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // NSS high
    memcpy(pBuffer, GRXBuffer+1, ReadNumbr);

    return 0;
    }

    int8_t SensorAPI_SPIx_Write(uint8_t subaddress, uint8_t *pBuffer, uint16_t WriteNumbr, void *intf_ptr)
    {
    GTXBuffer[0] = subaddress & 0x7F;
    memcpy(&GTXBuffer[1], pBuffer, WriteNumbr);

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // NSS low

    //HAL_SPI_TransmitReceive(&hspi2, pTxData, pRxData, WriteNumbr+1, BUS_TIMEOUT); // send register address + write data
    HAL_SPI_Transmit(&SPI_HANDLE, GTXBuffer, WriteNumbr+1, BUS_TIMEOUT); // send register address + write data
    while(SPI_HANDLE.State == HAL_SPI_STATE_BUSY); // wait for xmission complete

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // NSS high

    return 0;
    }

    int8_t bmi2_interface_init(struct bmi2_dev *bmi, uint8_t intf)
    {
    int8_t rslt = BMI2_OK;

    /* Bus configuration : I2C */
    if (intf == BMI2_I2C_INTF)
    {
    PDEBUG("I2C Interface \n");

    /* To initialize the user I2C function */
    bmi270_dev_addr = BMI2_I2C_SEC_ADDR;
    bmi->intf = BMI2_I2C_INTF;
    bmi->read = (bmi2_read_fptr_t)SensorAPI_I2Cx_Read;
    bmi->write = (bmi2_write_fptr_t)SensorAPI_I2Cx_Write;
    }
    /* Bus configuration : SPI */
    else if (intf == BMI2_SPI_INTF)
    {
    PDEBUG("SPI Interface \n");
    bmi->intf = BMI2_SPI_INTF;
    bmi->read = (bmi2_read_fptr_t)SensorAPI_SPIx_Read;
    bmi->write = (bmi2_write_fptr_t)SensorAPI_SPIx_Write;
    }


    /* Assign device address to interface pointer */
    bmi->intf_ptr = &bmi270_dev_addr;

    /* Configure delay in microseconds */
    bmi->delay_us = bmi2_delay_us;

    /* Configure max read/write length (in bytes) ( Supported length depends on target machine) */
    bmi->read_write_len = READ_WRITE_LEN;

    /* Assign to NULL to load the default config file. */
    bmi->config_file_ptr = NULL;

    return rslt;
    }

    hi robin

    thankyou for you fast respons.

    I tested my read and write and they work indipentently from the calls by the bosch library. The issue seems to be the errorcode -9. Do you know the reason why this error occurs. Then i would know where to look at in my code.

    BSTRobin
    Community Moderator
    Community Moderator

    Hello chuannes,


    Error code -9 means loading config failed, you could see it from BMI270 driver code.
    #define BMI2_E_CONFIG_LOAD INT8_C(-9)

    As you loaded config failed, you should check your read, write, delay funtion and your SPI hardware configuration.
    By the way, BMI270 SPI interface is incompatible with two mode: '00'[CPOL=0, CPHA=0], '11'[CPOL=1, CPHA=1]. When you initialized your PIC32 SPI interface, you need to choose one mode of 00 or 11.

    The default SPI read and write length is 32 byte, this length depends on the read and write length supported by the host SPI. You could modify it according to the actual host SPI maxmum length.
    #define READ_WRITE_LEN UINT8_C(32)

    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