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:
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);
Here's my event handler:
/* * 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; //Push the data into the queue ret = nrf_queue_push(&UARTE0RxBufferQueue, &pushBuffer); APP_ERROR_CHECK(ret); } //As recommended by nRF(Infocenter -> nRF52 Series -> nRF52840 -> Specs -> Peripherals -> UARTE -> Reception), release the data. nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length); nrf_drv_ break; case NRF_LIBUARTE_ASYNC_EVT_TX_DONE: break; case NRF_LIBUARTE_ASYNC_EVT_OVERRUN_ERROR: break; default: break; } }
Here's my application code:
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); UARTE0TransmitData(requestVersion, 16); } nrf_delay_ms(100); NRF_LOG_FLUSH(); } }
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.