
#include <string.h>
#include <errno.h>
#include <zephyr.h>
#include <sys/printk.h>
#include <device.h>
#include <drivers/spi.h>

#define STACKSIZE 1024
#define PRIORITY 99

void main(void)
{
	struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
	uint8_t data = 40, val =0, sz = 9;
	    struct spi_buf bufs = {
            .buf = &data,
            .len = sizeof(data)
    };
    struct spi_buf_set tx = {
        .buffers = &bufs
    };

    tx.count = 1;

	struct spi_buf rbufs = {
            .buf = &val,
            .len = sizeof(val)
    };
    struct spi_buf_set rx = {
        .buffers = &rbufs
    };
	struct spi_config spi_cfg;
	struct spi_cs_control cs_ctrl;

	cs_ctrl.gpio_dev = device_get_binding("GPIO_0");
    cs_ctrl.gpio_pin = 11;
    cs_ctrl.delay = 0;

	spi_cfg.operation = SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_LINES_SINGLE; 
    spi_cfg.frequency = 1000000;
	spi_cfg.cs = &cs_ctrl;

	const struct device * sp;
	sp = device_get_binding("SPI_0");

			data++;
			struct k_poll_event  async_evt =
		K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
					 K_POLL_MODE_NOTIFY_ONLY,
					 &async_sig);

			printk("transmitted data: %u \n", data);
			int error = spi_transceive_async(sp,&spi_cfg,&tx, &rx,&async_sig);
			int ret = k_poll(&async_evt, 10, K_MSEC(1000));
			printk("received data: %u \n", val);
}