/** * Copyright (C) 2018 Bosch Sensortec GmbH * * SPDX-License-Identifier: BSD-3-Clause * * @file bmi160_read_sensor_data.c * @brief Sample file to read BMI160 sensor data using COINES library * */ /*********************************************************************/ /* system header files */ /*********************************************************************/ #include #include #include /*********************************************************************/ /* own header files */ /*********************************************************************/ #include "coines.h" #include "bmi160.h" #define TIME_RESOLUTION 39E-6 /*********************************************************************/ /* local macro definitions */ /*! I2C interface communication, 1 - Enable; 0- Disable */ #define BMI160_INTERFACE_I2C 1 /*! SPI interface communication, 1 - Enable; 0- Disable */ #define BMI160_INTERFACE_SPI 0 #if (!((BMI160_INTERFACE_I2C == 1) && (BMI160_INTERFACE_SPI == 0)) && \ (!((BMI160_INTERFACE_I2C == 0) && (BMI160_INTERFACE_SPI == 1)))) #error "Invalid value given for the macros BMI160_INTERFACE_I2C / BMI160_INTERFACE_SPI" #endif /*! bmi160 shuttle id */ #define BMI160_SHUTTLE_ID 0x38 /*! bmi160 Device address */ #define BMI160_DEV_ADDR BMI160_I2C_ADDR /*********************************************************************/ /* global variables */ /*********************************************************************/ /*! @brief This structure containing relevant bmi160 info */ struct bmi160_dev bmi160dev; /*! @brief variable to hold the bmi160 accel data */ struct bmi160_sensor_data bmi160_accel; /*! @brief variable to hold the bmi160 gyro data */ struct bmi160_sensor_data bmi160_gyro; struct { uint32_t previous; uint32_t current; uint8_t overflows; } time; /*********************************************************************/ /* static function declarations */ /*********************************************************************/ /*! * @brief internal API is used to initialize the sensor interface */ static void init_sensor_interface(void); /*! * @brief This internal API is used to initialize the bmi160 sensor with default */ static void init_bmi160(void); /*! * @brief This internal API is used to initialize the sensor driver interface */ static void init_bmi160_sensor_driver_interface(void); double sensortime(uint32_t from); int8_t fifo_header_time_data(uint16_t); /*********************************************************************/ /* functions */ /*********************************************************************/ /*! * @brief This internal API is used to initialize the sensor interface depending * on selection either SPI or I2C. * * @param[in] void * * @return void * */ static void init_sensor_interface(void) { /* Switch VDD for sensor off */ coines_set_shuttleboard_vdd_vddio_config(0, 0); /* wait until the sensor goes off */ coines_delay_msec(10); #if BMI160_INTERFACE_I2C == 1 /* SDO pin is made low for selecting I2C address 0x68 */ coines_set_pin_config(COINES_SHUTTLE_PIN_15, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_LOW); /* set the sensor interface as I2C */ coines_config_i2c_bus(COINES_I2C_BUS_0, COINES_I2C_FAST_MODE); coines_delay_msec(10); /* CSB pin is made high for selecting I2C protocol*/ coines_set_pin_config(COINES_SHUTTLE_PIN_7, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_HIGH); #endif #if BMI160_INTERFACE_SPI == 1 /* CSB pin is made low for selecting SPI protocol*/ coines_set_pin_config(COINES_SHUTTLE_PIN_7, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_LOW); coines_delay_msec(10); coines_config_spi_bus(COINES_SPI_BUS_0, COINES_SPI_SPEED_5_MHZ, COINES_SPI_MODE3); #endif coines_delay_msec(10); /* Switch VDD for sensor on */ coines_set_shuttleboard_vdd_vddio_config(3300, 3300); #if BMI160_INTERFACE_SPI == 1 coines_delay_msec(10); /* CSB pin is made high for selecting SPI protocol * Note: CSB has to see rising after power up, to switch to SPI protocol */ coines_set_pin_config(COINES_SHUTTLE_PIN_7, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_HIGH); #endif } /*! * @brief This internal API is used to initializes the bmi160 sensor * settings like power mode and OSRS settings. * * @param[in] void * * @return void * */ static void init_bmi160(void) { int8_t rslt; rslt = bmi160_init(&bmi160dev); if (rslt == BMI160_OK) { printf("BMI160 initialization success !\n"); printf("Chip ID 0x%X\n", bmi160dev.chip_id); } else { printf("BMI160 initialization failure !\n"); exit(COINES_E_FAILURE); } /* Select the Output data rate, range of accelerometer sensor */ bmi160dev.accel_cfg.odr = BMI160_ACCEL_ODR_200HZ; bmi160dev.accel_cfg.range = BMI160_ACCEL_RANGE_16G; bmi160dev.accel_cfg.bw = BMI160_ACCEL_BW_NORMAL_AVG4; /* Select the power mode of accelerometer sensor */ bmi160dev.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE; /* Select the Output data rate, range of Gyroscope sensor */ bmi160dev.gyro_cfg.odr = BMI160_GYRO_ODR_200HZ; bmi160dev.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS; bmi160dev.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE; /* Select the power mode of Gyroscope sensor */ bmi160dev.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE; /* Set the sensor configuration */ rslt = bmi160_set_sens_conf(&bmi160dev); } /*! * @brief This internal API is used to set the sensor driver interface to * read/write the data. * * @param[in] void * * @return void * */ static void init_bmi160_sensor_driver_interface(void) { #if BMI160_INTERFACE_I2C == 1 /* I2C setup */ /* link read/write/delay function of host system to appropriate * bmi160 function call prototypes */ bmi160dev.write = coines_write_i2c; bmi160dev.read = coines_read_i2c; bmi160dev.delay_ms = coines_delay_msec; /* set correct i2c address */ bmi160dev.id = BMI160_DEV_ADDR; bmi160dev.intf = BMI160_I2C_INTF; #endif #if BMI160_INTERFACE_SPI == 1 /* SPI setup */ /* link read/write/delay function of host system to appropriate * bmi160 function call prototypes */ bmi160dev.write = coines_write_spi; bmi160dev.read = coines_read_spi; bmi160dev.delay_ms = coines_delay_msec; bmi160dev.id = COINES_SHUTTLE_PIN_7; bmi160dev.intf = BMI160_SPI_INTF; #endif } /*! * @brief Main Function where the execution getting started to test the code. * * @param[in] argc * @param[in] argv * * @return status * */ int main(int argc, char *argv[]) { uint16_t runtime = 0; // seconds if (argc < 2){ printf("specify a time in seconds\n"); return -1; } else { runtime = atoi(argv[1]); } printf("RUNTIME %d\n", runtime); time.previous = 0; time.current = 0; time.overflows = 0; uint16_t BUFSIZE = 1024; struct coines_board_info board_info; int16_t rslt; int times_to_read = 0; init_bmi160_sensor_driver_interface(); rslt = coines_open_comm_intf(COINES_COMM_INTF_USB); if (rslt < 0) { printf( "\n Unable to connect with Application Board ! \n" " 1. Check if the board is connected and powered on. \n" " 2. Check if Application Board USB driver is installed. \n" " 3. Check if board is in use by another application. (Insufficient permissions to access USB) \n"); exit(rslt); } rslt = coines_get_board_info(&board_info); if (rslt == COINES_SUCCESS) { if (board_info.shuttle_id != BMI160_SHUTTLE_ID) { printf("! Warning invalid sensor shuttle \n ," "This application will not support this sensor \n"); printf("Shuttle ID: %d\n", board_info.shuttle_id); //exit(COINES_E_FAILURE); } } init_sensor_interface(); /* After sensor init introduce 200 msec sleep */ coines_delay_msec(200); init_bmi160(); /* Declare memory to store the raw FIFO buffer information */ uint8_t fifo_buff[BUFSIZE]; /* Modify the FIFO buffer instance and link to the device instance */ struct bmi160_fifo_frame fifo_frame; fifo_frame.data = fifo_buff; fifo_frame.length = BUFSIZE; fifo_frame.fifo_time_enable = BMI160_FIFO_TIME_ENABLE; fifo_frame.fifo_header_enable = BMI160_FIFO_HEAD_ENABLE; fifo_frame.fifo_data_enable = BMI160_FIFO_G_A_ENABLE; bmi160dev.fifo = &fifo_frame; /* Declare instances of the sensor data structure to store the parsed FIFO data */ /* Configure the sensor's FIFO settings */ rslt = bmi160_set_fifo_config(BMI160_FIFO_GYRO | BMI160_FIFO_ACCEL | BMI160_FIFO_HEADER | BMI160_FIFO_TIME, BMI160_ENABLE, &bmi160dev); if (rslt != BMI160_OK) { printf("\n Setting FIFO configuration failed"); } else { while (times_to_read < 20 * runtime) { bmi160dev.delay_ms(50); fifo_header_time_data(BUFSIZE); times_to_read = times_to_read + 1; } } coines_close_comm_intf(COINES_COMM_INTF_USB); return EXIT_SUCCESS; } int8_t fifo_header_time_data(uint16_t bytes_to_read) { int8_t rslt = 0; uint16_t index = 0; uint8_t gyro_index; struct bmi160_sensor_data gyro_data[128]; uint8_t gyro_frames_req = 128; struct bmi160_sensor_data acc_data[128]; uint8_t acc_frames_req = 128; bmi160dev.fifo->length = bytes_to_read; /* Read data from the sensor's FIFO and store it the FIFO buffer,"fifo_buff" */ rslt = bmi160_get_fifo_data(&bmi160dev); if (rslt == BMI160_OK) { printf("\n AVAILABLE FIFO LENGTH : %d\n",bmi160dev.fifo->length); /* Parse the FIFO data to extract gyro data from the FIFO buffer */ rslt = bmi160_extract_gyro(gyro_data, &gyro_frames_req, &bmi160dev); if (rslt == BMI160_OK) { printf("\n AVAILABLE GYRO DATA FRAMES : %d\n ",gyro_frames_req); } else { printf("\n Gyro data extraction failed"); } rslt = bmi160_extract_accel(acc_data, &acc_frames_req, &bmi160dev); if (rslt == BMI160_OK){ printf("\n AVAILABLE ACC DATA FRAMES : %d\n ",acc_frames_req); /* Print the special FIFO frame data like sensortime */ printf("\n SENSOR TIME DATA : %lf \n",sensortime(bmi160dev.fifo->sensor_time)); } else { printf("\n Acc data extraction failed"); } } else { printf("\n Reading FIFO data failed"); } return rslt; } /* Convert from raw sensortime into seconds */ double sensortime(uint32_t from){ if (from > 0){ time.previous = time.current; time.current = from; if ( (time.previous > 0) && (time.current > 0) && (time.current < time.previous)) { time.overflows +=1; } double doubletime = (double)(time.current + (time.overflows << 24)) * TIME_RESOLUTION; return doubletime; } else { return 0.0; } }