Hello. I am currently working on UART0 Async data parser.
Whenever the data is being received, I send it via the message queue. On the different thread, I have parser that receives the messages on the queue and parses them accordingly.
For example, I want to send string "UART1:ping". I want to parse this data and split it with delimiter '" : ".
My function that parses the data:
void uart0_parser_thread(void)
{
uint8_t data[24];
while (1)
{
/* get a data item */
k_msgq_get(&uart0_message_queue, &data, K_FOREVER);
LOG_DBG("UART0_RX: %s (%d)", data, strlen(data));
if (strncmp((char *)data, "UART1:", 6) == 0)
{
strtok(data, ":"); // Split and ignore the "UART6" part
char *token = strtok(NULL, " ");
while (token != NULL)
{
char *message_buff = malloc(UART0_BUF_SIZE);
if (message_buff == NULL)
{
LOG_ERR("Failed to allocate memory \n");
k_msleep(1000);
return;
}
// Copy the token into message_buff
strncpy(message_buff, token, UART0_BUF_SIZE - 1);
message_buff[-1] = '\0'; // Ensure null termination
// Append newline character if space is available
size_t len = strlen(message_buff);
if (len < UART0_BUF_SIZE - 1)
{
message_buff[len] = '\n';
message_buff[len + 1] = '\0';
}
// Print the message
LOG_DBG("%s", message_buff); // Newline already included
LOG_DBG("Message length = %u \n", strlen(message_buff)); // Newline already included
// Free the allocated memory
free(message_buff);
// Move to the next token
token = strtok(NULL, " ");
}
return;
}
k_yield();
}
}
I have noticed that whenever I send data such as "UART1:test", the CPU crashes with the following error:
UART1:test [00:00:05.762,512] [1B][0m<dbg> uart0: uart0_parser_thread: UART0_RX: UART1:test (11)[1B][0m [00:00:09.603,851] [1B][0m<dbg> uart0: uart0_parser_thread: test [1B][0m [00:00:09.621,398] [1B][0m<dbg> uart0: uart0_parser_thread: Message length = 6 [1B][0m [00:00:11.469,635] [1B][1;31m<err> os: ***** USAGE FAULT *****[1B][0m [00:00:11.475,402] [1B][1;31m<err> os: Illegal use of the EPSR[1B][0m [00:00:11.481,384] [1B][1;31m<err> os: r0/a1: 0x00000000 r1/a2: 0x00007d94 r2/a3: 0x20001cbc[1B][0m [00:00:11.490,295] [1B][1;31m<err> os: r3/a4: 0x00000001 r12/ip: 0x200038c8 r14/lr: 0x00000635[1B][0m [00:00:11.499,176] [1B][1;31m<err> os: xpsr: 0x60000000[1B][0m [00:00:11.504,547] [1B][1;31m<err> os: Faulting instruction address (r15/pc): 0x00000000[1B][0m [00:00:11.512,634] [1B][1;31m<err> os: >>> ZEPHYR FATAL ERROR 35: Unknown error on CPU 0[1B][0m [00:00:11.520,721] [1B][1;31m<err> os: Current thread: 0 (unknown)[1B][0m [00:00:11.546,569] [1B][1;31m<err> fatal_error: Resetting system[1B][0m *** Booting nRF Connect SDK v2.5.1 ***
I cannot fully figure out what could be the issue since I used the exactly same method for parsing data in my other project for the STM32 MCU (I have basically copied and pasted the whole strtok part)
I have ran a debugger to try and understand what could be an issue and I have noticed that the loop:
while (token != NULL)
char *message_buff = malloc(UART0_BUF_SIZE);
if (message_buff == NULL)
{
LOG_ERR("Failed to allocate memory \n");
k_msleep(1000);
return;
}