Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Serial timeout issues with nrf_serial_read() and nrf_serial_write() with nrf_serial_mode_irq.

Hi all,

I'm facing an issue communicating with a UART-based sensor on the NRF52DK (52832).

I've followed the serial_port example and using various forum topics I've got to the point where I can exchange and read data from the sensor using the serial_port methods in IRQ mode. There are 2 issues that come up:

1. If I put "NULL" for the "timeout in ms" parameter in nrf_serial_read() , it seems that the sensor is not able to respond fast enough for my program to receive the transmission and thus I get the NRF_ERROR_TIMEOUT. When NRF_ERROR_TIMEOUT is returned, I  If I do increase the timeout parameter to, say, 1000 ms, the data transmission happens as expected. The problem then, however, is that eventually this might lead to a deadlock. What is the work around to this?

2. How can I decide the size of the read data, if I don't know it until I see what the sensor returns? For example, depending whether the sensor received a command successfully it can return an ACK or NACK with an error code and the size of those transmissions is different. If I put in a larger size of the expected transmission than is actually transmitted, I run into the NRF_ERROR_TIMEOUT again. It seems like there is no way to do that without handling this within the RX event handler, is that correct? What is the best way to handle it?

3. This is not an issue, but it might become later. I'm not completely clear on whether the queues get automatically cleared or not after the RX/TX is done. If not, how can I clear them for RX/TX?

Thanks in advance!

Parents
  • I requested to convert the ticket to public space, you need to accept it before it is converted.

    1. This section refers to calling the APIs from interrupt context of other peripherals, it does not refer to the IRQ mode used in the nrf_serial library. I'm not sure how the library implements support for timeout=0. To me, it looks like this will generate a TIMEOUT error immediately, but I can have overlooked something in the code.

    2. I would recommend that you filter this error before passing the error code to APP_ERROR_CHECK() macro, for instance like this:

    uint32_t read_bytes = 0;
    ret_code_t err_code = nrf_serial_read(&serial_uart, &c, sizeof(c), &read_bytes, 1000);
    
    if(err_code == NRF_ERROR_TIMEOUT)
    {
        if(read_bytes > 0)
        {
            //Process received data
        }
        else
        {
            //Try read again
        }
    }
    else
    {
        APP_ERROR_CHECK(err_code);
    }

Reply Children
No Data
Related