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

Read data from SPI is not added to the buffer

Hey guys!

I am using your spi-master driver from the SDK 11 with the nRF52 chip. I am trying to read data from a MPU-9250 chip, and through my logic analysis, it seems like everything works well. My problem is that the data on the MISO-line isn't always added to my receive buffer. (But a few rare times, it is!)

I'm at a loss here, maybe I have done something wrong with my configuration?

Here is my code for activating the spi-protocol:

void spi_init(void){
  
  uint32_t err_code;

	nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG(SPI_INSTANCE);

spi_config.ss_pin = SPI_CS_PIN;
	spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
	spi_config.mode = NRF_DRV_SPI_MODE_0;
	spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;

err_code = nrf_drv_spi_init(&spi, &spi_config, spi_event_handler); }

Here is my function for reading from a given register:

void spidev_readBytes(uint8_t* regAddr, uint8_t length, uint8_t* rx_buffer){

	regAddr[0] = regAddr[0] + 0x80;

	nrf_drv_spi_transfer(&spi, regAddr, length, rx_buffer, length); }

Here is my driver configuration:

#define SPI1_ENABLED 1

#if (SPI1_ENABLED == 1)
#define SPI1_USE_EASY_DMA 0

#define SPI1_CONFIG_SCK_PIN         10
#define SPI1_CONFIG_MOSI_PIN        9
#define SPI1_CONFIG_MISO_PIN        8
#define SPI1_CONFIG_IRQ_PRIORITY    APP_IRQ_PRIORITY_HIGH

I might add that I have also configured pin 7, 8, 9 and 10 to be used as GPIO.

Lastly, a sample of my logic analysis:

Logic analysis

The issue is that the value 0x71 is very, very rarely added to the rx_buffer.

  • I fixed it myself!

    I initialized the SPI-driver with an event handler, but the event handler didn't do anything. By removing the event handler, the transfer function returns when it is finished.

    I found the following in the info center:

    If an event handler was provided in the nrf_drv_spi_init call, this function returns immediately and the handler is called when the transfer is done. Otherwise, the transfer is performed in blocking mode, which means that this function returns when the transfer is finished.

    I should have read it more thoroughly! Anyhow, thanks!

Related