This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SPI/I2C Step on nRF52833 to read BMI160 outputs

Hello,

I am trying to read BMI160 outputs using SPI protocol but I am struggling to get a successful build from my code. 

The build fails at the first line of my code //static nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE( 0 ); // SPI instance// 

The complier doesn't recognise NRF_DRV_SPI_INSTANCE even though it is in the libraries into nrf_drv_spi library. 

I don't know what I am doing wrong? Could I get some help please?

Please find below my code. 

Thanks a lot.

  • The complier doesn't recognise NRF_DRV_SPI_INSTANCE even though it is in the libraries into nrf_drv_spi library

    Have you made the necessary settings in sdk_config.h ?

  • #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include "nrf.h"
    #include "nrf_drv_saadc.h"
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_timer.h"
    #include "nrf_drv_twi.h"
    #include "nrf_drv_spi.h"
    #include "nrf_drv_spis.h"
    #include "boards.h"
    #include "app_error.h"
    #include "sdk_config.h"
    #include "nrf_delay.h"
    #include "app_util_platform.h"
    #include "nrf_pwr_mgmt.h"
    #include <string.h>
    #include "nrf_gpio.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "bmi160.h"
    
    
    #define SPI_SS_PIN 28 
    #define SPI_MISO_PIN 12 
    #define SPI_MOSI_PIN 25
    #define SPI_SCK_PIN 29
    
    static nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE( 0 );  // SPI instance
    struct bmi160_dev sensor ; // bmi160 sensor struct 
    
    
    void user_delay_ms(uint32_t period) { 
    	// delay time
    	
    	
      nrf_delay_ms( period ) ;
    } // user_delay_ms()
    
    static uint8_t       no_use = 0xFF ;
    
    int8_t spi1_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(&spi, &reg_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;	
    }
    
    int8_t spi1_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(&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();
    
      	// 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 = 0 ; 
            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 // 
    
        // print result // 
    		SEGGER_RTT_printf( 0, "rslt = %d \n", rslt ) ; 
    		SEGGER_RTT_printf( 0, "data = %d \n", data ) ; 
    		
    } 
    
    
    
    
    
    

  • Thank you Awneil.

    Sorry for the code post. I repost it below.

    Could you tell me please what settings I need to made into sdk_config.h ?

    Thank you very much.

  • not used an nRF52833, so don't know.

    suggest you start with an SDK example which has working SPI ...

  • So you want to use the SPI 0 instance. Please make sure these are set in sdk_config.h:

    #define SPI_ENABLED 1
    #define SPI0_ENABLED 1
    #define SPI0_USE_EASY_DMA 1

    These are the same settings that are set in the spi example from SDK\examples\peripheral\spi

    Best regards,

    Edvin

Related