//----------------------------------------------------
/**
	\file		atsam4e16e/inc/can.h
	\ingroup	platform
	\ingroup	cortex-m4

	\brief		Ovladac CAN periferie
				<<< ATSAM4E16E >>>

	\author		ConTeK s.r.o., Pavel Bazika
	\version	01.01
	\date		07.05.2014
*/
//----------------------------------------------------

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

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


#define CAN_HW_CHANN_COUNT	2	//!< Pocet kanalu CANu


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

typedef struct _CANCTX
{
	CANCOMMDESC 	Parent;		//!< Rodic
	Can *			Channel;	//!< Reference na periferii CAN

	ccmCANTick		_Tick;		//!< Rodicovsky tick

	CANMODE 		Mode;		//!< Rezim ID
	CANSPD			Speed;		//!< Baudrate

	CAN_MB_SETTINGS MBSets[CAN_USED_MBCOUNT];	//!< Nastaveni pouzivanych schranek CAN
	uint8_t			CurTxMB;
	uint8_t			CurTxPriority;			//!< Aktualni priorita
	uint8_t			TxMBCount;				//!< Pocet prave vysilajicich schranek

	uint32_t		IrqConfig;

	// diagnostika chyby
	uint8_t			ErrRxCount;				//!< Citac rx chyb pri busoff
	uint8_t			ErrTxCount;				//!< Citac tx chyb pri busoff

#ifdef CAN_TXONLY
	bool_t			RestartCAN;
#endif
} CANCTX;


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

	\param	_ctx				Kontext CAN, ktery bude prirazen teto instanci
	\param	_Channel			Cislo kanalu CAN
	\param	_Mode				Rezim ID
	\param	_Spd				Baudrate CAN
	\param	_Timeout			Timeout

	\return	TRUE			Pokud inicializace probehla v poradku
*/
//----------------------------------------------------
bool_t CANInitialize(CANCTX * const _ctx,  const uint8_t _Channel, const CANMODE _Mode, const CANSPD _Spd, const ticks_t _Timeout);


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

	\param	_ctx		Kontext CAN
*/
//----------------------------------------------------
void CANFinalize(CANCTX * const _ctx);

#endif // _CAN_H_
