Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BMI160 Programming

    BMI160 Programming

    SaberKazemi1992
    Established Member

    Hi 

    I'd like to configure BMI160 with desired registers. To this aim, The associated functions are defined as follows:

    user_spi_write function

     

    int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
    {
            int8_t rslt = BMI160_OK; /* Return 0 for Success, non-zero for failure */
            SPI_BMI160_BEGIN;
            reg_addr = reg_addr & 0x7F;
            SPI_BMI160_TX(reg_addr); // For write operations, the register should be ANDed with 0x7F.
             __delay_cycles(15);
                 /* Communicate len number of bytes: if TX - the procedure doesn't overwrite pData */
                 for (i = 0; i < length; i++)
                 {
                   SPI_BMI160_TX(*reg_data++);
                 }
                 SPI_BMI160_END;
    
                 return rslt;
    }

     

    user_spi_read function:

     

    int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
    {
    int8_t rslt = BMI160_OK; /* Return 0 for Success, non-zero for failure */
    /* Communicate len number of bytes: if RX - the procedure sends 0x00 to push bytes from slave*/
    
    SPI_BMI160_BEGIN;
    reg_addr = reg_addr | 0x80; // For read operation, the reg_addr should be ORed with 0x80.
    SPI_BMI160_TX(reg_addr);
    __delay_cycles(15); //after write operation, at least 2 micro-seconds is required for the next operation.
    //SPI_BMI160_RX(rslt);
    
      for (i = 0; i < length; i++)
      {
          SPI_BMI160_TX(0);            /* Possible to combining read and write as one access type */
          SPI_BMI160_RX(*reg_data++);     /* Store pData from last pData RX */
      }
      SPI_BMI160_END;
        return rslt;
        }

     

    user_delay_ms function:

     

    int8_t user_delay_ms(uint32_t period)
    {
    int8_t rslt = BMI160_OK; /* Return 0 for Success, non-zero for failure */
    a=0;
    while(a<period)
    {
    __delay_cycles(1);
    a++;
    }
    return rslt;
    
    }

     

    and also, I have defined a configure function that defines the registers:

     

    int Configuring(void)
    {
        //  You may assign a chip select identifier to be handled later
          /**Initialize the BMI160***/
          struct bmi160_dev sensor;
          sensor.id = 0;
          sensor.interface = BMI160_SPI_INTF;
          sensor.read = user_spi_read;
          sensor.write = user_spi_write;
          sensor.delay_ms = user_delay_ms;
          int8_t rslt = BMI160_OK;
          bmi160_init(&sensor);
    
          /* After the above function call, accel_cfg and gyro_cfg parameters in the device
          structure are set with default values, found in the datasheet of the sensor */
          /*********Configuring the acc/gyro*************/
          // int8_t rslt = BMI160_OK;
    
          /* Select the Output data rate, range of accelerometer sensor */
          sensor.accel_cfg.odr = BMI160_ACCEL_ODR_1600HZ;
          sensor.accel_cfg.range = BMI160_ACCEL_RANGE_2G;
          sensor.accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4;
          /* Select the Output data rate, range of Gyroscope sensor */
          sensor.gyro_cfg.odr = BMI160_GYRO_ODR_3200HZ;
          sensor.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS;
          sensor.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE;
    
          /* Select the power mode */
          sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
          sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
    
          /* Set the Power mode */
          rslt = bmi160_set_power_mode(&sensor);
    
          /* Set the sensor configuration */
          rslt = bmi160_set_sens_conf(&sensor);
           return rslt;
    }

     

    The problem I have here is that at the start of executing the code in debug mode, it goes to exit.c (on code composer studio). Where do you think the problem comes from? Thanks and regards!

    Also for brevity, the spi functions are abbreviated as:

     

    //**The code is implemented on MSP432P401R**//
    #define SPI_BMI160_TX(X)   {while(!(UCB1IFG & UCTXIFG));UCB1TXBUF=X;}while(1==-1)
    #define SPI_BMI160_RX(X)   {while(!(UCB1IFG & UCRXIFG));X=UCB1RXBUF;}while(1==-1)
    #define SPI_BMI160_BEGIN   P6OUT &= ~BIT2;
    #define SPI_BMI160_END     P6OUT |= BIT2;

     

     

    1 REPLY 1

    fish
    Community Moderator
    Community Moderator

    Hi,

    I couldn't find out the error quicly.

    Maybe you can add printf() function and log more info, this can help you find the exception position.

    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