Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to get data from sensor ( bmi160 ) using spi?

Hello !

I am trying to get data from sensor ( chip_id of bmi160 ), but i always get 0 .

Could someone tell me how to do?

I follow these steps :

1. I Declared SPI instance and bmi160 sensor struct like this : 

Fullscreen
1
2
static nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE( 0 ); // SPI instance. //spi
struct bmi160_dev sensor ; // bmi160 sensor struct // bmi160
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2. Declare delay & read & write functions like this : 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void user_delay_ms(uint32_t period) {
// delay time
//
nrf_delay_ms( period ) ;
} // user_delay_ms()
int8_t spi1_read_transfer(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length){
// read data from sensor
//
ret_code_t ret;
nrf_drv_gpiote_out_clear(SPI_SS_PIN); /* To activate the sensor on the bus, i.e., set the chip/slave select low */
ret = nrf_drv_spi_transfer(&spi, &reg_addr, 1, NULL, 0);
if(ret == NRF_SUCCESS)
ret = nrf_drv_spi_transfer(&spi, NULL, 0, reg_data, length);
nrf_gpio_pin_set(SPI_SS_PIN); /* To deactivate the sensor on the bus, i.e., set the chip/slave select high*/
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

3. init spi_config and bmi160 sensor and print result in main like this :

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(void){
bsp_board_init(BSP_INIT_LEDS);
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
// init spi //
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin = SPI_SS_PIN;
spi_config.miso_pin = SPI_MISO_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin = SPI_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
// init bmi160 sensor //
sensor.id = 1 ;
sensor.interface = BMI160_SPI_INTF;
sensor.read = spi1_read_transfer;
sensor.write = spi1_write_transfer;
sensor.delay_ms = user_delay_ms;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

4.result : 

both of rslt and data are 0 .

I have referenced two websites : 

1. BMI160 sensor API Introduction 

2.How to initializate SPI user interface?

Thanks for help!