bmi160 read fifo config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Spi_sensor_fifo_init( void ) { | |
/* You may assign a chip select identifier to be handled later */ | |
s_spi_bmi160_sensor.id = 0 ; | |
s_spi_bmi160_sensor.interface = BMI160_SPI_INTF ; | |
s_spi_bmi160_sensor.read = bmi160_spi_bus_read ; | |
s_spi_bmi160_sensor.write = bmi160_spi_bus_write ; | |
s_spi_bmi160_sensor.delay_ms = Spi_delay_ms ; | |
int8_t rslt = BMI160_OK ; | |
rslt = bmi160_init( &s_spi_bmi160_sensor ) ; | |
if ( rslt == 0 ) { | |
SEGGER_RTT_printf( 0,"Get bmi160 data success ! \n", rslt ) ; | |
} // if | |
else | |
SEGGER_RTT_printf( 0,"Error : Get bmi160 data fail ! \n", rslt ) ; | |
/* 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 */ | |
rslt = BMI160_OK ; | |
/* Select the Output data rate, range of accelerometer sensor */ | |
s_spi_bmi160_sensor.accel_cfg.odr = BMI160_ACCEL_ODR_100HZ ; | |
s_spi_bmi160_sensor.accel_cfg.range = BMI160_ACCEL_RANGE_2G ; | |
s_spi_bmi160_sensor.accel_cfg.bw = BMI160_ACCEL_BW_OSR4_AVG1 ; | |
/* Select the power mode of accelerometer sensor */ | |
s_spi_bmi160_sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE ; | |
/* Select the Output data rate, range of Gyroscope sensor */ | |
s_spi_bmi160_sensor.gyro_cfg.odr = BMI160_GYRO_ODR_100HZ ; | |
s_spi_bmi160_sensor.gyro_cfg.range = BMI160_GYRO_RANGE_2000_DPS ; | |
s_spi_bmi160_sensor.gyro_cfg.bw = BMI160_GYRO_BW_NORMAL_MODE ; | |
/* Select the power mode of Gyroscope sensor */ | |
s_spi_bmi160_sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE ; | |
/* Set the sensor configuration */ | |
rslt = bmi160_set_sens_conf( &s_spi_bmi160_sensor ) ; | |
/* Select the power mode */ | |
s_spi_bmi160_sensor.accel_cfg.power = BMI160_ACCEL_NORMAL_MODE ; | |
s_spi_bmi160_sensor.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE ; | |
/* Set the Power mode */ | |
rslt = bmi160_set_power_mode( &s_spi_bmi160_sensor ) ; | |
nrf_delay_ms( 10 ) ; // delay_10ms | |
//rslt = start_foc(&s_spi_bmi160_sensor); | |
SEGGER_RTT_printf(0,"foc rslt=%d\n",rslt); | |
nrf_delay_ms(200); | |
struct bmi160_offsets sensor_offset; | |
rslt = bmi160_get_offsets(&sensor_offset,&s_spi_bmi160_sensor); | |
SEGGER_RTT_printf(0,"calibration value: ax=%d,ay=%d,az=%d,gx=%d,gy=%d,gz=%d\n",sensor_offset.off_acc_x,sensor_offset.off_acc_y,sensor_offset.off_acc_z,sensor_offset.off_gyro_x,sensor_offset.off_gyro_y,sensor_offset.off_gyro_z); | |
/* Link the FIFO memory location */ | |
fifo_frame.data = FIFO_BUFF; | |
fifo_frame.length = FIFO_LEN; | |
fifo_frame.fifo_time_enable =BMI160_ENABLE; | |
s_spi_bmi160_sensor.fifo = &fifo_frame; | |
/*Enable fifo*/ | |
uint8_t fifo_config = BMI160_FIFO_HEADER | BMI160_FIFO_ACCEL | BMI160_FIFO_GYRO | BMI160_FIFO_TIME ; | |
rslt = bmi160_set_fifo_config(fifo_config, BMI160_ENABLE, &s_spi_bmi160_sensor); | |
if (rslt != BMI160_OK) | |
{ | |
SEGGER_RTT_printf(0,"BMI160 failed FIFO setting"); | |
} | |
// instance for interrupt settings | |
struct bmi160_int_settg int_config; | |
int_config.int_channel = BMI160_INT_CHANNEL_1; | |
int_config.int_type = BMI160_ACC_GYRO_FIFO_WATERMARK_INT; // choose FIFO watermark IRQ (fires when a certain level of fullness is reached) | |
rslt = bmi160_set_fifo_wm((uint8_t) (FIFO_LEN - 20), &s_spi_bmi160_sensor); | |
if (rslt != BMI160_OK) | |
{ | |
SEGGER_RTT_printf(0,"BMI160 failed FIFO wm setting"); | |
} | |
int_config.int_pin_settg.output_en = BMI160_ENABLE; // enable output via INT_1 and INT_2 on BMI160 | |
int_config.int_pin_settg.output_mode = BMI160_DISABLE; // push-pull mode | |
int_config.int_pin_settg.output_type = BMI160_ENABLE; // active high | |
int_config.int_pin_settg.edge_ctrl = BMI160_ENABLE; // edge-triggered | |
int_config.int_pin_settg.input_en = BMI160_DISABLE; // don't act as input | |
int_config.int_pin_settg.latch_dur = BMI160_LATCH_DUR_NONE; // non-latched output (no need to clear manually) | |
int_config.fifo_WTM_int_en = BMI160_ENABLE; // enable FIFO WM IRQ | |
rslt = bmi160_set_int_config(&int_config, &s_spi_bmi160_sensor); | |
if (rslt != BMI160_OK) | |
{ | |
SEGGER_RTT_printf(0,"BMI160 failed IRQ config,rslt =%d\n",rslt); | |
} | |
} // Spi_sensor_fifo_init() | |
//after trigger call get_bmi160_fifo_data | |
static void get_bmi160_fifo_data(void){ | |
//s_spi_bmi160_sensor.fifo->length=FIFO_SIZE; | |
int8_t res = BMI160_OK; | |
uint8_t acc_frames = ACCEL_BUF_LEN; | |
res = bmi160_get_fifo_data(&s_spi_bmi160_sensor); | |
if (res != BMI160_OK) | |
{ | |
SEGGER_RTT_printf(0,"BMI160 failed to get FIFO data,res=%x\n",res); | |
} | |
SEGGER_RTT_printf(0,"there are %d data in the fifo\n",s_spi_bmi160_sensor.fifo->length); | |
res = bmi160_extract_accel(s_AccData, &acc_frames, &s_spi_bmi160_sensor); | |
if (res != BMI160_OK) | |
{ | |
SEGGER_RTT_printf(0,"BMI160 failed to extract FIFO accel data\n"); | |
} | |
res = bmi160_extract_gyro(s_GyroData, &acc_frames, &s_spi_bmi160_sensor); | |
if (res != BMI160_OK) | |
{ | |
SEGGER_RTT_printf(0,"BMI160 failed to extract FIFO gyro data\n"); | |
} | |
// print the acc and gyro | |
for (uint8_t i = 0; i < acc_frames; ++i) | |
{ | |
SEGGER_RTT_printf(0,"s_AccDataX[%d]=%d, s_AccDataY[%d]=%d, s_AccDataZ[%d]=%d\n", i, s_AccData[i].x,i,s_AccData[i].y,i,s_AccData[i].z); | |
} | |
for (uint8_t i = 0; i < acc_frames; ++i) | |
{ | |
SEGGER_RTT_printf(0,"s_AccDataX[%d]=%d, s_AccDataY[%d]=%d, s_AccDataZ[%d]=%d\n", i, s_GyroData[i].x,i,s_GyroData[i].y,i,s_GyroData[i].z); | |
} | |
SEGGER_RTT_printf(0," skipped_frame_count= %d\n",fifo_frame.skipped_frame_count); | |
} | |
//spi read interface | |
int8_t Spi_read_transfer(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length) | |
{ | |
ret_code_t ret ; | |
uint8_t read_temp[ length + 1 ] ; | |
ret = nrf_drv_spi_transfer(&s_spi, ®_addr, 1, read_temp, length + 1 ) ; | |
nrf_delay_ms(5); | |
for( int i = 1 ; i < length + 1 ; i ++ ) | |
reg_data[i-1] = read_temp[i] ; | |
return (int8_t)ret; | |
} | |
//spi write interface | |
int8_t Spi_write_transfer(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length) | |
{ | |
ret_code_t ret; | |
uint8_t write_temp[ length + 1 ] ; | |
write_temp[0] = reg_addr ; | |
for( int i = 1 ; i < length + 1 ; i ++ ) | |
write_temp[i] = reg_data[i-1] ; | |
ret = nrf_drv_spi_transfer(&s_spi, write_temp, length + 1, &no_use, 1 ) ; | |
nrf_delay_ms(5) ; | |
return (int8_t)ret; | |
} | |
int main(void){ | |
bsp_board_init(BSP_INIT_LEDS); | |
APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); | |
NRF_LOG_DEFAULT_BACKENDS_INIT(); | |
//spi bmi160 fifo | |
Spi_pin_init(); | |
Spi_sensor_fifo_init(); | |
//gpio_config(); //wait for wm trigger | |
nrf_delay_ms(1000); | |
get_bmi160_fifo_data(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment