How to clear UARTE buffer?

Hi everyone,

I use the UARTE to receive data, and I want to clear rx buffer every time, because If not when the buffer will be full, the next data I received will be split into two parts,

for example, the buffer is 32bytes, for now i received 30bytes, if i send 4bytes data next time, the data print like  that

# SEGGER J-Link RTT Viewer V7.00a Terminal Log File
# Compiled: 15:35:52 on Apr 16 2021
# Logging started @ 03 Mar 2023 09:44:31
00> buf.p_data[0] = 5A,len = 30
00> buf.p_data[1] = 5A,len = 30
00> buf.p_data[2] = 0,len = 30
00> buf.p_data[3] = 1,len = 30
00> buf.p_data[4] = 1,len = 30
00> buf.p_data[5] = 5A,len = 30
00> buf.p_data[6] = 5A,len = 30
00> buf.p_data[7] = 0,len = 30
00> buf.p_data[8] = 1,len = 30
00> buf.p_data[9] = 1,len = 30
00> buf.p_data[10] = 5A,len = 30
00> buf.p_data[11] = 5A,len = 30
00> buf.p_data[12] = 0,len = 30
00> buf.p_data[13] = 1,len = 30
00> buf.p_data[14] = 1,len = 30
00> buf.p_data[15] = 5A,len = 30
00> buf.p_data[16] = 5A,len = 30
00> buf.p_data[17] = 0,len = 30
00> buf.p_data[18] = 1,len = 30
00> buf.p_data[19] = 1,len = 30
00> buf.p_data[20] = 5A,len = 30
00> buf.p_data[21] = 5A,len = 30
00> buf.p_data[22] = 0,len = 30
00> buf.p_data[23] = 1,len = 30
00> buf.p_data[24] = 1,len = 30
00> buf.p_data[25] = 5A,len = 30
00> buf.p_data[26] = 5A,len = 30
00> buf.p_data[27] = 0,len = 30
00> buf.p_data[28] = 1,len = 30
00> buf.p_data[29] = 1,len = 30
00> buf.p_data[0] = 5A,len = 2
00> buf.p_data[1] = 5A,len = 2
00> buf.p_data[0] = 0,len = 2
00> buf.p_data[1] = 1,len = 2

# Logging stopped @ 03 Mar 2023 09:44:58

The rx buff is 32bytes, first time I send 30bytes from uart, in rtt log, you can see I received data length = 30, and then I send 4 bytes, the rtt log show that two part of it, a part is 5A 5A length = 2,

another is 0 1 length = 2.

I think the question is the rx buffer is full, but I use the nrf_libuarte_async_rx_free(p_libearte, p-evt->data.rxtx.p_data, p_evt->data.rxtx.length); to clear the buffer.

In this function I found that only when the buffer was full it will be cleared.

So, how should clear the buffer every time?

Best regards, 

Lurn

Parents
  • Here's what's happening:

    I initialised 3 buffers, each of 32 bytes wide.

    I use another system sends a 30 bytes data.

    Everything works as expected. I get an event after 30 bytes.

    After that, I again get an event from the other system, as expected, because one buffer is filled. And then the buffer fills from start.

    What I want is this:

    After I process a response from the other device, I want to reset the write location of Rx buffer so that it starts from beginning.

  • Ok,I used the method of judging the data header to solve the data split, and test OK.

    {
            if (!nrf_queue_is_empty(&m_buf_queue))
            {
                buffer_t buf;
                err_code = nrf_queue_pop(&m_buf_queue, &buf);
                APP_ERROR_CHECK(err_code);
                err_code = nrf_libuarte_async_tx(&libuarte, buf.p_data, buf.length);
                APP_ERROR_CHECK(err_code);
                if(buf.p_data[0] == 0x5A && (buf.p_data[1] == 0xC4 || buf.p_data[1] == 0xC5))
                {
                    memset(data_array, 0, sizeof(data_array));
                    index = 0;
                    memcpy(&data_array[index], buf.p_data, buf.length);
                    index = buf.length;
                }
                else
                {
                    memcpy(&data_array[index], buf.p_data, buf.length);
                    index += buf.length;
                }
            }
    }

Reply
  • Ok,I used the method of judging the data header to solve the data split, and test OK.

    {
            if (!nrf_queue_is_empty(&m_buf_queue))
            {
                buffer_t buf;
                err_code = nrf_queue_pop(&m_buf_queue, &buf);
                APP_ERROR_CHECK(err_code);
                err_code = nrf_libuarte_async_tx(&libuarte, buf.p_data, buf.length);
                APP_ERROR_CHECK(err_code);
                if(buf.p_data[0] == 0x5A && (buf.p_data[1] == 0xC4 || buf.p_data[1] == 0xC5))
                {
                    memset(data_array, 0, sizeof(data_array));
                    index = 0;
                    memcpy(&data_array[index], buf.p_data, buf.length);
                    index = buf.length;
                }
                else
                {
                    memcpy(&data_array[index], buf.p_data, buf.length);
                    index += buf.length;
                }
            }
    }

Children
No Data
Related