#ifndef __MAIN__H
#define __MAIN__H
#include "bsp.h"

#define	CRC16_BASE											0xA001

#define	APP_START_FLAG									0x55AA5A5A
#define	APP_UPDATE_FLAG									0xA5A5AA55

typedef enum {
	UPDATE_CMD_NULL,
	UPDATE_CMD_SLAVE_RES,					//从机应答主机下发
	UPDATE_CMD_SLAVE_REQ_DATA,		//从机请求主机下发数据
	UPDATE_CMD_MASTER_REQ = 0x81,	//主机请求升级
	UPDATE_CMD_MASTRE_RES_DATA,	//主机发送数据
}UpdateCmd_m;

//帧头结构体
typedef struct {
	uint8_t Header;
	uint8_t DevMac[6];
	uint8_t Cmd;		
	uint8_t PayloadLen;
}__attribute__ ((packed))FrameHeader_t, *FrameH;


//主机下发升级请求载荷结构体
typedef struct {
	uint16_t PackageNum;
	uint32_t AppSize;
	uint32_t AppCrc32;
}__attribute__ ((packed))UpDataRequset_t, *UDReq;

//从机请求下发结构体
typedef struct {
	uint16_t PackageIndex;
	uint32_t PackageNum;
}__attribute__ ((packed))GetDataRequest_t, *GDReq;

//主机应答下发数据结构体
typedef struct {
	uint16_t PackageIndex;
	uint16_t PackageNum;
	uint8_t DataLen;
}__attribute__ ((packed))DownResponse_t, *DownRes;

typedef enum {
	BL_IDLE,
	BL_ERASE_FLASH,
	BL_GET_DATA,
	BL_WAIT_REV,
	BL_GET_DATA_TIMEOUT,
	BL_START_APP,
	BL_RESET
}BootStatus_m;

typedef struct {
	BootPara_t Para;
	BootStatus_m Status;
	
	uint32_t AppPackageCnt;
	uint32_t AppPackageIdx;
	uint32_t RevErrCnt;
	
	uint32_t FeedDogDlyCnt;
	uint32_t BootLoader1mSDlyCnt;
	
	bool RxFlag;
	uint32_t UartRevTimeOutCnt;
	uint8_t RxBuff[260];
	uint16_t RxLen;
	uint16_t UpDateFlag;
}BootLoader_t;
#endif
