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

	\brief		ovladac obvodu half bridge TLE 6208-3
				<<< ALL >>>

	\author		ConTeK s.r.o., Pavel Bazika, Stanek Jan
	\version	01.10
	\date		12.04.2013
*/
//----------------------------------------------------
#ifndef _BRIDGE_TLE6208_H_
#define _BRIDGE_TLE6208_H_

#include <hwsubmdl.h>
#include <spi.h>
#include <ctktypes.h>
#include <iolib.h>
#include "stmachine.h"


#define HEXBRIDGE_DINP_COUNT	16
#define HEXBRIDGE_DOUTP_COUNT	16
#define HEXBRIDGE_AINP_COUNT	0
#define HEXBRIDGE_AOUTP_COUNT	0


//----------------------------------------------------
//! \name	Mapovani rozhrani hexbridge
//@{

	// hexbridge
#define _HB_DI_1L		6
#define _HB_DI_1H		5
#define _HB_DI_2L		4
#define _HB_DI_2H		3
#define _HB_DI_3L		2
#define _HB_DI_3H		1
#define _HB_DI_4L		0
#define _HB_DI_4H		15
#define _HB_DI_5L		14
#define _HB_DI_5H		13
#define _HB_DI_6L		12
#define _HB_DI_6H		11
#define _HB_DI_TEMP		7
#define _HB_DI_OVLO		10
#define _HB_DI_UVLO		9
#define _HB_DI_PWRFAIL	8


#define _HB_DO_1L	6
#define _HB_DO_1H	5
#define _HB_DO_2L	4
#define _HB_DO_2H	3
#define _HB_DO_3L	2
#define _HB_DO_3H	1
#define _HB_DO_4L	0
#define _HB_DO_4H	15
#define _HB_DO_5L	14
#define _HB_DO_5H	13
#define _HB_DO_6L	12
#define _HB_DO_6H	11


//@}

//----------------------------------------------------
//! Kontext bridge

typedef struct _BRIDGE_CTX
{
	IODEVICE	Device;

	STMACHINE		Stm;			//!< Stavovy automat obsluhy bridge
	bool_t			ResetStatus;	//!< Prepinac pozadavku na reset stavoveho registru bridge
	bool_t			Overcur;		//!< Prepinac nastaveni kontroly overcurrent
	bool_t			OVLO;			//!< Prepinac nastaveni kontroly overload
	bool_t			UVLO;			//!< Prepinac nastaveni kontroly underload

	SPIDESC	*   	Spi;			//!< Kontext pouzivaneho spi
	uint8_t			SpiChannel;		//!< Kanal pouzivaneho SPI
	volatile bool_t SpiReady;		//!< Priznak volneho kanalu spi

	volatile bool_t		flRunning;		//!< Priznak inizializace

	DECL_DMA_STRUCT_ITEM(uint16_t, LastStatusBuf);	//!< Buffer pro cteni statutu
	DECL_DMA_STRUCT_ITEM(uint16_t, Command);		//!< Buffer pro odesilani prikazu
} BRIDGE_CTX;


//----------------------------------------------------
/**
	\brief	Vyresetovani stavu bridge

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeResetStatus(BRIDGE_CTX * const ctx);


//----------------------------------------------------
/**
	\brief	Nastaveni overload stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeSetOverload(BRIDGE_CTX * const _ctx, const bool_t _enable);


//----------------------------------------------------
/**
	\brief	Povoleni detekce overload stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeEnableOverload(BRIDGE_CTX * const _ctx);


//----------------------------------------------------
/**
	\brief	Zruseni overload stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeDisableOverload(BRIDGE_CTX * const _ctx);


//----------------------------------------------------
/**
	\brief	Nastaveni detekce underload stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeSetUnderload(BRIDGE_CTX * const _ctx, const bool_t _enable);


//----------------------------------------------------
/**
	\brief	Povoleni detekce underload stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeEnableUnderload(BRIDGE_CTX * const _ctx);

//----------------------------------------------------
/**
	\brief	Potlaceni detekce underload stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------
//!

void BridgeDisableUnderload(BRIDGE_CTX * const _ctx);


//----------------------------------------------------
/**
	\brief	Nastaveni detekce overcurrent stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeSetOvercurrent(BRIDGE_CTX * const _ctx, const bool_t _enable);


//----------------------------------------------------
/**
	\brief	Povoleni detekce overcurrent stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeEnableOvercurrent(BRIDGE_CTX * const _ctx);


//----------------------------------------------------
/**
	\brief	Potlaceni detekce overcurrent stavu

	\param	ctx		Kontext bridge
*/
//----------------------------------------------------

void BridgeDisableOvercurrent(BRIDGE_CTX * const _ctx);

//----------------------------------------------------
/**
	\brief	Inicializace ovladace bridge

	\param	ctx			Kontext bridge
	\param	spi			Ovladac SPI, ke kteremu je bridge pripojen
	\param	spiChannel	Kanal SPI
*/
//----------------------------------------------------

bool_t BridgeInitialize(BRIDGE_CTX * const ctx, SPIDESC * const spi, const uint8_t spiChannel);


//----------------------------------------------------
/**
	\brief	Finalizace ovladace bridge

	\param	ctx			Kontext bridge
*/
//----------------------------------------------------

void BridgeFinalize(BRIDGE_CTX * const ctx);

#endif /*BRIDGE_TLE6208_H_*/
