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

nrf_serial_write timeout_ms parameter

Hello,

In the documentation for nrf_serial_write function (SDK 14.0.0) it is written:

timeout_ms Operation timeout, in milliseconds. Pass 0 to operate in non blocking mode.

As I always got NRF_ERROR_TIMEOUT as a return code of this function I inspected the actual code in components\libraries\serial\nrf_serial.c as found out that timeout_ms value is always, and only, compared with NRF_SERIAL_MAX_TIMEOUT (= UINT32_MAX), not with 0.

So, in my case, the docs shall say

timeout_ms Operation timeout, in milliseconds. Pass NRF_SERIAL_MAX_TIMEOUT to operate in non blocking mode.

Can this be confirmed or is there something I am doing incorrectly?

Parents
  • Hi,

    If you use timeout_ms = 0, you will in timeout_setup() never create the timer, and p_tout_ctx->expired is then set to true (timer would have expired immediately). The do-while loop will then only execute once when using timeout_ms = 0.

    If you are using timeout_ms = NRF_SERIAL_MAX_TIMEOUT, you will stay in the do-while loop until all the data is sent(blocking). The timeout-timer is not created here either, but in this case the p_tout_ctx->expired will always be false, and the only way to exit the do-while is when you have sent all the data.When all data is sent with serial_tx(), the variable left is set to 0, and you break out of the do-while-loop.

    So with NRF_SERIAL_MAX_TIMEOUT you are operating in blocking mode.

    You will get the error-code NRF_ERROR_TIMEOUT if have p_tout_ctx->expired=true, and you did not manage to send all the data with the serial_tx().

    The do-while loop looks like this:

    do
    {
        size_t wcnt = serial_tx(p_serial, p_buff, left);
        left -= wcnt;
        p_buff += wcnt;
        if (!left)
        {
            break;
        }
    
        sleep_handler(p_serial);
    } while (!tout_ctx.expired);
    
Reply
  • Hi,

    If you use timeout_ms = 0, you will in timeout_setup() never create the timer, and p_tout_ctx->expired is then set to true (timer would have expired immediately). The do-while loop will then only execute once when using timeout_ms = 0.

    If you are using timeout_ms = NRF_SERIAL_MAX_TIMEOUT, you will stay in the do-while loop until all the data is sent(blocking). The timeout-timer is not created here either, but in this case the p_tout_ctx->expired will always be false, and the only way to exit the do-while is when you have sent all the data.When all data is sent with serial_tx(), the variable left is set to 0, and you break out of the do-while-loop.

    So with NRF_SERIAL_MAX_TIMEOUT you are operating in blocking mode.

    You will get the error-code NRF_ERROR_TIMEOUT if have p_tout_ctx->expired=true, and you did not manage to send all the data with the serial_tx().

    The do-while loop looks like this:

    do
    {
        size_t wcnt = serial_tx(p_serial, p_buff, left);
        left -= wcnt;
        p_buff += wcnt;
        if (!left)
        {
            break;
        }
    
        sleep_handler(p_serial);
    } while (!tout_ctx.expired);
    
Children
Related