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

SPI master and DMA

Using softdevice s132, nRF52832 on a custom board

I don't quite understand how does the SPI work exactly?

I am using it on a battery powered low power sensor application, for this instance I have not set up a SPI event handler, but DMA is enabled.

I am calling nrf_drv_spi_transfer in a function, where I return the recieved data to the function that called it.

The SPI is running at 1mhz - so what is the CPU doing while waiting for the SPI slave to clock in the bytes?

Do I risk that I am working on memory that is not updated from the most recent info from the SPI peripheral device, or is the CPU waiting for the transfer to complete?

Should I set up the SPI event handler and instead signal my app to check the data after the event has indicated SPI transfer event is done - intuitively it seems like a better real-time/low power solution but this may already be sorted and abstracted away in the driver?

  • Any particular reason why you don't use SPI Master driver from nRF5 SKD? It shouldn't have any flaws for battery powered app... also formulation "what is the CPU doing while waiting for the SPI slave to clock in the bytes" doesn't make sense because SPI Master is not waiting for anything normally, it must driver CLK signal so slave can output data on MISO line. If you have another (usually called "interrupt") line from Slave to Master which signals that now Slave want's to talk (so Master must provide CLK and listen on MISO) then simply put the MCU to POWER ON SLEEP and wait for GPIOTE interrupt...)

  • I am using the SPI Master driver from the NRF5 SDK (nrf_drv_spi.c?) So if I understand you right the CPU is busy clocking out the bytes on the SPI? I thought the point of DMA was to let the CPU sleep or do something else instead of driving the SPI bus on a much slower frequency?

  • Hi,

    If you don't use an event handler the SPI driver is waiting in a while() loop until the transfer is completed and you get an SPI transfer END event.

    In SDK 14 this happens at line 575 in nrf_drv_spi.c:

    if (!p_cb->handler)
    {
        while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_END)){}
        if (p_cb->ss_pin != NRF_DRV_SPI_PIN_NOT_USED)
        {
            nrf_gpio_pin_set(p_cb->ss_pin);
        }
    }
    

    So yes, the CPU is waiting for the transfer to complete while wasting time and current. As you say, the point of DMA is to let the CPU sleep or do other things, but you are not actually using DMA if you don't use the SPIM as explained in the Advanced Usage section of the SPI driver documentation.

  • So i need to set up PPI then? My app is built on ble nus peripheral example. Is there any tutorials to set up ppi for beginners?

  • You only need it if you want to do multiple transfers triggered on some sort of event and don't want to use the CPU to set up each transfer. Such events can be generated by timers, external signals, etc. If you only need one transfer at the time I suggest that you just enable the event handler and let the CPU sleep until the event handler wakes it up after starting a transfer.

    We don't have any good examples in the SDK, but I posted an example in the comment section here that might be useful.

Related