#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/uart.h>
#include <sys/printk.h>
uint8_t uart_buf[1024];
int count;
void uart_cb(const struct device *x, void *user_data)
{
	uart_irq_update(x);
	int data_length = 0;

	if (uart_irq_rx_ready(x)) {
		data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
		uart_buf[data_length] = 0;
	}
    uart_fifo_fill(x, uart_buf, data_length);
}

void main(void)
{

    printk("sensor program start!\n"); //output on UART0, which is default for logging
		


const struct device *uart = device_get_binding("UART_1");
	uart_irq_callback_set(uart, uart_cb);
	uart_irq_rx_enable(uart);
    struct uart_config cfg ;
    cfg.baudrate = 9600 ;
    cfg.parity = UART_CFG_PARITY_NONE ;
    cfg.stop_bits = UART_CFG_STOP_BITS_1 ;
    cfg.data_bits = UART_CFG_DATA_BITS_8 ;
    cfg.flow_ctrl = UART_CFG_FLOW_CTRL_NONE ;
 uart_configure(uart, &cfg);

	printk("main function!\n"); //output on UART0, which is default for logging
	while (1) {
        uart_fifo_fill(uart, "UART loopback start!\r\n", 22);
        // count=uart_fifo_read(uart,uart_buf,12);
        // printf("read  %i\n",count);
        printk("receiveing data! %x %i %i\n",uart_buf[0],uart_buf[1],uart_buf[2]); //output on UART0, which is default for logging
	
        k_cpu_idle();
        k_sleep(K_MSEC(1000));
	}
}