#include <atmel_start.h>
#include "AS5048A_dma.h"

#define CONF_DELAY_US 10

#define BUFFER_LEN (6)
static uint8_t rx_buffer[BUFFER_LEN] = {0};
static uint8_t tx_buffer[BUFFER_LEN] = "Hello";



/* 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_SS, 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_SS, true);
}



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

/* SPI master IO and Callback Initialization  */
void spi_master_init(void)
{
	spi_m_dma_enable(&SPI_0);
	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();
	//spi_master_init();
	as5048a_init();
	//spi_m_dma_enable(&SPI_0);	
		//io_write(io_spi_master, tx_buffer, BUFFER_LEN );
	
	/* Replace with your application code */
	as5048a_read(AS5048A_CLEAR_ERROR_FLAG);
	while (1) {
		delay_ms(1000);
// 		gpio_set_pin_level(SPI_SS, false);
// 		spi_m_dma_transfer(&SPI_0, tx_buffer, rx_buffer, BUFFER_LEN );
		
		//as5048a_read(AS5048A_CLEAR_ERROR_FLAG);
		//delay_ms(100);
		//as5048a_read(AS5048A_CMD_NOP);
		
		//uint16_t raw_angle = (( ( in_buf[1] & 0xFF ) << 8 ) | ( in_buf[0] & 0xFF )) & ~0xC000);
	
		printf("%u\n\r", as5048a_read(AS5048A_ANGLE));
		//printf("%i\n\r", as5048a_read(AS5048A_ANGLE));
		
		
		//delay_ms(100);
		//as5048a_read(AS5048A_CMD_NOP);

	
	}
}