/*
 * 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 convert_cb_ADC_0(const struct adc_async_descriptor *const descr, const uint8_t channel)
{
}

/**
 * Example of using ADC_0 to generate waveform.
 */
void ADC_0_example(void)
{
	adc_async_enable_channel(&ADC_0, 0);
	adc_async_register_callback(&ADC_0, 0, ADC_ASYNC_CONVERT_CB, convert_cb_ADC_0);
	adc_async_start_conversion(&ADC_0);
}

static void convert_cb_ADC_1(const struct adc_async_descriptor *const descr, const uint8_t channel)
{
}

/**
 * Example of using ADC_1 to generate waveform.
 */
void ADC_1_example(void)
{
	adc_async_enable_channel(&ADC_1, 0);
	adc_async_register_callback(&ADC_1, 0, ADC_ASYNC_CONVERT_CB, convert_cb_ADC_1);
	adc_async_start_conversion(&ADC_1);
}

/**
 * Example of using DIGITAL_GLUE_LOGIC_0.
 */
void DIGITAL_GLUE_LOGIC_0_example(void)
{
	custom_logic_enable();
	/* Customer logic now works. */
}

static void button_on_PB05_pressed(void)
{
}

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

	ext_irq_register(PIN_PB05, button_on_PB05_pressed);
}

static void position_cb_POSITION_DECODER_0(struct pdec_async_descriptor *const descr, uint8_t ch)
{
	uint16_t count;
	count = pdec_async_read_position(descr, ch);
}

static void irq_handler_cb_POSITION_DECODER_0(struct pdec_async_descriptor *const descr,
                                              enum pdec_async_callback_type type, uint8_t ch)
{
}

/**
 * Example of using POSITION_DECODER_0.
 */
void POSITION_DECODER_0_example(void)
{
	pdec_async_register_callback(
	    &POSITION_DECODER_0, PDEC_ASYNC_POS_CHANGED_CB, (FUNC_PTR)position_cb_POSITION_DECODER_0);
	pdec_async_register_callback(&POSITION_DECODER_0, PDEC_ASYNC_IRQ_CB, (FUNC_PTR)irq_handler_cb_POSITION_DECODER_0);
	pdec_async_enable(&POSITION_DECODER_0);
}

/**
 * Example of using SPI_0 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_0[12] = "Hello World!";

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

void SPI_0_example(void)
{
	struct io_descriptor *io;
	spi_m_dma_get_io_descriptor(&SPI_0, &io);

	spi_m_dma_register_callback(&SPI_0, SPI_M_DMA_CB_TX_DONE, tx_complete_cb_SPI_0);
	spi_m_dma_enable(&SPI_0);
	io_write(io, example_SPI_0, 12);
}
