Bootstrap

HC32F4A0 10路串口UART 配置

HC32 小华MCU 使用一段时间了,反正芯片BUG 是比较多了,比如串口接收错误后导致再也无法接收,PWM模块无法输出 低电平 , CAN 接收错误导致 输出引脚 CAN_TXD 一直输出脉冲  。。。;好的一面也存在吧,IO 引脚 可以重定义的比较多 ,这里使用内部10路串口,初始化如下 :

源文件 :

#include "hc32_ddl.h"
#include "bsp_usart.h"
#include <stdio.h>
#include "systick.h"
#include "string.h"
#include "Global_GpioDefine.h"


static int8_t golbal_regTimeout_enable =0;
static struct uart_list_table *golbal_uart_list;
static uint32_t uart_list_table_size =0;

int fputc(int ch, FILE *f)
{
	while (Reset == USART_GetStatus(M4_USART1, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
	USART_SendData(M4_USART1, ch);
	return ch;
}

static void _uart_list_init(void){
	golbal_uart_list=NULL;
	uart_list_table_size =0;
}

static int _add_uart_list(struct uart_list_table* add_list){
	struct uart_list_table *list_p = golbal_uart_list;
	struct uart_list_table **next_ptr=&golbal_uart_list;
 	if(add_list==NULL)return -2;
    __disable_irq();
	if(uart_list_table_size!=0){
		next_ptr = &list_p->end_list;
		list_p= list_p->end_list;
		*next_ptr = add_list;
		next_ptr = &add_list->end_list;
		*next_ptr = list_p;
	}else{
		*next_ptr = add_list;
		next_ptr = &add_list->end_list;
		*next_ptr = add_list;
	}
	uart_list_table_size++;
  __enable_irq();
	return 0;   
}

static int _del_uart_list(struct uart_list_table* del_list){
	struct uart_list_table *list_p = golbal_uart_list;
	struct uart_list_table **target = &golbal_uart_list;
	uint32_t i=0;
	if(del_list==NULL)return -2;
	if(uart_list_table_size==0) return -4;
	__disable_irq();
	for(i=0;i<uart_list_table_size;i++){
		if(list_p==del_list){
			*target = list_p->end_list;
			break;
		}
		target = &list_p->end_list;
		list_p = list_p->end_list;	
	}
	uart_list_table_size--;
	__enable_irq();;
	return i>0?0:-1;
}


static void TMR0_1_ChACmp_IrqCallback(void)
{
#if	configUSE_PREEMPTION>0
	uint32_t ulPreviousMask= portSET_INTERRUPT_MASK_FROM_ISR();
	BaseType_t pxHigherPriorityTaskWoken;
#endif
	
	M4_TMR0_1->STFLR =0;  /* Clear the compare matching flag */
	struct uart_list_table *list_p = golbal_uart_list;
	uint32_t i=0,curr_dma_ptr;
	UART_MAG *uart_sel;

	for(i=0;i<uart_list_table_size;i++){
		uart_sel =list_p->uart_sel;
		if(uart_sel!=NULL){
			curr_dma_ptr = uart_sel->dma_mon_ptr[0];
			if(curr_dma_ptr != uart_sel->last_ptr){
				uart_sel->last_ptr = curr_dma_ptr;
				uart_sel->recv_flag  |=(1UL<<30) ;
				uart_sel->delay_tick_cnt =0;
			}else if(uart_sel->recv_flag &(1UL<<30)){
				if(++uart_sel->delay_tick_cnt >=  uart_sel->delay_tick_set){
					uart_sel->delay_tick_cnt =0;
					uart_sel->recv_flag  &=~(1UL<<30);
					uart_sel->recv_flag ++ ;
#if	(configUSE_PREEMPTION>0) 
					xSemaphoreGiveFromISR(uart_sel->suart_xsem,&pxHigherPriorityTaskWoken);
#endif 
				}
			}
			if(uart_sel->uart_SR[0]&(5UL<<1)){/*UART-ORE / FE -error*/
				uart_sel->uart_SR[3] |=(1UL<<19)|(1UL<<17); /*clear ORE ,FE*/
			}
		}
		list_p = list_p->end_list;
	}
#if	configUSE_PREEMPTION>0	
	portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );
#endif
}

static void _uart_timeout_scan_init(uint32_t output_clk){
    stc_tmr0_init_t stcTmr0Init;
    /* Enable timer0 peripheral clock */
    PWC_Fcg2PeriphClockCmd(PWC_FCG2_TMR0_1, Enable);
		uint32_t arr =0;
		int32_t psc =0;
		do{
			psc*=2;
			if(psc==0)psc=1;
			arr =HCLK_VALUE/2/(psc)/output_clk -1;
			if(psc>1024){/*param error*/
				psc = 1024;
				arr = 0xffff;
				break;
			}
		}while(arr>=0xffff);
		int i=0;
		for(i=0;i<10;i++){
			if(psc&(1UL<<i)){
				break;
			}
		}
    /* TIMER0 basetimer function initialize */
    (void)TMR0_StructInit(&stcTmr0Init);
    stcTmr0Init.u32ClockDivision = (uint32_t)i<<TMR0_BCONR_CKDIVA_POS;        
    stcTmr0Init.u32ClockSource = TMR0_CLK_SRC_PCLK1;        
    stcTmr0Init.u32Tmr0Func = TMR0_FUNC_CMP;          
    stcTmr0Init.u32HwTrigFunc = TMR0_BT_HWTRG_FUNC_NONE;
    stcTmr0Init.u16CmpValue = arr;            
    (void)TMR0_Init(M4_TMR0_1, TMR0_CH_A, &stcTmr0Init);
		
    DDL_DelayMS(1U); 
    TMR0_IntCmd(M4_TMR0_1, TMR0_CH_A, Enable);

		stc_irq_signin_config_t stcIrqSignConfig;
    stcIrqSignConfig.enIRQn = Int014_IRQn;
    stcIrqSignConfig.enIntSrc = INT_TMR0_1_CMPA;
    stcIrqSignConfig.pfnCallback = &TMR0_1_ChACmp_IrqCallback;
    (void)INTC_IrqSignIn(&stcIrqSignConfig);
    NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
    NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIORITY_15);
    NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
	
	_uart_list_init();

	TMR0_Cmd(M4_TMR0_1, TMR0_CH_A, Enable);
}
void _uart_timeout_scan_deinit(void){
	TMR0_Cmd(M4_TMR0_1, TMR0_CH_A, Disable);
	TMR0_IntCmd(M4_TMR0_1, TMR0_CH_A, Disable);
}

#if defined UART1_SERIAL_TXD_GPIOX &&  \
    defined UART1_SERIAL_RXD_GPIOX
UART_MAG uart1_msg;
static struct uart_list_table uart1_list;
void uart1_deinit(void){
	__DMA_UART_DEINIT(1,0);
	USART_FuncCmd(M4_USART1, (USART_RX | USART_TX), Disable);
	
	_del_uart_list(&uart1_list);
}
void uart1_init(uint32_t baudrate)
{
	static uint8_t Uart1RecvBuf[UART1_BUFSIZEMAX];
	GPIO_SetFunc( UART1_SERIAL_RXD_GPIOX, \
								UART1_SERIAL_RXD, \
								UART1_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(	UART1_SERIAL_TXD_GPIOX, \
								UART1_SERIAL_TXD, \
								UART1_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
	__DMA_UART_INIT(1,0,1,\
									uart1_msg,\
									Uart1RecvBuf,\
									UART1_BUFSIZEMAX,\
									uart1_list,DMA1_TRGSEL,\
						 EVT_USART1_RI);
#if	configUSE_PREEMPTION>0
	uart1_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart1_list);
}

uint16_t uart1_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART1->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART1->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart1_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart1_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart1_init(uint32_t baudrate){}
#endif

// uart 2

#if defined UART2_SERIAL_TXD_GPIOX &&  \
    defined UART2_SERIAL_RXD_GPIOX
   UART_MAG uart2_msg;
static struct uart_list_table uart2_list;
void uart2_deinit(void){
	__DMA_UART_DEINIT(1,1);
  USART_FuncCmd(M4_USART2, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart2_list);
}
void uart2_init(uint32_t baudrate)
{
	static uint8_t Uart2RecvBuf[UART2_BUFSIZEMAX];
	GPIO_SetFunc( 				UART2_SERIAL_RXD_GPIOX, \
								UART2_SERIAL_RXD, \
								UART2_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART2_SERIAL_TXD_GPIOX, \
								UART2_SERIAL_TXD, \
								UART2_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
	__DMA_UART_INIT(1,1,2,\
									uart2_msg,\
									Uart2RecvBuf,\
									UART2_BUFSIZEMAX,\
									uart2_list,DMA1_TRGSEL,\
						 	   EVT_USART2_RI);
#if	configUSE_PREEMPTION>0
	uart2_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart2_list);
}

uint16_t uart2_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART2->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART2->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart2_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart2_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart2_init(uint32_t baudrate){}
#endif

// uart3

#if defined UART3_SERIAL_TXD_GPIOX &&  \
    defined UART3_SERIAL_RXD_GPIOX
   UART_MAG uart3_msg;
static struct uart_list_table uart3_list;
void uart3_deinit(void){
	__DMA_UART_DEINIT(1,2);
  USART_FuncCmd(M4_USART3, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart3_list);
}
void uart3_init(uint32_t baudrate)
{
	static uint8_t Uart3RecvBuf[UART3_BUFSIZEMAX];
	GPIO_SetFunc( 				UART3_SERIAL_RXD_GPIOX, \
								UART3_SERIAL_RXD, \
								UART3_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART3_SERIAL_TXD_GPIOX, \
								UART3_SERIAL_TXD, \
								UART3_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
	__DMA_UART_INIT(1,2,3,\
									uart3_msg,\
									Uart3RecvBuf,\
									UART3_BUFSIZEMAX,\
									uart3_list,DMA1_TRGSEL,\
						 	   EVT_USART3_RI);
#if	configUSE_PREEMPTION>0
	uart3_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart3_list);
}

uint16_t uart3_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART3->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART3->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart3_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart3_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart3_init(uint32_t baudrate){}
#endif

//uart 4

#if defined UART4_SERIAL_TXD_GPIOX &&  \
    defined UART4_SERIAL_RXD_GPIOX
   UART_MAG uart4_msg;
static struct uart_list_table uart4_list;
void uart4_deinit(void){
	__DMA_UART_DEINIT(1,3);
  USART_FuncCmd(M4_USART4, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart4_list);
}
void uart4_init(uint32_t baudrate)
{
	static uint8_t Uart4RecvBuf[UART4_BUFSIZEMAX];
	GPIO_SetFunc( 				UART4_SERIAL_RXD_GPIOX, \
								UART4_SERIAL_RXD, \
								UART4_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART4_SERIAL_TXD_GPIOX, \
								UART4_SERIAL_TXD, \
								UART4_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
				__DMA_UART_INIT(1,3,4,\
								uart4_msg,\
								Uart4RecvBuf,\
								UART4_BUFSIZEMAX,\
								uart4_list,DMA1_TRGSEL,\
						   EVT_USART4_RI);
#if	configUSE_PREEMPTION>0
	uart4_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart4_list);
}

uint16_t uart4_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART4->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART4->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart4_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart4_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart4_init(uint32_t baudrate){}
#endif
// 5

#if defined UART5_SERIAL_TXD_GPIOX &&  \
    defined UART5_SERIAL_RXD_GPIOX
   UART_MAG uart5_msg;
static struct uart_list_table uart5_list;
void uart5_deinit(void){
	__DMA_UART_DEINIT(1,4);
  USART_FuncCmd(M4_USART5, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart5_list);
}
void uart5_init(uint32_t baudrate)
{
	static uint8_t Uart5RecvBuf[UART5_BUFSIZEMAX];
	GPIO_SetFunc( 				UART5_SERIAL_RXD_GPIOX, \
								UART5_SERIAL_RXD, \
								UART5_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART5_SERIAL_TXD_GPIOX, \
								UART5_SERIAL_TXD, \
								UART5_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
				__DMA_UART_INIT(1,4,5,\
								uart5_msg,\
								Uart5RecvBuf,\
								UART5_BUFSIZEMAX,\
								uart5_list,DMA1_TRGSEL,\
						   EVT_USART5_RI);
#if	configUSE_PREEMPTION>0
	uart5_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart5_list);
}

uint16_t uart5_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART5->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART5->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart5_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart5_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart5_init(uint32_t baudrate){}
#endif

// 6

#if defined UART6_SERIAL_TXD_GPIOX &&  \
    defined UART6_SERIAL_RXD_GPIOX
   UART_MAG uart6_msg;
static struct uart_list_table uart6_list;
void uart6_deinit(void){
	__DMA_UART_DEINIT(1,5);
  USART_FuncCmd(M4_USART6, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart6_list);
}
void uart6_init(uint32_t baudrate)
{
	static uint8_t Uart6RecvBuf[UART6_BUFSIZEMAX];
	GPIO_SetFunc( 				UART6_SERIAL_RXD_GPIOX, \
								UART6_SERIAL_RXD, \
								UART6_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART6_SERIAL_TXD_GPIOX, \
								UART6_SERIAL_TXD, \
								UART6_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
				__DMA_UART_INIT(1,5,6,\
								uart6_msg,\
								Uart6RecvBuf,\
								UART6_BUFSIZEMAX,\
								uart6_list,DMA1_TRGSEL,\
						   EVT_USART6_RI);
#if	configUSE_PREEMPTION>0
	uart6_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart6_list);
}

uint16_t uart6_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART6->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART6->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart6_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart6_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart6_init(uint32_t baudrate){}
#endif
//7 

#if defined UART7_SERIAL_TXD_GPIOX &&  \
    defined UART7_SERIAL_RXD_GPIOX
   UART_MAG uart7_msg;
static struct uart_list_table uart7_list;
void uart7_deinit(void){
	__DMA_UART_DEINIT(1,6);
  USART_FuncCmd(M4_USART7, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart7_list);
}
void uart7_init(uint32_t baudrate)
{
	static uint8_t Uart7RecvBuf[UART7_BUFSIZEMAX];
	GPIO_SetFunc( 				UART7_SERIAL_RXD_GPIOX, \
								UART7_SERIAL_RXD, \
								UART7_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART7_SERIAL_TXD_GPIOX, \
								UART7_SERIAL_TXD, \
								UART7_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
				__DMA_UART_INIT(1,6,7,\
								uart7_msg,\
								Uart7RecvBuf,\
								UART7_BUFSIZEMAX,\
								uart7_list,DMA1_TRGSEL,\
						   EVT_USART7_RI);
#if	configUSE_PREEMPTION>0
	uart7_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart7_list);
}

uint16_t uart7_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART7->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART7->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart7_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart7_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart7_init(uint32_t baudrate){}
#endif

// 8

#if defined UART8_SERIAL_TXD_GPIOX &&  \
    defined UART8_SERIAL_RXD_GPIOX
   UART_MAG uart8_msg;
static struct uart_list_table uart8_list;
void uart8_deinit(void){
	__DMA_UART_DEINIT(1,7);
  USART_FuncCmd(M4_USART8, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart8_list);
}
void uart8_init(uint32_t baudrate)
{
	static uint8_t Uart8RecvBuf[UART8_BUFSIZEMAX];
	GPIO_SetFunc( 				UART8_SERIAL_RXD_GPIOX, \
								UART8_SERIAL_RXD, \
								UART8_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART8_SERIAL_TXD_GPIOX, \
								UART8_SERIAL_TXD, \
								UART8_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
				__DMA_UART_INIT(1,7,8,\
								uart8_msg,\
								Uart8RecvBuf,\
								UART8_BUFSIZEMAX,\
								uart8_list,DMA1_TRGSEL,\
						   EVT_USART8_RI);
#if	configUSE_PREEMPTION>0
	uart8_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart8_list);
}

uint16_t uart8_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART8->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART8->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart8_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart8_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart8_init(uint32_t baudrate){}
#endif
//9

#if defined UART9_SERIAL_TXD_GPIOX &&  \
    defined UART9_SERIAL_RXD_GPIOX
   UART_MAG uart9_msg;
static struct uart_list_table uart9_list;
void uart9_deinit(void){
	__DMA_UART_DEINIT(2,0);
  USART_FuncCmd(M4_USART9, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart9_list);
}
void uart9_init(uint32_t baudrate)
{
	static uint8_t Uart9RecvBuf[UART9_BUFSIZEMAX];
	GPIO_SetFunc( 				UART9_SERIAL_RXD_GPIOX, \
								UART9_SERIAL_RXD, \
								UART9_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART9_SERIAL_TXD_GPIOX, \
								UART9_SERIAL_TXD, \
								UART9_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
				__DMA_UART_INIT(2,0,9,\
								uart9_msg,\
								Uart9RecvBuf,\
								UART9_BUFSIZEMAX,\
								uart9_list,DMA1_TRGSEL,\
						   EVT_USART9_RI);
#if	configUSE_PREEMPTION>0
	uart9_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart9_list);
}

uint16_t uart9_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART9->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART9->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart9_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart9_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart9_init(uint32_t baudrate){}
#endif
//10

#if defined UART10_SERIAL_TXD_GPIOX &&  \
    defined UART10_SERIAL_RXD_GPIOX
   UART_MAG uart10_msg;
static struct uart_list_table uart10_list;
void uart10_deinit(void){
	__DMA_UART_DEINIT(2,1);
  USART_FuncCmd(M4_USART10, (USART_RX | USART_TX), Disable);
	_del_uart_list(&uart10_list);
}
void uart10_init(uint32_t baudrate)
{
	static uint8_t Uart8RecvBuf[UART10_BUFSIZEMAX];
	GPIO_SetFunc( 				UART10_SERIAL_RXD_GPIOX, \
								UART10_SERIAL_RXD, \
								UART10_SERIAL_RXD_FUNC, PIN_SUBFUNC_DISABLE);
	GPIO_SetFunc(				UART10_SERIAL_TXD_GPIOX, \
								UART10_SERIAL_TXD, \
								UART10_SERIAL_TXD_FUNC, PIN_SUBFUNC_DISABLE);
				__DMA_UART_INIT(2,1,10,\
								uart10_msg,\
								Uart10RecvBuf,\
								UART10_BUFSIZEMAX,\
								uart10_list,DMA1_TRGSEL,\
						   EVT_USART10_RI);
#if	configUSE_PREEMPTION>0
	uart10_msg.suart_xsem = xSemaphoreCreateBinary();
#endif
	_add_uart_list(&uart10_list);
}

uint16_t uart10_Send(uint8_t *dat ,uint16_t len){
	uint16_t lens =len;
	while(len--){
		while (0 ==READ_REG32_BIT(M4_USART10->SR, USART_FLAG_TXE)){;} /* Wait Tx data register empty */
		M4_USART10->DR=((uint32_t)*dat++ & 0x01FFUL);
	}
return lens;
}

uint16_t uart10_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms){
	uint32_t cur_tick ;	
	uint16_t cur_count ;
	UART_MAG *uartv =&uart10_msg;	
#if	configUSE_PREEMPTION>0
	if((uartv->recv_flag&0xffff)>0){uartv->recv_flag--;}
	else{
		if(pdTRUE!=xSemaphoreTake(uartv->suart_xsem, recvtimeout_ms))return 0;
		if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
		else return 0;
	}
#else
	if(recvtimeout_ms){
		while(((uartv->recv_flag&0xffff)==0)&&(recvtimeout_ms>0)){
			delay_ms(1);
			recvtimeout_ms--;
		}
	}else{
		while((uartv->recv_flag&0xffff)==0){
			delay_ms(1);
		}
	}
	if((uartv->recv_flag&0xffff)>0)uartv->recv_flag --;
	else return 0;
#endif

	/*calc  recevice */
	uint32_t	last_ptr = uartv->last_ptr; //lock varable ,!!
	if(last_ptr> uartv->data_read_ptr){
		cur_tick = last_ptr - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
	}else{
		cur_tick = (uint32_t)uartv->data_buf ;
		cur_tick += uartv->data_buf_size;/*buffer end*/
		cur_tick = cur_tick - uartv->data_read_ptr;
		memcpy(data ,(void*)uartv->data_read_ptr,cur_tick);
		data += cur_tick;
		cur_count=  last_ptr - (uint32_t)uartv->data_buf;
		memcpy(data ,(void*)uartv->data_buf,cur_count);
		cur_tick +=cur_count;
	}
	uartv->data_read_ptr = last_ptr;
	return cur_tick;
}
#else
void uart10_init(uint32_t baudrate){}
#endif

void (*ttyScom_init[_SYS_TTYS_NUM_SIZE_MAX])(uint32_t bound)=
{
	&uart1_init,
	&uart2_init,
	&uart3_init,
	&uart4_init,
	&uart5_init,
	&uart6_init,
	&uart7_init,
	&uart8_init,
	&uart9_init,
	&uart10_init,
};

H 文件

#ifndef _BSP_USART_H_
#define _BSP_USART_H_
#include <stdint.h>
#ifndef __PROJ_TARGET_UBOOT
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#endif

typedef struct
{
	uint32_t	last_ptr;
	uint16_t 	delay_tick_set;
	uint16_t    delay_tick_cnt;
	uint32_t	data_read_ptr;
	uint32_t    data_buf_size;
	uint8_t 	*data_buf;
	uint32_t 	recv_flag;
	uint32_t    *dma_mon_ptr;
		uint32_t  *uart_SR;
#if	configUSE_PREEMPTION>0
	SemaphoreHandle_t suart_xsem;
#endif
}UART_MAG;


#define			UART1_BUFSIZEMAX				64
#define			UART2_BUFSIZEMAX				1024
#define			UART3_BUFSIZEMAX				64
#define			UART4_BUFSIZEMAX				64
#define			UART5_BUFSIZEMAX				64
#define			UART6_BUFSIZEMAX				64
#define			UART7_BUFSIZEMAX				64
#define			UART8_BUFSIZEMAX				2048
#define			UART9_BUFSIZEMAX				64
#define			UART10_BUFSIZEMAX				64

#define			__DMA_UART_DEINIT(dmax,CH){\
	M4_DMA##dmax->CHEN &=~(1UL<<CH) ;\
	M4_DMA##dmax->CHENCLR |=(1UL<<CH); \
}

#define		__DMA_UART_INIT(dmax,CH,utx,uartx_msg,UartxRecvBuf,UARTx_BUFSIZEMAX,uartx_list,DMAx_TRGSEL,EVT_USARTx_RI){\
	uartx_msg.last_ptr = (uint32_t)UartxRecvBuf;\
	uartx_msg.delay_tick_set = 2;\
	uartx_msg.delay_tick_cnt =0;\
	uartx_msg.data_read_ptr = \
	uartx_msg.last_ptr;\
	uartx_msg.data_buf_size = UARTx_BUFSIZEMAX;\
	uartx_msg.data_buf = UartxRecvBuf;\
	uartx_msg.recv_flag =0;\
	uartx_msg.dma_mon_ptr = (uint32_t*)&M4_DMA##dmax->MONDAR##CH;\
	uartx_msg.uart_SR  = (uint32_t*)&M4_USART##utx->SR;\
	\
	uartx_list.uart_sel = &uartx_msg;\
	if(golbal_regTimeout_enable==0){\
		golbal_regTimeout_enable = 1;\
		_uart_timeout_scan_init(125);\
	}\
	\
	PWC_Fcg0PeriphClockCmd((PWC_FCG0_DMA##dmax | PWC_FCG0_AOS), Enable);\
	PWC_Fcg3PeriphClockCmd(PWC_FCG3_USART##utx, Enable);\
	\
	M4_DMA##dmax->EN |=0x1 ;\
	M4_DMA##dmax->CHENCLR |=(1UL<<CH) ;\
	\
	M4_DMA##dmax->SAR##CH =(uint32_t)(&M4_USART##utx->DR)+2UL;\
	M4_DMA##dmax->DAR##CH = (uint32_t)UartxRecvBuf;\
	M4_DMA##dmax->DTCTL##CH = 0x1;\
	\
	M4_DMA##dmax->RPT##CH = (uint32_t)UARTx_BUFSIZEMAX<<16;\
	M4_DMA##dmax->RPTB##CH = (uint32_t)UARTx_BUFSIZEMAX<<16;\
	\
	M4_DMA##dmax->CHCTL##CH = 0UL<<8 | \
										1UL<<5 | \
										1UL<<2 ; \
	\
	M4_AOS->DMAx_TRGSEL##CH = EVT_USARTx_RI; \
	M4_DMA##dmax->CHEN |=(1UL<<CH) ; \
	\
	const stc_usart_uart_init_t stcUartInit = { \
			.u32Baudrate = baudrate, \
			.u32BitDirection = USART_LSB, \
			.u32StopBit = USART_STOPBIT_1BIT, \
			.u32Parity = USART_PARITY_NONE, \
			.u32DataWidth = USART_DATA_LENGTH_8BIT, \
			.u32ClkMode = USART_INTERNCLK_NONE_OUTPUT, \
			.u32PclkDiv = USART_PCLK_DIV64, \
			.u32OversamplingBits = USART_OVERSAMPLING_8BIT, \
			.u32NoiseFilterState = USART_NOISE_FILTER_DISABLE, \
			.u32SbDetectPolarity = USART_SB_DETECT_FALLING, \
	}; \
	USART_UartInit(M4_USART##utx, &stcUartInit);\
	\
	USART_FuncCmd(M4_USART##utx, (USART_RX | USART_TX), Enable); \
	\
}

/*uart x recvice timeout list_table*/
struct uart_list_table{
	UART_MAG *uart_sel;
	struct uart_list_table *end_list;
};
extern void _uart_timeout_scan_deinit(void);
extern UART_MAG uart1_msg;
extern void uart1_init(uint32_t baudrate);
extern uint16_t uart1_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart1_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart2_msg;
extern void uart2_init(uint32_t baudrate);
extern uint16_t uart2_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart2_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart3_msg;
extern void uart3_init(uint32_t baudrate);
extern uint16_t uart3_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart3_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart4_msg;
extern void uart4_init(uint32_t baudrate);
extern uint16_t uart4_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart4_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart5_msg;
extern void uart5_init(uint32_t baudrate);
extern uint16_t uart5_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart5_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart6_msg;
extern void 		uart6_init(uint32_t baudrate);
extern uint16_t uart6_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart6_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart7_msg;
extern void 		uart7_init(uint32_t baudrate);
extern uint16_t uart7_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart7_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart8_msg;
extern void uart8_init(uint32_t baudrate);
extern uint16_t uart8_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart8_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart9_msg;
extern void 		uart9_init(uint32_t baudrate);
extern uint16_t uart9_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart9_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);
extern UART_MAG uart10_msg;
extern void 		uart10_init(uint32_t baudrate);
extern uint16_t uart10_Send(uint8_t *dat ,uint16_t len);
extern uint16_t uart10_RecvForm(uint8_t *data ,uint32_t recvtimeout_ms);

#define		_SYS_TTYS_NUM_SIZE_MAX			10
extern void (*ttyScom_init[_SYS_TTYS_NUM_SIZE_MAX])(uint32_t bound);
#endif

外部定义全局头文件 ,如 :

//串口 --调试
#define     UART1_SERIAL_TXD_GPIOX        GPIO_PORT_I /*FG1*/
#define     UART1_SERIAL_RXD_GPIOX        GPIO_PORT_I
#define     UART1_SERIAL_TXD          GPIO_PIN_04
#define     UART1_SERIAL_RXD          GPIO_PIN_05
#define     UART1_SERIAL_TXD_FUNC            GPIO_FUNC_32_USART1_TX
#define     UART1_SERIAL_RXD_FUNC            GPIO_FUNC_33_USART1_RX
//RS 485 -3
#define     UART2_SERIAL_TXD_GPIOX        GPIO_PORT_B
#define     UART2_SERIAL_RXD_GPIOX        GPIO_PORT_B
#define     UART2_SERIAL_TXD          GPIO_PIN_03 /*FG1 */
#define     UART2_SERIAL_RXD          GPIO_PIN_04
#define     UART2_SERIAL_TXD_FUNC            GPIO_FUNC_34_USART2_TX
#define     UART2_SERIAL_RXD_FUNC            GPIO_FUNC_35_USART2_RX

自动激活相关函数配置 ,串口接收采用 独立定时器 判断接收超时,统一处理 ,并且串口错误后自动清除相关BIT 

;