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

#define UART_DEV_NAME DT_LABEL(DT_NODELABEL(uart0))
#define UART_BUF_SIZE 1024

static uint8_t index = 0;

static void uart_cb(const struct device * uart, void * user_data)
{
	static uint8_t data_array[UART_BUF_SIZE];
	uart_irq_update(uart);
    int data_length = 0;

	if (uart_irq_rx_ready(uart)) {
		data_length = uart_fifo_read(uart, data_array,
					     UART_BUF_SIZE-index);
						 
		index += data_length;
		//uart_buf[data_length] = 0;
    }
		if (index > 0) {
			printk("recv: %s\n", data_array);
			if ((index == UART_BUF_SIZE) ||
			   (data_array[index - 1] == '\n') ||
			   (data_array[index - 1] == '\r')) {
				}
		index = 0;
		memset(data_array, 0, sizeof(data_array));
		}
}

void main(void)
{
        
	const struct device *uart = device_get_binding(UART_DEV_NAME);

	uart_irq_callback_set(uart, uart_cb);
	uart_irq_rx_enable(uart);
	printk("UART sample start\n");
	while (1) {
		k_cpu_idle();
	}
}