

#ifndef BOOTLOADER_PROTOCOL_H
#define BOOTLOADER_PROTOCOL_H

#define PROTOCOL_HEADER 0xBA
//#define MAX_SECTOR_SIZE 0x01000
#define MAX_RX_BUFF_SIZE 0x100
#define MAX_TX_BUFF_SIZE 0x100

//typedef struct
//{
//	unsigned int SectorStartAddress;
//	unsigned short SectorSize;
//	unsigned int SectorData[MAX_SECTOR_SIZE];
//
//}stDataSector_t;

enum eBootloaderProtocolStates
{
	PROTOCOL_RX_HEADER,
	PROTOCOL_RX_CMD,
	PROTOCOL_RX_DATA_SIZE_1,
	PROTOCOL_RX_DATA_SIZE_2,
	PROTOCOL_RX_DATA_BYTE1,
	PROTOCOL_RX_DATA_BYTE2,
	PROTOCOL_RX_DATA_BYTE3,
	PROTOCOL_RX_DATA_BYTE4,
	PROTOCOL_RX_CHECKSUM
};

enum eBootloaderProtocolCmds
{
	ACKNOWLEDGE_CMD,
	BOOTLOADER_READY_CMD,	//Sent by the bootoloader at boot. If no answer... jump in app, else stay in update mode
	CONNECT_REQUEST_CMD,		//Sent by the PTE in answer to BOOTLOADER_READY_CMD	to enter boot mode
	BOOTLOADER_UNLOCK_REQUEST_CMD,
	HEARTBEAT_CMD,
	HEARTBEAT_REQUEST_CMD,
	START_UPDATE_REQUEST_CMD,
	SEND_RECORD_REQUEST_CMD,
	END_UPDATE_REQUEST_CMD,
	FLASH_API_VER_CMD,
	FLASH_API_VER_REQUEST_CMD,
	SCRUB_FLASH_REQUEST_CMD,
	DOWNLOAD_FLASH_REQUEST_CMD,
	FLASH_DATA_CMD,
	FIRMWARE_RESET_REQUEST = 0xFA
};

enum eBootloaderProtocolRet
{
	RET_PROTOCOL_OK,
	RET_PROTOCOL_READY,
	RET_PROTOCOL_FLASH_ERASE,
	RET_PROTOCOL_FLASH_WRITE,
	RET_PROTOCOL_ERR_BAD_CHECKSUM,
	RET_PROTOCOL_ERR_DATA_OVERSIZE,
	RET_PROTOCOL_ERR_DATA_BYTES_MISALIGNED,
	RET_PROTOCOL_ERR_UNKNOWN_COMMAND,
	RET_PROTOCOL_ERR_COMMAND_PENDING,
	RET_PROTOCOL_ERR_ILLEGAL_ADDRESS,
	RET_PROTOCOL_ERR_INVALID_UNLOCK_CODE,
	RET_PROTOCOL_ERR_UNLOCK_EXPECTED,
	RET_PROTOCOL_ERR_ERASE_FLASH_FAILED,
	RET_PROTOCOL_ERR_WRITE_FLASH_FAILED,
	RET_PROTOCOL_ERR_INVALID_ADDRESS
};

enum eBootloaderAcknowledgement
{
	PROTOCOL_NACK,
	PROTOCOL_ACK
};


extern Bool e_CommandPending;
extern Bool e_FlashUpdateMode;
extern unsigned char RxDataBuffer[MAX_RX_BUFF_SIZE];
//extern stDataSector_t CurrentDataSector;



void ProtocolInit(void);
void ProtocolRxData( char data );
void ProtocolResetSM(void);
void ProtocolExecCmd();
unsigned int ProtocolSendCmd(enum eBootloaderProtocolCmds Cmd, char *Data, unsigned int DataSize);
void ProtocolSendAck(enum eBootloaderAcknowledgement Ack, enum eBootloaderProtocolRet err);


#endif

//===========================================================================
// End of file.
//===========================================================================
