#ifndef __IAP_COMMUNICATE_H
#define __IAP_COMMUNICATE_H

//***------- <<< Use Configuration Wizard in Context Menu >>> ------------------

// <o>串口选择
// <i>串口选择：切换串口号后，需要手动修改InitGPIO()端口初始化确保升级或运行过程中不会误点灯或开MOS，另外带IAP/ISP的工程也需要同步切换
// <1=>Uart0 : P0.6 For RXD0,P0.7 For TXD0		 UART0CR=32(uart0默认)
// <2=>Uart0 : P0.6 For RXD0,P2.4 For TXD0		 UART0CR=42
// <3=>Uart0 : P0.6 For RXD0,P2.5 For TXD0       UART0CR=52
// <4=>Uart0 : P0.6 For RXD0,P0.0 For TXD0       UART0CR=02
// <5=>Uart0 : P0.6 For RXD0,P0.1 For TXD0       UART0CR=12
// <6=>Uart0 : P0.7 For RXD0,P0.6 For TXD0		 UART0CR=23
// <7=>Uart0 : P0.7 For RXD0,P2.4 For TXD0       UART0CR=43
// <8=>Uart0 : P0.7 For RXD0,P2.5 For TXD0       UART0CR=53
// <9=>Uart0 : P0.7 For RXD0,P0.0 For TXD0       UART0CR=03
// <10=>Uart0 : P0.7 For RXD0,P0.1 For TXD0      UART0CR=13
// <11=>Uart0 : P2.4 For RXD0,P0.6 For TXD0      UART0CR=24
// <12=>Uart0 : P2.4 For RXD0,P0.7 For TXD0      UART0CR=34
// <13=>Uart0 : P2.4 For RXD0,P2.5 For TXD0      UART0CR=54
// <14=>Uart0 : P2.4 For RXD0,P0.0 For TXD0      UART0CR=04
// <15=>Uart0 : P2.4 For RXD0,P0.1 For TXD0      UART0CR=14
// <16=>Uart0 : P2.5 For RXD0,P0.6 For TXD0      UART0CR=25
// <17=>Uart0 : P2.5 For RXD0,P0.7 For TXD0      UART0CR=35
// <18=>Uart0 : P2.5 For RXD0,P2.4 For TXD0      UART0CR=45
// <19=>Uart0 : P2.5 For RXD0,P0.0 For TXD0      UART0CR=05
// <20=>Uart0 : P2.5 For RXD0,P0.1 For TXD0      UART0CR=15
// <21=>Uart0 : P0.0 For RXD0,P0.7 For TXD0      UART0CR=30
// <22=>Uart0 : P0.0 For RXD0,P2.4 For TXD0      UART0CR=40
// <23=>Uart0 : P0.0 For RXD0,P2.5 For TXD0      UART0CR=50
// <24=>Uart0 : P0.0 For RXD0,P0.6 For TXD0      UART0CR=20
// <25=>Uart0 : P0.0 For RXD0,P0.1 For TXD0      UART0CR=10
// <26=>Uart0 : P0.1 For RXD0,P0.7 For TXD0      UART0CR=31
// <27=>Uart0 : P0.1 For RXD0,P2.4 For TXD0      UART0CR=41
// <28=>Uart0 : P0.1 For RXD0,P2.5 For TXD0      UART0CR=51
// <30=>Uart0 : P0.1 For RXD0,P0.6 For TXD0      UART0CR=21
// <29=>Uart0 : P0.1 For RXD0,P0.0 For TXD0      UART0CR=01

// <31=>Uart1 : P1.0 For RXD1,P1.1 For TXD1      UART1CR=10(uart1默认)
// <32=>Uart1 : P1.0 For RXD1,P3.3 For TXD1      UART1CR=40
// <33=>Uart1 : P1.0 For RXD1,P3.4 For TXD1      UART1CR=50
// <34=>Uart1 : P1.0 For RXD1,P2.6 For TXD1      UART1CR=20
// <35=>Uart1 : P1.0 For RXD1,P2.7 For TXD1      UART1CR=30
// <36=>Uart1 : P1.1 For RXD1,P1.0 For TXD1      UART1CR=01
// <37=>Uart1 : P1.1 For RXD1,P3.3 For TXD1      UART1CR=41
// <38=>Uart1 : P1.1 For RXD1,P3.4 For TXD1      UART1CR=51
// <39=>Uart1 : P1.1 For RXD1,P2.6 For TXD1      UART1CR=21
// <40=>Uart1 : P1.1 For RXD1,P2.7 For TXD1      UART1CR=31
// <41=>Uart1 : P3.3 For RXD1,P1.0 For TXD1      UART1CR=04
// <42=>Uart1 : P3.3 For RXD1,P1.1 For TXD1      UART1CR=14
// <43=>Uart1 : P3.3 For RXD1,P3.4 For TXD1      UART1CR=54
// <44=>Uart1 : P3.3 For RXD1,P2.6 For TXD1      UART1CR=24
// <45=>Uart1 : P3.3 For RXD1,P2.7 For TXD1      UART1CR=34
// <46=>Uart1 : P3.4 For RXD1,P1.0 For TXD1      UART1CR=05
// <47=>Uart1 : P3.4 For RXD1,P1.1 For TXD1      UART1CR=15
// <48=>Uart1 : P3.4 For RXD1,P3.3 For TXD1      UART1CR=45
// <49=>Uart1 : P3.4 For RXD1,P2.6 For TXD1      UART1CR=25
// <50=>Uart1 : P3.4 For RXD1,P2.7 For TXD1      UART1CR=35
// <51=>Uart1 : P2.6 For RXD1,P1.0 For TXD1      UART1CR=02
// <52=>Uart1 : P2.6 For RXD1,P1.1 For TXD1      UART1CR=12
// <53=>Uart1 : P2.6 For RXD1,P3.3 For TXD1      UART1CR=42
// <54=>Uart1 : P2.6 For RXD1,P3.4 For TXD1      UART1CR=52
// <55=>Uart1 : P2.6 For RXD1,P2.7 For TXD1      UART1CR=32
// <56=>Uart1 : P2.7 For RXD1,P1.0 For TXD1      UART1CR=03
// <57=>Uart1 : P2.7 For RXD1,P1.1 For TXD1      UART1CR=13
// <58=>Uart1 : P2.7 For RXD1,P3.3 For TXD1      UART1CR=43
// <59=>Uart1 : P2.7 For RXD1,P3.4 For TXD1      UART1CR=53
// <60=>Uart1 : P2.7 For RXD1,P2.6 For TXD1      UART1CR=23

// <61=>uart2 : P2.2 For RXD2,P2.1 For TXD2
#define UART_DEFINE			   1

// <o>波特率选择
// <0=>9600
// <1=>115200
#define UART_BRT			    0

#if     UART_BRT == 0
    #define	UART_BPS_H		0x7F
    #define	UART_BPS_L		0x64
    #define	UART_BPS_F		0x04
	#elif   UART_BRT == 1
    #define	UART_BPS_H		0x7F
    #define	UART_BPS_L		0xF3
    #define	UART_BPS_F		0x00
#endif

//*<<< end of configuration section >>>

#if ((UART_DEFINE >= 0)&&(UART_DEFINE<= 30))
    #define UartRxEn()          INSCON = 0x00; REN = 1;                                     //UART可以接收数据
    #define UartTxEn(TxData)    INSCON = 0x00; REN = 0; SBUF = TxData;                      //UART发送数据
#elif ((UART_DEFINE >= 31)&&(UART_DEFINE<= 60))
    #define UartRxEn()          INSCON = 0x40; REN1 = 1; INSCON = 0x00;                     //UART可以接收数据
    #define UartTxEn(TxData)    INSCON = 0x40; REN1 = 0; SBUF1 = TxData; INSCON = 0x00;     //UART发送数据
#elif UART_DEFINE == 61
    #define UartRxEn()          INSCON = 0x40; REN2 = 1; INSCON = 0x00;                     //UART可以接收数据
    #define UartTxEn(TxData)    INSCON = 0x40; REN2 = 0; SBUF2 = TxData; INSCON = 0x00;     //UART发送数据
#endif

#define IAP_FLG					0x5A
#define ISP_FLG					0xA5

#define IAP_MODE                0
#define ISP_MODE                1

//for 485 communication
#define HEARD1                  0x00
#define HEARD2                  0x01
#define LENGTH                  0x02
#define SOURCE                  0x03
#define TARGET                  0x04
#define COMMAND                 0x05
#define INDEXES                 0x06
#define DATA                    0x07

#define IAP_BMSID				0x07		//电池管理系统1
#define IAP_PCID				0x3D        //上位机系统

//IAP&ISP Command
#define IAP_CMD_HANDSHAKE		0x06
#define IAP_CMD_BEGIN			0x07
#define IAP_CMD_TRANS			0x08
#define IAP_CMD_VERIFY			0x09
#define IAP_CMD_RESET			0x0A

//CMD_IAP_ACK error
#define IAPERROR_SIZE           0x01        //固件大小超范围
#define IAPERROR_ERASE          0x02        //擦除flash失败
#define IAPERROR_WR				0x03        //写入flash失败
#define IAPERROR_UNLOCK         0x04        //车辆未处于锁车状态，未处于可更新固件状态
#define IAPERROR_INDEX          0x05        //数据索引错误
#define IAPERROR_BUSY           0x06        //IAP正忙
#define IAPERROR_FORM           0x07        //数据格式错误（非8的整数倍）
#define IAPERROR_CRC            0x08        //数据校验失败
#define IAPERROR_RESET          0x09        //芯片复位失败
#define IAPERROR_HANDSHAKE		0x0A		//握手失败


#define IAPERROR_CHECKSUM		0x80
#define IAPERROR_ADDR			0x40
#define IAPERROR_CMD			0x30


extern BOOL	bIapIspFlg;
extern BOOL bHandsheakOkFlg;

extern void BootIapIsp(void);

#endif