//----------------------------------------------------
/**
	\file		ctk/hw/dac_tlc5628.h
	\ingroup	hardware
	\ingroup	all

	\brief		Knihovna obsluhy obvodu DAC TLC5628
				<<< ALL >>>
	
	\author		ConTeK s.r.o., Ing. Pavel Bazika
	\version	01.01
	\date		17.3.2009
*/
//----------------------------------------------------

#ifndef _DAC_TLC5628_H_
#define _DAC_TLC5628_H_

#include <libsets.h>
#include <spi.h>

//----------------------------------------------------
//!	Stav DMA prenosu dvou hodnot do DAC

typedef enum _DAC_STATE
{
	DST_MIDDLE = 0,		//!< Prvni hodnota prenesena
	DST_END				//!< Obe hodnoty preneseny
} DAC_STATE;


//----------------------------------------------------
//!	Kontext DAC

typedef struct _DAC_CTX
{
	SPIDESC		* Spi;				//!< SPI, na kterem je DAC pripojen
	uint8_t		Channel;			//!< Kanal SPI
	AT91S_PIO	*pioLOAD;			//!< Periferie s pinem LOAD
	uint32_t	LOAD;				//!< Pin LOAD
	AT91S_PIO	*pioLDAC;			//!< Periferie s pinem LDAC
	uint32_t	LDAC;				//!< Pin LDAC

#ifdef SPI_IRQ_OVERRIDE
	uint16_t	TxBuf1;				//!< Buffer pro prvni prenasena data pres DMA
	uint16_t	TxBuf2;				//!< Buffer pro druha prenasena data pres DMA
	uint16_t	RxBuf;				//!< Pomocny buffer pro prijem
	volatile bool_t		DMAInitialized;		//!< Priznak inicializace DMA
	volatile DAC_STATE	State;			//!< Stav prenosu pres DMA
#endif

} DAC_CTX;


#ifdef SPI_IRQ_OVERRIDE

//----------------------------------------------------
/**
	\brief	Prikaz k prenosu dvou hodnot do dac pres DMA

	\param	ctx		Kontext DAC
	\param	dac1	Adresa DAC vytsupu pro 1.data
	\param	data1	Hodnota 1. dat
	\param	dac2	Adresa DAC vystupu pro 2.data
	\param	data2	Hodnota 2.dat

	\return		TRUE, pokud byla transakce zarazena ke zpracovani
*/
//----------------------------------------------------

bool_t DAC_DMA_Prepare(DAC_CTX * ctx, uint8_t dac1, uint8_t data1, uint8_t dac2, uint8_t data2);

#endif

//----------------------------------------------------
/**
	\brief	Funkce nahraje data do registru DAC, ale nepropaguje hodnotu na vystup

	\param	ctx		Kontext DAC
	\param	dac		Adresa vystupu DAC
	\param	data	Nastavovana hodnota
*/
//----------------------------------------------------

void DAC_Prepare(DAC_CTX * ctx, uint8_t dac, uint8_t data);


//----------------------------------------------------
/**
	\brief	Funkce propaguje predem nastavena data na vystup
*/
//----------------------------------------------------

void DAC_Trigger(DAC_CTX * ctx);


//----------------------------------------------------
/**
	\brief	Funkce nahraje data do registru DAC a propaguje hodnotu na vystup

	\param	ctx		Kontext DAC
	\param	dac		Adresa vystupu DAC
	\param	data	Nastavovana hodnota
*/
//----------------------------------------------------

void DAC(DAC_CTX * ctx, uint8_t dac, uint8_t data);


#ifdef SPI_IRQ_OVERRIDE

//----------------------------------------------------
/**
	\brief	Inicializace moznosti rychleho prenosu hodnot pres DMA

	\param	ctx		Kontext DAC
*/
//----------------------------------------------------

void DACDMAInitialize(DAC_CTX * ctx);


//----------------------------------------------------
/**
	\brief	Finalizace moznosti rychleho prenosu hodnot pres DMA

	\param	ctx		Kontext DAC
*/
//----------------------------------------------------

void DACDMAFinalize(DAC_CTX * ctx);

#endif


//----------------------------------------------------
/**
	\brief	Inicializace DAC

	\param	ctx			Kontext DAC
	\param	Spi			SPI, na kterem je pripojen DAC
	\param	Channel		Kanal SPI, na kterem je pripojen DAC
	\param	Load		Pin LOAD
	\param	LDac		Pin LDAC
*/
//----------------------------------------------------

void DACInitialize(DAC_CTX * ctx,
		SPIDESC * Spi,
		uint8_t Channel,
		AT91S_PIO * pioLOAD,
		uint32_t Load,
		AT91S_PIO * pioLDAC,
		uint32_t LDac);


//----------------------------------------------------
/**
	\brief	Finalizace DAC

	\param	ctx		Kontext DAC
*/
//----------------------------------------------------

void DACFinalize(DAC_CTX * ctx);

#endif /* _DAC_TLC5628_H_ */
