//----------------------------------------------------
/**
	\file		ctk/inc/canuart.h
	\ingroup	can
	\ingroup	all

	\brief		Knihovna prenasejici CAN zpravy pres UART
				<<< ALL >>>
			
	\author		ConTeK s.r.o., Pavel Bazika
	\version	01.01
	\date		30.11.2007
*/
//----------------------------------------------------

#ifndef CANUART_H_
#define CANUART_H_

#include <cancomm.h>
#include <libsets.h>
#include <ucrc16.h>

#ifdef MSVC
//#pragma pack(show)
#pragma pack(push,1)
//#pragma pack(show)
#endif // MSVC

//----------------------------------------------------
//!	\brief	Buffer pro CAN zpravy posilane pres UART 

typedef struct _CANUART_BUFFER {
	BUFDESC		Desc;
	CAN_MSG		Msg;
	uint16_t	_crc;
#ifdef MSVC
} CANUART_BUFFER;
#else
} __attribute__((packed)) CANUART_BUFFER;
#endif // MSVC

#ifdef MSVC
#pragma pack(pop)
//#pragma pack(show)
#endif // MSVC


//----------------------------------------------------
//!	Kontext ovladace CAN

typedef struct _CANUART_CTX {
	CANCOMMDESC 	Parent;			//!< Rodic
	
	CANUART_BUFFER	* sendBuf;
	COMMDESC		* Cmm;
	
	// puvodni callbacky seriove komunikacni knihovny
	cmReceived		CmmReceived;
	cmSendMsgEnd	CmmSendMsgEnd;

	bool_t			RecvDisabled;
	CANMODE 		Mode;			//!< Rezim ID
	CAN_MB_SETTINGS MBSets[CAN_RX_MBCOUNT + CAN_TX_MBCOUNT];	//!< Nastaveni pouzivanych schranek CAN
} CANUART_CTX;


//-----------------------------------------------------
//!
extern CANUART_CTX		gCANUARTCtx;

//----------------------------------------------------
/**
	\brief	Inicializace CAN

	\param	ctx			Kontext CAN, ktery bude prirazen teto instanci
	\param	uartcmm		Odkaz na pridruzeny uart, pres ktery bude probihat komunikace
	\param	Mode		Udava delku ID

	\return	TRUE		Pokud inicializace probehla v poradku, jinak FALSE
*/
//----------------------------------------------------
bool_t CANUARTInitialize(CANUART_CTX * ctx, COMMDESC * uartcmm, CANMODE Mode);


//----------------------------------------------------
/**
	\brief	Finalizace CAN

	\param	ctx				Kontext CAN
*/
//----------------------------------------------------
void CANUARTFinalize(CANUART_CTX * ctx);

#endif /*CANUART_H_*/
