/*
 * UART_LinkLayer_V10.h
 *
 * Created: 30.8.2015 19:06:44
 *  Author: Chenda
 */ 

#ifndef UART_LINKLAYER_V10_H_
	#define UART_LINKLAYER_V10_H_
	
	#include <stdbool.h>
	
	/************************************************************************/
	/* Link layer protocol version                                          */
	/************************************************************************/
	#define UART_LINK_LAYER_VER		101			// V1.00
	
	/************************************************************************/
	/* Link layer definitions                                               */
	/************************************************************************/
	#define UART_FRAME_START_MARK	0xAB
	#define UART_FRAME_STOP_MARK	0xBA
	#define UART_FRAME_SUBST_MARK	0x80
	#define UART_FRAME_SUBST_0X80	0x80
	#define UART_FRAME_SUBST_0XAB	0x81
	#define UART_FRAME_SUBST_0XBA	0x82
	
	/************************************************************************/
	/* Link layer error number                                              */
	/************************************************************************/
	#define UART_E_NO_ERROR			1
	#define UART_E_FRAME			2
	#define UART_E_FRAME_OVF		3
	#define UART_E_RX_TIMEOUT		4
	#define UART_E_RX_UNEXP_DATA	5			// UnExpected data receiving (missing start mark)
	#define UART_E_RX_START_MARK	6
	#define UART_E_RX_STOP_MARK		7
	#define UART_E_TX_LOW_PAC_LEN	8			// low length of packet (data before encoding and sending to bus)
	#define UART_E_TX_HIG_PAC_LEN	9			// high length of packet (data before encoding and sending to bus)
	#define UART_E_RX_LOW_FRM_LEN	10			// low length of frame (encoded data coming from bus)
	#define UART_E_RX_HIG_FRM_LEN	11			// high length of frame (encoded data coming from bus)
	#define UART_E_RX_DECODING		12
	#define UART_E_RX_EVEN_LEN		13
	#define UART_E_RX_CHKSUM		14
	#define UART_E_RX_MSG_TOUT		15
	
	/************************************************************************/
	/* Frame size max 254 bytes                                             */
	/************************************************************************/
	#define UART_PACKET_LEN_MIN		3
	#define UART_PACKET_LEN_MAX		120
	#define UART_FRAME_LEN_MIN		7
	#define UART_FRAME_LEN_MAX		(UART_PACKET_LEN_MAX*2+3)	// max povolena velikost 253 - ne 255 kvuli pretikani indexu bufferu
	#define UART_DEC_DATA_SIZE		(UART_PACKET_LEN_MAX+2)

	/************************************************************************/
	/* Link layer structure                                                 */
	/************************************************************************/
	
	struct stLL_UARTx
	{
		// Link layer - access only for LinkLayer functions
		// - RX
		struct LinkLayer
		{
			bool IsMasterPeriphery;
			bool DisTxWhileIdle;			// disabling TX if slave bus
			bool DisRxWhileTxOperate;
			bool RS485;					// Set dir before TX and clear after TX finished
										// optionally disable RX while TX operate
			struct LL_Receiver
			{
				volatile uint8_t MissedRxWhileException;
				volatile uint8_t Buff[UART_FRAME_LEN_MAX];
				volatile uint8_t Index;
				volatile bool FrameReceived;
				volatile bool FrameReceiving;
				bool DisableIt;
				// Message timeout
				volatile uint8_t TickForToutError;
				volatile uint8_t CurrentTicks;
				volatile bool ExpectRespondMsg;
				// error handling
				volatile bool E;
				volatile uint8_t E_Nr;
			}RX;
			struct LL_Transmitter
			{
				volatile uint8_t Buff[UART_FRAME_LEN_MAX];
				volatile uint8_t Index;
				volatile bool FrameTransmitted;
				volatile bool FrameTransmitting;
				bool LaunchIt;
				bool DisableIt;
			}TX;	
		}LL;			
		
		// Interface
		struct LinkLayerInterface
		{
			struct Interface_Receiver
			{
				bool Busy;		// informativni hodnota
				uint8_t MissedRx;
				uint8_t Buff[UART_DEC_DATA_SIZE];
				uint16_t PacketLength;
				bool PacketReceived;
				// error handling
				uint8_t E_Nr;
			}RX;
			struct Interface_Transmitter
			{
				bool Busy;						// higher layer - read only 
				uint8_t Buff[UART_DEC_DATA_SIZE];
				uint16_t PacketLength;
				bool PacketTransmitted;
				bool InitiateTransmition;
				// Respond message timeout
				bool ExpectRespond;
				// error handling
				uint8_t E_Nr;
			}TX;
		}LL_Int;		
	};
	

	/************************************************************************/
	/* Link layer functions                                                 */
	/************************************************************************/
	struct stLL_UARTx* UART_LL_U0_INT (void);
	void UART_LL_U0 (void);

#endif /* UART_LINKLAYER_V10_H_ */
