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
  • My approach is as follows:

    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};
            uint8_t m_tx_length = 1;
            uint8_t m_rx_length = (uint8_t)len;
            m_tx_buf[0] = reg_addr;
    
            // Reset rx buffer and transfer done flag
            memset(m_rx_buf, 0, m_rx_length);
            spi_xfer_done = false;
    
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_tx_length, m_rx_buf, m_rx_length));
            while (!spi_xfer_done)
            {
                __WFE();
            }
            memcpy_fast(data, m_rx_buf, m_rx_length);
        }
        else
        {
            NRF_LOG_DEBUG("Spi read over length.");
        }
        nrf_delay_us(1);
        nrf_gpio_pin_set(cs_pin);
    }
    
    
    int8_t user_spi_write(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[255] = {0};
            uint8_t m_length = (uint8_t)len;
            m_tx_buf[0] = reg_addr;
            memcpy(&m_tx_buf[1], data, strlen(data));
      
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
        }
        else
        {
            NRF_LOG_DEBUG("Spi write over length.");
        }
        nrf_delay_us(1);
        nrf_gpio_pin_set(cs_pin);
    }

Reply
  • My approach is as follows:

    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};
            uint8_t m_tx_length = 1;
            uint8_t m_rx_length = (uint8_t)len;
            m_tx_buf[0] = reg_addr;
    
            // Reset rx buffer and transfer done flag
            memset(m_rx_buf, 0, m_rx_length);
            spi_xfer_done = false;
    
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_tx_length, m_rx_buf, m_rx_length));
            while (!spi_xfer_done)
            {
                __WFE();
            }
            memcpy_fast(data, m_rx_buf, m_rx_length);
        }
        else
        {
            NRF_LOG_DEBUG("Spi read over length.");
        }
        nrf_delay_us(1);
        nrf_gpio_pin_set(cs_pin);
    }
    
    
    int8_t user_spi_write(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[255] = {0};
            uint8_t m_length = (uint8_t)len;
            m_tx_buf[0] = reg_addr;
            memcpy(&m_tx_buf[1], data, strlen(data));
      
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
        }
        else
        {
            NRF_LOG_DEBUG("Spi write over length.");
        }
        nrf_delay_us(1);
        nrf_gpio_pin_set(cs_pin);
    }

Children
Related