Getting stack overflow fault error

/*
 * Copyright (c) 2022 Libre Solar Technologies GmbH
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/zephyr.h>
#include <stdio.h>
#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <sys/printk.h>
#include <drivers/uart.h>
#include <cJSON.h>
#include <string.h>

/* change this to any other UART peripheral if desired */
#define UART_DEVICE_NODE DT_CHOSEN(zephyr_shell_uart)
cJSON *root;
#define MSG_SIZE 300

/* queue to store up to 10 messages (aligned to 4-byte boundary) */
K_MSGQ_DEFINE(uart_msgq, MSG_SIZE, 10, 4);

static const struct device *uart_dev = DEVICE_DT_GET(DT_NODELABEL(uart0));

/* receive buffer used in UART ISR callback */
static char rx_buf[MSG_SIZE];
static int rx_buf_pos;

/*
 * Read characters from UART until line end is detected. Afterwards push the
 * data to the message queue.
 */
void serial_cb(const struct device *dev, void *user_data)
{
	uint8_t c;

	if (!uart_irq_update(uart_dev)) {
		return;
	}

	while (uart_irq_rx_ready(uart_dev)) {

		uart_fifo_read(uart_dev, &c, 1);

		if ((c == '\n' || c == '\r') && rx_buf_pos > 0) {
			/* terminate string */
			rx_buf[rx_buf_pos] = '\0';

			/* if queue is full, message is silently dropped */
			k_msgq_put(&uart_msgq, &rx_buf, K_NO_WAIT);

			/* reset the buffer (it was copied to the msgq) */
			rx_buf_pos = 0;
		} else if (rx_buf_pos < (sizeof(rx_buf) - 1)) {
			rx_buf[rx_buf_pos++] = c;
		}
		/* else: characters beyond buffer size are dropped */
	}
}

/*
 * Print a null-terminated string character by character to the UART interface
 */
void uart_write(char *buf)
{
	int msg_len = strlen(buf);

	for (int i = 0; i < msg_len; i++) {
		uart_poll_out(uart_dev, buf[i]);
	}
}


int create_nrf5340rx_packet(cJSON* cmd) {
    printk("NRF5340 Rx: Create data packet called\n");
    cJSON *params;
    cJSON *motor_pos;
    cJSON *temp;
    cJSON_AddItemToObject(cmd, "cmd", cJSON_CreateString("manualMotorControl"));
    cJSON_AddItemToObject(cmd, "params", params = cJSON_CreateObject());
    cJSON_AddItemToObject(params, "motor_pos", motor_pos = cJSON_CreateArray());
    cJSON_AddItemToArray(motor_pos, temp = cJSON_CreateObject());
    cJSON_AddNumberToObject(temp, "0", 0);
    cJSON_AddItemToArray(motor_pos, temp = cJSON_CreateObject());
    cJSON_AddNumberToObject(temp, "1", 90);
    cJSON_AddItemToArray(motor_pos, temp = cJSON_CreateObject());
    cJSON_AddNumberToObject(temp, "2", 180);
    cJSON_AddItemToArray(motor_pos, temp = cJSON_CreateObject());
    cJSON_AddNumberToObject(temp, "3", 270);
    cJSON_AddNumberToObject(cmd, "timestamp", 1927);
	char* tx_buf = cJSON_Print(root);
	printk("Size of: %d \r\n", strlen(tx_buf));
	
	cJSON_Delete(params);
	cJSON_Delete(motor_pos);
	cJSON_Delete(temp);
    return 0;
}


int main(void)
{
	char *tx_buf;

	if (!device_is_ready(uart_dev)) {
		printk("UART device not found!");
		return;
	}

	/* configure interrupt and callback to receive data */
	uart_irq_callback_user_data_set(uart_dev, serial_cb, NULL);
	uart_irq_rx_enable(uart_dev);
    
    //uart_write(tx_buf); 
	uart_write("Hello! I'm your echo bot.\r\n");
	uart_write("Tell me something and press enter:\r\n");
	
	
    root = cJSON_CreateObject();
    create_nrf5340rx_packet(root);
    
	return 0; 
}

While running the mentioned code I am getting stack overflow error continuously. Can someone help me understand the reason behind that and what can be done to resolve such issue.

Error:

*** Booting Zephyr OS build v3.0.99-ncs1-1  *** system
Hello! I'm your echo bot.
Tell me something and press enter:
NRF5340 Rx: Create data packet called
[00:00:00.268,280] <err> os: ***** USAGE FAULT *****
[00:00:00.268,280] <err> os:   Stack overflow (context area not valid)
[00:00:00.268,310] <err> os: r0/a1:  0x492d8255  r1/a2:  0xffbff3fd  r2/a3:  0x0303188b
[00:00:00.268,310] <err> os: r3/a4:  0xdd2dfc5b r12/ip:  0x00018990 r14/lr:  0x81000000
[00:00:00.268,310] <err> os:  xpsr:  0x00019a00
[00:00:00.268,341] <err> os: Faulting instruction address (r15/pc): 0x200100e0
[00:00:00.268,341] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
[00:00:00.268,371] <err> os: Current thread: 0x20010930 (main)

Related