/*############################################################################################*/
/*############################################################################################*/
/**
 * \file    CR95HF.c
 * \author  Thibaut LALUQUE 3D-Ouest
 * \date    25/11/2014
 * \version 1.0
 * \brief Source file for CR95HF Driver.
 * \details All Right Reserved.
 */
/*############################################################################################*/
/*############################################################################################*/


/*############################################################################################*/
/*## INCLUDES ################################################################################*/
/*############################################################################################*/
#include "CR95HF.h"
/*############################################################################################*/
/*## END INCLUDES ############################################################################*/
/*############################################################################################*/


/*############################################################################################*/
/*## LOCAL DEFINES ###########################################################################*/
/*############################################################################################*/
#define CR95HF_FRAME_CMD_ECHO					({0x55})
#define CR95HF_FRAME_CMD_ECHO_SIZE				(1)
#define CR95HF_FRAME_ANS_ECHO					({0x55})
#define CR95HF_FRAME_ANS_ECHO_SIZE				(1)
/*############################################################################################*/
/*## END LOCAL DEFINES ######################################################################*/
/*############################################################################################*/


/*############################################################################################*/
/*## LOCAL FONCTIONS DECLARATION #############################################################*/
/*############################################################################################*/
void CR95HFWriteCommand(char* command, unsigned short commandSize, char* answerExpected, unsigned short answerSize, unsigned short timeoutMs, unsigned short completionTimeMs, unsigned short waitForNbrRx);
void CR95HFReadAnswer (void);
signed short CR95HFBufCompare (char* dataBuf, unsigned short dataBufSize, char* dataBufRef, unsigned short dataBufRefSize);
void CR95HFCommandManager (void);

void CR95HFEchoStateMachine (void);
void CR95HFFieldOffStateMachine (void);
void CR95HFField15693StateMachine (void);
void CR95HFHibernateStateMachine (void);
void CR95HFReadStateMachine (void);
void CR95HFWriteStateMachine (void);
void CR95HFBridgeStateMachine (void);
/*############################################################################################*/
/*## END LOCAL FONCTIONS DECLARATION #########################################################*/
/*############################################################################################*/


/*############################################################################################*/
/*## LOCAL VARIABLES #########################################################################*/
/*############################################################################################*/

/*############################################################################################*/
/*## END LOCAL VARIABLES #####################################################################*/
/*############################################################################################*/

/*############################################################################################*/
/*## GLOBAL VARIABLES ########################################################################*/
/*############################################################################################*/
tCR95HFCtrl CR95HFCtrl;
/*############################################################################################*/
/*## END GLOBAL VARIABLES ####################################################################*/
/*############################################################################################*/


/*############################################################################################*/
/*## LOCAL FONCTIONS #########################################################################*/
/*############################################################################################*/
void CR95HFWriteCommand(char* command, unsigned short commandSize, char* answerExpected, unsigned short answerSize, unsigned short timeoutMs, unsigned short completionTimeMs, unsigned short waitForNbrRx) {

	/*=== Copy answer expected into working buffer ===*/
	unsigned short i;
	for(i=0 ; i<answerSize ; i++) {
		CR95HFCtrl.answerKeyWord[i] = *(answerExpected+i);
	}
	CR95HFCtrl.answerSize = answerSize;

	/*=== set completion time ===*/
	CR95HFCtrl.completionTimeMs = completionTimeMs;

	/*=== Set timeout ===*/
	CR95HFCtrl.timeoutMs = timeoutMs;


	/*=== Clear Rx receiver ===*/
	CR95UartDisableReceiver();
	CR95UartResetReceiver();
	StrTool_ClearString(CR95HFCtrl.receiveBuf, CR95HF_RX_BUF_SIZE);
	CR95HFCtrl.rxPos = 0;

	/*=== Set nbr Rx Byte ===*/
	CR95HFCtrl.waitForNbrRx = waitForNbrRx;

	/*=== Get time (ms) ref ===*/
	CR95HFCtrl.refMs = TickGetMsCounter();

	/*=== Set Working flag ===*/
	CR95HFCtrl.flags.bits.isWorking =1;
	CR95HFCtrl.flags.bits.timeout = 0;
	CR95HFCtrl.flags.bits.anwserReceived = 0;
	CR95HFCtrl.flags.bits.sizeReceive = 0;

	CR95UartEnableReceiver();

	CR95UartSendData(command, commandSize);
}

void CR95HFReadAnswer (void) {

	char read;
	tUartCtrl* CR95HFUartCtrl;

	CR95HFUartCtrl = CR95UartGetUartStructCtrl();

	while(CR95HFUartCtrl->rx.nbrChar != 0) {
		read = CR95UartReadChar();
		CR95HFCtrl.receiveBuf[CR95HFCtrl.rxPos++] = read;
	}
}


signed short CR95HFBufCompare (char* dataBuf, unsigned short dataBufSize, char* dataBufRef, unsigned short dataBufRefSize) {

	unsigned short findId = 0;
	unsigned short id = 0;
	unsigned short eqId = 0xFFFF;

	while( id < dataBufSize) {

		if(findId < dataBufRefSize) {

			if(*(dataBufRef+findId) == *(dataBuf+id)) {
				findId++;
				if(eqId == 0xFFFF){
					eqId = id;
				}
			} else {
				eqId = 0xFFFF;
				findId = 0;
			}
		} else {
			return eqId;
		}
		id++;
	}
	if(findId == dataBufRefSize) {
		return eqId;
	} else {
		return -1;
	}
}


void CR95HFCommandManager (void)  {

	switch(CR95HFCtrl.command) {

	case CR95HF_COMMAND_NONE :
		//Wait State -> Do nothing
		break;

	case CR95HF_COMMAND_ECHO :
		CR95HFEchoStateMachine();
		break;

	case CR95HF_COMMAND_FIELDOFF :
		CR95HFFieldOffStateMachine();
		break;

	case CR95HF_COMMAND_FIELD15693 :
		CR95HFField15693StateMachine();
		break;

	case CR95HF_COMMAND_HIBERNATE :
		CR95HFHibernateStateMachine();
		break;

	case CR95HF_COMMAND_READ :
		CR95HFReadStateMachine();
		break;

	case CR95HF_COMMAND_WRITE :
		CR95HFWriteStateMachine();
		break;

	case CR95HF_COMMAND_BRIDGE :
		CR95HFBridgeStateMachine();
		break;

	default :
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		break;

	}
}

void CR95HFEchoStateMachine (void) {
	static unsigned char tryCounter = 0;
	char data[1];

	switch(CR95HFCtrl.commandState) {

	case CR95HF_EchoState_Start:
		tryCounter = 0;
		CR95HFCtrl.commandState = CR95HF_EchoState_Send;
		// No break;

	case CR95HF_EchoState_Send:
		data[0] = 0x55;
		CR95HFWriteCommand(data,1,data,1,50,0,0);
		CR95HFCtrl.commandState = CR95HF_EchoState_WaitAnswer;
		break;

	case CR95HF_EchoState_WaitAnswer:
		if(CR95HFCtrl.flags.bits.timeout) {
			tryCounter++;
			if(tryCounter < CR95HF_ECHO_MAX_TRY) {
				CR95HFCtrl.commandState = CR95HF_EchoState_Send;
			} else {
				CR95HFCtrl.resultCode = CR95HF_RETURN_ECHO_ERROR;
				CR95HFCtrl.commandState = CR95HF_EchoState_Finish;
			}
		} else {		//Answer receive
			CR95HFCtrl.resultCode = CR95HF_RETURN_OK;
			CR95HFCtrl.commandState = CR95HF_EchoState_Finish;
		}
		break;

	case CR95HF_EchoState_Finish:
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.flags.all = 0;
		break;

	default:	//ERROR
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.resultCode = CR95HF_RETURN_STATEMACHINE_ERROR;
		break;
	}
}

void CR95HFFieldOffStateMachine (void) {
	char data[4];
	char dataResult[2];

	switch(CR95HFCtrl.commandState) {

	case CR95HF_FieldOffState_Start:
		CR95HFCtrl.commandState = CR95HF_FieldOffState_Send;
		// No break;

	case CR95HF_FieldOffState_Send:
		data[0] = 0x02;
		data[1] = 0x02;
		data[2] = 0x00;
		data[3] = 0x00;
		dataResult[0] = 0x00;
		dataResult[1] = 0x00;
		CR95HFWriteCommand(data,4,dataResult,2,500,0,0);
		CR95HFCtrl.commandState = CR95HF_FieldOffState_WaitAnswer;
		break;

	case CR95HF_FieldOffState_WaitAnswer:
		CR95HFCtrl.commandState = CR95HF_EchoState_Finish;
		if(CR95HFCtrl.flags.bits.timeout) {
				CR95HFCtrl.resultCode = CR95HF_RETURN_FIELDOFF_ERROR;
		} else {		//Answer receive
			CR95HFCtrl.resultCode = CR95HF_RETURN_OK;
		}
		break;

	case CR95HF_FieldOffState_Finish:
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.flags.all = 0;
		break;

	default:	//ERROR
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.resultCode = CR95HF_RETURN_STATEMACHINE_ERROR;
		break;
	}
}


void CR95HFField15693StateMachine (void) {
	char data[4];
	char dataResult[2];

	switch(CR95HFCtrl.commandState) {

	case CR95HF_Field15693State_Start:
		CR95HFCtrl.commandState = CR95HF_Field15693State_Send;
		// No break;

	case CR95HF_Field15693State_Send:
		data[0] = 0x02;
		data[1] = 0x02;
		data[2] = 0x01;
		data[3] = 0x0D;
		dataResult[0] = 0x00;
		dataResult[1] = 0x00;
		CR95HFWriteCommand(data,4,dataResult,2,500,0,0);
		CR95HFCtrl.commandState = CR95HF_Field15693State_WaitAnswer;
		break;

	case CR95HF_Field15693State_WaitAnswer:
		CR95HFCtrl.commandState = CR95HF_Field15693State_Finish;
		if(CR95HFCtrl.flags.bits.timeout) {
				CR95HFCtrl.resultCode = CR95HF_RETURN_FIELD15693_ERROR;
		} else {		//Answer receive
			CR95HFCtrl.resultCode = CR95HF_RETURN_OK;
		}
		break;

	case CR95HF_Field15693State_Finish:
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.flags.all = 0;
		break;

	default:	//ERROR
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.resultCode = CR95HF_RETURN_STATEMACHINE_ERROR;
		break;
	}
}

void CR95HFHibernateStateMachine (void) {
	char data[16];

	switch(CR95HFCtrl.commandState) {

	case CR95HF_HibernateState_Start:
		CR95HFCtrl.commandState = CR95HF_HibernateState_Send;
		// No break;

	case CR95HF_HibernateState_Send:
		data[0] = 0x07;
		data[1] = 0x0E;
		data[2] = 0x08;
		data[3] = 0x04;
		data[4] = 0x00;
		data[5] = 0x04;
		data[6] = 0x00;
		data[7] = 0x18;
		data[8] = 0x00;
		data[9] = 0x00;
		data[10] = 0x00;
		data[11] = 0x00;
		data[12] = 0x00;
		data[13] = 0x00;
		data[14] = 0x00;
		data[15] = 0x00;
		CR95HFWriteCommand(data,16,data,0,50,150,0);	//End with timeout
		CR95UartDisableReceiver();		//Disable Uart to rpevent loop interrupt
		CR95HFCtrl.commandState = CR95HF_HibernateState_WaitAnswer;
		break;

	case CR95HF_HibernateState_WaitAnswer:
		CR95HFCtrl.commandState = CR95HF_HibernateState_Finish;
		CR95HFCtrl.resultCode = CR95HF_RETURN_OK;
		break;

	case CR95HF_HibernateState_Finish:
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.flags.all = 0;
		break;

	default:	//ERROR
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.resultCode = CR95HF_RETURN_STATEMACHINE_ERROR;
		break;
	}
}


void CR95HFReadStateMachine (void) {
	char data[6];

	switch(CR95HFCtrl.commandState) {

	case CR95HF_ReadState_Start:
		CR95HFCtrl.commandState = CR95HF_ReadState_Send;
		// No break;

	case CR95HF_ReadState_Send:
		data[0] = 0x04;
		data[1] = 0x04;
		data[2] = 0x0A;
		data[3] = 0x20;
		data[4] = (char)((CR95HFCtrl.deviceRegAddr >> 0) & 0xFF);
		data[5] = (char)((CR95HFCtrl.deviceRegAddr >> 8) & 0xFF);

		CR95HFWriteCommand(data,6,data,0,200,25,10);	//End with nbr Rx

		CR95HFCtrl.commandState = CR95HF_ReadState_WaitAnswer;
		break;

	case CR95HF_ReadState_WaitAnswer:
		if(CR95HFCtrl.flags.bits.sizeReceive) {
			CR95HFCtrl.deviceReg = ((((unsigned long)CR95HFCtrl.receiveBuf[3]) << 0) & 0x000000FF) |
					((((unsigned long)CR95HFCtrl.receiveBuf[4]) << 8) & 0x0000FF00) |
					((((unsigned long)CR95HFCtrl.receiveBuf[5]) << 16) & 0x00FF0000) |
					((((unsigned long)CR95HFCtrl.receiveBuf[6]) << 24) & 0xFF000000);
			CR95HFCtrl.commandState = CR95HF_ReadState_Finish;
			CR95HFCtrl.resultCode = CR95HF_RETURN_OK;
		} else {
			CR95HFCtrl.commandState = CR95HF_ReadState_Finish;
			CR95HFCtrl.resultCode = CR95HF_RETURN_READ_ERROR;
		}
		break;

	case CR95HF_ReadState_Finish:
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.flags.all = 0;
		break;

	default:	//ERROR
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.resultCode = CR95HF_RETURN_STATEMACHINE_ERROR;
		break;
	}
}

void CR95HFWriteStateMachine (void) {
	char data[10];
	char dataResult[6];

	switch(CR95HFCtrl.commandState) {

	case CR95HF_WriteState_Start:
		CR95HFCtrl.commandState = CR95HF_WriteState_Send;
		// No break;

	case CR95HF_WriteState_Send:
		data[0] = 0x04;
		data[1] = 0x08;
		data[2] = 0x0A;
		data[3] = 0x21;
		data[4] = (char)((CR95HFCtrl.deviceRegAddr >> 0) & 0xFF);
		data[5] = (char)((CR95HFCtrl.deviceRegAddr >> 8) & 0xFF);
		data[6] = (char)((CR95HFCtrl.deviceReg >> 0) & 0xFF);
		data[7] = (char)((CR95HFCtrl.deviceReg >> 8) & 0xFF);
		data[8] = (char)((CR95HFCtrl.deviceReg >> 16) & 0xFF);
		data[9] = (char)((CR95HFCtrl.deviceReg >> 24) & 0xFF);

		dataResult[0] = 0x80;
		dataResult[1] = 0x04;
		dataResult[2] = 0x00;
		dataResult[3] = 0x78;
		dataResult[4] = 0xF0;
		dataResult[5] = 0x00;

		CR95HFWriteCommand(data,10,dataResult,6,200,25,0);	//End with nbr Rx

		CR95HFCtrl.commandState = CR95HF_WriteState_WaitAnswer;
		break;

	case CR95HF_WriteState_WaitAnswer:
		if(CR95HFCtrl.flags.bits.timeout) {
			CR95HFCtrl.commandState = CR95HF_WriteState_Finish;
			CR95HFCtrl.resultCode = CR95HF_RETURN_WRITE_ERROR;
		} else {
			CR95HFCtrl.commandState = CR95HF_WriteState_Finish;
			CR95HFCtrl.resultCode = CR95HF_RETURN_OK;
		}
		break;

	case CR95HF_WriteState_Finish:
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.flags.all = 0;
		break;

	default:	//ERROR
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.resultCode = CR95HF_RETURN_STATEMACHINE_ERROR;
		break;
	}
}


void CR95HFBridgeStateMachine (void) {
	char dataResult[2];

	switch(CR95HFCtrl.commandState) {

	case CR95HF_BridgeState_Start:
		CR95HFCtrl.commandState = CR95HF_BridgeState_Send;
		// No break;

	case CR95HF_BridgeState_Send:

		dataResult[0] = 0x87;
		dataResult[1] = 0x00;

		CR95HFWriteCommand(CR95HFCtrl.bridgeCommand,CR95HFCtrl.bridgeCommandLength,dataResult,2,50,25,0);

		CR95HFCtrl.commandState = CR95HF_BridgeState_WaitAnswer;
		break;

	case CR95HF_BridgeState_WaitAnswer:
		CR95HFCtrl.commandState = CR95HF_BridgeState_Finish;
		CR95HFCtrl.resultCode = CR95HF_RETURN_OK;
		break;

	case CR95HF_BridgeState_Finish:
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.flags.all = 0;
		break;

	default:	//ERROR
		CR95HFCtrl.flags.all = 0;
		CR95HFCtrl.rxPos = 0;
		CR95HFCtrl.refMs = 0;
		CR95HFCtrl.timeoutMs = 0;
		CR95HFCtrl.completionTimeMs = 0;
		CR95HFCtrl.command = CR95HF_COMMAND_NONE;
		CR95HFCtrl.commandState = 0;
		CR95HFCtrl.resultCode = CR95HF_RETURN_STATEMACHINE_ERROR;
		break;
	}
}
/*############################################################################################*/
/*## END LOCAL FONCTIONS #####################################################################*/
/*############################################################################################*/


/*############################################################################################*/
/*## EXTERNAL FONCTIONS ######################################################################*/
/*############################################################################################*/
void CR95HFInit (void) {
	CR95UartInit(CR95HF_UART_BAUDRATE);

	CR95HFCtrl.flags.all = 0;
	CR95HFCtrl.rxPos = 0;
	CR95HFCtrl.refMs = 0;
	CR95HFCtrl.timeoutMs = 0;
	CR95HFCtrl.completionTimeMs = 0;
	CR95HFCtrl.command = CR95HF_COMMAND_NONE;
	CR95HFCtrl.commandState = 0;
}


void CR95HFManager (void) {

	unsigned long timeElapsed = 0;

	if(CR95HFCtrl.flags.bits.isWorking) {
		timeElapsed = TickMilliSecElapsedFrom(CR95HFCtrl.refMs);

		if(timeElapsed > CR95HFCtrl.timeoutMs) {
			CR95HFCtrl.flags.bits.timeout = 1;
		}
	}

	if(CR95HFCtrl.flags.all == 0x0001) {		//Only working flag

		if(timeElapsed >= CR95HFCtrl.completionTimeMs) {

			CR95HFReadAnswer();

			if(CR95HFCtrl.waitForNbrRx == 0) {

				if(CR95HFBufCompare(CR95HFCtrl.receiveBuf, CR95HFCtrl.rxPos, CR95HFCtrl.answerKeyWord, CR95HFCtrl.answerSize) != -1) {
					CR95HFCtrl.flags.bits.anwserReceived = 1;
				}
			} else {

				if((CR95HFCtrl.rxPos + 1) > CR95HFCtrl.waitForNbrRx) {
					CR95HFCtrl.flags.bits.sizeReceive = 1;
				}
			}
		}
	} else {
		CR95HFCommandManager();
	}
}



void CR95HFStartEcho (void) {

	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {
		CR95HFCtrl.resultCode = CR95HF_RETURN_WORKING;
		CR95HFCtrl.command = CR95HF_COMMAND_ECHO;
		CR95HFCtrl.commandState = CR95HF_EchoState_Start;
		CR95HFCtrl.flags.all = 0;
	}
}

void CR95HFStartFieldOff (void) {

	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {
		CR95HFCtrl.resultCode = CR95HF_RETURN_WORKING;
		CR95HFCtrl.command = CR95HF_COMMAND_FIELDOFF;
		CR95HFCtrl.commandState = CR95HF_FieldOffState_Start;
		CR95HFCtrl.flags.all = 0;
	}
}

void CR95HFStartField15693 (void) {

	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {
		CR95HFCtrl.resultCode = CR95HF_RETURN_WORKING;
		CR95HFCtrl.command = CR95HF_COMMAND_FIELD15693;
		CR95HFCtrl.commandState = CR95HF_Field15693State_Start;
		CR95HFCtrl.flags.all = 0;
	}
}


void CR95HFStartHibernate (void) {

	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {
		CR95HFCtrl.resultCode = CR95HF_RETURN_WORKING;
		CR95HFCtrl.command = CR95HF_COMMAND_HIBERNATE;
		CR95HFCtrl.commandState = CR95HF_HibernateState_Start;
		CR95HFCtrl.flags.all = 0;
	}
}

void CR95HFStartRead (unsigned short addr) {

	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {
		CR95HFCtrl.deviceRegAddr = addr;
		CR95HFCtrl.resultCode = CR95HF_RETURN_WORKING;
		CR95HFCtrl.command = CR95HF_COMMAND_READ;
		CR95HFCtrl.commandState = CR95HF_ReadState_Start;
		CR95HFCtrl.flags.all = 0;
	}
}

void CR95HFStartWrite (unsigned short addr, unsigned long data) {

	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {
		CR95HFCtrl.deviceRegAddr = addr;
		CR95HFCtrl.deviceReg = data;
		CR95HFCtrl.resultCode = CR95HF_RETURN_WORKING;
		CR95HFCtrl.command = CR95HF_COMMAND_WRITE;
		CR95HFCtrl.commandState = CR95HF_WriteState_Start;
		CR95HFCtrl.flags.all = 0;
	}
}

void CR95HFStartBridge (char* data, unsigned short dataLength) {

	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {

		unsigned char i;

		for(i=0 ; i<dataLength ; i++) {
			CR95HFCtrl.bridgeCommand[i] = *(data+i);
		}

		CR95HFCtrl.bridgeCommandLength = dataLength;

		CR95HFCtrl.resultCode = CR95HF_RETURN_WORKING;
		CR95HFCtrl.command = CR95HF_COMMAND_BRIDGE;
		CR95HFCtrl.commandState = CR95HF_BridgeState_Start;
		CR95HFCtrl.flags.all = 0;
	}
}

tCR95HFIsReady CR95HFManagerIsReady (void) {
	if(CR95HFCtrl.command == CR95HF_COMMAND_NONE) {
		return CR95HF_IS_READY;
	} else {
		return CR95HF_IS_BUSY;
	}
}
/*############################################################################################*/
/*## END EXTERNAL FONCTIONS ##################################################################*/
/*############################################################################################*/
