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

UARTE Clear Rx Buffer

Hello, there,

I am trying to get the UARTE working on a custom nRF52840 board. In my setup, I am expecting a response to every transmission on UART.

Here's my setup code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NRF_LIBUARTE_ASYNC_DEFINE(UARTE0, 0, 0, 0, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 255, 3);
NRF_QUEUE_DEF(UARTERxBuffer, UARTE0RxBufferQueue, 4, NRF_QUEUE_MODE_NO_OVERFLOW);
//Initialising UARTE module
nrf_libuarte_async_config_t nrf_libuarte_async_config = {
.tx_pin = JETFILE2_UART_TX_PIN,
.rx_pin = JETFILE2_UART_RX_PIN,
.baudrate = NRF_UARTE_BAUDRATE_115200,
.parity = NRF_UARTE_PARITY_EXCLUDED,
.hwfc = NRF_UARTE_HWFC_DISABLED,
.timeout_us = 100,
.int_prio = APP_IRQ_PRIORITY_LOW,
};
ret_code_t err_code = nrf_libuarte_async_init(&UARTE0, &nrf_libuarte_async_config, UARTE0EventHandler, (void *)&UARTE0);
APP_ERROR_CHECK(err_code);
nrf_libuarte_async_enable(&UARTE0);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Here's my event handler:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* Function: UARTE0EventHandler
* Params: (Please refer to nRF documentation)
* returns: None
* Description: Handle for all UARTE events.
*/
void UARTE0EventHandler(void *context, nrf_libuarte_async_evt_t *p_evt) {
nrf_libuarte_async_t *p_libuarte = (nrf_libuarte_async_t *)context;
ret_code_t ret;
switch (p_evt->type) {
case NRF_LIBUARTE_ASYNC_EVT_ERROR:
break;
case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
//Data received on UART. If a queue is available, "push" the Rx data into that queue.
if (nrf_queue_available_get(&UARTE0RxBufferQueue) > 0) {
UARTERxBuffer pushBuffer;
pushBuffer.length = p_evt->data.rxtx.length;
pushBuffer.p_data = p_evt->data.rxtx.p_data;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Here's my application code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void coreApplication(void) {
char requestVersion[20] = {0x55, 0xA7, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x03, 0x01, 0x00, 0x00};
UARTE0TransmitData(requestVersion, 16);
while (1) {
uint8_t receivedData[30] = {0};
//If a queue is not empty, read the content and process the data.
if (nrf_queue_is_empty(&UARTE0RxBufferQueue) == false) {
UARTERxBufferStruct buf;
nrf_queue_pop(&UARTE0RxBufferQueue, &buf);
//Reset the queue
nrf_queue_reset(&UARTE0RxBufferQueue);
memcpy(receivedData, buf.data, buf.length);
//Toggle the LED
bsp_board_led_invert(0);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

While I am able to successfully transmit and receive, I want to know how to clear the Rx Buffer and reset the write position after each response from the other device.

Currently, the buffer wold just overflow, which is undesirable in my application.

Thank you.