/********************************************************************************
Copyright (C), Sinowealth Electronic. Ltd.
Author: 	Sino
Version: 	V0.0
Date: 		2020/04/26
History:
	V2.0	2020/04/26		 Preliminary
********************************************************************************/
#include "MCURegister.h"
#include "C51_TYPE.H"
#include "Flash.h"
#include "IapIsp.h"
#include "INTRINS.H"



/*************************************************************************************************
* 函数名: InitGPIO
* 参  数: 无
* 返回值: 无
* 描  述: 只需要初始化和供电系统相关的IO端口
*************************************************************************************************/
void InitGPIO(void)
{
    P0 = 0xF8;
	P0CR = 0x8F;
    P0PCR = 0xC8;
    
    P1 = 0x80;
	P1CR = 0x7F;
    P1PCR = 0x80;

	P2 = 0xE0;
	P2CR = 0x3F;
    P2PCR = 0xC0;

	P3 = 0x19;
	P3CR = 0xE6;
    P3PCR = 0x19;
	
	#if (UART_DEFINE == 1)
	P0 |= 0xC0;
	P0PCR |= 0x80;
	#endif
	
	#if	(UART_DEFINE == 2)
	P0 |= 0x40;
	P2 |= 0x10;
	P2PCR |= 0x10;
	#endif
	
	#if	(UART_DEFINE == 3)
	P0 |= 0x40;
	P2 |= 0x20;
	P2PCR |= 0x20;
	#endif	
	
	#if	(UART_DEFINE == 4)
	P0 |= 0x41;
	P0PCR |= 0x01;
	#endif
	
	#if	(UART_DEFINE == 5)
	P0 |= 0x42;
	P0PCR |= 0x02;
	#endif
	
	#if	(UART_DEFINE == 6)
	P0 |= 0xC0;
	P0PCR |= 0x40;
	#endif
	
	#if	(UART_DEFINE == 7)
	P0 |= 0x80;
	P2 |= 0x10;
	P2PCR |= 0x10;
	#endif

	#if	(UART_DEFINE == 8)
	P0 |= 0x80;
	P2 |= 0x20;
	P2PCR |= 0x20;
	#endif
	
	#if	(UART_DEFINE == 9)
	P0 |= 0x81;
	P0PCR |= 0x01;
	#endif

	#if	(UART_DEFINE == 10)
	P0 |= 0x82;
	P0PCR |= 0x02;
	#endif
	
	#if	(UART_DEFINE == 11)
	P0 |= 0x40;
	P2 |= 0x10;
	P0PCR |= 0x40;
	#endif	

	#if	(UART_DEFINE == 12)
	P0 |= 0x80;
	P2 |= 0x10;
	P0PCR |= 0x80;
	#endif
	
	#if	(UART_DEFINE == 13)
	P2 |= 0x30;
	P2PCR |= 0x20;
	#endif

	#if	(UART_DEFINE == 14)
	P0 |= 0x01;
	P2 |= 0x10;
	P0PCR |= 0x01;
	#endif

	#if	(UART_DEFINE == 15)
	P0 |= 0x02;
	P2 |= 0x10;
	P0PCR |= 0x02;
	#endif

	#if	(UART_DEFINE == 16)
	P0 |= 0x40;
	P2 |= 0x20;
	P0PCR |= 0x40;
	#endif	

	#if	(UART_DEFINE == 17)
	P0 |= 0x80;
	P2 |= 0x20;
	P0PCR |= 0x80;
	#endif
	
	#if	(UART_DEFINE == 18)
	P2 |= 0x30;
	P2PCR |= 0x10;
	#endif

	#if	(UART_DEFINE == 19)
	P0 |= 0x01;
	P2 |= 0x20;
	P0PCR |= 0x01;
	#endif

	#if	(UART_DEFINE == 20)
	P0 |= 0x02;
	P2 |= 0x20;
	P0PCR |= 0x02;
	#endif

	#if	(UART_DEFINE == 21)
	P0 |= 0x81;
	P0PCR |= 0x80;
	#endif	

	#if	(UART_DEFINE == 22)
	P0 |= 0x01;
	P2 |= 0x10;
	P2PCR |= 0x10;
	#endif
	
	#if	(UART_DEFINE == 23)
	P0 |= 0x01;
	P2 |= 0x20;
	P2PCR |= 0x20;
	#endif

	#if	(UART_DEFINE == 24)
	P0 |= 0x41;
	P0PCR |= 0x40;
	#endif

	#if	(UART_DEFINE == 25)
	P0 |= 0x03;
	P0PCR |= 0x02;
	#endif
	
	#if	(UART_DEFINE == 26)
	P0 |= 0x82;
	P0PCR |= 0x80;
	#endif	

	#if	(UART_DEFINE == 27)
	P0 |= 0x02;
	P2 |= 0x10;
	P2PCR |= 0x10;
	#endif
	
	#if	(UART_DEFINE == 28)
	P0 |= 0x02;
	P2 |= 0x20;
	P2PCR |= 0x20;
	#endif

	#if	(UART_DEFINE == 29)
	P0 |= 0x42;
	P0PCR |= 0x40;
	#endif

	#if	(UART_DEFINE == 30)
	P0 |= 0x03;
	P0PCR |= 0x01;
	#endif
	
//UART1相关IO配置	
	#if (UART_DEFINE == 31)
		P1 |= 0x03;
		P1PCR |= 0x02;
	#endif
	
	#if (UART_DEFINE == 32)
		P1 |= 0x01;
		P3 |= 0x08;
		P3PCR |= 0x08;
	#endif
	
	#if (UART_DEFINE == 33)
		P1 |= 0x01;
		P3 |= 0x10;
		P3PCR |= 0x10;
	#endif
	
	#if (UART_DEFINE == 34)
		P1 |= 0x01;
		P2 |= 0x40;
		P2PCR |= 0x40;
	#endif
	
	#if (UART_DEFINE == 35)
		P1 |= 0x01;
		P2 |= 0x80;
		P2PCR |= 0x80;
	#endif
	
	#if (UART_DEFINE == 36)
		P1 |= 0x03;
		P1PCR |= 0x01;
	#endif
	
	#if (UART_DEFINE == 37)
		P1 |= 0x02;
		P3 |= 0x08;
		P3PCR |= 0x08;
	#endif
	
	#if (UART_DEFINE == 38)
		P1 |= 0x02;
		P3 |= 0x10;
		P3PCR |= 0x10;
	#endif
	
	#if (UART_DEFINE == 39)
		P1 |= 0x02;
		P2 |= 0x40;
		P2PCR |= 0x40;
	#endif
	
	#if (UART_DEFINE == 40)
		P1 |= 0x02;
		P2 |= 0x80;
		P2PCR |= 0x80;
	#endif
	
	#if (UART_DEFINE == 41)
		P1 |= 0x01;
		P3 |= 0x08;
		P1PCR |= 0x01;
	#endif
	
	#if (UART_DEFINE == 42)
		P1 |= 0x02;
		P3 |= 0x08;
		P1PCR |= 0x01;
	#endif
	
	#if (UART_DEFINE == 43)
		P3 |= 0x18;
		P3PCR |= 0x10;
	#endif
	
	#if (UART_DEFINE == 44)
		P3 |= 0x08;
		P2 |= 0x40;
		P2PCR |= 0x40;
	#endif
	
	#if (UART_DEFINE == 45)
		P3 |= 0x08;
		P2 |= 0x80;
		P2PCR |= 0x80;
	#endif
	
	#if (UART_DEFINE == 46)
		P1 |= 0x01;
		P3 |= 0x10;
		P1PCR |= 0x01;
	#endif
	
	#if (UART_DEFINE == 47)
		P1 |= 0x02;
		P3 |= 0x10;
		P1PCR |= 0x02;
	#endif
	
	#if (UART_DEFINE == 48)
		P3 |= 0x18;
		P3PCR |= 0x08;
	#endif
	
	#if (UART_DEFINE == 49)
		P3 |= 0x10;
		P2 |= 0x40;
		P2PCR |= 0x40;
	#endif
	
	#if (UART_DEFINE == 50)
		P3 |= 0x10;
		P2 |= 0x80;
		P2PCR |= 0x80;
	#endif
	
	#if (UART_DEFINE == 51)
		P1 |= 0x01;
		P2 |= 0x40;
		P1PCR |= 0x01;
	#endif
	
	#if (UART_DEFINE == 52)
		P1 |= 0x02;
		P2 |= 0x40;
		P1PCR |= 0x02;
	#endif
	
	#if (UART_DEFINE == 53)
		P2 |= 0x40;
		P3 |= 0x08;
		P3PCR |= 0x08;
	#endif
	
	#if (UART_DEFINE == 54)
		P3 |= 0x10;
		P2 |= 0x40;
		P3PCR |= 0x10;
	#endif
	
	#if (UART_DEFINE == 55)
		P2 |= 0xC0;
		P2PCR |= 0x80;
	#endif
	
	#if (UART_DEFINE == 56)
		P1 |= 0x01;
		P2 |= 0x80;
		P1PCR |= 0x01;
	#endif
	
	#if (UART_DEFINE == 57)
		P1 |= 0x02;
		P2 |= 0x80;
		P1PCR |= 0x02;
	#endif
	
	#if (UART_DEFINE == 58)
		P2 |= 0x80;
		P3 |= 0x08;
		P3PCR |= 0x08;
	#endif
	
	#if (UART_DEFINE == 59)
		P3 |= 0x10;
		P2 |= 0x80;
		P3PCR |= 0x10;
	#endif
	
	#if (UART_DEFINE == 60)
		P2 |= 0xC0;
		P2PCR |= 0x40;
	#endif	
	
//UART2相关IO配置，当前demo板此串口做LED灯显示功能，不支持串口通讯，故demo板配置为不使能uart2串口功能
	#if (UART_DEFINE == 61)
		P2 |= 0x06;
		P2PCR |= 0x02;
	#endif	
}

/*************************************************************************************************
* 函数名: InitIRQ
* 参  数: 无
* 返回值: 无
* 描  述: 关闭所有中断即可
*************************************************************************************************/
void InitIRQ(void)
{
 	IEN0 = 0x00;
	IEN1 = 0x00;
    IEN2 = 0x00;
}
#if ((UART_DEFINE >= 1)&&(UART_DEFINE<= 30))
/*************************************************************************************************
* 函数名: UARTInit
* 参  数: Bps: UART通讯波特率
* 返回值: 无
* 描  述: 初始化UART模块
*************************************************************************************************/
void UARTInit(void)
{
	#if UART_DEFINE == 1
		UART0CR = 0x32;
	#elif UART_DEFINE == 2
		UART0CR = 0x42;
	#elif UART_DEFINE == 3
		UART0CR = 0x52;
	#elif UART_DEFINE == 4
		UART0CR = 0x02;
	#elif UART_DEFINE == 5
		UART0CR = 0x12;
	#elif UART_DEFINE == 6
		UART0CR = 0x23;
	#elif UART_DEFINE == 7
		UART0CR = 0x43;
	#elif UART_DEFINE == 8
		UART0CR = 0x53;
	#elif UART_DEFINE == 9
		UART0CR = 0x03;
	#elif UART_DEFINE == 10
		UART0CR = 0x13;
	#elif UART_DEFINE == 11
		UART0CR = 0x24;
	#elif UART_DEFINE == 12
		UART0CR = 0x34;
	#elif UART_DEFINE == 13
		UART0CR = 0x54;
	#elif UART_DEFINE == 14
		UART0CR = 0x04;
	#elif UART_DEFINE == 15
		UART0CR = 0x14;
	#elif UART_DEFINE == 16
		UART0CR = 0x25;
	#elif UART_DEFINE == 17
		UART0CR = 0x35;
	#elif UART_DEFINE == 18
		UART0CR = 0x45;
	#elif UART_DEFINE == 19
		UART0CR = 0x05;
	#elif UART_DEFINE == 20
		UART0CR = 0x15;
	#elif UART_DEFINE == 21
		UART0CR = 0x30;
	#elif UART_DEFINE == 22
		UART0CR = 0x40;
	#elif UART_DEFINE == 23
		UART0CR = 0x50;
	#elif UART_DEFINE == 24
		UART0CR = 0x20;
	#elif UART_DEFINE == 25
		UART0CR = 0x10;
	#elif UART_DEFINE == 26
		UART0CR = 0x31;
	#elif UART_DEFINE == 27
		UART0CR = 0x41;
	#elif UART_DEFINE == 28
		UART0CR = 0x51;
	#elif UART_DEFINE == 29
		UART0CR = 0x21;
	#elif UART_DEFINE == 30
		UART0CR = 0x01;		
	#endif
	
	PCON = 0x00;	
	SCON = 0x50;
	SBRTH = UART_BPS_H;
	SBRTL = UART_BPS_L;
	SFINE = UART_BPS_F;
	SBRTH |= 0x80;
	SADDR = 0x0A;
	SADEN = 0xFF;
}

#elif ((UART_DEFINE >= 31)&&(UART_DEFINE<= 60))
void UARTInit(void)
{
	INSCON = 0x00;
	#if UART_DEFINE == 31
		UART1CR = 0x10;
	#elif UART_DEFINE == 32
		UART1CR = 0x40;
	#elif UART_DEFINE == 33
		UART1CR = 0x50;
	#elif UART_DEFINE == 34
		UART1CR = 0x20;
	#elif UART_DEFINE == 35
		UART1CR = 0x30;
	#elif UART_DEFINE == 36
		UART1CR = 0x01;
	#elif UART_DEFINE == 37
		UART1CR = 0x41;
	#elif UART_DEFINE == 38
		UART1CR = 0x51;
	#elif UART_DEFINE == 39
		UART1CR = 0x21;
	#elif UART_DEFINE == 40
		UART1CR = 0x31;
	#elif UART_DEFINE == 41
		UART1CR = 0x04;
	#elif UART_DEFINE == 42
		UART1CR = 0x14;
	#elif UART_DEFINE == 43
		UART1CR = 0x54;
	#elif UART_DEFINE == 44
		UART1CR = 0x24;
	#elif UART_DEFINE == 45
		UART1CR = 0x34;
	#elif UART_DEFINE == 46
		UART1CR = 0x05;
	#elif UART_DEFINE == 47
		UART1CR = 0x15;
	#elif UART_DEFINE == 48
		UART1CR = 0x45;
	#elif UART_DEFINE == 49
		UART1CR = 0x25;
	#elif UART_DEFINE == 50
		UART1CR = 0x35;
	#elif UART_DEFINE == 51
		UART1CR = 0x02;
	#elif UART_DEFINE == 52
		UART1CR = 0x12;
	#elif UART_DEFINE == 53
		UART1CR = 0x42;
	#elif UART_DEFINE == 54
		UART1CR = 0x52;
	#elif UART_DEFINE == 55
		UART1CR = 0x32;
	#elif UART_DEFINE == 56
		UART1CR = 0x03;
	#elif UART_DEFINE == 57
		UART1CR = 0x13;
	#elif UART_DEFINE == 58
		UART1CR = 0x43;
	#elif UART_DEFINE == 59
		UART1CR = 0x53;
	#elif UART_DEFINE == 60
		UART1CR = 0x23;
	#endif
	INSCON = 0x40;
	
	PCON1 = 0x00;	
	SCON1 = 0x50;                                   //配置Uart工作在模式1
	SBRTH1 = UART_BPS_H;
	SBRTL1 = UART_BPS_L;
	SFINE1 = UART_BPS_F;
	SBRTH1 |= 0x80;                                 //使能波特率发生器
	SADDR1 = 0x0A;                      //UART地址初始化，默认为0x0A
	SADEN1 = 0xFF;
	INSCON = 0x00;
}
#elif   (UART_DEFINE == 61)
void UARTInit(void)
{
	INSCON = 0x40;
	PCON2 = 0x00;	
	SCON2 = 0x50;								//配置Uart工作在模式1
	SBRTH2 = UART_BPS_H;								//115200bps
	SBRTL2 = UART_BPS_L;
	SFINE2 = UART_BPS_F;
	SBRTH2 |= 0x80;								//使能波特率发生器
	SADDR2 = 0x0A;								//UART地址初始化，默认为0x0A
	SADEN2 = 0xFF;
	INSCON = 0x00;
}
#else
#Error: "UART_DEFINE ERROR" 
#endif


/*************************************************************************************************
* 函数名: Initial
* 参  数: 无
* 返回值: 无
* 描  述: 初始化系统
*************************************************************************************************/
void Initial(void)
{
    InitIRQ();
    InitGPIO();
    bHandsheakOkFlg = 0;
}


/*************************************************************************************************
* 函数名: main
* 参  数: 无
* 返回值: 无
* 描  述: Boot区主函数
*************************************************************************************************/
void main(void)
{
    BootMcuWdtClear();
    Initial();                                              //硬件资源初始化
    
//1.获取远程升级标志位状态，存在IAP程序升级标志，需要进行程序升级处理
    if(McuFlashRdOneByte(BOOT_FLG_IAP_ADDR, MCU_TYPE_CODE) == IAP_FLG)
    {
		bIapIspFlg = IAP_MODE;
		ucMcuFlashWrValid = 0x55;                           //设置MCU Code区可操作标志，防止误写
		if(BootUpgradeCode())                               //升级程序
		{
			ucMcuFlashWrValid = 0;
            ((void(code*)(void))IAP_CODE_START_ADDR)();		//升级成功则跳转到应用程序首地址					
		}
        else if(BootUpgradeCode())                          //升级程序
		{
			ucMcuFlashWrValid = 0;
            ((void(code*)(void))IAP_CODE_START_ADDR)();     //升级成功则跳转到应用程序首地址					
		}
		else
		{
            while(1)
            {
                ucMcuFlashWrValid = 0;
                SUSLO = 0x55;
                PCON |= 0x02;
                _nop_();
                _nop_();
                _nop_();
                _nop_();
                _nop_();
            }
		}
    }
//2.判断是否ISP成功，如果该标志存在表示之前的ISP失败，需要在BOOT区重新ISP
	else if(McuFlashRdOneByte(BOOT_FLG_ISP_ADDR, MCU_TYPE_CODE) == ISP_FLG)
	{
		bIapIspFlg = ISP_MODE;
		BootIapIsp();
	}
//3.无升级需求,直接跳转到应用程序区
    else
    {
        ((void(code*)(void))IAP_CODE_START_ADDR)();
    }
}



