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

What are memset() and nrf_driv_spis_buffers_set() doing in the spi_slave example?

I have two NRF52840 dev kits that are running what are essentially the spi master and spi slave examples. They both work and I've altered them slightly. I'm just kind of confused by this block of code in the spi slave example:


    while (1)
    {
        if (spis_xfer_done)
        {
        NRF_LOG_FLUSH();
        bsp_board_led_invert(BSP_BOARD_LED_1);
        memset(spi_rx_buf, 0, spi_length);
        spis_xfer_done = false;
        APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&spis, spi_tx_buf, spi_length, spi_rx_buf, spi_length));
        }
    }

Why do I need to reset the rx_buf after each transaction? Why can't I have the master keep shifting in data without the slave running memset() and nrf_driv_spis_buffers_set()?

Also just to confirm my understanding of how this works,  when a spi transaction comes in, CS goes low, data gets shifted into some buffer in memory. Then CS goes high and an interrupt is triggered (in the slave). When this interrupt is triggered, a handler will read out the data and do whatever I tell it to do with it. Is this using the easyDMA?

Sorry if these are dumb questions, I appreciate all/any help someone can give me.

  • So I was hoping to have all of the i/o handling organized neatly in the interrupts so that I wouldn't need to worry about timing issues while taking SPI packets and sending them over USB. Since I hope to build this into a wireless link (Peripheral<-SPI-> Dev kit <-BLE-> Dev kit <-USB-> host PC), I figured this interrupt-based organization would make my life a little easier. Unfortunately, it seems that may be a bad idea and that I'll have to leave nrf_drv_spis_buffers_set in the main inside the if(spi_transfer_done) scope.

    Furthermore, I would advice you to use the __WFE() wait-for-event function, rather than wasting the full cycles in the while(1) loop. Using __WFE will give you considerably lower power consumption.

    I didn't realize that! That will be very helpful for the future. Thank you!

Related