/*
 * Code generated from Atmel Start.
 *
 * This file will be overwritten when reconfiguring your Atmel Start project.
 * Please copy examples or other code you want to keep to a separate file
 * to avoid losing it when reconfiguring.
 */

#include "driver_examples.h"
#include "driver_init.h"
#include "utils.h"

static void button_on_PA18_pressed(void)
{
}

static void button_on_PA06_pressed(void)
{
}

static void button_on_PA14_pressed(void)
{
}

/**
 * Example of using EXTERNAL_IRQ_0
 */
void EXTERNAL_IRQ_0_example(void)
{

	ext_irq_register(PIN_PA18, button_on_PA18_pressed);
	ext_irq_register(PIN_PA06, button_on_PA06_pressed);
	ext_irq_register(PIN_PA14, button_on_PA14_pressed);
}

static uint8_t buf[16] = {0x0};

static void xfer_complete_cb_ECAT_QSPI(struct _dma_resource *resource)
{
	/* Transfer completed */
}

/**
 * Example of using ECAT_QSPI to get N25Q256A status value,
 * and check bit 0 which indicate embedded operation is busy or not.
 */
void ECAT_QSPI_example(void)
{
	struct _qspi_command cmd = {
	    .inst_frame.bits.inst_en      = 1,
	    .inst_frame.bits.data_en      = 1,
	    .inst_frame.bits.addr_en      = 1,
	    .inst_frame.bits.dummy_cycles = 8,
	    .inst_frame.bits.tfr_type     = QSPI_READMEM_ACCESS,
	    .instruction                  = 0x0B,
	    .address                      = 0,
	    .buf_len                      = 14,
	    .rx_buf                       = buf,
	};

	qspi_dma_register_callback(&ECAT_QSPI, QSPI_DMA_CB_XFER_DONE, xfer_complete_cb_ECAT_QSPI);
	qspi_dma_enable(&ECAT_QSPI);
	qspi_dma_serial_run_command(&ECAT_QSPI, &cmd);
}

/**
 * Example of using SPI_M2 to write "Hello World" using the IO abstraction.
 *
 * Since the driver is asynchronous we need to use statically allocated memory for string
 * because driver initiates transfer and then returns before the transmission is completed.
 *
 * Once transfer has been completed the tx_cb function will be called.
 */

static uint8_t example_SPI_M2[12] = "Hello World!";

static void tx_complete_cb_SPI_M2(struct _dma_resource *resource)
{
	/* Transfer completed */
}

void SPI_M2_example(void)
{
	struct io_descriptor *io;
	spi_m_dma_get_io_descriptor(&SPI_M2, &io);

	spi_m_dma_register_callback(&SPI_M2, SPI_M_DMA_CB_TX_DONE, tx_complete_cb_SPI_M2);
	spi_m_dma_enable(&SPI_M2);
	io_write(io, example_SPI_M2, 12);
}

/**
 * Example of using SPI_M1 to write "Hello World" using the IO abstraction.
 *
 * Since the driver is asynchronous we need to use statically allocated memory for string
 * because driver initiates transfer and then returns before the transmission is completed.
 *
 * Once transfer has been completed the tx_cb function will be called.
 */

static uint8_t example_SPI_M1[12] = "Hello World!";

static void tx_complete_cb_SPI_M1(struct _dma_resource *resource)
{
	/* Transfer completed */
}

void SPI_M1_example(void)
{
	struct io_descriptor *io;
	spi_m_dma_get_io_descriptor(&SPI_M1, &io);

	spi_m_dma_register_callback(&SPI_M1, SPI_M_DMA_CB_TX_DONE, tx_complete_cb_SPI_M1);
	spi_m_dma_enable(&SPI_M1);
	io_write(io, example_SPI_M1, 12);
}

/**
 * Example of using SPI_M3 to write "Hello World" using the IO abstraction.
 *
 * Since the driver is asynchronous we need to use statically allocated memory for string
 * because driver initiates transfer and then returns before the transmission is completed.
 *
 * Once transfer has been completed the tx_cb function will be called.
 */

static uint8_t example_SPI_M3[12] = "Hello World!";

static void tx_complete_cb_SPI_M3(struct _dma_resource *resource)
{
	/* Transfer completed */
}

void SPI_M3_example(void)
{
	struct io_descriptor *io;
	spi_m_dma_get_io_descriptor(&SPI_M3, &io);

	spi_m_dma_register_callback(&SPI_M3, SPI_M_DMA_CB_TX_DONE, tx_complete_cb_SPI_M3);
	spi_m_dma_enable(&SPI_M3);
	io_write(io, example_SPI_M3, 12);
}
