/* 
 * File:   ChaletPowerRelay.h
 * Author: JF
 *
 * Created on November 30, 2018, 7:33 PM
 */

#ifndef BOOTLOADERINTERFACE_H
#define	BOOTLOADERINTERFACE_H
#include "define.h"


#define BOOTLOADER_FILE_HEADER_CODE (int)0xBAADBEEF 
#define BOOTLOADER_RECORD_HEADER_CODE (int)0xDEADBEEF 
#define BOOTLOADER_FLASH_FIRMWARE_VALIDATOR 0xA8
#define BOOTLOADER_ENTER_UPLOAD_MODE_VALIDATOR 0x5A

typedef struct
{
	unsigned char BootloaderAction;
	unsigned char BootloaderActionValidator;
	
}stBootloaderFlags_t;

typedef struct
{
    unsigned int Firmwareflags;
    unsigned int NbRecords;
    unsigned int FirmwareSize;
    unsigned int Versioncode;
    unsigned int DataCRC32;
}stStoredBootloaderInfo;

extern stBootloaderFlags_t mBootloaderFlags;
stStoredBootloaderInfo mStoredBootloaderInfo;

enum eBootloaderFlagsIndex
{
    BOOTLOADER_FLAGS_ACTION_FLAG_INDEX = 0,
    BOOTLOADER_FLAGS_ACTION_VALIDATOR_INDEX,
    
    
    BOOTLOADER_FLAGS_MAX_INDEX = 12
};

enum eBootloaderActionFlagValues
{
    BOOTLOADER_ACTION_JUMP_TO_APP_VALUE = 0,
    BOOTLOADER_ACTION_FLASH_FIRMWARE_VALUE = 1,
    BOOTLOADER_ACTION_ENTER_UPDATE_MODE_VALUE = 2,
    
    BOOTLOADER_ACTION_MAX_VALUE
    
};


enum eBootloaderStates
{
    BOOTLOADER_STANDBY_STATE,
    BOOTLOADER_ACTIVE_STATE,
    BOOTLOADER_ERASE_FLASH_STATE,
    BOOTLOADER_RECEIVING_FIRMWARE_STATE,
    BOOTLOADER_SENDING_FIRMWARE_COPY_STATE,
    BOOTLOADER_PRINTING_FIRMWARE_STATE,
    
    BOOTLOADER_MAX_STATE            
};

enum eBootloaderStateMachineEvents
{
    BOOTLOADER_TICK_EVENT,
    BOOTLOADER_NEW_CMD_EVENT,
    BOOTLOADER_TIMEOUT_EVENT,
    
    BOOTLOADER_MAX_EVENT
};

enum eBootloaderFlashEraseStates
{
    BOOTLOADER_FLASH_ERASE_SECTOR_STATE,
    BOOTLOADER_FLASH_ERASE_WAIT_FOR_SECTOR_DONE,
    BOOTLOADER_FLASH_ERASE_CHECKBACK_STATE,
    BOOTLOADER_FLASH_ERASE_FINISHED_STATE,
    BOOTLOADER_FLASH_ERASE_ERROR_STATE,
    
    BOOTLOADER_FLASH_ERASE_MAX_STATE
};

enum eBootloaderFlashEraseResults
{
    BOOTLOADER_FLASH_ERASE_RUNNING_RES,
    BOOTLOADER_FLASH_ERASE_FINISHED_RES,
    BOOTLOADER_FLASH_ERASE_ERROR_RES,
    BOOTLOADER_FLASH_ERASE_ABORT_RES,
    
    BOOTLOADER_FLASH_ERASE_MAX_RES
};

enum eBootloaderFlahsEraseSMEvents
{
    BOOTLOADER_FLASH_ERASE_SM_TICK_EVENT,
    BOOTLOADER_FLASH_ERASE_SM_ABORT_EVENT,
    
    BOOTLOADER_FLASH_ERASE_SM_MAX_EVENT
};

enum eBootloaderFlashWriteStates
{
    BOOTLOADER_FLASH_WRITE_STANDBY_STATE,
    BOOTLOADER_FLASH_WRITE_BUFFER_STATE,
    BOOTLOADER_FLASH_WRITE_WAIT_FOR_BYTE_DONE,
    BOOTLOADER_FLASH_WRITE_CHECKBACK_STATE,
    BOOTLOADER_FLASH_WRITE_FINISHED_STATE,
    BOOTLOADER_FLASH_WRITE_ERROR_STATE,
    
    BOOTLOADER_FLASH_WRITE_MAX_STATE
};

enum eBootloaderFlashWriteResults
{
    BOOTLOADER_FLASH_WRITING_RES,
    BOOTLOADER_FLASH_WRITE_FINISHED_RES,
    BOOTLOADER_FLASH_WRITE_ERROR_RES,
    BOOTLOADER_FLASH_WRITE_ABORT_RES,
    
    BOOTLOADER_FLASH_WRITE_MAX_RES
};

enum eBootloaderFlahsWriteSMEvents
{
    BOOTLOADER_FLASH_WRITE_SM_TICK_EVENT,
    BOOTLOADER_FLASH_WRITE_SM_NEW_BUFFER_EVENT,
    BOOTLOADER_FLASH_WRITE_SM_ABORT_EVENT,
    
    BOOTLOADER_FLASH_WRITE_SM_MAX_EVENT
};


enum eBootloaderStateMachineCmds
{
    BOOTLOADER_SM_ACTIVATE_CMD,
    BOOTLOADER_SM_ABORT_CMD,
    BOOTLOADER_SM_ERASE_FLASH_CMD,
    BOOTLOADER_SM_INIT_UPLOAD_CMD,
    BOOTLOADER_SM_NEW_DATA_CHUNK_CMD,
    BOOTLOADER_SM_UPLOAD_FINISHED_CMD,
    BOOTLOADER_SM_EXECUTE_UPGRAGE_CMD,
    BOOTLOADER_SM_INVALID_CRC_CMD,
    BOOTLOADER_SM_CHECK_FLASH_CMD,
    BOOTLOADER_SM_GET_FIRMWARE_DATA_CMD
};


extern unsigned char BootloaderBuffer[300];

int BootloaderInterfaceInit();
void BootloaderExecuteCmd(char Cmd,bool CRCValid);
void BootloaderCRCError(char Cmd, int RxCRC, int ExpectedCRC);


void BootloaderInterfaceTick();
void BootloaderInterfaceStateMachine(int Event, int Param);
void BootloaderResetStateMachine();
void BootloaderActivateBootloader();
void BootloaderDeactivateBootloader();

int BootloaderFlashEraseStateMachine(int event);
int ResetBootloaderFlashEraseStateMachine();

int BootloaderFlashWriteStateMachine(int event);
int ResetBootloaderFlashWriteStateMachine();

int BootloaderCheckFlashBootloaderData();
int BootloaderPrintFlashData();

int BootloaderBytesToInt(unsigned char *Bytes);
int BootloaderIntToBytes(unsigned char *Buf, unsigned int Input);



#endif	/* BOOTLOADERINTERFACE_H */

