/*
 * 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_init.h"
#include <peripheral_clk_config.h>
#include <utils.h>
#include <hal_init.h>

#include <hpl_adc_base.h>
#include <hpl_adc_base.h>

/* The channel amount for ADC */
#define ADC_0_CH_AMOUNT 1

/* The buffer size for ADC */
#define ADC_0_BUFFER_SIZE 16

/* The maximal channel number of enabled channels */
#define ADC_0_CH_MAX 0

/* The channel amount for ADC */
#define ADC_1_CH_AMOUNT 1

/* The buffer size for ADC */
#define ADC_1_BUFFER_SIZE 16

/* The maximal channel number of enabled channels */
#define ADC_1_CH_MAX 0

struct adc_async_descriptor         ADC_0;
struct adc_async_channel_descriptor ADC_0_ch[ADC_0_CH_AMOUNT];
struct adc_async_descriptor         ADC_1;
struct adc_async_channel_descriptor ADC_1_ch[ADC_1_CH_AMOUNT];

static uint8_t ADC_0_buffer[ADC_0_BUFFER_SIZE];
static uint8_t ADC_0_map[ADC_0_CH_MAX + 1];
static uint8_t ADC_1_buffer[ADC_1_BUFFER_SIZE];
static uint8_t ADC_1_map[ADC_1_CH_MAX + 1];

struct pdec_async_descriptor POSITION_DECODER_0;

struct spi_m_dma_descriptor SPI_0;

/**
 * \brief ADC initialization function
 *
 * Enables ADC peripheral, clocks and initializes ADC driver
 */
void ADC_0_init(void)
{
	hri_mclk_set_APBDMASK_ADC0_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, ADC0_GCLK_ID, CONF_GCLK_ADC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
	adc_async_init(&ADC_0, ADC0, ADC_0_map, ADC_0_CH_MAX, ADC_0_CH_AMOUNT, &ADC_0_ch[0], (void *)NULL);
	adc_async_register_channel_buffer(&ADC_0, 0, ADC_0_buffer, ADC_0_BUFFER_SIZE);

	// Disable digital pin circuitry
	gpio_set_pin_direction(PB08, GPIO_DIRECTION_OFF);

	gpio_set_pin_function(PB08, PINMUX_PB08B_ADC0_AIN2);
}

/**
 * \brief ADC initialization function
 *
 * Enables ADC peripheral, clocks and initializes ADC driver
 */
void ADC_1_init(void)
{
	hri_mclk_set_APBDMASK_ADC1_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, ADC1_GCLK_ID, CONF_GCLK_ADC1_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
	adc_async_init(&ADC_1, ADC1, ADC_1_map, ADC_1_CH_MAX, ADC_1_CH_AMOUNT, &ADC_1_ch[0], (void *)NULL);
	adc_async_register_channel_buffer(&ADC_1, 0, ADC_1_buffer, ADC_1_BUFFER_SIZE);

	// Disable digital pin circuitry
	gpio_set_pin_direction(PB09, GPIO_DIRECTION_OFF);

	gpio_set_pin_function(PB09, PINMUX_PB09B_ADC1_AIN1);
}

void DIGITAL_GLUE_LOGIC_0_PORT_init(void)
{

	gpio_set_pin_function(HALL_C, PINMUX_PA04N_CCL_IN0);

	gpio_set_pin_function(HALL_B, PINMUX_PA05N_CCL_IN1);

	gpio_set_pin_function(HALL_A, PINMUX_PA06N_CCL_IN2);
}

void DIGITAL_GLUE_LOGIC_0_CLOCK_init(void)
{
	hri_mclk_set_APBCMASK_CCL_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, CCL_GCLK_ID, CONF_GCLK_CCL_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
}

void DIGITAL_GLUE_LOGIC_0_init(void)
{
	DIGITAL_GLUE_LOGIC_0_CLOCK_init();
	custom_logic_init();
	DIGITAL_GLUE_LOGIC_0_PORT_init();
}

void EXTERNAL_IRQ_0_init(void)
{
	hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, CONF_GCLK_EIC_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
	hri_mclk_set_APBAMASK_EIC_bit(MCLK);

	// Set pin direction to input
	gpio_set_pin_direction(nDRV_RESET, GPIO_DIRECTION_IN);

	gpio_set_pin_pull_mode(nDRV_RESET,
	                       // <y> Pull configuration
	                       // <id> pad_pull_config
	                       // <GPIO_PULL_OFF"> Off
	                       // <GPIO_PULL_UP"> Pull-up
	                       // <GPIO_PULL_DOWN"> Pull-down
	                       GPIO_PULL_OFF);

	gpio_set_pin_function(nDRV_RESET, PINMUX_PB05A_EIC_EXTINT5);

	ext_irq_init();
}

void EVENT_SYSTEM_0_init(void)
{
	hri_gclk_write_PCHCTRL_reg(GCLK, EVSYS_GCLK_ID_0, CONF_GCLK_EVSYS_CHANNEL_0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
	hri_gclk_write_PCHCTRL_reg(GCLK, EVSYS_GCLK_ID_1, CONF_GCLK_EVSYS_CHANNEL_1_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));

	hri_mclk_set_APBBMASK_EVSYS_bit(MCLK);

	event_system_init();
}

void POSITION_DECODER_0_PORT_init(void)
{
}

void POSITION_DECODER_0_CLOCK_init(void)
{

	hri_mclk_set_APBCMASK_PDEC_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, PDEC_GCLK_ID, CONF_GCLK_PDEC_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
}

void POSITION_DECODER_0_init(void)
{
	POSITION_DECODER_0_CLOCK_init();
	pdec_async_init(&POSITION_DECODER_0, PDEC);
	POSITION_DECODER_0_PORT_init();
}

void SPI_0_PORT_init(void)
{

	gpio_set_pin_level(PB16,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(PB16, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(PB16, PINMUX_PB16C_SERCOM5_PAD0);

	gpio_set_pin_level(PB17,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(PB17, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(PB17, PINMUX_PB17C_SERCOM5_PAD1);

	// Set pin direction to input
	gpio_set_pin_direction(PB01, GPIO_DIRECTION_IN);

	gpio_set_pin_pull_mode(PB01,
	                       // <y> Pull configuration
	                       // <id> pad_pull_config
	                       // <GPIO_PULL_OFF"> Off
	                       // <GPIO_PULL_UP"> Pull-up
	                       // <GPIO_PULL_DOWN"> Pull-down
	                       GPIO_PULL_OFF);

	gpio_set_pin_function(PB01, PINMUX_PB01D_SERCOM5_PAD3);
}

void SPI_0_CLOCK_init(void)
{
	hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM5_GCLK_ID_CORE, CONF_GCLK_SERCOM5_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
	hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM5_GCLK_ID_SLOW, CONF_GCLK_SERCOM5_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));

	hri_mclk_set_APBDMASK_SERCOM5_bit(MCLK);
}

void SPI_0_init(void)
{
	SPI_0_CLOCK_init();
	spi_m_dma_init(&SPI_0, SERCOM5);
	SPI_0_PORT_init();
}

void TC_SPEED_CLOCK_init(void)
{
	hri_mclk_set_APBAMASK_TC0_bit(MCLK);

	hri_mclk_set_APBAMASK_TC1_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, TC0_GCLK_ID, CONF_GCLK_TC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
}

void TC_ECAT_CLOCK_init(void)
{
	hri_mclk_set_APBDMASK_TC7_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, TC7_GCLK_ID, CONF_GCLK_TC7_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
}

void TCC_PWM2_PORT_init(void)
{

	gpio_set_pin_function(M2_0, PINMUX_PC04F_TCC0_WO0);

	gpio_set_pin_function(M2_1, PINMUX_PD08F_TCC0_WO1);

	gpio_set_pin_function(M2_2, PINMUX_PD09F_TCC0_WO2);

	gpio_set_pin_function(M2_3, PINMUX_PD10F_TCC0_WO3);

	gpio_set_pin_function(M2_4, PINMUX_PA16G_TCC0_WO4);

	gpio_set_pin_function(M2_5, PINMUX_PA17G_TCC0_WO5);

	gpio_set_pin_function(M3_0, PINMUX_PC22F_TCC0_WO6);

	gpio_set_pin_function(M3_1, PINMUX_PC23F_TCC0_WO7);
}

void TCC_PWM2_CLOCK_init(void)
{
	hri_mclk_set_APBBMASK_TCC0_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, CONF_GCLK_TCC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
}

void TCC_PWM_PORT_init(void)
{

	gpio_set_pin_function(M3_2, PINMUX_PC14G_TCC1_WO0);

	gpio_set_pin_function(DRV_INC, PINMUX_PB26F_TCC1_WO2);

	gpio_set_pin_function(DRV_INB, PINMUX_PB27F_TCC1_WO3);

	gpio_set_pin_function(DRV_ENB, PINMUX_PB28F_TCC1_WO4);

	gpio_set_pin_function(DRV_ENC, PINMUX_PB29F_TCC1_WO5);

	gpio_set_pin_function(DRV_ENA, PINMUX_PA22F_TCC1_WO6);

	gpio_set_pin_function(DRV_INA, PINMUX_PA23F_TCC1_WO7);
}

void TCC_PWM_CLOCK_init(void)
{
	hri_mclk_set_APBBMASK_TCC1_bit(MCLK);
	hri_gclk_write_PCHCTRL_reg(GCLK, TCC1_GCLK_ID, CONF_GCLK_TCC1_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
}

void system_init(void)
{
	init_mcu();

	// GPIO on PA18

	gpio_set_pin_level(M3_3,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(M3_3, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(M3_3, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PA27

	gpio_set_pin_level(DRV_RESET,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(DRV_RESET, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(DRV_RESET, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PB00

	gpio_set_pin_level(ECAT_SPI_CS_PIN,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   true);

	// Set pin direction to output
	gpio_set_pin_direction(ECAT_SPI_CS_PIN, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(ECAT_SPI_CS_PIN, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PB14

	gpio_set_pin_level(M3_4,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(M3_4, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(M3_4, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PB15

	gpio_set_pin_level(M3_5,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(M3_5, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(M3_5, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PB31

	// Set pin direction to input
	gpio_set_pin_direction(SW0, GPIO_DIRECTION_IN);

	gpio_set_pin_pull_mode(SW0,
	                       // <y> Pull configuration
	                       // <id> pad_pull_config
	                       // <GPIO_PULL_OFF"> Off
	                       // <GPIO_PULL_UP"> Pull-up
	                       // <GPIO_PULL_DOWN"> Pull-down
	                       GPIO_PULL_UP);

	gpio_set_pin_function(SW0, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PC01

	gpio_set_pin_level(DEBUG_1,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(DEBUG_1, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(DEBUG_1, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PC02

	gpio_set_pin_level(DEBUG_3,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(DEBUG_3, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(DEBUG_3, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PC03

	gpio_set_pin_level(DEBUG_2,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(DEBUG_2, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(DEBUG_2, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PC18

	gpio_set_pin_level(LED0,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(LED0, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(LED0, GPIO_PIN_FUNCTION_OFF);

	// GPIO on PC30

	gpio_set_pin_level(DEBUG_4,
	                   // <y> Initial level
	                   // <id> pad_initial_level
	                   // <false"> Low
	                   // <true"> High
	                   false);

	// Set pin direction to output
	gpio_set_pin_direction(DEBUG_4, GPIO_DIRECTION_OUT);

	gpio_set_pin_function(DEBUG_4, GPIO_PIN_FUNCTION_OFF);

	ADC_0_init();
	ADC_1_init();

	DIGITAL_GLUE_LOGIC_0_init();

	EXTERNAL_IRQ_0_init();

	EVENT_SYSTEM_0_init();

	POSITION_DECODER_0_init();

	SPI_0_init();

	TC_SPEED_CLOCK_init();

	TC_SPEED_init();

	TC_ECAT_CLOCK_init();

	TC_ECAT_init();

	TCC_PWM2_CLOCK_init();

	TCC_PWM2_PORT_init();

	TCC_PWM2_init();

	TCC_PWM_CLOCK_init();

	TCC_PWM_PORT_init();

	TCC_PWM_init();
}
