async UART coming in broken packets

Hello,

This is probably just me not knowing what I'm doing, but I could really use some help. I am trying to set up two UARTs on the nRF52840. We have a custom board but I am starting with the DK. I have gotten both UART0 and UART1 to work simultaneously, while using RTT to verify that things were coming in. However, I need to manipulate the data coming into the RX buffers before sending it out. I'm having trouble:

-Getting UART data in large packets (the async callback seems to break up larger packets into multiple callbacks)

-Using a workqueue to effectively offload computing

-USING THE RINGBUFFER!!! I think a big part of my issue is that the head/tail is getting off or I'm not doing it right or something.

-When recieving larger test messages, such as "123456789012345678901234567890", I lose bytes or sometimes it gets outright truncated. After this, my program seems to be "off" or "shifted" as if the ringbuffer head or tail is off.

-using "uart_tx" with a string. I have converted the rx input (when it works) to a string (char array) but when I test out just sending it back with uart_tx it fails. I have tried typecasting into uint8_t* and lots of other things. I just can't get it to work.

I would also just love for someone to explain a little more about how to best receive UART data effectively. Things like best practices, etc. A code snippet would really help me.

Test setup:

nRF52840 DK plugged into my mac. There is also a uart/usb bridge plugged into P0.11 and P0.12 which I changed to UART0 in the device tree overlay. I use this to talk to the UART0 device. I am ignoring UART1 while I figure out how to accurately get messages from UART0. I am using RTT for my LOG messages, displayed on RTT-viewer or in VSC.

NCS 2.2.0

In my code I put some LOG_INF stuff. I included some screenshots of some basic tests. You can see that smaller tests from the serial terminal like "0123456789" are fine. But "01234567890123456789" will go freeze the program. This is with a uart_rx timeout of 1000 ms. I'm assuming I would want something more like 10-100ms. When I set uart_rx to 100ms it will freeze with just "0123456789" or even smaller.

main.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/devicetree.h>
#include <zephyr/sys/ring_buffer.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/logging/log.h>
#include <string.h>
//setup the logging module
#define LOG_MODULE_NAME app
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
//THREAD SETUP
#define UART0_INTERPRETER_STACKSIZE 512
#define UART0_INTERPRETER_PRIORITY 4
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

prj.conf

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# nothing here
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_RING_BUFFER=y
CONFIG_UART_CONSOLE=n
#CONFIG_CONSOLE=n
CONFIG_LOG=y
CONFIG_LOG_BACKEND_RTT=y
#CONFIG_RTT_CONSOLE=y
CONFIG_UART_ASYNC_API=y
CONFIG_BUILD_OUTPUT_EXE=y
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

nrf52840dk_nrf52840.overlay

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ {
pin-controller {
uart0_default {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 11)>,
<NRF_PSEL(UART_RTS, 0, 5)>;
};
group2 {
psels = <NRF_PSEL(UART_RX, 0, 12)>,
<NRF_PSEL(UART_CTS, 0, 7)>;
bias-pull-up;
};
};
};
};
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

serial terminal

RTT-output