/*
 * A1335.h
 *
 * Created: 24/07/2021 17:53:53
 *  Author: Nick-XMG
 */ 


#ifndef A1335_H_
#define A1335_H_
#include <atmel_start.h>

struct SPI_A1335 {
	struct spi_m_sync_descriptor *SPI_descr;
	struct io_descriptor *io_descr;
	uint8_t state;
	uint32_t SS_pin;
	struct spi_xfer xfer;
	uint8_t *tx_buffer;
	uint8_t *rx_buffer;
};

struct SPI_A1335 A1;
struct io_descriptor *io;

void a1335_init(struct spi_m_sync_descriptor *spi, struct SPI_A1335 *Sn);
void a1335_State_Machine(struct SPI_A1335 *Sn);

uint16_t PrimaryRead(struct SPI_A1335 *Sn, uint16_t address, uint16_t* value);
uint16_t PrimaryWrite(uint16_t cs, uint16_t address, uint16_t value);
uint16_t ExtendedRead(uint16_t cs, uint16_t address, uint32_t value);
uint16_t ExtendedWrite(uint16_t cs, uint16_t address, uint32_t value);
uint16_t SetProcessorStateToRun(uint8_t cs);
uint16_t SetProcessorStateToIdle(uint8_t cs);
uint16_t SetProcessorStateToRun(uint8_t cs);
bool CalculateParity(uint16_t input);
uint8_t CalculateCRC(uint16_t input);
uint16_t a1335_transfer16(struct SPI_A1335 *Sn, uint16_t value);

uint16_t a1335_getRawRotation(struct SPI_A1335 *Sn);
float a1335_getRotationInDegrees(struct SPI_A1335 *Sn);
uint16_t getErrors(void);


#endif /* A1335_H_ */