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 : 

static nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE( 0 );  // SPI instance. //宣告spi例子
struct bmi160_dev sensor ; // bmi160 sensor struct // 宣告bmi160感應器結構

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

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*/
	
	return (int8_t)ret;	
} // spi1_read_transfer()

int8_t spi1_write_transfer(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length){
	// write data to 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, reg_data, length, NULL, 0);

	nrf_gpio_pin_set(SPI_SS_PIN); /* To deactivate the sensor on the bus, i.e., set the chip/slave select high*/
	
	return (int8_t)ret;	
} // spi1_write_transfer()

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

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;

	int8_t rslt = BMI160_OK;
    uint8_t reg_addr = BMI160_CHIP_ID_ADDR; // chip_id address // chip_id暫存器位置
    uint8_t data;
    uint16_t len = 1;
    rslt = bmi160_get_regs(reg_addr, &data, len, &sensor); // read sensor chip_id // 讀取chip_id資料

    // print result // 輸出結果
		SEGGER_RTT_printf( 0, "rslt = %d \n", rslt ) ; 
		SEGGER_RTT_printf( 0, "data = %d \n", data ) ; 
		
} // main()

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!

Related