Hi,
I have added libuarte example in-app template and sent data from Arduino to nRF52840 successfully. But after receiving some data and I am getting the following error. Any solution please?
Thanks!

Hi,
I have added libuarte example in-app template and sent data from Arduino to nRF52840 successfully. But after receiving some data and I am getting the following error. Any solution please?
Thanks!

Hi,
Have you remembered to free the RX buffer in the uart event handler? Could you show me how the handler has been implemented?
regards
Jared
Thanks for the reply. Here is the function
void uart_event_handler(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:
if((unsigned char)*(p_evt->data.rxtx.p_data)!=0)
{
received_data[count_data]=(unsigned char)*(p_evt->data.rxtx.p_data);
count_data=count_data+1;
if((unsigned char)*(p_evt->data.rxtx.p_data)==35)
data_completed=true;
}
m_loopback_phase = true;
break;
case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
if (m_loopback_phase)
{
nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
if (!nrf_queue_is_empty(&m_buf_queue))
{
buffer_t buf;
ret = nrf_queue_pop(&m_buf_queue, &buf);
APP_ERROR_CHECK(ret);
UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
}
}
printf("Tx Called\n");
break;
default:
break;
}
}
//UART
You removed the nrf_libuarte_async_tx call from the RX event for instance? I dont see the reasoning behind that.
Actually, I want to save coming data from Arduino in the array so I remove that. If add the following part it sends the same received data to tx.
//UART
void uart_event_handler(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:
ret = nrf_libuarte_async_tx(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
if (ret == NRF_ERROR_BUSY) {
buffer_t buf = {
.p_data = p_evt->data.rxtx.p_data,
.length = p_evt->data.rxtx.length,
};
ret = nrf_queue_push(&m_buf_queue, &buf);
APP_ERROR_CHECK(ret);
} else {
APP_ERROR_CHECK(ret);
}
if (count_data < 50) {
if ((unsigned char)*(p_evt->data.rxtx.p_data) != 0) {
received_data[count_data] = (unsigned char)*(p_evt->data.rxtx.p_data);
count_data = count_data + 1;
if ((unsigned char)*(p_evt->data.rxtx.p_data) == 35)
data_completed = true;
}
} else {
count_data = 0;
}
m_loopback_phase = true;
break;
case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
if (m_loopback_phase) {
nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
if (!nrf_queue_is_empty(&m_buf_queue)) {
buffer_t buf;
ret = nrf_queue_pop(&m_buf_queue, &buf);
APP_ERROR_CHECK(ret);
UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
}
}
printf("Tx Called\n");
break;
default:
break;
}
}
//UART
At the heart of the issue, you are trying to free more bytes than you have actually received somewhere. But with potential modifications made in several places its hard to know where the issue stems from.
I have not made so many changes but just merge two codes. if you asked I upload the complete code.
What version of the nRF5SDK are you using?
nRF5_SDK_17.0.2_d674dde.
code work but after receiving some data it shows the following error.
The issue is related to RX buffer. can you guide how can I handle that?
Does any suggestions please?
Hey Muqarrab!
I guess it is understandable that you dont want the TX event, as the echo part of the example isn't what you are after. But the buffer is being freed in the case for TX, so you would have to move a couple lines around. As I mentioned earlier, the buffer not being freed in the right spots is what is causing your error.
I would suggest that you try to remove the echo-like TX lines, save the data from the buffer in an array or whatever you want, and then free the buffer. All in the RX event.
Regards,
Elfving
Thanks, Elfving
I am trying to do the same, saving data from the buffer in an array and then freeing the buffer. All in the RX event. Here is my event handler. is this the right way to free RX buffer?void uart_event_handler(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:
if (count_data < 50) {
if ((unsigned char)*(p_evt->data.rxtx.p_data) != 0)
{
// Receive complete
received_data[count_data] = (unsigned char)*(p_evt->data.rxtx.p_data);
count_data = count_data + 1;
if ((unsigned char)*(p_evt->data.rxtx.p_data) == 35)
{
check_data();
data_completed = true;
}
}
} else {
count_data = 0;
}
buffer_t buf;
if (!nrf_queue_is_empty(&m_buf_queue)) {
buffer_t buf;
ret = nrf_queue_pop(&m_buf_queue, &buf);
APP_ERROR_CHECK(ret);
UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
}
// ret = nrf_libuarte_async_tx(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
// if (ret == NRF_ERROR_BUSY) {
// buffer_t buf = {
// .p_data = p_evt->data.rxtx.p_data,
// .length = p_evt->data.rxtx.length,
// };
// ret = nrf_queue_push(&m_buf_queue, &buf);
// APP_ERROR_CHECK(ret);
//} else {
// APP_ERROR_CHECK(ret);
//}
// printf("Counter : %d",count_data);
// nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
m_loopback_phase = true;
break;
case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
if (m_loopback_phase) {
nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
if (!nrf_queue_is_empty(&m_buf_queue)) {
buffer_t buf;
ret = nrf_queue_pop(&m_buf_queue, &buf);
APP_ERROR_CHECK(ret);
UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
}
}
// printf("Tx Called\n");
break;
default:
break;
}
}
//UART
Reply, please.
Reply, please.