#include <atmel_start.h>
#include "spi_slave_dma_config.h"
#include "A1335.h"
#include "AS5048.h"

/* Buffer length to transfer/receive */
#define BUFFER_LEN (6)

/* DMA channel Descriptor */
extern DmacDescriptor _descriptor_section[DMAC_CH_NUM];
extern DmacDescriptor _write_back_section[DMAC_CH_NUM];

/* IO descriptor for SPI master */
struct io_descriptor *io_spi_master;

volatile uint8_t spi_master_tx_complete = 0;
volatile uint8_t spi_master_rx_complete = 0;
volatile uint8_t cs_pulled_high         = 0;
volatile uint8_t received_data_len      = 0;

static uint8_t rx_buffer[BUFFER_LEN] = {0};
static uint8_t tx_buffer[BUFFER_LEN] = "Hell0";

static struct timer_task TIMER_0_task1;

static void task1_cb(const struct timer_task *const timer_task)
{
	volatile int i = 0;
	//a1335_State_Machine(&A1);
	as5048a_State_Machine(&AS_1);
	
}

void TIMER_0_init(void)
{
	TIMER_0_task1.interval = 100;
	TIMER_0_task1.cb       = task1_cb;
	TIMER_0_task1.mode     = TIMER_TASK_REPEAT;
	timer_add_task(&TIMER_0, &TIMER_0_task1);
	timer_start(&TIMER_0);
}


/* DMA Transfer complete callback for SPI master TX */
static void spi_master_tx_complete_cb(struct _dma_resource *resource)
{
	/* Transfer completed */
	delay_us(CONF_DELAY_US);
	gpio_set_pin_level(SPI_CS, true);
	spi_master_tx_complete = true;
}

/* DMA Transfer complete callback for SPI master RX */
static void spi_master_rx_complete_cb(struct _dma_resource *resource)
{
	/* Transfer completed */
	gpio_set_pin_level(SPI_CS, true);
	spi_master_rx_complete = true;
}

/* Start SPI master data transfer */
void spi_master_tx(uint8_t *buffer, uint8_t length)
{
	//spi_m_dma_enable(&SPI_0);
	gpio_set_pin_level(SPI_CS, false);
	//io_write(io_spi_master, buffer, length);
	spi_m_dma_transfer(&SPI_0,tx_buffer,rx_buffer,BUFFER_LEN);
	
}

/* Start SPI master data Reception from Slave  */
void spi_master_rx(uint8_t *buffer, uint8_t length)
{
	spi_m_dma_enable(&SPI_0);
	gpio_set_pin_level(SPI_CS, false);
	io_read(io_spi_master, buffer, length);
}

/* SPI master IO and Callback Initialization  */
void spi_master_init(void)
{
	spi_m_dma_get_io_descriptor(&SPI_0, &io_spi_master);
	spi_m_dma_register_callback(&SPI_0, SPI_M_DMA_CB_TX_DONE, spi_master_tx_complete_cb);
	spi_m_dma_register_callback(&SPI_0, SPI_M_DMA_CB_RX_DONE, spi_master_rx_complete_cb);
}


int main(void)
{
	/* Initializes MCU, drivers and middleware */
	atmel_start_init();
	/* Initialize SPI master IO and Callback  */
	//spi_master_init();
	//a1335_init(&SPI_1, &A1);
	as5048a_init(&SPI_1, &AS_1);
	TIMER_0_init();
	
	//spi_m_dma_enable(&SPI_0);
	
	/* Start SPI Master data transfer using DMA */
	//spi_master_rx(rx_buffer, BUFFER_LEN);
	//spi_master_tx(tx_buffer, BUFFER_LEN);
	/* Start SPI Master data reception using DMA */
	//spi_master_rx(tx_buffer, BUFFER_LEN);
	printf("Init Complete\n\r");
	/* Replace with your application code */
	while (1) {
		//delay_ms(1000);
		//tx_buffer[4] = tx_buffer[4]+1;
		////spi_master_rx(rx_buffer, BUFFER_LEN);
		//spi_master_tx(tx_buffer, BUFFER_LEN);
		//
		//if (spi_master_tx_complete) {
			//spi_master_rx_complete = false;
			//printf("Master Sent DATA = ");
			///* Print Received data by SPI Master from SPI Slave on Console */
			//for (int i = 0; i < BUFFER_LEN; i++) {
				//printf("%c", tx_buffer[i]);
			//}
			//printf("\n\r");
				//
		//}
		///* Check for SPI Master is received data from SPI Slave */
		//if (spi_master_rx_complete) {
			//spi_master_rx_complete = false;
			//printf("Master Recieved DATA = ");
			///* Print Received data by SPI Master from SPI Slave on Console */
			//for (int i = 0; i < BUFFER_LEN; i++) {
				//printf("%c", rx_buffer[i]);
			//}
			//printf("\n\r");
		//}		
	}
}
