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

Difference between nrf_spim and spi examples in peripheral folder and purpose of __WFEwhile (!spi_xfer_done) { __WFE(); }

I am basically wondering what the difference between these two examples in the SDK are?

Also, I see in both cases after the spi_transfer is called there is the following piece of code

while (!spi_xfer_done)
{
        __WFE();
}

I am just wondering what the purpose of it is and if it is necessary? It seems when I try to make multiple calls without including it between calls it does not work correctly.

Thanks

  • Hi Michael

    The SPI examples are made for the older SPI master peripheral that didn't support DMA, which was present in the nRF51 and nRF52 series. 

    The SPIM module is the SPI master with DMA support, which was added in the nRF52 series. 

    For the nRF52 I would recommend sticking to the SPIM examples, to leverage the new and improved hardware. 

    The while (!spi_xfer_done) {  __WFE(); } is essentially a way to make a non-blocking driver blocking, by entering into a sleep loop as long as the SPI transfer is ongoing. The call to __WFE() puts the CPU in system ON sleep mode, to avoid wasting energy by running in a loop. 

    Still, if you plan to do a lot of SPI communication I would suggest going for a more event driven approach where you use the SPI callback to schedule new transactions, rather than waiting for the SPI transaction to finish in a loop. 

    Best regards
    Torbjørn

Related