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

NRF5340, ZEPHYR - SPI_READ ACTING ASYNCHRONOUSLY?

Hello!

I'm working on a project with the new nRF5340 & Zephyr and I'm trying to get two SPI peripherals on the same bus sampling data correctly.

It's a simple program thus far... I've been able to configure both peripherals successfully, writing to their registers and sending commands using the SPI bus. I've initialized a single GPIO interrupt with an associated callback, which seems is functioning correctly. 

Within that callback, I have two consecutive spi_read()'s using the same structures, configuration, etc. that are working correctly to configure the device using spi_writes()'s. Here is the callback and the relevant read function:

void adsDataReadyIsr(struct device *port, struct gpio_callback *cb, u32_t pins)
{
    u8_t read_buffer[BYTES_PER_CONVERSION] = {0};
    int rc;

    //gpio_pin_write(ads_1.gpio_cs->port, ads_1.gpio_cs->pin, 0);
    rc = adsReadData(&ads_1, read_buffer);
    //gpio_pin_write(ads_1.gpio_cs->port, ads_1.gpio_cs->pin, 1);


    //gpio_pin_write(ads_2.gpio_cs->port, ads_2.gpio_cs->pin, 0);
    rc = adsReadData(&ads_2, read_buffer);
    //gpio_pin_write(ads_2.gpio_cs->port, ads_2.gpio_cs->pin, 1);
}

int adsReadData(struct AdsProxy *ads, u8_t *data)
{
    struct spi_buf bufs[] = {
        {.buf = data, .len = BYTES_PER_CONVERSION}
    };
    struct spi_buf_set rx = {
        .buffers = bufs,
        .count = ARRAY_SIZE(bufs)
    };

    return spi_read(ads_spi.device, &ads->spi_config, &rx);
}

The problem that I'm running into is that the second spi_read(), which is meant to read data from the second peripheral, gets initiated ~40-50 microseconds into the execution of the first and results in an error returned because the bus is already in use. The chip select for the first peripheral also does not get released properly.

If I remove the second spi_read()... everything functions correctly.

I've also tried manually toggling the chip selects myself and disabling the automatic chip selection (see the commented code) and it results in the same problem, the first spi_read() exits early and cs1 is released and the second is pull low prior to the first spi_read() finishing.



It is my understanding that spi_read() is supposed to be synchronous, but it seems as though it is actually acting asynchronously.

https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/peripherals/spi.html#_CPPv49spi_writeP6devicePK10spi_configPK11spi_buf_set

Is my understanding incorrect?

Any thoughts?

Thanks!

Related