//----------------------------------------------------
/**
	\file		ctk/inc/xbus.h
	\ingroup	xbus
	\ingroup	all

	\brief		Knihovna pristupu na sbernici XBus
				<<< ALL >>>
			
	\author		ConTeK s.r.o., Ing. Vladimir Kastner
	\version	01.02
	\date		15.01.2007
*/
//----------------------------------------------------

#ifndef _XBUS_H_
#define _XBUS_H_		//!< Definice pritomnosti knihovny

#include <libsets.h>
#include <ctktypes.h>
#include <sync.h>
#include <hwmodel.h>

#define MAX_IO_BLOCK_SIZE	256		//!< velikost IO prostoru sbernice XBus


//----------------------------------------------------
//!	\name	Porty zakladniho modulu
//!	\todo	sjednotit s dokumentaci
//@{

#define XBUS_REG_CTL		0xFF	//!< Ridici registr base modulu
#define XBUS_REG_STS		0xFF	//!< Stavovy registr base modulu

#define XBUS_BIT_CTL_EN		0x01	//!< Priznak povoleni desky v CTL registru
#define XBUS_BIT_CTL_IEN	0x02	//!< Priznak povoleni preruseni v CTL registru

#define XBUS_BIT_STS_TEST	0x02	//!< Priznak pro detekci desky
#define XBUS_BIT_CTL_TEST	0x02

#define XBUS_BIT_STS_EN		0x01	//!< Stav povoleni desky
#define XBUS_BIT_STS_INTA	0x02	//!< Priznak INTA v STS registru
#define XBUS_BIT_STS_INTB	0x04	//!< Priznak INTB v STS registru
//@}

//----------------------------------------------------
//!	\name	Zamykani sbernice
//@{

typedef flock_t		xbuslock_t;
#define	XBusLock	FastLock
#define	XBusUnlock	FastUnlock
//@}

//----------------------------------------------------
//!	Typ adresy na sbernici xbus

typedef uint8_t xaddr_t;


//----------------------------------------------------
//!	Cislo preruseni

typedef enum _XBUS_IRQ
{
	XBUS_IRQ_A = 0,
	XBUS_IRQ_B
} XBUS_IRQ;


//----------------------------------------------------
//!	Typ masky preruseni

typedef uint8_t xbus_irqmask_t;


static xbus_irqmask_t XBusIrqMaskBit(const XBUS_IRQ _irq) _const;
static inline xbus_irqmask_t XBusIrqMaskBit(const XBUS_IRQ _irq)
{
	return (1 << _irq);
}

//----------------------------------------------------
//!	Priorita prerusovaci rutiny

typedef enum _XBUS_IRQ_PRIORITY
{
	XBUS_IRQPRIO_0, 
	XBUS_IRQPRIO_1, 
	XBUS_IRQPRIO_2, 
	XBUS_IRQPRIO_3, 
	XBUS_IRQPRIO_4, 
	XBUS_IRQPRIO_5, 
	XBUS_IRQPRIO_6, 
	XBUS_IRQPRIO_7,
	XBUS_IRQPRIO_8, 
	XBUS_IRQPRIO_9, 
	XBUS_IRQPRIO_10, 
	XBUS_IRQPRIO_11, 
	XBUS_IRQPRIO_12, 
	XBUS_IRQPRIO_13, 
	XBUS_IRQPRIO_14, 
	XBUS_IRQPRIO_15
} XBUS_IRQ_PRIORITY;


//----------------------------------------------------
//!	Priorita preruseni

//typedef uint16_t irqPriority_t;


//----------------------------------------------------
/**
	\brief	Prototyp rutiny pro obsluhu preruseni
*/
//----------------------------------------------------

typedef void (*xbus_irq_handler_t) (void * const _userData);


//----------------------------------------------------
//!	Definice typu statistiky XBusu

typedef struct _XBUS_STATISTICS
{
	uint32_t	Timeouts;
	uint32_t	Interrupts;
	uint32_t	AddressWrites;
    uint32_t	DataWrites;
    uint32_t	DataReads;
    uint32_t	Reserved1;
    uint32_t	Reserved2;
    uint32_t	Reserved3;
} XBUS_STATISTICS;


//----------------------------------------------------
//!	\name	Jednoduche pristupy ke sbernici
//@{

//----------------------------------------------------
/**
	\brief	Cteni byte ze sbernice

	\param	_addr	adresa na sbernici

	\return	hodnota prectena z dane adresy
*/
//----------------------------------------------------

uint8_t XBusInpB(const xaddr_t _addr);


//----------------------------------------------------
/**
	\brief	Zapis byte na sbernici

	\param	_addr	adresa na sbernici
	\param	_value	Zapisovana hodnota
*/
//----------------------------------------------------

void XBusOutB(const xaddr_t _addr, uint8_t _value);


//----------------------------------------------------
/**
	\brief	Cteni wordu ze sbernice

	\param	_addr	adresa na sbernici

	\return	hodnota prectena z dane adresy
*/
//----------------------------------------------------

uint16_t XBusInpW(xaddr_t _addr);


//----------------------------------------------------
/**
	\brief	Zapis wordu na sbernici

	\param	_addr	adresa na sbernici
	\param	_value	zapisovana hodnota
*/
//----------------------------------------------------

void XBusOutW(xaddr_t _addr, uint16_t _value);


//----------------------------------------------------
/**
	\brief	Cteni double wordu ze sbernice

	\param	_addr	adresa na sbernici

	\return	hodnota prectena z dane adresy
*/
//----------------------------------------------------

uint32_t XBusInpD(xaddr_t _addr);


//----------------------------------------------------
/**
	\brief	Zapis double wordu na sbernici

	\param	_addr	adresa na sbernici
	\param	_value	zapisovana hodnota
*/
//----------------------------------------------------

void XBusOutD(xaddr_t _addr, uint32_t _value);
//@}

//----------------------------------------------------
//!	\name	Blokove pristupy ke sbernici
//@{

//----------------------------------------------------
/**
	\brief	Cteni bloku dat byte z jednoho portu sbernice

	\param	_addr	adresa na sbernici
	\param	_count	pocet ctenych dat
	\param	_buffer	adresa bufferu pro nactena data
*/
//----------------------------------------------------

void XBusInpBuffB(xaddr_t _addr, uint32_t _count, uint8_t * _buffer);


//----------------------------------------------------
/**
	\brief	Zapis bloku dat byte z jednoho portu sbernice

	\param	_addr	adresa na sbernici
	\param	_count	pocet zapisovanych dat
	\param	_buffer	adresa bufferu se zapisovanymi daty
*/
//----------------------------------------------------

void XBusOutBuffB(xaddr_t _addr, uint32_t _count, uint8_t * _buffer);


//----------------------------------------------------
/**
	\brief	Cteni bloku dat word z jednoho portu sbernice

	\param	_addr	adresa na sbernici
	\param	_count	pocet ctenych dat
	\param	_buffer	adresa bufferu pro nactena data
*/
//----------------------------------------------------

void XBusInpBuffW(xaddr_t _addr, uint32_t _count, uint16_t * _buffer);


//----------------------------------------------------
/**
	\brief	Zapis bloku dat word do jednoho portu sbernice

	\param	_addr	adresa na sbernici
	\param	_count	pocet zapisovanych dat
	\param	_buffer	adresa bufferu se zapisovanymi daty
*/
//----------------------------------------------------

void XBusOutBuffW(xaddr_t _addr, uint32_t _count, uint16_t * _buffer);


//----------------------------------------------------
/**
	\brief	Cteni bloku dat double word z jednoho portu sbernice

	\param	_addr	adresa na sbernici
	\param	_count	pocet ctenych dat
	\param	_buffer	adresa bufferu pro nactena data
*/
//----------------------------------------------------

void XBusInpBuffD( xaddr_t _addr, uint32_t _count, uint32_t * _buffer);


//----------------------------------------------------
/**
	\brief	Zapis bloku dat double word do jednoho portu sbernice

	\param	_addr	adresa na sbernici
	\param	_count	pocet zapisovanych dat
	\param	_buffer	adresa bufferu se zapisovanymi daty
*/
//----------------------------------------------------

void XBusOutBuffD(xaddr_t _addr, uint32_t _count, uint32_t * _buffer);


//----------------------------------------------------
/**
	\brief	Cteni bloku dat byte ze souvisleho bloku portu sbernice

	\param	_addr	pocatecni adresa na sbernici
	\param	_count	pocet ctenych dat
	\param	_buffer	adresa bufferu pro nactena data
*/
//----------------------------------------------------

void XBusInpBlock(xaddr_t _addr,  uint32_t _count, uint8_t * _buffer);


//----------------------------------------------------
/**
	\brief	Zapis bloku dat byte do souvisleho bloku portu sbernice

	\param	_addr	pocatecni adresa na sbernici
	\param	_count	pocet zapisovanych dat
	\param	_buffer	adresa bufferu se zapisovanymi daty
*/
//----------------------------------------------------

void XBusOutBlock(xaddr_t _addr, uint32_t _count, uint8_t * _buffer);
//@}

//----------------------------------------------------
//!	\name	Nastaveni a stav sbernice XBus
//@{

//----------------------------------------------------
/**
	\brief	Zjisteni chyby na sbernici

	\return	V pripade chyby sbernice navraci TRUE.

	\note	Jen pri slozitejsich pristupech ke sbernici (LptXBus).
*/
//----------------------------------------------------

bool_t XBusError(void);


//----------------------------------------------------
/**
	\brief	Zjisteni statistiky XBusu

	\param	_statistics adresa struktury pro ulozeni statistiky

	\return	navraci TRUE, probehlo-li zjisteni v poradku
*/
//----------------------------------------------------

bool_t XBusGetStatistics(const XBUS_STATISTICS * const _statistics);


//----------------------------------------------------
/**
	\brief	Cteni masky povolenych preruseni

	\return	maska povolenych preruseni
*/
//----------------------------------------------------

xbus_irqmask_t XBusGetIrqMask(void);


//----------------------------------------------------
/**
	\brief	Povoleni urcenych preruseni

	\param	_irqMask	maska povolovanych preruseni

	\return	maska preruseni, ktera se volanim povolila
*/
//----------------------------------------------------

xbus_irqmask_t XBusEnableIrq(const xbus_irqmask_t _irqMask);


//----------------------------------------------------
/**
	\brief	Zakazani urcenych preruseni

	\param	_irqMask	maska zakazovanych preruseni

	\return	maska preruseni, ktera se volanim zakazala
*/
//----------------------------------------------------

xbus_irqmask_t XBusDisableIrq(const xbus_irqmask_t _irqMask);


//----------------------------------------------------
/**
	\brief	Instalace ovladace maskovatelneho preruseni

	\param	_irq			cislo obsluhovaneho preruseni
	\param	_priority		priorita obsluhy
	\param	_irqHandler		ovladac preruseni
	\param	_userData		data predana rutine obsluhy preruseni

	\return	navraci TRUE probehla-li instalace v poradku
*/
//----------------------------------------------------

bool_t XBusInstallIrqHandler(const XBUS_IRQ _irq, const XBUS_IRQ_PRIORITY _priority, const xbus_irq_handler_t _irqHandler, void * const _userData);


//----------------------------------------------------
/**
	\brief	Odebrani ovladace maskovatelneho preruseni

	\param	_irq			cislo obsluhovaneho preruseni
	\param	_irqHandler		ovladac preruseni

	\return	navraci TRUE probehlo-li odebrani v poradku
*/
//----------------------------------------------------

bool_t XBusUninstallIrqHandler(const XBUS_IRQ _irq, const xbus_irq_handler_t _irqHandler);


//----------------------------------------------------
/**
	\brief	Test pritomnosti Xm desky na dane adrese
	
	\param	_addr	adresa desky na sbernici

	\return	navraci TRUE, je-li deska pritomna
*/
//----------------------------------------------------

bool_t XBusTestBd(const xaddr_t _addr);


//----------------------------------------------------
/**
	\brief	Provede povoleni sbernice, ukonceni signalu RESET

	\return	V pripade chyby sbernice navraci TRUE.
*/
//----------------------------------------------------

bool_t XBusEnable(void);


//----------------------------------------------------
/**
	\brief	Provede zakazani sbernice, nastaveni signalu RESET

	\return	V pripade chyby sbernice navraci TRUE.
*/
//----------------------------------------------------

bool_t XBusDisable(void);



//----------------------------------------------------
/**
	\brief	Zjisti, zda je sbenice povolena

	\return	navraci TRUE, je-li sbernice povolena
*/
//----------------------------------------------------

bool_t XBusEnabled(void);


//----------------------------------------------------
/**
	\brief	Provede reset sbernice

	\return	V pripade chyby sbernice navraci TRUE.
*/
//----------------------------------------------------

bool_t XBusReset(void);


//----------------------------------------------------
/**
	\brief	Inicializace pristupu k sbernici XBus

	\return	navraci TRUE, probehla-li inicializace v poradku
*/
//----------------------------------------------------

bool_t XBusInitialize(void);


//----------------------------------------------------
/**
	\brief	Zjisti, zda je knihovna inicializovana

	\return	navraci TRUE, je-li knihovna nainicialializovana
*/
//----------------------------------------------------

bool_t XBusInitialized(void);


//----------------------------------------------------
/**
	\brief	Ukonceni pristupu k sbernici XBus
			Provede reset sbernice.
*/
//----------------------------------------------------

void XBusFinalize(void);
//@}

#endif // _XBUS_H_
