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

Questions about spi read and write function mapping

I'm having trouble driving the BMI088 with the official driver of Bosch BMI088 (link to this driver) in the ble_app_uart routine. I never get the chip ID. Bosch BMI088 official driver link address: https://github.com/BoschSensortec/BMI08x-Sensor-API . The spi read and write functions are encapsulated into the following two APIs: 

int8_t user_spi_write(uint8_t cs_pin, uint8_t reg_addr, uint8_t *data, uint16_t len);

int8_t user_spi_read(uint8_t cs_pin, uint8_t reg_addr, uint8_t *data, uint16_t len);

So How can I map these two APIs into Nordic operation functions? 

   

And The automatic selection between [CPOL = '0' and CPHA = '0'] and [CPOL = '1' and CPHA = '1'] is controlled based on the value of SCK after a falling edge of CSB (1 or 2). What SPI mode should I configure?

Parents
  • The read function shown seems to be doing a write since the LS bit is '0':

    int8_t user_spi_read(uint8_t cs_pin, uint8_t reg_addr, uint8_t *data, uint16_t len)
    {
        nrf_gpio_pin_clear(cs_pin);
        nrf_delay_us(1);
        if(len<=255)
        {
            uint8_t m_tx_buf[1] = {0};

    I would expect setting the LS bit to '1' would perform a read, but that is either MSB first or LS Bit first, not sure which for this part:

    int8_t user_spi_read(uint8_t cs_pin, uint8_t reg_addr, uint8_t *data, uint16_t len)
    {
        nrf_gpio_pin_clear(cs_pin);
        nrf_delay_us(1);
        if(len<=255)
        {
            uint8_t m_tx_buf[1] = {0x01}; // if MS Bit first
            uint8_t m_tx_buf[1] = {0x80}; // if LS Bit first

    It would help if you show both initialisation code (use SPI Mode 0) and detail pin connections as this part is not a simple SPI part (eg accelerometer has to be changed from I2C to SPI).

  • Sorry for the long wait.

    The read function should look like this:

    int8_t user_spi_read(uint8_t cs_pin, uint8_t reg_addr, uint8_t *data, uint16_t len)
    {
        nrfx_err_t ret_code = NRFX_SUCCESS;
        nrfx_spim_xfer_desc_t my_xfer_desc = NRFX_SPIM_XFER_RX(&data, (len + 1));
        
        uint8_t m_tx_buf[MAX_LENGTH];   // create buffer
        memset(&m_tx_buf, 0, sizeof(m_tx_buf)); //clear buffer
        
        m_tx_buf[0] = (1 << 0) | (reg_addr << 1);
        
        /* Configure the Chip Select pin, SPIM3 supports HW controlled CS pin*/
        NRF_SPIM3->PSEL.CSN = cs_pin;
        
        ret_code = nrfx_spim_xfer(&my_spim_instance, my_xfer_desc, flags);
        APP_ERROR_CHECK(ret_code);
        
        return;
    }


  • Although I have solved the problem myself, thank you for replying to me!

Reply Children
  • Hi june6,

    i got the same issue , i am trying to interface spi nor flash with nRF52840 but not able to read the device ID?

  • Hi,

    I use Bosch driver, there is no problem at all. Here is my SPI configuration:

    #define NRF_DRV_SPI_BMI088_DEFAULT_CONFIG                    \
    {                                                            \
        .sck_pin      = NRF_DRV_SPI_PIN_NOT_USED,                \
        .mosi_pin     = NRF_DRV_SPI_PIN_NOT_USED,                \
        .miso_pin     = NRF_DRV_SPI_PIN_NOT_USED,                \
        .ss_pin       = NRF_DRV_SPI_PIN_NOT_USED,                \
        .irq_priority = SPI_DEFAULT_CONFIG_IRQ_PRIORITY,         \
        .orc          = 0xFF,                                    \
        .frequency    = NRF_DRV_SPI_FREQ_8M,                     \
        .mode         = NRF_DRV_SPI_MODE_0,                      \
        .bit_order    = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,         \
    }
    

    #define SPI_INSTANCE_0                  0                                           /**< SPI instance index. */
    
    static const nrf_drv_spi_t spi_0_bmi088 = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE_0);     /**< SPI instance. */
    
    
    nrf_drv_spi_config_t spi_bmi088_config = NRF_DRV_SPI_BMI088_DEFAULT_CONFIG;
    
    nrf_gpio_cfg_output(MCU_GPIO_BMI088_CSB1);
    nrf_gpio_cfg_output(MCU_GPIO_BMI088_CSB2);
    nrf_gpio_pin_set(MCU_GPIO_BMI088_CSB1);
    nrf_gpio_pin_set(MCU_GPIO_BMI088_CSB2);
    
    spi_bmi088_config.ss_pin   = NRF_DRV_SPI_PIN_NOT_USED;
    spi_bmi088_config.miso_pin = SPI_BMI088_MISO_PIN;
    spi_bmi088_config.mosi_pin = SPI_BMI088_MOSI_PIN;
    spi_bmi088_config.sck_pin  = SPI_BMI088_SCK_PIN;
    APP_ERROR_CHECK(nrf_drv_spi_init(&spi_0_bmi088, &spi_bmi088_config, spi_bmi088_event_handler, NULL));
    

Related