#ifndef _BOOTLOAD_FUNCTION_H_
#define _BOOTLOAD_FUNCTION_H_

#include "apt32f102.h"
#include "apt32f102_ifc.h"
#include "apt32f102_syscon.h"
#include "uart.h"

#define App_Procedure_Ready         	0x66        //APP准备就绪标志位

#define APP_Flash_PageSize				0x100

#if DBG_LOG_EN
#define APP_Flash_StartAddr				PROM_PageAdd96		//调试APP起始地址
#else
#define APP_Flash_StartAddr				PROM_PageAdd40
#endif

#define APP_Flash_EndAddr				PROM_PageAdd255
#define APP_FEATURE_Flash_Addr			PROM_PageAdd255

#define MCU_EEPROM_PageSize				0x40
#define MCU_EEPROM_StartAddr			DROM_PageAdd0
#define MCU_EEPROM_EndAddr				(DROM_PageAdd31 + MCU_EEPROM_PageSize)

#define BCOMM_CMD_Handshake				0xC0
#define BCOMM_CMD_Jump					0xC1
#define BCOMM_CMD_SetInfo				0xC2
#define BCOMM_CMD_WriteFlash			0xC3
#define BCOMM_CMD_ReadFlash				0xC4
#define BCOMM_CMD_EraseFlash			0xC5
#define BCOMM_CMD_WriteEEPROM			0xC6
#define BCOMM_CMD_ReadEEPROM			0xC7
#define BCOMM_CMD_EraseEEPROM			0xC8
#define BCOMM_CMD_CheckData				0xC9

#define BCOMM_CMD_ReplySUCC				0x00
#define BCOMM_CMD_ReplyFAIL				0x01

#define BCOMM_ACKSize					300
#define BCOMM_ParaSize					280

typedef enum
{
    BCOMM_FMT_TXAddr,
    BCOMM_FMT_SN,
    BCOMM_FMT_TYPE,
    BCOMM_FMT_RXAddr,
	BCOMM_FMT_LEN_L,
	BCOMM_FMT_LEN_H,
	BCOMM_FMT_CKS,
	BCOMM_FMT_CMD,
    BCOMM_FMT_PARAM,
}BOOT_COMM_FMT_e;

typedef struct{
    U8_T  	jump_start;
	U8_T	sn;
	U8_T	cmd;
	U8_T   	pc_addr;
	U8_T 	ackBuffer[BCOMM_ACKSize];
	U16_T	ackLen;
	U16_T   ackValidity;					//数据回复有效期
	
    U32_T 	bootTimeout;
    U32_T 	bootTick;
}BOOT_INFO_T;

typedef struct{
    U8_T     crcL_check;
    U8_T     crcH_check;
    U8_T     app_flag;
    U8_T     app_crc[241];
	U16_T    app_crc_size;
    U16_T    app_crc_len;
	U32_T    app_start_addr;
    U32_T    app_end_addr;
}APP_FEATURE_INFO_T;		/*字节空间是256Byte*/

extern BOOT_INFO_T g_boot;

void Boot_Function_Init(void);
U8_T Boot_Comm_UpgradeProcess(U8_T *data,U16_T len);
void Boot_Time_Refresh(void);
void Boot_TimeOut_Task(void);
void Jump_To_APP(void);
U8_T Check_APP_Feature(void);
#endif
