#ifndef Communicatewithesc_H_
#define Communicatewithesc_H_
#include "stdint.h"

#define AREA_BYTES                      48
#define parameter_BYTES                 32
#define item_AREA_BYTES                 16

#define SoftwareVer_Str  0x0008A000     //0x3F0000
#define SoftwareVer_End  0x0008A010     //0x3F0010

#define PACKAGE_TYPE                    2           //
#define FULL_PACKAGE_LENGTH             24          //
#define FULL_PACKAGE_DATALENGTH         16          //

#ifndef _CRC_
#define _CRC_
#define CRC_CCITT 0x1021
#define CRC_16 0x8005
#endif

#define TRA_INFORMATION_NO              100  //

#define TRA_ESC_current_NO              96  //
#define REC_Configure_NO                48  //


// Define standard constants
#define TRUE   1
#define FALSE  0

#define CONTION_TIME    10          /



#define CONNECT                         0x00
#define CONNECT_FEADBACK                0X01
#define HAND_SUCC                       0X02
#define HAND_FAIL                       0X03
#define UP_APP                          0x04
#define READ_FRIMWARE_INFORMATION       0x07
#define SEND_CONFIGURE_DATA             0X08
#define READ_CONFIGURE_DATA             0x09
#define SET_DEFAULT                     0x0a
#define UP_COM                          0x0c

#define CONNECT_USB_HEAD                0xc3
#define CONNECT_ESC_HEAD                0xA5



#define CONNECT_USB_END                 0x3c
#define CONNECT_ESC_END                 0x5a


#define REC_IDLE                    0x00            //
#define REC_TRUE                    0x01            //
#define REC_ERROR                   0x02            //
#define REC_END                     0x03            //
#define TRA_NEXT_PACKAGE            0x04            //
#define RE_TRA                      0x05            //
#define REC_HANDSHACK               0x06            //


#define PACKAGE_START_BYTES             1
#define REQUEST_TYPE_BYTES              1
#define PACKAGE_DATA_LENGTH_BYTES       1
#define PACKAGE_NUMBER_BYTES            2           //2 Bytes
#define FULL_PACKAGE_DATA_BYTES         16          //16 bytes
#define PACKAGE_CRC_BYTES               2
#define PACKAGE_END_BYTES               1


#define PACKAGE_START                   0
#define REQUEST_TYPE                    PACKAGE_START_BYTES+PACKAGE_START   //1
#define PACKAGE_DATA_LENGTH             REQUEST_TYPE_BYTES+REQUEST_TYPE     //1+1
#define PACKAGE_NUMBER                  PACKAGE_DATA_LENGTH+PACKAGE_DATA_LENGTH_BYTES//1+1+1            //2 Bytes
#define PACKAGE_DATA                    PACKAGE_NUMBER_BYTES+PACKAGE_NUMBER//1+1+1+2
#define FULL_PACKAGE_CRC                FULL_PACKAGE_DATA_BYTES+FULL_PACKAGE_DATA
#define FULL_PACKAGE_END                PACKAGE_CRC_BYTES+FULL_PACKAGE_CRC


#ifndef _FLASH_PAGE_SIZE_
#define _FLASH_PAGE_SIZE_
#define FLASH_PAGE_SIZE             1024                    //
#endif

#ifndef _MAX_PACKAGE_SIZE_
#define _MAX_PACKAGE_SIZE_
#define MAX_PACKAGE_SIZE            16                  //
#endif

#ifndef _PACKAGE_PR_PAGE_
#define _PACKAGE_PR_PAGE_
#define PACKAGE_PR_PAGE             FLASH_PAGE_SIZE/MAX_PACKAGE_SIZE     //
#endif

#ifndef _RAMTEMP_PR_SIZE_
#define _RAMTEMP_PR_SIZE_
#define RAMTEMP_PR_SIZE             3
#endif

#ifndef _RAMTEMP_TH_SIZE_
#define _RAMTEMP_TH_SIZE_
#define RAMTEMP_TH_SIZE             16
#endif

#define SYSTEM_CHECK_ADDRESS        0x0008FFF8

#define FLASH_START_ADDR            0x0008A000
#define FLASH_END_ADDR              0x0008FFFF

#define  WORDS_IN_FLASH_BUFFER      0x200        // Programming data buffer, Words

typedef struct
{                                               //
    unsigned char PackageHead;
    unsigned char PackageEnd;
}PACKAGE_HEAD_END;


typedef struct {
     unsigned long StartAddr;
     unsigned long EndAddr;
} SECTOR;



#define FRAME_HEADER_START                    0xF5


#define FRAME_REQUEST                         0x00
#define FRAME_RESPOND                         0x01

#define FRAME_FLAG_REQUEST                    0x00
#define FRAME_FLAG_RESPOND                    0x80


#define FRAME_FLAG_NO_ERROR                   0x00
#define FRAME_FLAG_ERROR                      0x40


#define FRAME_FLAG_SHORT_PACKET               0x00
#define FRAME_FLAG_LONG_PACKET                0x20


#define FRAME_DEVICE_TYPE_NONE                0x00
#define FRAME_DEVICE_TYPE_ESC_DRV             0x01  //


#define FRAME_MASK_REQUEST_RESPOND            0x80
#define FRAME_MASK_ERROR_FLAG                 0x40
#define FRAME_MASK_DATA_USE_FLAG              0x20
#define FRAME_MASK_DEVICE_TYPE                0x1F


#define FRAME_TYPE_REQ_UPGRADE                0x00
#define FRAME_TYPE_LINK                       0x01
#define FRAME_TYPE_GET_DEVICE_INFO            0x02
#define FRAME_TYPE_ERASE_FLASH_AERA           0x03
#define FRAME_TYPE_ERASE_FLASH_FULL           0x04
#define FRAME_TYPE_FRAME_DATA_DEF             0x05
#define FRAME_TYPE_UPGRADE_DATA               0x06
#define FRAME_TYPE_IMAGE_CHECK                0x07


#define FRAME_ERR_NONE                        0x00
#define FRAME_ERR_CHECK                       0x01
#define FRAME_ERR_DATA_LOST                   0x02
#define FRAME_ERR_PACKET_LOST                 0x03
#define FRAME_ERR_DEVICE_MISMATHING           0x04
#define FRAME_ERR_MSGTYPE_NODEFINE            0x05
#define FRAME_ERR_DATA_NO_ANIAN_16            0x06
#define FRAME_ERR_FLASH_WRITE_OVR             0x07
#define FRAME_ERR_UNKNOW                      0xFF


#define PACKET_NUMBER_GET_INFO                0x00
#define PACKET_NUMBER_MAX                     0x01




typedef struct
{
    unsigned long     ComImageCheckSize;        //
    unsigned int      ComImageCheckCrcDecode;   //
    unsigned int      ComImageCreatTimeYear;    //
    unsigned int      ComImageCreatTimeMonthDay;    //
//  unsigned char     ComImageCreatTimeDay;     //
    unsigned int      ComImageCreatTimeHourMin; //
//  unsigned char     ComImageCreatTimeMin;     //
    unsigned int      Reserved;                 //
    unsigned int      ComImageCheckCrcEncode;   //
}FLASH_IMGINFO_STRUCT;

typedef struct
{
    FLASH_IMGINFO_STRUCT     *ImageInfoRO;
}SYSTEM_INFOMATION_STRUCT;


typedef struct
{
    unsigned char       Statr;          //
    unsigned char       Identify;       //
    unsigned char       IdentifyNot;    //
    unsigned char       MsgType;        //
    unsigned char       ErrorCode;      //
}UPGRADE_PACKET_HEADER_STRUCT;

typedef struct
{
    unsigned char       Length[2];
    unsigned char       Number[2];
    unsigned char       Data[1024];
}UPGRADE_PACKET_BODY_STRUCT;

typedef struct
{
    UPGRADE_PACKET_HEADER_STRUCT *pHeader;
    UPGRADE_PACKET_BODY_STRUCT   *pBody;
    unsigned int                 *pCheck;
}UPGRADE_STRUCT;




typedef struct
{
    UPGRADE_STRUCT                              Rx;
    UPGRADE_STRUCT                              Tx;
    unsigned char                                                       AutoRequestUpdate;
    unsigned int                                                        Number;
    unsigned int                                                        RxPacketNumberLast;
    unsigned int                                                        RxPacketNumberNow;
    unsigned long                           FlashWriteAddress;

    unsigned long                                                       dTotalImageSize;
    unsigned long                                                       dTotalPacketNumber;
    unsigned long                                                       dSinglePacketNumber;
    unsigned char                                                       AppImageCheckStatus;

}UPGRADE_CTRL;


typedef struct                      //
{
    unsigned char ReportID;
    unsigned char Buffer[1152];
}_CommunicateData;
extern _CommunicateData CommunicateData;
#define CONFIGURE_DATA_AREA             0x03                //

extern void UpgradeRequest(unsigned char MsgType, unsigned char DeviceType);
extern unsigned char SystemApplicationChecking(void);
extern unsigned char Read_First_From_Flash(void);

extern unsigned char ReadConfDataMode;  //

extern signed int          TimeOutFlag;
extern unsigned short int     Tim_OutCon;
extern unsigned short int     bRecESCTimeout;

extern unsigned long    rx_count;
extern unsigned char          Uart_time_out;
extern unsigned char          Uart_time_out_en;
extern unsigned char          Uart_st;

extern unsigned long           boot_time_out;
extern unsigned long           power_sw_time;
extern unsigned char           boot_time_out_en;
extern unsigned char           updata_over;


extern unsigned char TraData[TRA_INFORMATION_NO]; //

extern unsigned char debug_Counter;
extern unsigned char ProgrameCardRecDataFlag;
extern unsigned short int wflash_ok;
extern unsigned short int first_time;

extern unsigned char TraDataNo;

extern void Send_Connect(void);
extern void Send_HandSucc(void);
extern void Connect_ESC_Send_Inf(void);

extern void Tra_ESC(unsigned char *pData,unsigned char uNumBytes);
extern void Go_to_app(void);
extern void read_flagflash(void);

extern UPGRADE_CTRL   UpgradeCtrl;

#endif
