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

                                                                                                                                                                                                                                   
#if	(UART0_DEFINE != 0)
/*************************************************************************************************
* 函数名: UART0Init
* 参  数: Bps: UART通讯波特率
* 返回值: 无
* 描  述: 初始化UART0模块
*************************************************************************************************/
void UART0Init(void)
{
	#if UART0_DEFINE == 1
        UART0CR = 0x32;
    #elif UART0_DEFINE == 2
        UART0CR = 0x42;
    #elif UART0_DEFINE == 3
        UART0CR = 0x52;
	#elif UART0_DEFINE == 4
        UART0CR = 0x02;
	#elif UART0_DEFINE == 5
        UART0CR = 0x12;
	#elif UART0_DEFINE == 6
        UART0CR = 0x23;
	#elif UART0_DEFINE == 7
        UART0CR = 0x43;
	#elif UART0_DEFINE == 8
        UART0CR = 0x53;
	#elif UART0_DEFINE == 9
        UART0CR = 0x03;
	#elif UART0_DEFINE == 10
        UART0CR = 0x13;
	#elif UART0_DEFINE == 11
        UART0CR = 0x24;
	#elif UART0_DEFINE == 12
        UART0CR = 0x34;
	#elif UART0_DEFINE == 13
        UART0CR = 0x54;
	#elif UART0_DEFINE == 14
        UART0CR = 0x04;
	#elif UART0_DEFINE == 15
        UART0CR = 0x14;
	#elif UART0_DEFINE == 16
        UART0CR = 0x25;
	#elif UART0_DEFINE == 17
        UART0CR = 0x35;
	#elif UART0_DEFINE == 18
        UART0CR = 0x45;
	#elif UART0_DEFINE == 19
        UART0CR = 0x05;
	#elif UART0_DEFINE == 20
        UART0CR = 0x15;
	#elif UART0_DEFINE == 21
        UART0CR = 0x30;
	#elif UART0_DEFINE == 22
        UART0CR = 0x40;
	#elif UART0_DEFINE == 23
        UART0CR = 0x50;
	#elif UART0_DEFINE == 24
        UART0CR = 0x20;
	#elif UART0_DEFINE == 25
        UART0CR = 0x10;
	#elif UART0_DEFINE == 26
        UART0CR = 0x31;
	#elif UART0_DEFINE == 27
        UART0CR = 0x41;
	#elif UART0_DEFINE == 28
        UART0CR = 0x51;
	#elif UART0_DEFINE == 29
        UART0CR = 0x01;
	#elif UART0_DEFINE == 30
        UART0CR = 0x21;	
	#else
	#Error: "UART0_DEFINE ERROR" 		
    #endif
	{
		U16 TempVal;
		PCON = 0x00;	
		SCON = 0x50;                                    //配置Uart工作在模式1
//		SBRTH = UART_BPS_H;
//		SBRTL = UART_BPS_L;
//		SFINE = UART_BPS_F;

		TempVal = 32768 - (U16)(8000000 / 16 / 9600);   //计算公式：(int)X=FSY/(16*波特率) ;  SBRT=32768-X  ;   SFINE=（FSY/波特率）-16*X   FSY=24M
	    SBRTH = TempVal / 256;
	    SBRTL = TempVal % 256;
	    SFINE = (float)8000000 / 9600 - (16 * (32768 - TempVal));

		SBRTH |= 0x80;                                  //使能波特率发生器
		SADDR = UART_SLAVE_SADDR;					//UART地址初始化，默认为0x0A
		SADEN = 0xFF;
	}
}


/*************************************************************************************************
* 函数名: InterruptUART0
* 参  数: 无
* 返回值: 无
* 描  述: UART0中断处理
*************************************************************************************************/
void InterruptUART0(void) interrupt 4
{
	U8 xdata RxData;
	
	_push_(INSCON);
    McuBank0Sel();

	if(RI)                              //RT1 interrupt means MCU recept data though UART
	{
		RI = 0;
		RxData = SBUF;
	InterruptUart0AppRx(RxData);	//中颖协议
	}
	
	if(TI)
	{
		TI = 0;
		InterruptUart0AppTx();	//中颖协议
	
	}
	
	_pop_(INSCON);
}
#endif


#if	(UART1_DEFINE != 0)
/*************************************************************************************************
* 函数名: UART1Init
* 参  数: Bps: UART通讯波特率
* 返回值: 无
* 描  述: 初始化UART1模块
*************************************************************************************************/
void UART1Init(void)
{
//	   U16 TempVal;
	
	_push_(INSCON);
    McuBank0Sel();
    #if UART1_DEFINE == 1
        UART1CR = 0x10;
    #elif UART1_DEFINE == 2
        UART1CR = 0x40;
    #elif UART1_DEFINE == 3
        UART1CR = 0x50;
	#elif UART1_DEFINE == 4
        UART1CR = 0x20;
	#elif UART1_DEFINE == 5
        UART1CR = 0x30;
	#elif UART1_DEFINE == 6
        UART1CR = 0x01;
	#elif UART1_DEFINE == 7
        UART1CR = 0x41;
	#elif UART1_DEFINE == 8
        UART1CR = 0x51;
	#elif UART1_DEFINE == 9
        UART1CR = 0x21;
	#elif UART1_DEFINE == 10
        UART1CR = 0x31;
	#elif UART1_DEFINE == 11
        UART1CR = 0x04;
	#elif UART1_DEFINE == 12
        UART1CR = 0x14;
	#elif UART1_DEFINE == 13
        UART1CR = 0x54;
    #elif UART1_DEFINE == 14
        UART1CR = 0x24;
	#elif UART1_DEFINE == 15
        UART1CR = 0x34;
	#elif UART1_DEFINE == 16
        UART1CR = 0x05;
	#elif UART1_DEFINE == 17
        UART1CR = 0x15;
	#elif UART1_DEFINE == 18
        UART1CR = 0x45;
	#elif UART1_DEFINE == 19
        UART1CR = 0x25;
	#elif UART1_DEFINE == 20
        UART1CR = 0x35;
	#elif UART1_DEFINE == 21
        UART1CR = 0x02;
	#elif UART1_DEFINE == 22
        UART1CR = 0x12;
	#elif UART1_DEFINE == 23
        UART1CR = 0x42;
	#elif UART1_DEFINE == 24
        UART1CR = 0x52;
	#elif UART1_DEFINE == 25
        UART1CR = 0x32;
	#elif UART1_DEFINE == 26
        UART1CR = 0x03;
	#elif UART1_DEFINE == 27
        UART1CR = 0x13;
	#elif UART1_DEFINE == 28
        UART1CR = 0x43;
	#elif UART1_DEFINE == 29
        UART1CR = 0x53;
	#elif UART1_DEFINE == 30
        UART1CR = 0x23;
	#else
	#Error: "UART1_DEFINE ERROR" 
    #endif
	{
		McuBank1Sel();
		PCON1 = 0x00;	
		SCON1 = 0x50;                                   //配置Uart工作在模式1
		SBRTH1 = UART_BPS_H;
		SBRTL1 = UART_BPS_L;
		SFINE1 = UART_BPS_F;
		SBRTH1 |= 0x80;                                 //使能波特率发生器
		SADDR1 = UART_SLAVE_SADDR;                      //UART地址初始化，默认为0x0A
		SADEN1 = 0xFF;
		McuBank0Sel();
	}  	
	_pop_(INSCON);
	
	
}


/*************************************************************************************************
* 函数名: InterruptUart1
* 参  数: 无
* 返回值: 无
* 描  述: UART1接收和发送中断服务程序
          InterruptUart1AppRx(RxData)，调用APP层的接收中断处理函数
          InterruptUart1AppTx()，调用APP层的发送中断处理函数
*************************************************************************************************/
void InterruptUart1(void) interrupt 15
{
	U8 xdata RxData;
	
	_push_(INSCON);
    McuBank1Sel();

	if(RI1)
	{
		RI1 = 0;
		RxData = SBUF1;
		McuBank0Sel();                              //调用APP函数之前，将寄存器切换到BANK0
		InterruptUart1AppRx(RxData);
	}
	McuBank1Sel();
	if(TI1)
	{
		TI1 = 0;
		McuBank0Sel();                              //调用APP函数之前，将寄存器切换到BANK0
		InterruptUart1AppTx();
	}
	
	_pop_(INSCON);
}
#endif

#if	(UART2_DEFINE != 0)
/*************************************************************************************************
* 函数名: UART2Init
* 参  数: Bps: UART通讯波特率
* 返回值: 无
* 描  述: 初始化UART2模块
*************************************************************************************************/
void UART2Init(void)
{
	{
		McuBank1Sel();		
		PCON2 = 0x00;	
		SCON2 = 0x50;                                   //配置Uart工作在模式1
		SBRTH2 = UART_BPS_H;
		SBRTL2 = UART_BPS_L;
		SFINE2 = UART_BPS_F;
		SBRTH2 |= 0x80;                                 //使能波特率发生器
		SADDR2 = UART_SLAVE_SADDR;                      //UART地址初始化，默认为0x0A
		SADEN2 = 0xFF;	
		McuBank0Sel();
	}
}

/*************************************************************************************************
* 函数名: InterruptUart2
* 参  数: 无
* 返回值: 无
* 描  述: UART2接收和发送中断服务程序
          InterruptUart2AppRx(RxData)，调用APP层的接收中断处理函数
          InterruptUart2AppTx()，调用APP层的发送中断处理函数
*************************************************************************************************/
void InterruptUart2(void) interrupt 16
{
	U8 xdata RxData;
	
	_push_(INSCON);
    McuBank1Sel();

	if(RI2)
	{
		RI2 = 0;
		RxData = SBUF2;
		McuBank0Sel();                              //调用APP函数之前，将寄存器切换到BANK0
		InterruptUart2AppRx(RxData);
	}
	McuBank1Sel();
	if(TI2)
	{
		TI2 = 0;
		McuBank0Sel();                              //调用APP函数之前，将寄存器切换到BANK0
		InterruptUart2AppTx();
	}
	
	_pop_(INSCON);
}
#endif

