/***********************************************************************************
 * @Copyright (c)   :(C)2020, Qindao ieslab Co., Ltd
 * @FileName        :hc32_uart_driver.c
 * @Author          :
 * @Version         :V1.0
 * @Date            :2020-7-1
 * @Description :the function of the entity of system processor
 ************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "sys_config.h"
#include "UWater_uart_driver.h"
#include "UWater_timer1uS_driver.h"
#include "UWater_ir_app.h"
#include "reset.h"

/* Private variables ---------------------------------------------------------*/
static UART_STRUCT uart_type[COM_MAX_NUM];
u8 ir_tx_end_rdy_flag;				//ir tx发送预结束标志
#if COM0_OPEN
static u8 s_uart0_rcv_buf[UART0_BUF_SIZE];  //串口0接收缓冲区
#endif

#if COM1_OPEN
static u8 s_uart1_rcv_buf[UART1_BUF_SIZE];  //串口1接收缓冲区
#endif

#if COM2_OPEN
static u8 s_uart2_rcv_buf[UART2_BUF_SIZE];  //串口2接收缓冲区
#endif

#if COM3_OPEN
static u8 s_uart3_rcv_buf[UART3_BUF_SIZE];  //串口3接收缓冲区
#endif

#if LPCOM0_OPEN
static u8 s_lpuart0_rcv_buf[LPUART0_BUF_SIZE];  //低功耗串口0接收缓冲区
#endif

#if LPCOM1_OPEN
static u8 s_lpuart1_rcv_buf[LPUART1_BUF_SIZE];  //低功耗串口1接收缓冲区
#endif

/* Private function prototypes -----------------------------------------------*/
static void UartRcvMachine(void);        //串口接收前台状态机
void UartSendMachine(void);
static void UartPortCfg(u8 com);         //串口IO及中断控制器初始化
static void UartStartReceiveIT(u8 com);  //启动中断接收
static void UartStopReceiveIT(u8 com);   //停止中断接收
static void UartIRQHandler(u8 com);      //串口中断处理函数
static void UartReceiveIT(u8 com);       //串口接收中断处理函数
static void UartTransmitIT(u8 com);      //串口发送中断处理函数

static void HAL_UartCfg(u8 com);
//void IR_Uart_38K_Config(void);
//void IR_Uart_38K_Enable(boolean_t enCmd);
/* Exported functions -------------------------------------------------------*/
/*************************************************
Function:u8 UartInit(u8 com,u8 baud,u8 databit,u8 stop,u8 check,u8 lpmode)
Description: 串口初始化
Input:  com 串口；baud：波特率；databit：数据位；stop：停止位；check：校验位;
lpmode：仅对5和lp有效选择是否作为低功耗串口使用（UART_LPUART_Disable，UART_LPUART_Enable）
return: SUCCESS：初始化成功；ERROR ：初始化失败
Others: 处于MainSpace，第一类接口：上电初始化接口。可供选择串口1、2、3、4、5、lp
*************************************************/
u8 UartInit(u8 com, u8 baud, u8 databit, u8 stop, u8 check, u8 lpmode)
{
    switch (com)  //选择要配置的串口
    {
#if COM0_OPEN
		case (COM0): 
		{
		    uart_type[com].Instance = M0P_UART0;
		} 
		break;
#endif
		
#if COM1_OPEN
		case (COM1): 
		{
		    uart_type[com].Instance = M0P_UART1;
		} 
		break;
#endif

#if COM2_OPEN
		case (COM2): 
		{
		    uart_type[com].Instance = M0P_UART2;
		} 
		break;
#endif

#if COM3_OPEN
		case (COM3): 
		{
		    uart_type[com].Instance = M0P_UART3;
		} 
		break;
#endif

#if LPCOM0_OPEN
		case (LPCOM0): 
		{
		    uart_type[com].LP_Instance = M0P_LPUART0;
		} 
		break;
#endif

#if LPCOM1_OPEN
		case (LPCOM1): 
		{
		    uart_type[com].LP_Instance = M0P_LPUART1;
		} 
		break;
#endif
        default: 
		{
			return ERROR;
		}
    }
    uart_type[com].uart_status = TASK_IDLE;
    switch (baud)  //设置波特率
    {
		case (BAUD1200): 
		{
			uart_type[com].para.BaudRate = 1200;
		} 
		break;

		case (BAUD2400): 
		{
		    uart_type[com].para.BaudRate = 2400;
		} 
		break;

		case (BAUD4800): 
		{
		    uart_type[com].para.BaudRate = 4800;
		} 
		break;

		case (BAUD9600): 
		{
		    uart_type[com].para.BaudRate = 9600;
		} 
		break;

		case (BAUD19200): 
		{
		    uart_type[com].para.BaudRate = 19200;
		} 
		break;

		case (BAUD38400): 
		{
		    uart_type[com].para.BaudRate = 38400;
		} 
		break;

		case (BAUD57600): 
		{
		    uart_type[com].para.BaudRate = 57600;
		} 
		break;

		case (BAUD115200): 
		{
		    uart_type[com].para.BaudRate = 115200;
		} 
		break;

		default: 
		{
			return ERROR;
		}
    }

    switch (databit)  //设置数据长度
    {
		case (DATA_BIT_8): 
		{
			uart_type[com].para.WordLength = UART_WORDLEN_8B;
		} 
		break;
		
		default: 
		{
			return ERROR;
		}
    }

    switch (stop)  //设置停止位
    {
		case (STOPBIT_1): 
		{
			uart_type[com].para.StopBits = UART_STOPBIT_1;
		} 
		break;

		case (STOPBIT_2): 
		{
		    uart_type[com].para.StopBits = UART_STOPBIT_2;
		} 
		break;

		case (STOPBIT_1_5): 
		{
		    uart_type[com].para.StopBits = UART_STOPBIT_1_5;
		} 
		break;

		default: 
		{
			return ERROR;
		}
    }

    switch (check)  //设置校验位
    {
		case (UART_NONE_PARITY): 
		{
			uart_type[com].para.Parity = UART_NONE_PARITY;
			uart_type[com].para.UARTMode = UartMskMode1;
		} 
		break;

		case (UART_ODD_PARITY): 
		{
		    uart_type[com].para.Parity = UART_ODD_PARITY;
		    uart_type[com].para.UARTMode = UartMskMode3;
		} 
		break;

		case (UART_EVEN_PARITY): 
		{
		    uart_type[com].para.Parity = UART_EVEN_PARITY;
		    uart_type[com].para.UARTMode = UartMskMode3;
		} 
		break;

		default: 
		{
			return ERROR;
		}
    }          
    uart_type[com].para.OverSampling = UartMsk8Or16Div;//UartMsk16Or32Div;  // 16位采样
    
    if((LPCOM0 == com)||(LPCOM1 == com))
	{
		uart_type[com].para.LPModeFunction = lpmode;  //选择串口是否使用低功耗功能
    }
    UartPortCfg(com);  //配置管脚及中断优先级等
    HAL_UartCfg(com);
    UartStartReceiveIT(com);  //开启接收中断
    return SUCCESS;
}


void IrTxDealMachine(void)
{
	if(TRUE == ir_tx_end_rdy_flag)
	{
		if (0 == Check100msTimer(TIMER_100MS_IR_TX_38K_END)) //TX BUF 空
		{
			ir_tx_end_rdy_flag = FALSE;
			IR_38KCarrier_Close();
		}
	} 
}

/*************************************************
Function: void UartMachineDriver (void)
Description:串口前台处理机
Input:  None
Return: None
Others: 处于MainSpace，第二类接口：工作接口。
*************************************************/
void UartMachineDriver(void)
{    
    UartRcvMachine();
	UartSendMachine();
	IrTxDealMachine();
}
/*************************************************
Function: u8 UartIfIdle (u8 com)
Description: 串口是否空闲
Input:  com：端口号
return: TASK_IDLE:空闲；TASK_BUSY:忙碌
Others: 处于MainSpace，第二类接口：工作接口。
*************************************************/
u8 UartIfIdle(u8 com)
{
	if(com < COM_MAX_NUM)
	{
		return uart_type[com].uart_status;
	}
	else
	{
		return TASK_IDLE;
	}	  
}

/*************************************************
Function:void UartSend (u8 com, u16 len, u8 *data,u8 *msg)
Description: 串口发生数据
Input:  com：端口号；len:数据长度；*data：要发送的内容；*msg：发送输出信息
return: TASK_IDLE:空闲；TASK_BUSY:忙碌
Others: 处于MainSpace，第二类接口：工作接口。
*************************************************/
void UartSend(u8 com, u16 len, u8 *data, u8 *out_msg)
{
	if(com < COM_MAX_NUM)
	{
        if((len > 0) && (len < UP_BUF_SIZE*2))//对发送的数据长度进行限制
        {
            uart_type[com].send_point = data;
            uart_type[com].send_len = len;
            uart_type[com].send_status_point = out_msg;
            *uart_type[com].send_status_point = TASK_BUSY;
            uart_type[com].uart_status = TASK_BUSY;	
			uart_type[com].start_send_flag = TRUE;	
			uart_type[com].send_over_time = uart_type[com].send_len/10;  //100ms至少发送10个字节
			if(uart_type[com].send_over_time < 30)
			{
				uart_type[com].send_over_time = 30;  //至少3秒
			}				
			if(LPCOM0 == com)//红外
			{
				IR_38KCarrier_Open();
				ir_tx_end_rdy_flag = FALSE;				
				LPUart_DisableIrq(uart_type[com].LP_Instance,LPUartRxIrq);   //发送时关闭接收，红外会互相干扰
				LPUart_ClrStatus(uart_type[com].LP_Instance,LPUartTC);      ///<清发送中断请求				
				LPUart_SendDataIt(uart_type[com].LP_Instance, *uart_type[com].send_point);
				uart_type[com].send_point++;
				uart_type[com].send_len--;            
				LPUart_EnableIrq(uart_type[com].LP_Instance, LPUartTxIrq);   ///<开启发送中断
			}
			else if(LPCOM1 == com)//NB
			{					
				LPUart_SendDataIt(uart_type[com].LP_Instance, *uart_type[com].send_point);
				uart_type[com].send_point++;
				uart_type[com].send_len--;            
				LPUart_EnableIrq(uart_type[com].LP_Instance, LPUartTxIrq);   ///<开启发送中断
			}
			else
			{
				Uart_SendDataIt(uart_type[com].Instance, *uart_type[com].send_point); 		  //发送数据
				uart_type[com].send_point++;
				uart_type[com].send_len--;            
				Uart_EnableIrq(uart_type[com].Instance, UartTxIrq);  	   ///<开启发送中断	
            }
		}
	}
}
/*************************************************
Function: u8 UartRcvFinish (u8 com)
Description: 串口接收完成
Input:  com:端口号
Return: None
Others: 处于MainSpace，第二类接口：工作接口。
*************************************************/
u8 UartRcvFinish(u8 com)
{
	if(com < COM_MAX_NUM)
	{
		if (TRUE == uart_type[com].rcv_msg) 
		{
			return TRUE;
		} 
		else 
		{
		    return FALSE;
		}
    }
	else
	{
        return FALSE;
    }
}

/*************************************************
Function: void UartClearMsg (u8 com)
Description: 清除串口接收完成事件Msg,并使能接收
Input:  com 端口号
Return: None
Others: 处于MainSpace，第二类接口：工作接口。
*************************************************/
void UartClearMsg(u8 com)
{
    if(com < COM_MAX_NUM)
	{
        uart_type[com].rcv_msg = FALSE;
        uart_type[com].rcv_len = 0;
        UartStartReceiveIT(com);  //开启接收中断
    } 
}
/*************************************************
Function: void UartRcv (u8 com ,u16 *out_len , u8 **out_data)
Description: 串口接收数据
Input:  com：端口号；out_len：数据长度；out_data:数据指针
Return: None
Others: 处于MainSpace，第二类接口：工作接口。
*************************************************/
void UartRcv(u8 com, u16 *out_len, u8 **out_data)
{
	if(com < COM_MAX_NUM)
	{
		*out_len = uart_type[com].rcv_len;
		switch (com)
		{
#if COM0_OPEN
			case (COM0): 
			{
			    *out_data = s_uart0_rcv_buf;
			} 
			break;
#endif
			
#if COM1_OPEN
			case (COM1): 
			{
			    *out_data = s_uart1_rcv_buf;
			} 
			break;			
#endif

#if COM2_OPEN
			case (COM2): 
			{			  
			    *out_data = s_uart2_rcv_buf;
			} 
			break;
#endif

#if COM3_OPEN
			case (COM3): 
			{			    
			    *out_data = s_uart3_rcv_buf;
			} 
			break;
#endif

#if LPCOM0_OPEN
			case (LPCOM0): 
			{
			    *out_data = s_lpuart0_rcv_buf;
			} 
			break;
#endif

#if LPCOM1_OPEN
			case (LPCOM1): 
			{
			    *out_data = s_lpuart1_rcv_buf;
			} 
			break;
#endif
			default:
			break;
		}
	}
}
/*************************************************
Function: u8 UartIfSleep(u8 com)
Description: 串口是否允许休眠
Input:  None
Return: TASK_IDLE:TASK_BUSY
Others: 处于MainSpace，第三类接口：休眠前接口。
*************************************************/
u8 UartIfSleep(u8 com)
{
	if(com < COM_MAX_NUM)
	{
		return uart_type[com].uart_status;
	}
	else
	{
		return TASK_IDLE;
    } 	
}
/*************************************************
Function: void UartPreSleep(u8 com)
Description: 休眠前处理
Input:  com:端口号
Return: None
Others: 处于MainSpace，第三类接口：休眠前接口。
*************************************************/
void UartPreSleep(u8 com)
{
    switch (com) 
	{
#if COM0_OPEN
		case COM0: 
		{
		    uart_type[COM0].Instance->SCON = 0;    //关闭所有功能
		    M0P_SYSCTRL->PERI_CLKEN0_f.UART0 = 0;  //关闭串口时钟
		} 
		break;
#endif
		
#if COM1_OPEN
		case COM1: 
		{
		    uart_type[COM1].Instance->SCON = 0;    //关闭所有功能
		    M0P_SYSCTRL->PERI_CLKEN0_f.UART1 = 0;  //关闭串口时钟
		} 
		break;
#endif

#if COM2_OPEN
		case COM2: 
		{
		    uart_type[COM2].Instance->SCON = 0;    //关闭所有功能
		    M0P_SYSCTRL->PERI_CLKEN1_f.UART2 = 0;  //关闭串口时钟
		} 
		break;
#endif

#if COM3_OPEN

		case COM3: 
		{
		    uart_type[COM3].Instance->SCON = 0;    //关闭所有功能
		    M0P_SYSCTRL->PERI_CLKEN1_f.UART3 = 0;  //关闭串口时钟
		} 
		break;
#endif

#if LPCOM0_OPEN
		case LPCOM0:
		{
	#if LPCOM0_ENABLESLEEPMODE
		    uart_type[LPCOM1].LP_Instance->SCON_f.RCIE = 1;
		    uart_type[LPCOM1].LP_Instance->SCON_f.REN = 1;
	#else
		    uart_type[LPCOM0].LP_Instance->SCON = 0;  //关闭所有功能
		    M0P_SYSCTRL->PERI_CLKEN0_f.LPUART0 = 0;   //关闭串口时钟
	#endif
		} break;
#endif

#if LPCOM1_OPEN
		case LPCOM1: 
		{
	#if LPCOM1_ENABLESLEEPMODE
		    uart_type[LPCOM1].LP_Instance->SCON_f.RCIE = 1;//接收中断打开
		    uart_type[LPCOM1].LP_Instance->SCON_f.REN = 1; //发送中断打开

	#else
		    uart_type[LPCOM1].LP_Instance->SCON = 0;  //关闭所有功能
		    M0P_SYSCTRL->PERI_CLKEN0_f.LPUART1 = 0;   //关闭串口时钟
	#endif
		}
		break;
#endif

        default:
        break;
    }
}
/*************************************************
Function:void UartWakeSleep(u8 com,u8 baud,u8 databit,u8 stop,u8 check,lpmode)
Description: 串口唤醒
Input:  Input:com
串口；baud：波特率；databit：数据位；stop：停止位；check：校验位; return: None
Others: 处于MainSpace，第四类接口：唤醒后接口。
*************************************************/
void UartWakeSleep(u8 com, u8 baud, u8 databit, u8 stop, u8 check, u8 lpmode)
{
    UartInit(com, baud, databit, stop, check, lpmode);
}

/*************************************************
Function:void USART0_IRQHandler(void)
Description: 串口0中断入口函数
Input:  None
Return: None
Others: None
*************************************************/
#if COM0_OPEN
void Uart0_IRQHandler(void) { UartIRQHandler(COM0); }
#endif

/*************************************************
Function:void USART1_IRQHandler(void)
Description: 串口1中断入口函数
Input:  None
Return: None
Others: None
*************************************************/
#if COM1_OPEN
void Uart1_IRQHandler(void) { UartIRQHandler(COM1); }
#endif

/*************************************************
Function:void USART2_IRQHandler(void)
Description: 串口2中断入口函数
Input:  None
Return: None
Others: None
*************************************************/
#if COM2_OPEN
void Uart2_IRQHandler(void) { UartIRQHandler(COM2); }
#endif

/*************************************************
Function:void UART3_IRQHandler(void)
Description: 串口3中断入口函数
Input:  None
Return: None
Others: None
*************************************************/
#if COM3_OPEN
void Uart3_IRQHandler(void) { UartIRQHandler(COM3); }
#endif

/*************************************************
Function:void UART5_IRQHandler(void)
Description: 串口5中断入口函数
Input:  None
Return: None
Others: None
*************************************************/
#if LPCOM0_OPEN
void LpUart0_IRQHandler(void) { UartIRQHandler(LPCOM0); }
#endif

/*************************************************
Function:void LPUART1_IRQHandler(void)
Description: 低功耗串口中断入口函数
Input:  None
Return: None
Others: None
*************************************************/
#if LPCOM1_OPEN
void LpUart1_IRQHandler(void) { UartIRQHandler(LPCOM1); }
#endif

/*************************************************
Function:void UartRcvMachine(void)
Description: 串口接收前台状态机
Input:  None
return: None
Others: 内部接口
*************************************************/
static void UartRcvMachine(void)
{
#if COM0_OPEN
    switch (uart_type[COM0].rcv_state) 
	{
		case (UART_RCV_OVERTIME_S0): 
		{
		    if (1 == uart_type[COM0].rcv_flag) 
			{
				uart_type[COM0].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART0, FRAMEOVERTIME);
				uart_type[COM0].rcv_state = UART_RCV_OVERTIME_S1;
		    } 
		} 
		break;

		case (UART_RCV_OVERTIME_S1): 
		{
			if (1 == uart_type[COM0].rcv_flag) 
			{
				uart_type[COM0].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART0, FRAMEOVERTIME);
				uart_type[COM0].rcv_state = UART_RCV_OVERTIME_S1;
			}
			else if ((0 == uart_type[COM0].rcv_flag) &&(0 == Check5msTimer(TIMER_5MS_UART0))) 
			{
				uart_type[COM0].rcv_msg = TRUE;				
				uart_type[COM0].rcv_state = UART_RCV_OVERTIME_S0;
				UartStopReceiveIT(COM0);
		    } 
		} 
		break;

		default: 
		{
			uart_type[COM0].rcv_state = UART_RCV_OVERTIME_S0;
		} 
		break;
  }
#endif	
	
#if COM1_OPEN
	switch (uart_type[COM1].rcv_state) 
	{
		case (UART_RCV_OVERTIME_S0): 
		{
			if (1 == uart_type[COM1].rcv_flag) 
			{
				uart_type[COM1].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART1, FRAMEOVERTIME);
				uart_type[COM1].rcv_state = UART_RCV_OVERTIME_S1;
		    }
		} 
		break;

		case (UART_RCV_OVERTIME_S1):
		{
			if (1 == uart_type[COM1].rcv_flag) 
			{
				uart_type[COM1].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART1, FRAMEOVERTIME);
				uart_type[COM1].rcv_state = UART_RCV_OVERTIME_S1;
			} 
			else if ((0 == uart_type[COM1].rcv_flag) && (0 == Check5msTimer(TIMER_5MS_UART1))) 
			{
				uart_type[COM1].rcv_msg = TRUE;				
				uart_type[COM1].rcv_state = UART_RCV_OVERTIME_S0;
				UartStopReceiveIT(COM1);
			} 
		} 
		break;

		default: 
		{
			uart_type[COM1].rcv_state = UART_RCV_OVERTIME_S0;
		} 
		break;
	}
#endif

#if COM2_OPEN
	switch (uart_type[COM2].rcv_state) 
	{
		case (UART_RCV_OVERTIME_S0): 
		{
		    if (1 == uart_type[COM2].rcv_flag) 
			{
				uart_type[COM2].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART2, FRAMEOVERTIME);
				uart_type[COM2].rcv_state = UART_RCV_OVERTIME_S1;
			}
		} 
		break;

		case (UART_RCV_OVERTIME_S1):
		{
			if (1 == uart_type[COM2].rcv_flag) 
			{
				uart_type[COM2].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART2, FRAMEOVERTIME);
				uart_type[COM2].rcv_state = UART_RCV_OVERTIME_S1;
			}
			else if ((0 == uart_type[COM2].rcv_flag) && (0 == Check5msTimer(TIMER_5MS_UART2))) 
			{
				uart_type[COM2].rcv_msg = TRUE;				
				uart_type[COM2].rcv_state = UART_RCV_OVERTIME_S0;
				UartStopReceiveIT(COM2);
			} 
		} 
		break;

		default: 
		{
		    uart_type[COM2].rcv_state = UART_RCV_OVERTIME_S0;
		} 
		break;
	}
#endif

#if COM3_OPEN
	switch (uart_type[COM3].rcv_state) 
	{
		case (UART_RCV_OVERTIME_S0): 
		{
		    if (1 == uart_type[COM3].rcv_flag) 
			{
				uart_type[COM3].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART3, FRAMEOVERTIME);
				uart_type[COM3].rcv_state = UART_RCV_OVERTIME_S1;
			}
		} 
		break;

		case (UART_RCV_OVERTIME_S1): 
		{
			if (1 == uart_type[COM3].rcv_flag) 
			{
				uart_type[COM3].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_UART3, FRAMEOVERTIME);
				uart_type[COM3].rcv_state = UART_RCV_OVERTIME_S1;
			}
			else if ((0 == uart_type[COM3].rcv_flag) && (0 == Check5msTimer(TIMER_5MS_UART3))) 
			{
				uart_type[COM3].rcv_msg = TRUE;				
				uart_type[COM3].rcv_state = UART_RCV_OVERTIME_S0;
				UartStopReceiveIT(COM3);
			} 			
		} 
		break;

		default: 
		{
		    uart_type[COM3].rcv_state = UART_RCV_OVERTIME_S0;
		} 
		break;
    }
#endif

#if LPCOM0_OPEN
	switch (uart_type[LPCOM0].rcv_state) 
	{
		case (UART_RCV_OVERTIME_S0): 
		{
			if (1 == uart_type[LPCOM0].rcv_flag) 
			{
				uart_type[LPCOM0].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_LPUART0, FRAMEOVERTIME);
				uart_type[LPCOM0].rcv_state = UART_RCV_OVERTIME_S1;
		    }
		} 
		break;

		case (UART_RCV_OVERTIME_S1):
		{
			if (1 == uart_type[LPCOM0].rcv_flag) 
			{
				uart_type[LPCOM0].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_LPUART0, FRAMEOVERTIME);
				uart_type[LPCOM0].rcv_state = UART_RCV_OVERTIME_S1;
			}
			else if ((0 == uart_type[LPCOM0].rcv_flag) && (0 == Check5msTimer(TIMER_5MS_LPUART0))) 
			{
				uart_type[LPCOM0].rcv_msg = TRUE;
				uart_type[LPCOM0].rcv_state = UART_RCV_OVERTIME_S0;
				UartStopReceiveIT(LPCOM0);
			} 
		} 
		break;

		default: 
		{
		    uart_type[LPCOM0].rcv_state = UART_RCV_OVERTIME_S0;
		} 
		break;
  }
#endif

#if LPCOM1_OPEN
	#if MODULE_BLE_USED
	switch (uart_type[LPCOM1].rcv_state) 
	{
		case (UART_RCV_OVERTIME_S0): 
		{
			if (1 == BleRcvFrameDataFlag())//if (1 == uart_type[LPCOM1].rcv_flag) 
			{
				BleClrRcvFrameDataFlag();//uart_type[LPCOM1].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_LPUART1, FRAMEOVERTIME);
				uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S1;
			}
		}
		break;

		case (UART_RCV_OVERTIME_S1): 
		{
			if (1 == BleRcvFrameDataFlag())//if (1 == uart_type[LPCOM1].rcv_flag) 
			{
				BleClrRcvFrameDataFlag();//uart_type[LPCOM1].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_LPUART1, FRAMEOVERTIME);
				uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S1;
			}
			else if ((0 == BleRcvFrameDataFlag()) && (0 == Check5msTimer(TIMER_5MS_LPUART1))) //else if ((0 == uart_type[LPCOM1].rcv_flag) && (0 == Check5msTimer(TIMER_5MS_LPUART1))) 
			{
				uart_type[LPCOM1].rcv_msg = TRUE;
				uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S0;
				UartStopReceiveIT(LPCOM1);
			} 
		} 
		break;

		default: 
		{
		    uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S0;
		} 
		break;
    }
	#else
	switch (uart_type[LPCOM1].rcv_state) 
	{
		case (UART_RCV_OVERTIME_S0): 
		{
			if (1 == uart_type[LPCOM1].rcv_flag) 
			{
				uart_type[LPCOM1].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_LPUART1, FRAMEOVERTIME);
				uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S1;
			}
		}
		break;

		case (UART_RCV_OVERTIME_S1): 
		{
			if (1 == uart_type[LPCOM1].rcv_flag) 
			{
				uart_type[LPCOM1].rcv_flag = 0;
				Set5msTimer(TIMER_5MS_LPUART1, FRAMEOVERTIME);
				uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S1;
			}
			else if ((0 == uart_type[LPCOM1].rcv_flag) && (0 == Check5msTimer(TIMER_5MS_LPUART1))) 
			{
				uart_type[LPCOM1].rcv_msg = TRUE;
				uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S0;
				UartStopReceiveIT(LPCOM1);
			} 
		} 
		break;

		default: 
		{
		    uart_type[LPCOM1].rcv_state = UART_RCV_OVERTIME_S0;
		} 
		break;
    }
    #endif
#endif
}
//判串口接收空闲
u8 UartRcvMachineIfIdle(u8 com)
{
	switch(com)
	{
#if COM0_OPEN
		case(COM0):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM0].rcv_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
//		break;
#endif
	
#if COM1_OPEN
		case(COM1):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM1].rcv_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
//		break;
#endif
		
#if COM2_OPEN
		case(COM2):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM2].rcv_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
//		break;
#endif		
		
#if COM3_OPEN
		case(COM3):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM3].rcv_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
//		break;
#endif			
		
#if LPCOM0_OPEN
		case(LPCOM0):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[LPCOM0].rcv_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
//		break;
#endif		

#if LPCOM1_OPEN
		case(LPCOM1):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[LPCOM1].rcv_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
//		break;
#endif		
		default:
		break;
	}
	return TASK_IDLE;
}
/*************************************************
Function:void UartSendMachine(void)
Description: 串口发送前台状态机
Input:  None
return: None
Others: 内部接口
*************************************************/
void UartSendMachine(void)
{
#if COM0_OPEN
	if(TASK_IDLE == uart_type[COM0].uart_status)
	{
		Set100msTimer(TIMER_100MS_UART0_BUSY,UART_BUSY_OVERTIME);
	}
	else if(0 == Check100msTimer(TIMER_100MS_UART0_BUSY))
	{
		uart_type[COM0].uart_status = TASK_IDLE;//超时，强制复位
		Reset_RstPeripheral0(ResetMskUart0);
		uart_type[COM0].finish_send_flag = FALSE;				
		UartInit(COM0,BAUD2400,DATA_BIT_8,STOPBIT_1,UART_NONE_PARITY,UART_LPUART_Disable);
		uart_type[COM0].send_state = UART_SEND_OVERTIME_S0;		
	}
    switch (uart_type[COM0].send_state) 
	{
		case (UART_SEND_OVERTIME_S0): 
		{
			if (TRUE == uart_type[COM0].start_send_flag) 
			{				
				uart_type[COM0].start_send_flag = FALSE;
				Set100msTimer(TIMER_100MS_UART0_SEND, uart_type[COM0].send_over_time);
				uart_type[COM0].send_state = UART_SEND_OVERTIME_S1;
		    }
		} 
		break;
		case (UART_SEND_OVERTIME_S1):
		{
			if (TRUE == uart_type[COM0].finish_send_flag) 
			{
				uart_type[COM0].finish_send_flag = FALSE;
				uart_type[COM0].send_state = UART_SEND_OVERTIME_S0;
			} 
			else if (0 == Check100msTimer(TIMER_100MS_UART0_SEND)) 
			{
				uart_type[COM0].uart_status = TASK_IDLE;//发送超时，强制复位
				Reset_RstPeripheral0(ResetMskUart0);				
				uart_type[COM0].finish_send_flag = FALSE;
				UartInit(COM0,BAUD2400,DATA_BIT_8,STOPBIT_1,UART_NONE_PARITY,UART_LPUART_Disable);
				uart_type[COM0].send_state = UART_SEND_OVERTIME_S0;
			} 
		} 
		break;
		default: 
		{
			uart_type[COM0].send_state = UART_SEND_OVERTIME_S0;
		} 
		break;
	}
#endif	
#if COM1_OPEN
	if(TASK_IDLE == uart_type[COM1].uart_status)
	{
		Set100msTimer(TIMER_100MS_UART1_BUSY,UART_BUSY_OVERTIME);
	}
	else if(0 == Check100msTimer(TIMER_100MS_UART1_BUSY))
	{
		uart_type[COM1].uart_status = TASK_IDLE;//超时，强制复位
		Reset_RstPeripheral0(ResetMskUart1);
		uart_type[COM1].finish_send_flag = FALSE;				
		UartInit(NB_UART_COM, BAUD9600, DATA_BIT_8, STOPBIT_1, UART_NONE_PARITY,UART_LPUART_Disable);	
		uart_type[COM1].send_state = UART_SEND_OVERTIME_S0;		
	}
	switch (uart_type[COM1].send_state) 
	{
		case (UART_SEND_OVERTIME_S0): 
		{
			if (TRUE == uart_type[COM1].start_send_flag) 
			{				
				uart_type[COM1].start_send_flag = FALSE;
				Set100msTimer(TIMER_100MS_UART1_SEND, uart_type[COM1].send_over_time);
				uart_type[COM1].send_state = UART_SEND_OVERTIME_S1;
		    }
		} 
		break;
		case (UART_SEND_OVERTIME_S1):
		{
			if (TRUE == uart_type[COM1].finish_send_flag) 
			{
				uart_type[COM1].finish_send_flag = FALSE;
				uart_type[COM1].send_state = UART_SEND_OVERTIME_S0;
			} 
			else if (0 == Check100msTimer(TIMER_100MS_UART1_SEND)) 
			{
				uart_type[COM1].uart_status = TASK_IDLE;//发送超时，强制复位
				Reset_RstPeripheral0(ResetMskUart1);
				uart_type[COM1].finish_send_flag = FALSE;				
				UartInit(NB_UART_COM, BAUD9600, DATA_BIT_8, STOPBIT_1, UART_NONE_PARITY,UART_LPUART_Disable);	
				uart_type[COM1].send_state = UART_SEND_OVERTIME_S0;
			} 
		} 
		break;
		default: 
		{
			uart_type[COM1].send_state = UART_SEND_OVERTIME_S0;
		} 
		break;
	}
#endif
#if COM2_OPEN
	if(TASK_IDLE == uart_type[COM2].uart_status)
	{
		Set100msTimer(TIMER_100MS_UART2_BUSY,UART_BUSY_OVERTIME);
	}
	else if(0 == Check100msTimer(TIMER_100MS_UART2_BUSY))
	{
		uart_type[COM2].uart_status = TASK_IDLE;//超时，强制复位
		Reset_RstPeripheral1(ResetMskUart2);
		uart_type[COM2].finish_send_flag = FALSE;				
		UartInit(COM2,BAUD2400,DATA_BIT_8,STOPBIT_1,UART_NONE_PARITY,UART_LPUART_Disable);
		uart_type[COM2].send_state = UART_SEND_OVERTIME_S0;		
	}
	switch (uart_type[COM2].send_state) 
	{
		case (UART_SEND_OVERTIME_S0): 
		{
			if (TRUE == uart_type[COM2].start_send_flag) 
			{				
				uart_type[COM2].start_send_flag = FALSE;
				Set100msTimer(TIMER_100MS_UART2_SEND, uart_type[COM2].send_over_time);
				uart_type[COM2].send_state = UART_SEND_OVERTIME_S1;
		    }
		} 
		break;
		case (UART_SEND_OVERTIME_S1):
		{
			if (TRUE == uart_type[COM2].finish_send_flag) 
			{
				uart_type[COM2].finish_send_flag = FALSE;
				uart_type[COM2].send_state = UART_SEND_OVERTIME_S0;
			} 
			else if (0 == Check100msTimer(TIMER_100MS_UART2_SEND)) 
			{
				uart_type[COM2].uart_status = TASK_IDLE;//发送超时，强制复位
				Reset_RstPeripheral1(ResetMskUart2);
				uart_type[COM2].finish_send_flag = FALSE;
				UartInit(COM2,BAUD2400,DATA_BIT_8,STOPBIT_1,UART_NONE_PARITY,UART_LPUART_Disable);
				uart_type[COM2].send_state = UART_SEND_OVERTIME_S0;
			} 
		} 
		break;
		default: 
		{
			uart_type[COM2].send_state = UART_SEND_OVERTIME_S0;
		} 
		break;
	}
#endif
#if COM3_OPEN
	if(TASK_IDLE == uart_type[COM3].uart_status)
	{
		Set100msTimer(TIMER_100MS_UART3_BUSY,UART_BUSY_OVERTIME);
	}
	else if(0 == Check100msTimer(TIMER_100MS_UART3_BUSY))
	{
		uart_type[COM3].uart_status = TASK_IDLE;//超时，强制复位
		Reset_RstPeripheral1(ResetMskUart3);
		uart_type[COM3].finish_send_flag = FALSE;				
		UartInit(COM3,BAUD2400,DATA_BIT_8,STOPBIT_1,UART_NONE_PARITY,UART_LPUART_Disable);
		uart_type[COM3].send_state = UART_SEND_OVERTIME_S0;		
	}
	switch (uart_type[COM3].send_state) 
	{
		case (UART_SEND_OVERTIME_S0): 
		{
			if (TRUE == uart_type[COM3].start_send_flag) 
			{				
				uart_type[COM3].start_send_flag = FALSE;
				Set100msTimer(TIMER_100MS_UART3_SEND, uart_type[COM3].send_over_time);
				uart_type[COM3].send_state = UART_SEND_OVERTIME_S1;
		    }
		} 
		break;
		case (UART_SEND_OVERTIME_S1):
		{
			if (TRUE == uart_type[COM3].finish_send_flag) 
			{
				uart_type[COM3].finish_send_flag = FALSE;
				uart_type[COM3].send_state = UART_SEND_OVERTIME_S0;
			} 
			else if (0 == Check100msTimer(TIMER_100MS_UART3_SEND)) 
			{
				uart_type[COM3].uart_status = TASK_IDLE;//发送超时，强制复位
				Reset_RstPeripheral1(ResetMskUart3);
				uart_type[COM3].finish_send_flag = FALSE;
				UartInit(COM3,BAUD2400,DATA_BIT_8,STOPBIT_1,UART_NONE_PARITY,UART_LPUART_Disable);
				uart_type[COM3].send_state = UART_SEND_OVERTIME_S0;
			} 
		} 
		break;
		default: 
		{
			uart_type[COM3].send_state = UART_SEND_OVERTIME_S0;
		} 
		break;
	}
#endif
#if LPCOM0_OPEN
	if(TASK_IDLE == uart_type[LPCOM0].uart_status)
	{
		Set100msTimer(TIMER_100MS_LPUART0_BUSY,UART_BUSY_OVERTIME);
	}
	else if(0 == Check100msTimer(TIMER_100MS_LPUART0_BUSY))
	{
		uart_type[LPCOM0].uart_status = TASK_IDLE;//超时，强制复位
		Reset_RstPeripheral0(ResetMskLpUart0);
		uart_type[LPCOM0].finish_send_flag = FALSE;				
		UartInit(IR_UART_COM, BAUD2400, DATA_BIT_8, STOPBIT_1, UART_EVEN_PARITY, UART_LPUART_Disable);
		uart_type[LPCOM0].send_state = UART_SEND_OVERTIME_S0;		
	}
	switch (uart_type[LPCOM0].send_state) 
	{
		case (UART_SEND_OVERTIME_S0): 
		{
			if (TRUE == uart_type[LPCOM0].start_send_flag) 
			{				
				uart_type[LPCOM0].start_send_flag = FALSE;
				Set100msTimer(TIMER_100MS_LPUART0_SEND, uart_type[LPCOM0].send_over_time);
				uart_type[LPCOM0].send_state = UART_SEND_OVERTIME_S1;
		    }
		} 
		break;
		case (UART_SEND_OVERTIME_S1):
		{
			if (TRUE == uart_type[LPCOM0].finish_send_flag) 
			{
				uart_type[LPCOM0].finish_send_flag = FALSE;
				uart_type[LPCOM0].send_state = UART_SEND_OVERTIME_S0;
			} 
			else if (0 == Check100msTimer(TIMER_100MS_LPUART0_SEND)) 
			{
				uart_type[LPCOM0].uart_status = TASK_IDLE;//发送超时，强制复位
				Reset_RstPeripheral0(ResetMskLpUart0);
				uart_type[LPCOM0].finish_send_flag = FALSE;
				UartInit(IR_UART_COM, BAUD2400, DATA_BIT_8, STOPBIT_1, UART_EVEN_PARITY, UART_LPUART_Disable);
				uart_type[LPCOM0].send_state = UART_SEND_OVERTIME_S0;
			} 
		} 
		break;
		default: 
		{
			uart_type[LPCOM0].send_state = UART_SEND_OVERTIME_S0;
		} 
		break;
	}
#endif
#if LPCOM1_OPEN
	if(TASK_IDLE == uart_type[LPCOM1].uart_status)
	{
		Set100msTimer(TIMER_100MS_LPUART1_BUSY,UART_BUSY_OVERTIME);
	}
	else if(0 == Check100msTimer(TIMER_100MS_LPUART1_BUSY))
	{
		uart_type[LPCOM1].uart_status = TASK_IDLE;//超时，强制复位
		Reset_RstPeripheral0(ResetMskLpUart1);
		uart_type[LPCOM1].finish_send_flag = FALSE;				
		#if MODULE_BLE_USED
			UartInit(BLE_UART_COM, BAUD38400, DATA_BIT_8, STOPBIT_1, UART_EVEN_PARITY,UART_LPUART_Disable);
		#else //NB
			UartInit(NB_UART_COM, BAUD9600, DATA_BIT_8, STOPBIT_1, UART_NONE_PARITY,UART_LPUART_Disable);
		#endif
		uart_type[LPCOM1].send_state = UART_SEND_OVERTIME_S0;		
	}
	switch (uart_type[LPCOM1].send_state) 
	{
		case (UART_SEND_OVERTIME_S0): 
		{
			if (TRUE == uart_type[LPCOM1].start_send_flag) 
			{				
				uart_type[LPCOM1].start_send_flag = FALSE;
				Set100msTimer(TIMER_100MS_LPUART1_SEND, uart_type[LPCOM1].send_over_time);
				uart_type[LPCOM1].send_state = UART_SEND_OVERTIME_S1;
		    }
		} 
		break;
		case (UART_SEND_OVERTIME_S1):
		{
			if (TRUE == uart_type[LPCOM1].finish_send_flag) 
			{
				uart_type[LPCOM1].finish_send_flag = FALSE;
				uart_type[LPCOM1].send_state = UART_SEND_OVERTIME_S0;
			} 
			else if (0 == Check100msTimer(TIMER_100MS_LPUART1_SEND)) 
			{
				uart_type[LPCOM1].uart_status = TASK_IDLE;//发送超时，强制复位
				Reset_RstPeripheral0(ResetMskLpUart1);
				uart_type[LPCOM1].finish_send_flag = FALSE;
				#if MODULE_BLE_USED
					UartInit(BLE_UART_COM, BAUD38400, DATA_BIT_8, STOPBIT_1, UART_EVEN_PARITY,UART_LPUART_Disable);
				#else //NB
					UartInit(NB_UART_COM, BAUD9600, DATA_BIT_8, STOPBIT_1, UART_NONE_PARITY,UART_LPUART_Disable);
				#endif
				uart_type[LPCOM1].send_state = UART_SEND_OVERTIME_S0;
			}
		}
		break;
		default: 
		{
			uart_type[LPCOM1].send_state = UART_SEND_OVERTIME_S0;
		} 
		break;
	}
#endif
}
//判串口发送空闲
u8 UartSendMachineIfIdle(u8 com)
{
	switch(com)
	{
#if COM0_OPEN
		case(COM0):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM0].send_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
#endif
#if COM1_OPEN
		case(COM1):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM1].send_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
#endif
#if COM2_OPEN
		case(COM2):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM2].send_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
#endif		
#if COM3_OPEN
		case(COM3):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[COM3].send_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
#endif			
#if LPCOM0_OPEN
		case(LPCOM0):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[LPCOM0].send_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
#endif		

#if LPCOM1_OPEN
		case(LPCOM1):
		{
			if(UART_RCV_OVERTIME_S0 == uart_type[LPCOM1].send_state)
			{
				return TASK_IDLE;
			}
			else
			{
				return TASK_BUSY;
			}
		}
#endif		
		default:
		break;
	}
	return TASK_IDLE;
}
/*************************************************
Function:void UartStartReceiveIT(u8 com)
Description: 开启串口接收中断
Input:  com:端口号
Return: void
Others: 内部接口
*************************************************/
static void UartStartReceiveIT(u8 com) 
{
	if ((LPCOM0 == com) || (LPCOM1 == com))
	{
		//uart_type[com].LP_Instance->SCON_f.TCIE = 0;  //关闭发送中断
		LPUart_EnableIrq(uart_type[com].LP_Instance,LPUartRxIrq);      ///<开启接收中断
		LPUart_EnableIrq(uart_type[com].LP_Instance,LPUartPEIrq);    //奇偶校验错误中断	
		LPUart_EnableIrq(uart_type[com].LP_Instance,LPUartFEIrq);   //帧错误中断
	} 
	else 
	{
		//uart_type[com].Instance->SCON_f.TCIE = 0;  //关闭发送中断
		Uart_EnableIrq(uart_type[com].Instance,UartRxIrq);      ///<开启接收中断
		Uart_EnableIrq(uart_type[com].Instance,UartPEIrq);    //奇偶校验错误中断	
		Uart_EnableIrq(uart_type[com].Instance,UartFEIrq);   //帧错误中断
	}
}
/*************************************************
Function:void UartStopReceiveIT(u8 com)
Description: 停止串口接收中断
Input:  com:端口号
Return: void
Others: 内部接口
*************************************************/
static void UartStopReceiveIT(u8 com) 
{
	if ((LPCOM0 == com) || (LPCOM1 == com)) 
	{
		LPUart_DisableIrq(uart_type[com].LP_Instance,LPUartRxIrq);   //<接收中断
		LPUart_DisableIrq(uart_type[com].LP_Instance,LPUartPEIrq);   //奇偶校验错误中断	
		LPUart_DisableIrq(uart_type[com].LP_Instance,LPUartFEIrq);   //帧错误中断
	} 
	else 
	{
		Uart_DisableIrq(uart_type[com].Instance,UartRxIrq);   ///<接收中断
		Uart_DisableIrq(uart_type[com].Instance,UartPEIrq);   //奇偶校验错误中断	
		Uart_DisableIrq(uart_type[com].Instance,UartFEIrq);   //帧错误中断
	}
}

/*************************************************
Function:void UartPortCfg(u8 com)
Description: 串口IO及中断控制器初始化
Input:  com,端口号
Return: void
Others: 内部接口
*************************************************/
static void UartPortCfg(u8 com) 
{
    stc_gpio_cfg_t stcGpioCfg;
    DDL_ZERO_STRUCT(stcGpioCfg);

	stcGpioCfg.bOutputVal = TRUE;
#if COM0_OPEN
	if (com == COM0) 
	{
		Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);  ///<使能GPIO时钟
		// IO  配置
		///< TX
		stcGpioCfg.enDir = GpioDirOut;
		Gpio_Init(UART0_TX_PORT, UART0_TX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART0_TX_PORT, UART0_TX_PIN, UART0_TX_AF);  //端口为URART0_TX

		///< RX
		stcGpioCfg.enDir = GpioDirIn;
		Gpio_Init(UART0_RX_PORT, UART0_RX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART0_RX_PORT, UART0_RX_PIN, UART0_RX_AF);  //端口为URART0_RX
		Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0,TRUE);  
		EnableNvic(UART0_2_IRQn, IrqLevel3, TRUE);  ///<系统中断使能
	}
#endif

#if COM1_OPEN
	if (com == COM1) 
	{
		Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);  ///<使能GPIO时钟
		// IO  配置
		///< TX
		stcGpioCfg.enDir = GpioDirOut;
		Gpio_Init(UART1_TX_PORT, UART1_TX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART1_TX_PORT, UART1_TX_PIN, UART1_TX_AF);  //端口为URART1_TX

		///< RX
		stcGpioCfg.enDir = GpioDirIn;
		Gpio_Init(UART1_RX_PORT, UART1_RX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART1_RX_PORT, UART1_RX_PIN, UART1_RX_AF);  //端口为URART1_RX
		Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);  
		EnableNvic(UART1_3_IRQn, IrqLevel3, TRUE);  ///<系统中断使能
	}
#endif

#if COM2_OPEN
	if (com == COM2) 
	{
		Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);  ///<使能GPIO时钟
		// IO  配置
		///< TX
		stcGpioCfg.enDir = GpioDirOut;
		Gpio_Init(UART2_TX_PORT, UART2_TX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART2_TX_PORT, UART2_TX_PIN, UART2_TX_AF);  //端口为URART2_TX

		///< RX
		stcGpioCfg.enDir = GpioDirIn;
		Gpio_Init(UART2_RX_PORT, UART2_RX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART2_RX_PORT, UART2_RX_PIN, UART2_RX_AF);  //端口为URART2_RX
		Sysctrl_SetPeripheralGate(SysctrlPeripheralUart2,TRUE);  
		EnableNvic(UART0_2_IRQn, IrqLevel3, TRUE);  ///<系统中断使能
	}
#endif

#if COM3_OPEN
	if (com == COM3) 
	{
		Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);  ///<使能GPIO时钟
		// IO  配置
		///< TX
		stcGpioCfg.enDir = GpioDirOut;
		Gpio_Init(UART3_TX_PORT, UART3_TX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART3_TX_PORT, UART3_TX_PIN, UART3_TX_AF);  //端口为URART3_TX

		///< RX
		stcGpioCfg.enDir = GpioDirIn;
		Gpio_Init(UART3_RX_PORT, UART3_RX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(UART3_RX_PORT, UART3_RX_PIN, UART3_RX_AF);  //端口为URART3_RX
		Sysctrl_SetPeripheralGate(SysctrlPeripheralUart3,TRUE);  
		EnableNvic(UART1_3_IRQn, IrqLevel3, TRUE);  ///<系统中断使能
	}
#endif

#if LPCOM0_OPEN
	if (com == LPCOM0) 
	{
		Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);  ///<使能GPIO时钟
		// IO  配置
		///< TX
		stcGpioCfg.enDir = GpioDirOut;
		Gpio_Init(LPUART0_TX_PORT, LPUART0_TX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(LPUART0_TX_PORT, LPUART0_TX_PIN,LPUART0_TX_AF);  //端口为LPURART0_TX

		///< RX
		stcGpioCfg.enPu = GpioPuEnable;  ///< 端口上下拉配置->上拉
		stcGpioCfg.enDir = GpioDirIn;
		Gpio_Init(LPUART0_RX_PORT, LPUART0_RX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(LPUART0_RX_PORT, LPUART0_RX_PIN,LPUART0_RX_AF);  //端口为LPURART0_RX
		Sysctrl_SetPeripheralGate(SysctrlPeripheralLpUart0,TRUE);  
		EnableNvic(LPUART0_IRQn, IrqLevel3, TRUE);  ///<系统中断使能
	}
#endif

#if LPCOM1_OPEN
	if (com == LPCOM1) 
	{
		Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);  ///<使能GPIO时钟
		// IO  配置
		///< TX
		stcGpioCfg.enDir = GpioDirOut;
		Gpio_Init(LPUART1_TX_PORT, LPUART1_TX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(LPUART1_TX_PORT, LPUART1_TX_PIN, LPUART1_TX_AF);  //端口为LPURART1_TX

		///< RX
		stcGpioCfg.enPu = GpioPuEnable;  ///< 端口上下拉配置->上拉
		stcGpioCfg.enDir = GpioDirIn;
		Gpio_Init(LPUART1_RX_PORT, LPUART1_RX_PIN, &stcGpioCfg);
		Gpio_SetAfMode(LPUART1_RX_PORT, LPUART1_RX_PIN, LPUART1_RX_AF);  //端口为LPURART1_RX
		Sysctrl_SetPeripheralGate(SysctrlPeripheralLpUart1,TRUE);  
		EnableNvic(LPUART1_IRQn, IrqLevel3, TRUE);  ///<系统中断使能		 
	}
#endif
}

/*************************************************
Function: u8 HAL_GetUart_Hander(u8 com,M0P_UART_TypeDef* UARTx)
Description: 通过串口号获取串口的句柄
Input:  com：串口号
Return: void
Others: 内部接口
*************************************************/
M0P_UART_TypeDef *HAL_GetUart_Hander(u8 com) 
{   
	M0P_UART_TypeDef *Addr;
	switch (com) 
	{
		case COM0:
			Addr = M0P_UART0;
		break;
		
		case COM1:
		    Addr = M0P_UART1;
		break;
		
		case COM2:
		    Addr = M0P_UART2;
		break;
		
		case COM3:
		    Addr = M0P_UART3;
		break;
    
		default:
		break;
	}
	return ((M0P_UART_TypeDef *)Addr);
}

M0P_LPUART_TypeDef *HAL_GetLPUart_Hander(u8 com) 
{
    M0P_LPUART_TypeDef *Addr;

	if (com == LPCOM0) 
	{
		Addr = M0P_LPUART0;
	}
	else
	{
	    Addr = M0P_LPUART1;
	}
    return ((M0P_LPUART_TypeDef *)Addr);
}
/*************************************************
Function: void HAL_UartCfg(u8 com)
Description: 串口初始化
Input:  com：串口号
Return: void
Others: 内部接口
*************************************************/
static void HAL_UartCfg(u8 com) 
{
	stc_uart_cfg_t stcCfg;
	stc_lpuart_cfg_t stclpCfg;
	M0P_LPUART_TypeDef *LPUARTx = NULL;
	M0P_UART_TypeDef *UARTx = NULL;
	//获取串口句柄
	if ((LPCOM0 == com) || (LPCOM1 == com)) 
	{
		DDL_ZERO_STRUCT(stclpCfg);
		LPUARTx = HAL_GetLPUart_Hander(com);		
		///< LPUART 初始化
		stclpCfg.enStopBit = (en_lpuart_stop_t)uart_type[com].para.StopBits;  ///< 停止位
		stclpCfg.enMmdorCk = (en_lpuart_mmdorck_t)uart_type[com].para.Parity;    ///<校验

		if (uart_type[com].para.LPModeFunction == UART_LPUART_Disable)  //非低功耗模式下
		{
		    stclpCfg.stcBaud.enSclkSel = LPUartMskPclk;        ///<传输时钟源
		    stclpCfg.stcBaud.u32Sclk = Sysctrl_GetPClkFreq();  ///< PCLK获取
		    stclpCfg.stcBaud.enSclkDiv = (en_lpuart_clkdiv_t)uart_type[com].para.OverSampling;    ///<采样分频
		    stclpCfg.stcBaud.u32Baud = uart_type[com].para.BaudRate;  ///<波特率
		} 
		else 
		{
		    stclpCfg.stcBaud.enSclkSel = LPUartMskRcl;  ///<传输时钟源
		    stclpCfg.stcBaud.u32Sclk = 38400;           ///< RCL时钟频率 38400Hz
		    stclpCfg.stcBaud.enSclkDiv = LPUartMsk4Or8Div;            ///<采样分频
		    stclpCfg.stcBaud.u32Baud = uart_type[com].para.BaudRate;  ///<波特率
		}
		stclpCfg.enRunMode = (en_lpuart_mode_t)uart_type[com].para.UARTMode;  ///<工作模式
		LPUart_Init(LPUARTx, &stclpCfg);
//		LPUARTx->SADDR = 0;
//		LPUARTx->SADEN = 0;
//		///< LPUART 中断
		LPUart_ClrStatus(LPUARTx, LPUartRC);      ///<清接收中断请求
		LPUart_ClrStatus(LPUARTx, LPUartTC);      ///<清发送中断请求
		LPUart_EnableIrq(LPUARTx, LPUartRxIrq);   ///<使能接收中断
		LPUart_DisableIrq(LPUARTx, LPUartTxIrq);  ///<禁止发送中断
	} 
	else
	{
		DDL_ZERO_STRUCT(stcCfg);
		UARTx = HAL_GetUart_Hander(com);
		///< UART Init		
		stcCfg.enStopBit = (en_uart_stop_t)uart_type[com].para.StopBits;        ///<停止位
		stcCfg.enMmdorCk = (en_uart_mmdorck_t)uart_type[com].para.Parity;          ///<偶检验	
		stcCfg.stcBaud.u32Pclk = Sysctrl_GetPClkFreq();  ///<获得外设时钟（PCLK）频率值	
		stcCfg.stcBaud.enClkDiv = (en_uart_clkdiv_t)uart_type[com].para.OverSampling;  ///<通道采样分频配置
		stcCfg.stcBaud.u32Baud = uart_type[com].para.BaudRate;  ///<波特率
		
		stcCfg.enRunMode = (en_uart_mode_t)uart_type[com].para.UARTMode;        ///<模式
		Uart_Init(UARTx, &stcCfg);  ///<串口初始化
//		UARTx->SADDR = 0;
//		UARTx->SADEN = 0;
//		///< UART中断使能
		Uart_ClrStatus(UARTx, UartRC);     ///<清接收请求
		Uart_ClrStatus(UARTx, UartTC);     ///<清接收请求
		Uart_EnableIrq(UARTx, UartRxIrq);  ///<使能串口接收中断
		Uart_DisableIrq(UARTx, UartTxIrq);  ///<禁止串口发送中断
	}
}
/*************************************************
Function:void UartTransmitIT(u8 com)
Description: 串口发送数据
Input:  com：端口号；
return: void
Others: 内部接口
*************************************************/
static void UartTransmitIT(u8 com) 
{
    if (uart_type[com].send_len > 0) 
	{
		if ((LPCOM0 == com) || (LPCOM1 == com))  
		{
			LPUart_SendDataIt(uart_type[com].LP_Instance, *uart_type[com].send_point); 		
		} 
		else 
		{
		    Uart_SendDataIt(uart_type[com].Instance, *uart_type[com].send_point); 		  //发送数据
		}
		uart_type[com].send_point++;
		uart_type[com].send_len--;
	}

    else if (0 == uart_type[com].send_len) 
	{
		if(LPCOM0 == com)//红外    
		{
			LPUart_DisableIrq(uart_type[com].LP_Instance,LPUartTxIrq);///<禁止发送中断
			LPUart_EnableIrq(uart_type[com].LP_Instance,LPUartRxIrq); ///<红外发送完成，开启接收中断
            ir_tx_end_rdy_flag = TRUE;
			Set100msTimer(TIMER_100MS_IR_TX_38K_END,10); 

		} 
		else if(LPCOM1 == com)//NB
		{
			LPUart_DisableIrq(uart_type[com].LP_Instance,LPUartTxIrq);///<禁止发送中断
		}
		else 
		{
		    Uart_DisableIrq(uart_type[com].Instance,UartTxIrq);///<禁止发送中断
		}		
		*uart_type[com].send_status_point = TASK_IDLE;
		uart_type[com].uart_status = TASK_IDLE;		
		uart_type[com].finish_send_flag = TRUE;
    }
}

/*************************************************
Function:void UartReceiveIT(u8 com)
Description: 串口接收中断
Input:  com:端口号
Return: void
Others: 内部接口
*************************************************/
static void UartReceiveIT(u8 com)
{
    volatile u8 temp = 0;  	
	if (uart_type[com].rcv_len < UP_BUF_SIZE) 
	{
		uart_type[com].rcv_flag = 1;  //接收到数据
		switch(com)
		{
#if COM0_OPEN
			case(COM0):
			{
				s_uart0_rcv_buf[uart_type[com].rcv_len] = Uart_ReceiveData(M0P_UART0);///读取数据
			}
			break;
#endif		
			
#if COM1_OPEN
			case(COM1):
			{
				NBUartReceiveIT();					//NB uart接口，单独处理
				uart_type[com].rcv_len = 0;
				//s_uart1_rcv_buf[uart_type[com].rcv_len] = Uart_ReceiveData(M0P_UART1);///读取数据
			}
			break;
#endif
			
#if COM2_OPEN
			case(COM2):
			{
				s_uart2_rcv_buf[uart_type[com].rcv_len] = Uart_ReceiveData(M0P_UART2);///读取数据
			}
			break;
#endif

#if COM3_OPEN
			case(COM3):
			{
				s_uart3_rcv_buf[uart_type[com].rcv_len] = Uart_ReceiveData(M0P_UART3);///读取数据
			}
			break;
#endif

#if LPCOM0_OPEN
			case (LPCOM0):
			{
				s_lpuart0_rcv_buf[uart_type[com].rcv_len] = LPUart_ReceiveData(M0P_LPUART0);///读取数据
			}
			break;
#endif		

#if LPCOM1_OPEN
			case (LPCOM1):
			{
			#if MODULE_BLE_USED
				uart_type[com].rcv_flag = 0;  
				BleUartReceiveIT();			  
				uart_type[com].rcv_len = 0;
			#else
				uart_type[com].rcv_flag = 0;  //NB不用帧间超时判断
				NBUartReceiveIT();			  //NB uart接口，单独处理
				uart_type[com].rcv_len = 0;
			#endif
			}
			break;
#endif			
		}
		uart_type[com].rcv_len++;
	} 
	else 
	{
		if ((LPCOM0 == com) || (LPCOM1 == com))    
		{
			temp = LPUart_ReceiveData(uart_type[com].LP_Instance);			
		}
		else
		{
			temp = Uart_ReceiveData(uart_type[com].Instance);			
		}
		uart_type[com].rcv_len = 0;
	}  
}
/*************************************************
Function:void UartIRQHandler(u8 com)
Description: 中断处理函数
Input:  com:端口号
Return: None
Others: 处于中断程序空间；内部接口
*************************************************/
static void UartIRQHandler(u8 com) 
{
	if((LPCOM0 == com) || (LPCOM1 == com))   //LPUART
	{
		//奇偶校验错误中断
		if (TRUE == LPUart_GetStatus(uart_type[com].LP_Instance,LPUartPE)) 
		{
		    LPUart_ClrStatus(uart_type[com].LP_Instance,LPUartPE); 
		}
		//帧错误中断
		if (TRUE == LPUart_GetStatus(uart_type[com].LP_Instance,LPUartFE)) 
		{		    
			LPUart_ClrStatus(uart_type[com].LP_Instance,LPUartFE); 
		}
		//发送完成中断
		if (TRUE == LPUart_GetStatus(uart_type[com].LP_Instance, LPUartTC)) 
		{
			LPUart_ClrStatus(uart_type[com].LP_Instance,LPUartTC); 
		    UartTransmitIT(com);
		}
		//接收中断 收到数据，且接收中断开启
		if ((TRUE == LPUart_GetStatus(uart_type[com].LP_Instance, LPUartRC)) && (TRUE == uart_type[com].LP_Instance->SCON_f.RCIE)) 
		{
			LPUart_ClrStatus(uart_type[com].LP_Instance,LPUartRC); 
		    UartReceiveIT(com);			
		}
	}
	else 
	{	
		//奇偶校验错误中断
		if (TRUE == Uart_GetStatus(uart_type[com].Instance,UartPE)) 
		{
		    Uart_ClrStatus(uart_type[com].Instance,UartPE); 
		}
		//帧错误中断
		if (TRUE == Uart_GetStatus(uart_type[com].Instance,UartFE)) 
		{		    
			Uart_ClrStatus(uart_type[com].Instance,UartFE); 
		}
		//发送中断
		if (TRUE == Uart_GetStatus(uart_type[com].Instance, UartTC)) 
		{
			Uart_ClrStatus(uart_type[com].Instance,UartTC); 
		    UartTransmitIT(com);			
		}
		//接收中断
		if ((TRUE == Uart_GetStatus(uart_type[com].Instance, UartRC)) && (TRUE == uart_type[com].Instance->SCON_f.RCIE))  
		{
			Uart_ClrStatus(uart_type[com].Instance,UartRC); 
		    UartReceiveIT(com);			
		}		
	}
}





