Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BMI160 Output Zeros

    BMI160 Output Zeros

    showns
    New Poster

    I followed the steps for initializing the BMI160 sensor on Cypress PSoC6 BLE pioneer kit as introduced in this video (https://youtu.be/gyWWnTfdNOQ); however, the output values are always zero regardless of the motion of the pioneer kit board. 
    Could someone help me figuring out what I should do to get the correct output values? 
    Thank you.

    #include "project.h"
    // Added
    #include "FreeRTOS.h"
    #include "task.h"
    #include <stdio.h>
    #include "bmi160.h"
    
    #include "bmi160_defs.h"
    
    static struct bmi160_dev bmi160Dev; // Added
    
    static int8_t BMI160BurstWrite(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
    {
        Cy_SCB_I2C_MasterSendReStart(I2C_1_HW, dev_addr, CY_SCB_I2C_WRITE_XFER, 0, &I2C_1_context);
        Cy_SCB_I2C_MasterWriteByte(I2C_1_HW, reg_addr, 0, &I2C_1_context);
        for(int i=0; i<len; i++) 
        {
            Cy_SCB_I2C_MasterWriteByte(I2C_1_HW, data[i], 0, &I2C_1_context);
        }
        Cy_SCB_I2C_MasterSendStop(I2C_1_HW, 0, &I2C_1_context);
        
        return 0;  
    }    
    
    // This function supports the BMP?180 library and read I2C Registers
    static int8_t BMI160BurstRead(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len) 
    {
        Cy_SCB_I2C_MasterSendReStart(I2C_1_HW, dev_addr, CY_SCB_I2C_WRITE_XFER, 0, &I2C_1_context);
        Cy_SCB_I2C_MasterWriteByte(I2C_1_HW, reg_addr, 0, & I2C_1_context);
        Cy_SCB_I2C_MasterSendReStart(I2C_1_HW, dev_addr, CY_SCB_I2C_WRITE_XFER, 0, &I2C_1_context);
        for(int i=0; i<len-1; i++)
        {
            Cy_SCB_I2C_MasterReadByte(I2C_1_HW, CY_SCB_I2C_ACK, &data[i], 0, &I2C_1_context);
        }
        Cy_SCB_I2C_MasterReadByte(I2C_1_HW, CY_SCB_I2C_NAK, &data[len-1], 0, &I2C_1_context);
        
        Cy_SCB_I2C_MasterSendStop(I2C_1_HW, 0, &I2C_1_context);
        
        return 0;    
    }
    
    static void bmi160Init(void)
    {
        vTaskDelay(100); // Wait to boot
        
        /* BMI160 */
        bmi160Dev.read = (bmi160_read_fptr_t)BMI160BurstRead;
        bmi160Dev.write = (bmi160_write_fptr_t)BMI160BurstWrite;
        bmi160Dev.delay_ms = (bmi160_delay_fptr_t)vTaskDelay;
        bmi160Dev.id = BMI160_I2C_ADDR; // I2C device address
       
        
        bmi160_init(&bmi160Dev); // Initialize the device
        
        bmi160Dev.gyro_cfg.odr = BMI160_GYRO_ODR_800HZ;
        bmi160Dev.gyro_cfg.range = BMI160_GYRO_RANGE_125_DPS;
        bmi160Dev.gyro_cfg.bw = BMI160_GYRO_BW_OSR4_MODE;
        
        /* Select the power mode of Gyroscope sensor */
        bmi160Dev.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
        
        bmi160Dev.accel_cfg.odr = BMI160_ACCEL_ODR_1600HZ;
        bmi160Dev.accel_cfg.range = BMI160_ACCEL_RANGE_2G;
        bmi160Dev.accel_cfg.bw = BMI160_ACCEL_BW_OSR4_AVG1;
        bmi160Dev.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE;
        
        /* Set the sensor configuration */
        bmi160_set_sens_conf(&bmi160Dev);
        bmi160Dev.delay_ms(50);     
    }
    
    // 32768 = 2g
    #define MAXACCEL (32768/2)
    
    void motionTask(void *arg)
    {
        (void)arg;
        I2C_1_Start();
        bmi160Init();
        struct bmi160_sensor_data acc;
        
        float gx, gy, gz;
        
        while(1) 
        {
            bmi160_get_sensor_data(BMI160_ACCEL_ONLY, &acc, NULL, &bmi160Dev);
            
            gx = (float)acc.x/MAXACCEL;
            gy = (float)acc.y/MAXACCEL;
            gz = (float)acc.z/MAXACCEL;
            
            printf ("x=%1.2f y=%1.2f z=%1.2f\r\n", gx, gy, gz);
            
            vTaskDelay(200);
        }        
        
        
    }    
    
    int main(void)
    {
        __enable_irq(); /* Enable global interrupts. */
        
        UART_1_Start();
        
        xTaskCreate(motionTask, "Motion Task", 400, 0, 1, 0);
        vTaskStartScheduler();
        /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    
    }
    
    /* [] END OF FILE */
    1 REPLY 1

    BSTRobin
    Community Moderator
    Community Moderator

    Hi showns,

    There was example code worked well on STM32 for your reference, based on BMI160 Github code: https://github.com/BoschSensortec/BMI160_driver/tree/master/examples/read_sensor_data

    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