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

SPIM ERROR:17 [NRF_ERROR_BUSY]

Hello,

I have merged nrfx_spim and ble_app_uart. I was testing the spi tx function as my application has specific timing requirements. I have experienced something that I think is weird. I want to share it and have some expert opinions and also let others benefit from it too.

I did not change the spim_event_handler function at all. 

void spim_event_handler(nrfx_spim_evt_t const * p_event,
                       void *                  p_context)
{
    spi_xfer_done = true;
    NRF_LOG_INFO("Transfer completed.");
    if (m_rx_buf[0] != 0)
    {
        NRF_LOG_INFO(" Received:");
        NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
    }
}

I am not using dcx pin therefore I changed the tx function a little. Also, I am sending only 1 byte and not receiving anything. 

void spi_send_command(uint8_t cmd)
{
    m_tx_buf[0] = cmd
    APP_ERROR_CHECK(nrfx_spim_xfer(&spi, &xfer_desc, 0));

        while (!spi_xfer_done)
        {
            __WFE();
        }
}

And in the main I call this function in while(1)

while(1)
{
    spi_send_command(myCmd);
}

If I set a break point in the while loop where I call the tx function or give a delay of 10us after each tx function call, it works fine.  It can be called again and again without any problems. If I let it run freely without any delays, it gives NRF_ERROR_BUSY. The interesting part is that the line " while(!spi_xfer_done) "  is used to avoid such an error, which is caused by an transfer attempt while spim module is still busy sending data. I set a break point in the handler function to see if it is called at all, and if I am not wrong, it is called however not at the first tx function call but at the second call. 

The solution to this problem, as I have found in some discussions, is to use the following line in spi_send_command(). 

while(nrfx_spim_xfer(&spi, &xfer_desc, 0) == NRF_ERROR_BUSY);

 This works fine, as expected. However can someone please explain me what is wrong with the spim_event_handler() - so called the callback function. Why does the line 

"while (!spi_xfer_done)"  NOT save me?

If you want to have a look at the related posts;

https://devzone.nordicsemi.com/f/nordic-q-a/23120/ble-and-spi-in-nrf52

https://devzone.nordicsemi.com/f/nordic-q-a/46960/spi-communication-problem-with-interfaced-sensor-getting-assertion-failed/185961#185961

Parents Reply Children
Related