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

Uart event handler problems

Let's first compare two pieces code:

This one works:

static void serial_rx_cb(struct nrf_serial_s const *p_serial, nrf_serial_event_t event) {
    if (event == NRF_SERIAL_EVENT_RX_DATA) {
        char c;
        APP_ERROR_CHECK(nrf_serial_read(&serial_uart, &c, sizeof(c), NULL, 500));
        APP_ERROR_CHECK(nrf_serial_write(&serial_uart, &c, sizeof(c), NULL, 1000));
    }
}

But this one doesn't:

static char buff[255]={};
static int n=0;

static void serial_rx_cb(struct nrf_serial_s const *p_serial, nrf_serial_event_t event) {
    if (event == NRF_SERIAL_EVENT_RX_DATA) {
        char c;
        APP_ERROR_CHECK(nrf_serial_read(&serial_uart, &c, sizeof(c), NULL, 500));
        buff[n++]=c;
        if(n<MSG_LEN)
            return;
        APP_ERROR_CHECK(nrf_serial_write(&serial_uart, buff, n*sizeof(c), NULL, 1000));
        n=0;
    }
}

My problem is that as soon as I try to process received data, some of data is lost. 
Is this a concurrency issue? It seems to me that the problem occurs if too much time is spent by the callback?  Or is it something else going on?
Please give me some advice, how to solve this issue?

[NRF SDK 15.2, Mesh SDK 2.2, SD140 6.1]

Parents Reply
  • Yes, that does make sense, the event handler is basically an interrupt, so when you spend a lot of time in the event handler, you will get a buffer overrun and loose some bytes. The best way to approach this type of code is to only copy the bytes into the buffer in the event handler and then process the buffer outside of the interrupt using the app scheduler. 

Children
No Data
Related