#include "includes.h"

/*******************************************************************************
* Description    : 串口的全局参数
*******************************************************************************/
UART_t g_Uart[4];


/*******************************************************************************
* Function Name  : USART_INIT
* Description    : 串口1初始化
* Input          : 	usart    - 串口号
							baudrate - 串口波特率
* Return         : None
*******************************************************************************/
void USART_INIT(uint8_t usart,uint32_t baudrate)
{
#if (USE_CORE_TYPE == 1)    //使用C1F核心板

	if(usart == USART_0)		//初始化串口0
	{
		//设置UART0 GPIO模式 TXD:PB7  RXD:PB4
		GPIOB_SetBits(GPIO_Pin_7);
		GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		//初始化串口配置
		UART0_BaudRateCfg( baudrate );
		R8_UART0_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART0_LCR = RB_LCR_WORD_SZ;	
		R8_UART0_IER = RB_IER_TXD_EN;
		R8_UART0_DIV = 1;
		
		//初始化485_1控制引脚 - PA10
		GPIOA_ModeCfg(MCU485_PIN_1, GPIO_ModeOut_PP_20mA);
		MCU485_EN1_L;
		
		//设置中断方式
		UART0_ByteTrigCfg( UART_7BYTE_TRIG );
		UART0_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART0_IRQn );
		
		memset(&g_Uart[USART_0],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_0],baudrate);
		
		g_Uart[USART_0].RX_Buffer_ReadAddr = SRAM_UART0_RecvBuffer_Start_Addr;
		g_Uart[USART_0].RX_Buffer_WriteAddr = SRAM_UART0_RecvBuffer_Start_Addr;
		g_Uart[USART_0].TX_Buffer_ReadAddr = SRAM_UART0_SendBuffer_Start_Addr;
		g_Uart[USART_0].TX_Buffer_WriteAddr = SRAM_UART0_SendBuffer_Start_Addr;
	}else if(usart == USART_1)		//初始化串口1
	{
		//设置UART1 GPIO模式	TXD:PA9  RXD:PA8
		GPIOA_SetBits(GPIO_Pin_9);
		GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		
		//初始化串口配置
		UART1_BaudRateCfg(baudrate);
		R8_UART1_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART1_LCR = RB_LCR_WORD_SZ;	
		R8_UART1_IER = RB_IER_TXD_EN;
		R8_UART1_DIV = 1;
		//设置中断方式
		UART1_ByteTrigCfg( UART_7BYTE_TRIG );
		UART1_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART1_IRQn );
		
		memset(&g_Uart[USART_1],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_1],baudrate);
		
		g_Uart[USART_1].RX_Buffer_ReadAddr = SRAM_UART1_RecvBuffer_Start_Addr;
		g_Uart[USART_1].RX_Buffer_WriteAddr = SRAM_UART1_RecvBuffer_Start_Addr;
		g_Uart[USART_1].TX_Buffer_ReadAddr = SRAM_UART1_SendBuffer_Start_Addr;
		g_Uart[USART_1].TX_Buffer_WriteAddr = SRAM_UART1_SendBuffer_Start_Addr;
		
	}else if(usart == USART_2)		//初始化串口2
	{
		//设置UART1 GPIO模式	TXD:PB23  RXD:PB22
		GPIOB_SetBits(GPIO_Pin_23);
		GPIOB_ModeCfg(GPIO_Pin_22, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOB_ModeCfg(GPIO_Pin_23, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		GPIOPinRemap( ENABLE, RB_PIN_UART2);   						//重映射引脚
		
		//初始化485_2控制引脚 - PA11
		GPIOA_ModeCfg(MCU485_PIN_2, GPIO_ModeOut_PP_20mA);
		MCU485_EN2_L;
		
		//初始化串口配置
		UART2_BaudRateCfg( baudrate );
		R8_UART2_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART2_LCR = RB_LCR_WORD_SZ;	
		R8_UART2_IER = RB_IER_TXD_EN;
		R8_UART2_DIV = 1;	
		//设置中断方式
		UART2_ByteTrigCfg( UART_7BYTE_TRIG );
		UART2_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART2_IRQn );
		
		memset(&g_Uart[USART_2],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_2],baudrate);
		
		g_Uart[USART_2].RX_Buffer_ReadAddr = SRAM_UART2_RecvBuffer_Start_Addr;
		g_Uart[USART_2].RX_Buffer_WriteAddr = SRAM_UART2_RecvBuffer_Start_Addr;
		g_Uart[USART_2].TX_Buffer_ReadAddr = SRAM_UART2_SendBuffer_Start_Addr;
		g_Uart[USART_2].TX_Buffer_WriteAddr = SRAM_UART2_SendBuffer_Start_Addr;
		
	}else if(usart == USART_3)		//初始化串口3
	{
		//设置UART1 GPIO模式	TXD:PA5  RXD:PA4
		GPIOA_SetBits(GPIO_Pin_5);
		GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		
		//初始化485_3控制引脚 PB11
		#ifndef MCU_Local_Test_Button_EN
		GPIOB_ModeCfg(MCU485_PIN_3, GPIO_ModeOut_PP_20mA);
		MCU485_EN3_L;
		#endif
		
		//初始化串口配置
		UART3_BaudRateCfg( baudrate );
		R8_UART3_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART3_LCR = RB_LCR_WORD_SZ;	
		R8_UART3_IER = RB_IER_TXD_EN;
		R8_UART3_DIV = 1;
		//设置中断方式
		UART3_ByteTrigCfg( UART_7BYTE_TRIG );
		UART3_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART3_IRQn );
		
		memset(&g_Uart[USART_3],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_3],baudrate);
		
		g_Uart[USART_3].RX_Buffer_ReadAddr = SRAM_UART3_RecvBuffer_Start_Addr;
		g_Uart[USART_3].RX_Buffer_WriteAddr = SRAM_UART3_RecvBuffer_Start_Addr;
		g_Uart[USART_3].TX_Buffer_ReadAddr = SRAM_UART3_SendBuffer_Start_Addr;
		g_Uart[USART_3].TX_Buffer_WriteAddr = SRAM_UART3_SendBuffer_Start_Addr;
	}
#elif (USE_CORE_TYPE == 2)   //使用C1核心板

	if(usart == USART_0)		//初始化串口0
	{
		//设置UART0 GPIO模式 TXD:PB7  RXD:PB4
		GPIOB_SetBits(GPIO_Pin_7);
		GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		//初始化串口配置
		UART0_BaudRateCfg( baudrate );
		R8_UART0_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART0_LCR = RB_LCR_WORD_SZ;	
		R8_UART0_IER = RB_IER_TXD_EN;
		R8_UART0_DIV = 1;
		//设置中断方式
		UART0_ByteTrigCfg( UART_7BYTE_TRIG );
		UART0_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART0_IRQn );
		
		memset(&g_Uart[USART_0],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_0],baudrate);
		
		g_Uart[USART_0].RX_Buffer_ReadAddr = SRAM_UART0_RecvBuffer_Start_Addr;
		g_Uart[USART_0].RX_Buffer_WriteAddr = SRAM_UART0_RecvBuffer_Start_Addr;
		g_Uart[USART_0].TX_Buffer_ReadAddr = SRAM_UART0_SendBuffer_Start_Addr;
		g_Uart[USART_0].TX_Buffer_WriteAddr = SRAM_UART0_SendBuffer_Start_Addr;
	}else if(usart == USART_1)		//初始化串口1
	{
		//设置UART1 GPIO模式	TXD:PA9  RXD:PA8
		GPIOA_SetBits(GPIO_Pin_9);
		GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		
		//初始化485_1控制引脚 - PB21
		GPIOB_ModeCfg(MCU485_PIN_1, GPIO_ModeOut_PP_20mA);
		MCU485_EN1_L;
		
		//初始化串口配置
		UART1_BaudRateCfg(baudrate);
		R8_UART1_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART1_LCR = RB_LCR_WORD_SZ;	
		R8_UART1_IER = RB_IER_TXD_EN;
		R8_UART1_DIV = 1;
		//设置中断方式
		UART1_ByteTrigCfg( UART_7BYTE_TRIG );
		UART1_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART1_IRQn );
		
		memset(&g_Uart[USART_1],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_1],baudrate);
		
		g_Uart[USART_1].RX_Buffer_ReadAddr = SRAM_UART1_RecvBuffer_Start_Addr;
		g_Uart[USART_1].RX_Buffer_WriteAddr = SRAM_UART1_RecvBuffer_Start_Addr;
		g_Uart[USART_1].TX_Buffer_ReadAddr = SRAM_UART1_SendBuffer_Start_Addr;
		g_Uart[USART_1].TX_Buffer_WriteAddr = SRAM_UART1_SendBuffer_Start_Addr;
		
	}else if(usart == USART_2)		//初始化串口2
	{
		//设置UART1 GPIO模式	TXD:PA7  RXD:PA6
		GPIOA_SetBits(GPIO_Pin_7);
		GPIOA_ModeCfg(GPIO_Pin_6, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOA_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		
		//初始化485_2控制引脚 - PA1
		GPIOA_ModeCfg(MCU485_PIN_2, GPIO_ModeOut_PP_20mA);
		MCU485_EN2_L;
		
		//初始化串口配置
		UART2_BaudRateCfg( baudrate );
		R8_UART2_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART2_LCR = RB_LCR_WORD_SZ;	
		R8_UART2_IER = RB_IER_TXD_EN;
		R8_UART2_DIV = 1;	
		//设置中断方式
		UART2_ByteTrigCfg( UART_7BYTE_TRIG );
		UART2_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART2_IRQn );
		
		memset(&g_Uart[USART_2],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_2],baudrate);
		
		g_Uart[USART_2].RX_Buffer_ReadAddr = SRAM_UART2_RecvBuffer_Start_Addr;
		g_Uart[USART_2].RX_Buffer_WriteAddr = SRAM_UART2_RecvBuffer_Start_Addr;
		g_Uart[USART_2].TX_Buffer_ReadAddr = SRAM_UART2_SendBuffer_Start_Addr;
		g_Uart[USART_2].TX_Buffer_WriteAddr = SRAM_UART2_SendBuffer_Start_Addr;
		
	}else if(usart == USART_3)		//初始化串口3
	{
		//设置UART1 GPIO模式	TXD:PA5  RXD:PA4
		GPIOA_SetBits(GPIO_Pin_5);
		GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
		GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
		
		//初始化485_3控制引脚 PA0
		#ifndef MCU_Local_Test_Button_EN
		GPIOA_ModeCfg(MCU485_PIN_3, GPIO_ModeOut_PP_20mA);
		MCU485_EN3_L;
		#endif
		
		//初始化串口配置
		UART3_BaudRateCfg( baudrate );
		R8_UART3_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
		R8_UART3_LCR = RB_LCR_WORD_SZ;	
		R8_UART3_IER = RB_IER_TXD_EN;
		R8_UART3_DIV = 1;
		//设置中断方式
		UART3_ByteTrigCfg( UART_7BYTE_TRIG );
		UART3_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
		NVIC_EnableIRQ( UART3_IRQn );
		
		memset(&g_Uart[USART_3],0,sizeof(UART_t));
		Set_Uart_recvTimeout(&g_Uart[USART_3],baudrate);
		
		g_Uart[USART_3].RX_Buffer_ReadAddr = SRAM_UART3_RecvBuffer_Start_Addr;
		g_Uart[USART_3].RX_Buffer_WriteAddr = SRAM_UART3_RecvBuffer_Start_Addr;
		g_Uart[USART_3].TX_Buffer_ReadAddr = SRAM_UART3_SendBuffer_Start_Addr;
		g_Uart[USART_3].TX_Buffer_WriteAddr = SRAM_UART3_SendBuffer_Start_Addr;
	}
	
#endif  //USE_CORE_TYPE == CORE_TYPE_C1F
	
}

/*******************************************************************************
* Function Name  : Set_Uart_recvTimeout
* Description    : 设置串口接收超时时间
* Return         : None
*******************************************************************************/
void Set_Uart_recvTimeout(UART_t *set_uart,uint32_t baud)
{
	if(baud == 115200)
	{
		set_uart->recvTimeout = 6;   
	}else if(baud == 512000)
	{
		set_uart->recvTimeout = 6;   
	}else 
	{
		set_uart->recvTimeout = 10;         //2024-02-23
	}
}

/*******************************************************************************
* Function Name  : Uart0_GetStart
* Description    : 获取串口0状态
* Return         : 串口0状态寄存器值
*******************************************************************************/
uint8_t Uart0_GetStart(void)
{
	return R8_UART0_LSR;
}

/*******************************************************************************
* Function Name  : Uart0_GetStart
* Description    : 获取串口1状态
* Return         : 串口1状态寄存器值
*******************************************************************************/
uint8_t Uart1_GetStart(void)
{
	return R8_UART1_LSR;
}

/*******************************************************************************
* Function Name  : Uart0_GetStart
* Description    : 获取串口2状态
* Return         : 串口2状态寄存器值
*******************************************************************************/
uint8_t Uart2_GetStart(void)
{
	return R8_UART2_LSR;
}

/*******************************************************************************
* Function Name  : Uart0_GetStart
* Description    : 获取串口3状态
* Return         : 串口3状态寄存器值
*******************************************************************************/
uint8_t Uart3_GetStart(void)
{
	return R8_UART3_LSR;
}

/*******************************************************************************
* Function Name  : MCU485_SendString_0
* Description    : 485_0 发送函数
* Input          : 
		buf - 发送数据
		l - 发送数据长度
* Return         : None
*******************************************************************************/
void MCU485_SendString_0(uint8_t *buf, uint16_t l)
{
	uint16_t delay_num = 0;
	
	MCU485_EN1_H;

	UART0_SendString(buf,l);

	//等待发送完成 - 50ms
	while(1)
	{
		WDT_Feed();
		if((Uart0_GetStart() & 0x40) && (R8_UART0_TFC == 0x00))	break;	//判断发送FIFO为空,同时FIFO计数为空
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	
	MCU485_EN1_L;
}

/*******************************************************************************
* Function Name  : MCU485_SendString_1
* Description    : 485_1 发送函数
* Input          : 
		buf - 发送数据
		l - 发送数据长度
* Return         : None
*******************************************************************************/
void MCU485_SendString_1(uint8_t *buf, uint16_t l)
{
	uint16_t delay_num = 0;
	
	MCU485_EN1_H;

	UART1_SendString(buf,l);

	//等待发送完成 - 50ms
	while(1)
	{
		WDT_Feed();
		if((Uart1_GetStart() & 0x40) && (R8_UART1_TFC == 0x00))	break;	//判断发送FIFO为空,同时FIFO计数为空
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	
	MCU485_EN1_L;
}

/*******************************************************************************
* Function Name  : MCU485_SendString_2
* Description    : 485_2 发送函数
* Input          : 
		buf - 发送数据
		l - 发送数据长度
* Return         : None
*******************************************************************************/
void MCU485_SendString_2(uint8_t *buf, uint16_t l)
{
	uint16_t delay_num = 0;
	MCU485_EN2_H;
	
	UART2_SendString(buf,l);
	//等待发送完成 - 50ms
	while(1)
	{
		WDT_Feed();
		if(Uart2_GetStart() & 0x40)	break;
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	MCU485_EN2_L;
}

/*******************************************************************************
* Function Name  : MCU485_SendString_3
* Description    : 485_3 发送函数
* Input          : 
		buf - 发送数据
		l - 发送数据长度
* Return         : None
*******************************************************************************/
void MCU485_SendString_3(uint8_t *buf, uint16_t l)
{
	uint16_t delay_num = 0;
	
	#ifndef MCU_Local_Test_Button_EN
	MCU485_EN3_H;
	#endif
	
	UART3_SendString(buf,l);
	//等待发送完成 - 50ms
	while(1)
	{
		WDT_Feed();
		if(Uart3_GetStart() & 0x40)	break;
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	
	#ifndef MCU_Local_Test_Button_EN
	MCU485_EN3_L;
	#endif
}

/*******************************************************************************
* Function Name  : UART0_ChangeBaud
* Description    : 串口0 - 改变波特率
* Return         : None
*******************************************************************************/
void UART0_ChangeBaud(uint32_t baudrate)
{
	/*关闭全局中断*/
	ENTER_CRITICAL();
	
	UART0_Reset();
	
	#if (USE_CORE_TYPE == 1)    //使用C1F核心板
	//设置UART0 GPIO模式 TXD:PB7  RXD:PB4
	GPIOB_SetBits(GPIO_Pin_7);
	GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
	//初始化串口配置
	UART0_BaudRateCfg( baudrate );
	R8_UART0_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART0_LCR = RB_LCR_WORD_SZ;	
	R8_UART0_IER = RB_IER_TXD_EN;
	R8_UART0_DIV = 1;

	//设置中断方式
	UART0_ByteTrigCfg( UART_7BYTE_TRIG );
	UART0_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART0_IRQn );
	
	//清空串口 FIFO
	UART0_CLR_RXFIFO();
	UART0_CLR_TXFIFO();

	Set_Uart_recvTimeout(&g_Uart[USART_0],baudrate);
	
	#elif (USE_CORE_TYPE == 2)   //使用C1核心板
	//设置UART0 GPIO模式 TXD:PB7  RXD:PB4
	GPIOB_SetBits(GPIO_Pin_7);
	GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
	//初始化串口配置
	UART0_BaudRateCfg( baudrate );
	R8_UART0_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART0_LCR = RB_LCR_WORD_SZ;	
	R8_UART0_IER = RB_IER_TXD_EN;
	R8_UART0_DIV = 1;
	//设置中断方式
	UART0_ByteTrigCfg( UART_7BYTE_TRIG );
	UART0_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART0_IRQn );
	
	//清空串口 FIFO
	UART0_CLR_RXFIFO();
	UART0_CLR_TXFIFO();
	
	Set_Uart_recvTimeout(&g_Uart[USART_0],baudrate);
	
	#endif
	
	EXIT_CRITICAL();
}

/*******************************************************************************
* Function Name  : UART1_ChangeBaud
* Description    : 串口1 - 改变波特率
* Return         : None
*******************************************************************************/
void UART1_ChangeBaud(uint32_t baudrate)
{
	/*关闭全局中断*/
	ENTER_CRITICAL();
	
	UART1_Reset();
	
	#if (USE_CORE_TYPE == 1)    //使用C1F核心板
	
	//设置UART1 GPIO模式	TXD:PA9  RXD:PA8
	GPIOA_SetBits(GPIO_Pin_9);
	GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平

	//初始化串口配置
	UART1_BaudRateCfg(baudrate);
	R8_UART1_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART1_LCR = RB_LCR_WORD_SZ;	
	R8_UART1_IER = RB_IER_TXD_EN;
	R8_UART1_DIV = 1;
	//设置中断方式
	UART1_ByteTrigCfg( UART_7BYTE_TRIG );
	UART1_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART1_IRQn );
	
	//清空串口 FIFO
	UART1_CLR_RXFIFO();
	UART1_CLR_TXFIFO();

	Set_Uart_recvTimeout(&g_Uart[USART_1],baudrate);
	
	#elif (USE_CORE_TYPE == 2)   //使用C1核心板
	
	//设置UART1 GPIO模式	TXD:PA9  RXD:PA8
	GPIOA_SetBits(GPIO_Pin_9);
	GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
	
	//初始化串口配置
	UART1_BaudRateCfg(baudrate);
	R8_UART1_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART1_LCR = RB_LCR_WORD_SZ;	
	R8_UART1_IER = RB_IER_TXD_EN;
	R8_UART1_DIV = 1;
	//设置中断方式
	UART1_ByteTrigCfg( UART_7BYTE_TRIG );
	UART1_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART1_IRQn );
	
	//清空串口 FIFO
	UART1_CLR_RXFIFO();
	UART1_CLR_TXFIFO();
	
	Set_Uart_recvTimeout(&g_Uart[USART_1],baudrate);
	
	#endif
	
	EXIT_CRITICAL();
}

/*******************************************************************************
* Function Name  : UART2_ChangeBaud
* Description    : 串口2 - 改变波特率
* Return         : None
*******************************************************************************/
void UART2_ChangeBaud(uint32_t baudrate)
{
	/*关闭全局中断*/
	ENTER_CRITICAL();
	
	UART2_Reset();
	
	#if (USE_CORE_TYPE == 1)    //使用C1F核心板
	
	//设置UART1 GPIO模式	TXD:PB23  RXD:PB22
	GPIOB_SetBits(GPIO_Pin_23);
	GPIOB_ModeCfg(GPIO_Pin_22, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOB_ModeCfg(GPIO_Pin_23, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
	GPIOPinRemap( ENABLE, RB_PIN_UART2);   						//重映射引脚
	
	//初始化串口配置
	UART2_BaudRateCfg( baudrate );
	R8_UART2_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART2_LCR = RB_LCR_WORD_SZ;	
	R8_UART2_IER = RB_IER_TXD_EN;
	R8_UART2_DIV = 1;
	
	//设置中断方式
	UART2_ByteTrigCfg( UART_7BYTE_TRIG );
	UART2_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART2_IRQn );
	
	//清空串口 FIFO
	UART2_CLR_RXFIFO();
	UART2_CLR_TXFIFO();
	
	Set_Uart_recvTimeout(&g_Uart[USART_2],baudrate);
	
	#elif (USE_CORE_TYPE == 2)   //使用C1核心板
	
	//设置UART1 GPIO模式	TXD:PA7  RXD:PA6
	GPIOA_SetBits(GPIO_Pin_7);
	GPIOA_ModeCfg(GPIO_Pin_6, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOA_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
	
	//初始化串口配置
	UART2_BaudRateCfg( baudrate );
	R8_UART2_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART2_LCR = RB_LCR_WORD_SZ;	
	R8_UART2_IER = RB_IER_TXD_EN;
	R8_UART2_DIV = 1;	
	//设置中断方式
	UART2_ByteTrigCfg( UART_7BYTE_TRIG );
	UART2_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART2_IRQn );
	
	//清空串口 FIFO
	UART2_CLR_RXFIFO();
	UART2_CLR_TXFIFO();
	
	Set_Uart_recvTimeout(&g_Uart[USART_2],baudrate);
	
	#endif
	
	EXIT_CRITICAL();
}

/*******************************************************************************
* Function Name  : UART3_ChangeBaud
* Description    : 串口3 - 改变波特率
* Return         : None
*******************************************************************************/
void UART3_ChangeBaud(uint32_t baudrate)
{
	/*关闭全局中断*/
	ENTER_CRITICAL();
	
	UART3_Reset();
	
	#if (USE_CORE_TYPE == 1)    //使用C1F核心板
	
	//设置UART3 GPIO模式	TXD:PA5  RXD:PA4
	GPIOA_SetBits(GPIO_Pin_5);
	GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
	
	//初始化串口配置
	UART3_BaudRateCfg( baudrate );
	R8_UART3_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART3_LCR = RB_LCR_WORD_SZ;	
	R8_UART3_IER = RB_IER_TXD_EN;
	R8_UART3_DIV = 1;
	
	//设置中断方式
	UART3_ByteTrigCfg( UART_7BYTE_TRIG );
	UART3_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART3_IRQn );
	
	//清空串口 FIFO
	UART3_CLR_RXFIFO();
	UART3_CLR_TXFIFO();
	
	Set_Uart_recvTimeout(&g_Uart[USART_3],baudrate);
	
	#elif (USE_CORE_TYPE == 2)   //使用C1核心板
	
	//设置UART1 GPIO模式	TXD:PA5  RXD:PA4
	GPIOA_SetBits(GPIO_Pin_5);
	GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU);			// RXD-配置上拉输入
	GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeOut_PP_5mA);		// TXD-配置推挽输出，注意先让IO口输出高电平
	
	//初始化串口配置
	UART3_BaudRateCfg( baudrate );
	R8_UART3_FCR = (2<<6) | RB_FCR_TX_FIFO_CLR | RB_FCR_RX_FIFO_CLR | RB_FCR_FIFO_EN;		// FIFO打开，触发点4字节
	R8_UART3_LCR = RB_LCR_WORD_SZ;	
	R8_UART3_IER = RB_IER_TXD_EN;
	R8_UART3_DIV = 1;
	//设置中断方式
	UART3_ByteTrigCfg( UART_7BYTE_TRIG );
	UART3_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
	NVIC_EnableIRQ( UART3_IRQn );
	
	//清空串口 FIFO
	UART3_CLR_RXFIFO();
	UART3_CLR_TXFIFO();

	Set_Uart_recvTimeout(&g_Uart[USART_3],baudrate);
	
	#endif
	
	EXIT_CRITICAL();
}

/*******************************************************************************
* Function Name  : Uart0_RecvString_To_SRAM
* Description    : 串口0 - 将接收到的数据写入SRAM接收发送缓冲区
* Return         : None
*******************************************************************************/
uint16_t Uart0_RecvString_To_SRAM(uint32_t addr)
{
	uint32_t w_addr = addr;
	uint16_t len = 0;
	uint8_t data = 0;
	
    while( R8_UART0_RFC )
    {
        data = R8_UART0_RBR;
		SRAM_Write_Byte(data,w_addr);
		w_addr++;
        len ++;
    }

    return (len);
}

/*******************************************************************************
* Function Name  : UART0_IRQHandler
* Description    : 串口0中断服务函数
* Return         : None
*******************************************************************************/
void UART0_IRQHandler(void)
{
	uint16_t i = 0;
    switch( UART0_GetITFlag() )
    {
        case UART_II_LINE_STAT:        	//线路状态错误
            UART0_GetLinSTA();
            break;
        case UART_II_RECV_RDY:          	//数据达到设置触发点
        case UART_II_RECV_TOUT:         	//接收超时，暂时一帧数据接收完成
            i = R8_UART0_RFC;
			if((g_Uart[USART_0].recvLen + i) >= UART_BUFFER_SIZE)	g_Uart[USART_0].recvLen = 0;
			i = UART0_RecvString(&g_Uart[USART_0].recvBuffer[g_Uart[USART_0].recvLen]);
			g_Uart[USART_0].recvLen += i;
			g_Uart[USART_0].recvIdleTiming = SysTick_1ms;
			g_Uart[USART_0].recvFlag = 0x01;			
            break;
        case UART_II_THR_EMPTY:         	//发送缓存区空，可继续发送
            break;
        case UART_II_MODEM_CHG:         	//只支持串口0
            break;
        default:
            break;
    }
}

/*******************************************************************************
* Function Name  : UART0_RECEIVE
* Description    : 串口0接收处理
* Return         : None
*******************************************************************************/
void UART0_RECEIVE(void)
{
	uint16_t i = 0;
	if(g_Uart[USART_0].recvFlag == 1)
	{
		if(SysTick_1ms - g_Uart[USART_0].recvIdleTiming > g_Uart[USART_0].recvTimeout)
		{
			g_Uart[USART_0].recvIdleTiming = SysTick_1ms;
			/*判断RX FIFO中是否还有没有数据*/
			if(UART0_GetLinSTA() & 0x01)	
			{
				i = R8_UART0_RFC;
				if((g_Uart[USART_0].recvLen + i) >= UART_BUFFER_SIZE)	g_Uart[USART_0].recvLen = 0;
				i = UART0_RecvString(&g_Uart[USART_0].recvBuffer[g_Uart[USART_0].recvLen]);
				g_Uart[USART_0].recvLen += i;
				return ;
			}
			
			g_Uart[USART_0].recvFlag = 0;
			
			//将数据存入SRAM数据处理缓冲区 - 数据接收完毕在数据头写入数据长度
			SRAM_Write_Byte((uint8_t)(g_Uart[USART_0].recvLen & 0xFF),g_Uart[USART_0].RX_Buffer_WriteAddr);
			SRAM_Write_Byte((uint8_t)((g_Uart[USART_0].recvLen >> 8) & 0xFF),g_Uart[USART_0].RX_Buffer_WriteAddr+1);
			SRAM_Write_Buff(g_Uart[USART_0].recvBuffer,g_Uart[USART_0].recvLen,g_Uart[USART_0].RX_Buffer_WriteAddr+2);
			
			g_Uart[USART_0].RX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_0].RX_Buffer_WriteAddr > SRAM_UART0_RecvBuffer_End_Addr)	g_Uart[USART_0].RX_Buffer_WriteAddr = SRAM_UART0_RecvBuffer_Start_Addr;
			g_Uart[USART_0].recvLen = 0;
		}
	}
}

/*******************************************************************************
* Function Name  : Uart3_RecvString_To_SRAM
* Description    : 串口3 - 将接收到的数据写入SRAM接收发送缓冲区
* Return         : None
*******************************************************************************/
uint16_t Uart1_RecvString_To_SRAM(uint32_t addr)
{
	uint32_t w_addr = addr;
	uint16_t len = 0;
	uint8_t data = 0;
	
    while( R8_UART1_RFC )
    {
        data = R8_UART1_RBR;
		SRAM_Write_Byte(data,w_addr);
		w_addr++;
        len ++;
    }

    return (len);
}

/*******************************************************************************
* Function Name  : UART1_IRQHandler
* Description    : 串口1中断服务函数
* Return         : None
*******************************************************************************/
void UART1_IRQHandler(void)
{
	uint16_t i = 0;
    switch( UART1_GetITFlag() )
    {
        case UART_II_LINE_STAT:        	//线路状态错误
            UART1_GetLinSTA();
            break;
        case UART_II_RECV_RDY:          	//数据达到设置触发点
        case UART_II_RECV_TOUT:         	//接收超时，暂时一帧数据接收完成
            i = R8_UART1_RFC;
			if((g_Uart[USART_1].recvLen + i) >= UART_BUFFER_SIZE) g_Uart[USART_1].recvLen = 0;
			i = UART1_RecvString(&g_Uart[USART_1].recvBuffer[g_Uart[USART_1].recvLen]);
			g_Uart[USART_1].recvLen += i;
			g_Uart[USART_1].recvIdleTiming = SysTick_1ms;
			g_Uart[USART_1].recvFlag = 0x01;		
            break;
        case UART_II_THR_EMPTY:         	//发送缓存区空，可继续发送
            break;
        case UART_II_MODEM_CHG:         	//只支持串口0
            break;
        default:
            break;
    }
}

/*******************************************************************************
* Function Name  : UART1_RECEIVE
* Description    : 串口1接收处理
* Return         : None
*******************************************************************************/
void UART1_RECEIVE(void)
{
	uint16_t i = 0;
	if(g_Uart[USART_1].recvFlag == 0x01)
	{
		if(SysTick_1ms - g_Uart[USART_1].recvIdleTiming > g_Uart[USART_1].recvTimeout)
		{
			g_Uart[USART_1].recvIdleTiming = SysTick_1ms;
			/*判断RX FIFO中是否还有没有数据*/
			if(UART1_GetLinSTA() & 0x01)	
			{
				i = R8_UART1_RFC;
				if((g_Uart[USART_1].recvLen + i) >= UART_BUFFER_SIZE) g_Uart[USART_1].recvLen = 0;
				i = UART1_RecvString(&g_Uart[USART_1].recvBuffer[g_Uart[USART_1].recvLen]);
				g_Uart[USART_1].recvLen += i;
				return ;
			}
			g_Uart[USART_1].recvFlag = 0;
			
			//将数据存入SRAM数据处理缓冲区 - 数据接收完毕在数据头写入数据长度
			SRAM_Write_Byte((uint8_t)(g_Uart[USART_1].recvLen & 0xFF),g_Uart[USART_1].RX_Buffer_WriteAddr);
			SRAM_Write_Byte((uint8_t)((g_Uart[USART_1].recvLen >> 8) & 0xFF),g_Uart[USART_1].RX_Buffer_WriteAddr+1);
			SRAM_Write_Buff(g_Uart[USART_1].recvBuffer,g_Uart[USART_1].recvLen,g_Uart[USART_1].RX_Buffer_WriteAddr+2);
			
			g_Uart[USART_1].RX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_1].RX_Buffer_WriteAddr > SRAM_UART1_RecvBuffer_End_Addr)	g_Uart[USART_1].RX_Buffer_WriteAddr = SRAM_UART1_RecvBuffer_Start_Addr;
			g_Uart[USART_1].recvLen = 0;
			
		}
	}
}

/*******************************************************************************
* Function Name  : Uart3_RecvString_To_SRAM
* Description    : 串口3 - 将接收到的数据写入SRAM接收发送缓冲区
* Return         : None
*******************************************************************************/
uint16_t Uart2_RecvString_To_SRAM(uint32_t addr)
{
	uint32_t w_addr = addr;
	uint16_t len = 0;
	uint8_t data = 0;
	
    while( R8_UART2_RFC )
    {
        data = R8_UART2_RBR;
		SRAM_Write_Byte(data,w_addr);
		w_addr++;
        len ++;
    }

    return (len);
}

/*******************************************************************************
* Function Name  : UART2_IRQHandler
* Description    : 串口2中断服务函数
* Return         : None
*******************************************************************************/
void UART2_IRQHandler(void)
{
	uint16_t i = 0;
    switch( UART2_GetITFlag() )
    {
        case UART_II_LINE_STAT:        	//线路状态错误
            UART2_GetLinSTA();
            break;
        case UART_II_RECV_RDY:          	//数据达到设置触发点
        case UART_II_RECV_TOUT:         	//接收超时，暂时一帧数据接收完成
			i = R8_UART2_RFC;				//获取FIFO中长度
			if((g_Uart[USART_2].recvLen + i) >= UART_BUFFER_SIZE)	g_Uart[USART_2].recvLen = 0;
			i = UART2_RecvString(&g_Uart[USART_2].recvBuffer[g_Uart[USART_2].recvLen]);
			g_Uart[USART_2].recvLen += i;
			g_Uart[USART_2].recvIdleTiming = SysTick_1ms;
			g_Uart[USART_2].recvFlag = 0x01;
            break;
        case UART_II_THR_EMPTY:         	//发送缓存区空，可继续发送
            break;
        case UART_II_MODEM_CHG:         	//只支持串口0
            break;
        default:
            break;
    }
}

/*******************************************************************************
* Function Name  : UART2_RECEIVE
* Description    : 串口2接收处理
* Return         : None
*******************************************************************************/
void UART2_RECEIVE(void)
{
	uint16_t i = 0;
	if(g_Uart[USART_2].recvFlag == 0x01)
	{
		if(SysTick_1ms - g_Uart[USART_2].recvIdleTiming > g_Uart[USART_2].recvTimeout)
		{
			g_Uart[USART_2].recvIdleTiming = SysTick_1ms;
			/*判断RX FIFO中是否还有没有数据*/
			if(UART2_GetLinSTA() & 0x01)	
			{
				i = R8_UART2_RFC;				//获取FIFO中长度
				if((g_Uart[USART_2].recvLen + i) >= UART_BUFFER_SIZE)	g_Uart[USART_2].recvLen = 0;
				i = UART2_RecvString(&g_Uart[USART_2].recvBuffer[g_Uart[USART_2].recvLen]);
				g_Uart[USART_2].recvLen += i;
				return ;
			}
			g_Uart[USART_2].recvFlag = 0;
			
			//将数据存入SRAM数据处理缓冲区 - 数据接收完毕在数据头写入数据长度
			SRAM_Write_Byte((uint8_t)(g_Uart[USART_2].recvLen & 0xFF),g_Uart[USART_2].RX_Buffer_WriteAddr);
			SRAM_Write_Byte((uint8_t)((g_Uart[USART_2].recvLen >> 8) & 0xFF),g_Uart[USART_2].RX_Buffer_WriteAddr+1);
			SRAM_Write_Buff(g_Uart[USART_2].recvBuffer,g_Uart[USART_2].recvLen,g_Uart[USART_2].RX_Buffer_WriteAddr+2);
			
			g_Uart[USART_2].RX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_2].RX_Buffer_WriteAddr > SRAM_UART2_RecvBuffer_End_Addr)	g_Uart[USART_2].RX_Buffer_WriteAddr = SRAM_UART2_RecvBuffer_Start_Addr;
			g_Uart[USART_2].recvLen = 0;
			
		}
	}
}

/*******************************************************************************
* Function Name  : Uart3_RecvString_To_SRAM
* Description    : 串口3 - 将接收到的数据写入SRAM接收发送缓冲区
* Return         : None
*******************************************************************************/
uint16_t Uart3_RecvString_To_SRAM(uint32_t addr)
{
	uint32_t w_addr = addr;
	uint16_t len = 0;
	uint8_t data = 0;
	
    while( R8_UART3_RFC )
    {
        data = R8_UART3_RBR;
		SRAM_Write_Byte(data,w_addr);
		w_addr++;
        len ++;
    }

    return (len);
}

/*******************************************************************************
* Function Name  : UART3_IRQHandler
* Description    : 串口3中断服务函数
* Return         : None
*******************************************************************************/
void UART3_IRQHandler(void)
{ 

	uint16_t i = 0;
    switch( UART3_GetITFlag() )
    {
        case UART_II_LINE_STAT:        	//线路状态错误
            UART3_GetLinSTA();
            break;
        case UART_II_RECV_RDY:          	//数据达到设置触发点
        case UART_II_RECV_TOUT:         	//接收超时，暂时一帧数据接收完成
			i = R8_UART3_RFC;				//获取FIFO中长度
			if((g_Uart[USART_3].recvLen + i) >= UART_BUFFER_SIZE) 	g_Uart[USART_3].recvLen = 0;
			i = UART3_RecvString(&g_Uart[USART_3].recvBuffer[g_Uart[USART_3].recvLen]);
			g_Uart[USART_3].recvLen += i;
			g_Uart[USART_3].recvIdleTiming = SysTick_1ms;
			g_Uart[USART_3].recvFlag = 0x01;			
            break;
        case UART_II_THR_EMPTY:         	//发送缓存区空，可继续发送
            break;
        case UART_II_MODEM_CHG:         	//只支持串口0
            break;
        default:
            break;
    }
}

/*******************************************************************************
* Function Name  : UART3_RECEIVE
* Description    : 串口3接收处理
* Return         : None
*******************************************************************************/
void UART3_RECEIVE(void)
{
	uint16_t i = 0;
	if(g_Uart[USART_3].recvFlag == 0x01)
	{
		if(SysTick_1ms - g_Uart[USART_3].recvIdleTiming > g_Uart[USART_3].recvTimeout)
		{
			g_Uart[USART_3].recvIdleTiming = SysTick_1ms;
			
			/*判断RX FIFO中是否还有没有数据*/
			if(UART3_GetLinSTA() & 0x01)	
			{
				i = R8_UART3_RFC;				//获取FIFO中长度
				if((g_Uart[USART_3].recvLen + i) >= UART_BUFFER_SIZE)	g_Uart[USART_3].recvLen = 0;
				i = UART3_RecvString(&g_Uart[USART_3].recvBuffer[g_Uart[USART_3].recvLen]);
				g_Uart[USART_3].recvLen += i;
			}
			g_Uart[USART_3].recvFlag = 0;
			
			//将数据存入SRAM数据处理缓冲区 - 数据接收完毕在数据头写入数据长度
			SRAM_Write_Byte((uint8_t)(g_Uart[USART_3].recvLen & 0xFF),g_Uart[USART_3].RX_Buffer_WriteAddr);
			SRAM_Write_Byte((uint8_t)((g_Uart[USART_3].recvLen >> 8) & 0xFF),g_Uart[USART_3].RX_Buffer_WriteAddr+1);
			SRAM_Write_Buff(g_Uart[USART_3].recvBuffer,g_Uart[USART_3].recvLen,g_Uart[USART_3].RX_Buffer_WriteAddr+2);
			
			g_Uart[USART_3].RX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_3].RX_Buffer_WriteAddr > SRAM_UART3_RecvBuffer_End_Addr)	g_Uart[USART_3].RX_Buffer_WriteAddr = SRAM_UART3_RecvBuffer_Start_Addr;
			g_Uart[USART_3].recvLen = 0;
		}
	}
	
	
	
}

/*******************************************************************************
* Function Name  : Uart_Search_Cmd
* Description    : 串口接收升级搜索处理函数
* Input          : 	buff - 串口数据
					len  - 串口数据长度
* Return         : None
*******************************************************************************/
uint8_t Uart_Search_Cmd(uint8_t *buff,uint8_t len)
{
	char Boot_cmd[10] = "Search:1";
	
	uint8_t rev = 0;
	
	if((buff[len - 1] == 0x0A) && (buff[len - 2] == 0x0D))
	{
		for(int i =0;i<len-2;i++)
		{
			if(buff[i] != Boot_cmd[i])
			{
				 rev++;
				break;
			}
		}
	}else{
	  rev++;
	}
	return rev;
}

/*******************************************************************************
* Function Name  : Uart_Search_Cmd
* Description    : 串口接收处理函数
* Input          : 	buff - 串口数据
					len  - 串口数据长度
* Return         : None
*******************************************************************************/
uint8_t Uart_Jump_Cmd(uint8_t *buff,uint8_t len)
{
	char Boot_cmd[10] = "Jump:1";
	
	uint8_t rev = 0;
	
	if((buff[len - 1] == 0x0A) && (buff[len - 2] == 0x0D))
	{
		for(int i =0;i<len-2;i++)
		{
			if(buff[i] != Boot_cmd[i])
			{
				 rev++;
				break;
			}
		}
	}else{
	  rev++;
	}
	return rev;
}

/*******************************************************************************
* Function Name  : Uart_Search_Cmd_from_SRAM
* Description    : 串口接收升级搜索处理函数
* Input          : 	addr - SRAM地址
					len  - 串口数据长度
* Return         : None
*******************************************************************************/
uint8_t Uart_Search_Cmd_from_SRAM(uint32_t addr, uint8_t len)
{
	char Boot_cmd[10] = "Search:1";
	
	uint8_t rev = 0;
	
	if((len-2) > 8)	return 0xFF;
	
	uint8_t* buff = malloc(len);
	if(buff == NULL)  return 0xFF;
	
	SRAM_Read_Buff(buff, len, addr);
	
	
	
	if((buff[len - 1] == 0x0A) && (buff[len - 2] == 0x0D))
	{
		for(int i =0;i<len-2;i++)
		{
			if(buff[i] != Boot_cmd[i])
			{
				 rev++;
				break;
			}
		}
	}else{
	  rev++;
	}
	free(buff);
	return rev;
}

/*******************************************************************************
* Function Name  : Uart_Jump_Cmd_from_SRAM
* Description    : 串口升级跳转
* Input          : 	addr - SRAM地址
					len  - 串口数据长度
* Return         : None
*******************************************************************************/
uint8_t Uart_Jump_Cmd_from_SRAM(uint32_t addr, uint8_t len)
{
	char Boot_cmd[10] = "Jump:1";
	
	uint8_t rev = 0;
	
	if((len-2) > 6)	return 0xFF;
	
	uint8_t* buff = malloc(len);
	if(buff == NULL)  return 0xFF;
	
	SRAM_Read_Buff(buff, len, addr);
	
	if((buff[len - 1] == 0x0A) && (buff[len - 2] == 0x0D))
	{
		for(int i =0;i<len-2;i++)
		{
			if(buff[i] != Boot_cmd[i])
			{
				 rev++;
				break;
			}
		}
	}else{
	  rev++;
	}
	free(buff);
	return rev;
}

/*******************************************************************************
* Function Name  : Uart0_Flush
* Description    : 串口0等待发送完成
* Input			 : over_time -- 等待超时时间	
* Return         : None
*******************************************************************************/
void Uart0_Flush(uint16_t over_time)
{
	uint16_t delay_num = 0;
	
	//等待发送完成 - 50ms
	while(1)
	{
		WDT_Feed();		//防止看门狗复位
		if((Uart0_GetStart() & 0x40) && (R8_UART0_TFC == 0x00))	break;	//判断发送FIFO为空,同时FIFO计数为空
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	
}

/*******************************************************************************
* Function Name  : Uart0_Flush
* Description    : 串口0等待发送完成
* Input			 : over_time -- 等待超时时间	
* Return         : None
*******************************************************************************/
void Uart1_Flush(uint16_t over_time)
{
	uint16_t delay_num = 0;
	
	//等待发送完成 - 50ms
	while(1)
	{
		if((Uart1_GetStart() & 0x40) && (R8_UART1_TFC == 0x00))	break;	//判断发送FIFO为空,同时FIFO计数为空
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	
}

/*******************************************************************************
* Function Name  : Uart0_Flush
* Description    : 串口0等待发送完成
* Input			 : over_time -- 等待超时时间	
* Return         : None
*******************************************************************************/
void Uart2_Flush(uint16_t over_time)
{
	uint16_t delay_num = 0;
	
	//等待发送完成 - 50ms
	while(1)
	{
		if((Uart2_GetStart() & 0x40) && (R8_UART2_TFC == 0x00))	break;	//判断发送FIFO为空,同时FIFO计数为空
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	
}

/*******************************************************************************
* Function Name  : Uart0_Flush
* Description    : 串口0等待发送完成
* Input			 : over_time -- 等待超时时间	
* Return         : None
*******************************************************************************/
void Uart3_Flush(uint16_t over_time)
{
	uint16_t delay_num = 0;
	
	//等待发送完成 - 50ms
	while(1)
	{
		if((Uart3_GetStart() & 0x40) && (R8_UART3_TFC == 0x00))	break;	//判断发送FIFO为空,同时FIFO计数为空
		DelayUs(100);
		delay_num++;
		if(delay_num > 500)	break;
	}
	
}

/*******************************************************************************
* Function Name  : Uart2_RECEIVE_Process
* Description    : 串口2接收处理函数
* Return         : None
*******************************************************************************/
void Uart2_RECEIVE_Process(void)
{
	if(Uart_Search_Cmd(g_Uart[USART_2].recvBuffer,g_Uart[USART_2].recvLen) == 0x00)
	{
		UART2_SendString((uint8_t *)"Search:0\r\nBLV_C1\r\n",16);
	}
	else if(Uart_Jump_Cmd(g_Uart[USART_2].recvBuffer,g_Uart[USART_2].recvLen) == 0x00)
	{
		UART2_SendString((uint8_t *)"Jump:0\r\nBLV_C1\r\n",14);
		Uart2_Flush(500);
		SYS_ResetExecute();
		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"串口升级复位");
		LOG_LogicInfo_DebugRecord("串口升级复位");
	}
	
	g_Uart[USART_2].recvLen = 0;
}




/*******************************************************************************
* Function Name  : Uart_SendString
* Description    : 串口发送函数函数
* Input          : 	uart_id - 发送的串口号
					buff  - 发送数据
					len  -- 发送数据长度
* Return         : None
*******************************************************************************/
void Uart_SendString(uint8_t uart_id,uint8_t* buff,uint16_t len)
{
	switch(uart_id)
	{
		case USART_0:
			UART0_SendString(buff,len);
			break;
		case USART_1:
			UART1_SendString(buff,len);
			break;
		case USART_2:
			UART2_SendString(buff,len);
			break;
		case USART_3:
			UART3_SendString(buff,len);
			break;
		default:
			break;
	}
}

/*******************************************************************************
* Function Name  : MCU485_SendString
* Description    : 485发送函数函数
* Input          : 	uart_id - 发送的串口号
					buff  - 发送数据
					len  -- 发送数据长度
* Return         : None
*******************************************************************************/
void MCU485_SendString(uint8_t uart_id,uint8_t* buff,uint16_t len)
{
	switch(uart_id)
	{
		case USART_1:
			if(Poll485_Info.port_mode == Port_Monitoring_mode )		//轮询端口
			{
				Udp_Internal_SeriaNet_Uploading2(Polling_Port,Poll485_Info.baud,buff,len);
			}
		#if (USE_CORE_TYPE == 1)    //使用C1F核心板    
			MCU485_SendString_0(buff,len);
		#elif (USE_CORE_TYPE == 2)   //使用C1核心板
			MCU485_SendString_1(buff,len);
		#endif  //USE_CORE_TYPE == CORE_TYPE_C1F		
			
			break;
		case USART_2:
			if(Act485_Info.port_mode == Port_Monitoring_mode )		//轮询端口
			{
				Udp_Internal_SeriaNet_Uploading2(Active_Port,Act485_Info.baud,buff,len);
			}
			MCU485_SendString_2(buff,len);
			break;
		case USART_3:
			if(BUS485_Info.port_mode == Port_Monitoring_mode )		//轮询端口
			{
				Udp_Internal_SeriaNet_Uploading2(Bus_port,BUS485_Info.baud,buff,len);
			}
			MCU485_SendString_3(buff,len);
			break;
	}
}

/*******************************************************************************
* Function Name  : MCU485_SendString
* Description    : 485发送函数函数
* Input          : 	uart_id - 发送的串口号
					data_addr - SRAM中发送数据地址
					len  -- 发送数据长度
* Return         : None
*******************************************************************************/
void MCU485_SendSRAMData(uint8_t uart_id,uint32_t data_addr,uint16_t len)
{
	uint16_t buff_len = len;
	uint8_t send_buff[buff_len];
	
	memset(send_buff,0,sizeof(send_buff));
	
	SRAM_DMA_Read_Buff(send_buff,buff_len,data_addr);	//读取数据内容
	
	MCU485_SendString(uart_id,send_buff,buff_len);
}


/*******************************************************************************
* Function Name  : Write_Uart_SendBuff 
* Description    : 写uart发送缓冲区
* Input          : 	uart_id - 发送的串口号
					uart_baud - 发送数据
					buff  - 发送数据
					len  -- 发送数据长度
*******************************************************************************/
void Write_Uart_SendBuff(uint8_t uart_id,uint8_t uart_outime,uint8_t* buff,uint16_t len)
{
	switch(uart_id)
	{
		case Polling_Port:   //轮询
			uart_id = USART_0;
			break;
		case Active_Port:   //主动
			uart_id = USART_2;
      break;
		case Bus_port:   //bus总线
			uart_id = USART_3;
			break;
	} 
	switch(uart_id)
	{
		case USART_0:
			/*数据长度*/
			SRAM_Write_Word(len,g_Uart[USART_0].TX_Buffer_WriteAddr);

			/*数据发送 - 等待回复时间 , 单位：S*/
			SRAM_Write_Byte(uart_outime,g_Uart[USART_0].TX_Buffer_WriteAddr+2);
			/*数据内容*/
			SRAM_DMA_Write_Buff(buff,len,g_Uart[USART_0].TX_Buffer_WriteAddr+3);
			
			g_Uart[USART_0].TX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_0].TX_Buffer_WriteAddr > SRAM_UART0_SendBuffer_End_Addr)	g_Uart[USART_0].TX_Buffer_WriteAddr = SRAM_UART0_SendBuffer_Start_Addr;
			break;
		case USART_1:
			/*数据长度*/
			SRAM_Write_Word(len,g_Uart[USART_1].TX_Buffer_WriteAddr);
			/*数据发送 - 等待回复时间 , 单位：S*/
			SRAM_Write_Byte(uart_outime,g_Uart[USART_1].TX_Buffer_WriteAddr+2);
			/*数据内容*/
			SRAM_DMA_Write_Buff(buff,len,g_Uart[USART_1].TX_Buffer_WriteAddr+3);
			
			g_Uart[USART_1].TX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_1].TX_Buffer_WriteAddr > SRAM_UART1_SendBuffer_End_Addr)	g_Uart[USART_1].TX_Buffer_WriteAddr = SRAM_UART1_SendBuffer_Start_Addr;
			break;
		case USART_2:
			/*数据长度*/
			SRAM_Write_Word(len,g_Uart[USART_2].TX_Buffer_WriteAddr);
			/*数据发送 - 等待回复时间 , 单位：S*/
			SRAM_Write_Byte(uart_outime,g_Uart[USART_2].TX_Buffer_WriteAddr+2);
			/*数据内容*/
			SRAM_DMA_Write_Buff(buff,len,g_Uart[USART_2].TX_Buffer_WriteAddr+3);
			
			g_Uart[USART_2].TX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_2].TX_Buffer_WriteAddr > SRAM_UART2_SendBuffer_End_Addr)	g_Uart[USART_2].TX_Buffer_WriteAddr = SRAM_UART2_SendBuffer_Start_Addr;
			break;
		case USART_3:
			/*数据长度*/
			SRAM_Write_Word(len,g_Uart[USART_3].TX_Buffer_WriteAddr);
			/*数据发送 - 等待回复时间 , 单位：S*/
			SRAM_Write_Byte(uart_outime,g_Uart[USART_3].TX_Buffer_WriteAddr+2);
			/*数据内容*/
			SRAM_DMA_Write_Buff(buff,len,g_Uart[USART_3].TX_Buffer_WriteAddr+3);
		
			g_Uart[USART_3].TX_Buffer_WriteAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_3].TX_Buffer_WriteAddr > SRAM_UART3_SendBuffer_End_Addr)	g_Uart[USART_3].TX_Buffer_WriteAddr = SRAM_UART3_SendBuffer_Start_Addr;
			break;
		default:
			break;
	}
}


