Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Blocking UART with timeout using soft timers.

Hi,

I am trying to use UART APIs in blocking mode to receive data from another controller. 

I intend to have a timeout on receiving function so not to stuck if no byte is received from the other controller. Here is the snippet of UART receive function:

//soft timer timeout handler
static void uart_timer_handler(void * p_context)
{
    uart_to = 1;
}

//uart rx function with timeout
//parameter in  - uart input data expected
//parameter len - length of the input data in bytes.

uint8_t uart_read_nrf(char *in, uint8_t len)
{
    uint16_t index = 0;
    
    //while the expected data is not received.
    while((strncmp(data_array, in, len) != 0))
    {
        switch(uart_to)         //timeout flag from soft timer
        {
            case 1:             //if time out flag is set by soft timer event
                uart_to = 0;
                nrf_drv_uart_rx_disable(&app_uart_inst);
                return 1;
            break;

            case 0:             //continue receiving if timeout have not occurred yet
                nrf_drv_uart_rx(&app_uart_inst, &data_array[index], 1);
                index++;

            break;
        }
    }
    
    nrf_drv_uart_rx_disable(&app_uart_inst);

    return 0;
}

And in main function I am starting the soft timer with 2 sec of timeout like this:

uart_to = 0;
app_timer_start(UART_TIMER, APP_TIMER_TICKS(2000), NULL);
uart_read_nrf("ON", 2);

I am intentionally passing the wrong data "ON" instead of "OK", just to check the time out functionality of the code. But the code get stuck in the following do while loop of nrfx_uarte_rx() function from nrfx_uarte.c 

do {
            endrx  = nrf_uarte_event_check(p_instance->p_reg, NRF_UARTE_EVENT_ENDRX);
            rxto   = nrf_uarte_event_check(p_instance->p_reg, NRF_UARTE_EVENT_RXTO);
            error  = nrf_uarte_event_check(p_instance->p_reg, NRF_UARTE_EVENT_ERROR);
        } while ((!endrx) && (!rxto) && (!error));

I am using SDK version 15.0.0 and SEGGER Embedded studio for Arm V5.32a with S140 Softdevice. My target is nRF52840 rev2.

Thanking in advance.

Regards,

Shivek

Parents
  • Hi,

    How is data_array[] defined?

    Like this?:

    static uint8_t data_array[1]
    But the code get stuck in the following do while loop of nrfx_uarte_rx() function from nrfx_uarte.c 

    Here the program waits for either:

    1. The buffer to fill up which should produce the ENDRX event.
    2. The receiver is stopped which produces the RXTO event.
    3. An error appears, which produces the Error event.

    It seems that your buffer isn't filling up properly. Are you using HWFC? Have you tried probing the data lines and ensured that the data is sent correctly?

    regards

    Jared 

Reply
  • Hi,

    How is data_array[] defined?

    Like this?:

    static uint8_t data_array[1]
    But the code get stuck in the following do while loop of nrfx_uarte_rx() function from nrfx_uarte.c 

    Here the program waits for either:

    1. The buffer to fill up which should produce the ENDRX event.
    2. The receiver is stopped which produces the RXTO event.
    3. An error appears, which produces the Error event.

    It seems that your buffer isn't filling up properly. Are you using HWFC? Have you tried probing the data lines and ensured that the data is sent correctly?

    regards

    Jared 

Children
Related