⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 C语言源代码及相关资料
💻 C
📖 第 1 页 / 共 3 页
字号:
			uiCurNData = pucOut - pucIn;
		}
	} 
	if (uiWhichFifo == RX_FIFO)
	{
		pucIn = PUartInfo->PdqReviceBuf->In;
		pucOut = PUartInfo->PdqReviceBuf->Out;
		uiMaxData = PUartInfo->PdqReviceBuf->MaxData;		
		
		if ( pucIn >= pucOut ) {
			uiCurNData = pucIn - pucOut;
		} else {
			uiCurNData = uiMaxData - (pucOut - pucIn);
		}
	}	
#if defined(UART_SEMCONTROL)
	OSSemPost(PUartInfo->pUartSem);
#else
	OS_EXIT_CRITICAL();
#endif // end #if defined(UART_SEMCONTROL)	
	return ( uiCurNData );
	
#else
	if ((uiWhichFifo == TX_FIFO) && ((PUartInfo->uiUartFlag) < 0x80))
	{
		return (PUartInfo->PdqSendBuf->MaxData - PUartInfo->PdqSendBuf->NData);
	}

	if (uiWhichFifo == RX_FIFO)
	{
		return (PUartInfo->PdqReviceBuf->NData);		
	}
	return 0;
#endif // end of #if defined(QUEUE_NEW_OPRT)
}

/*********************************************************************************************************
** Function name:           uartFifoStatus
** Descriptions:            获取Queue队列的状态
** input parameters:        uiId:           子设备号
**                          uiWhichFifo:    TX_FIFO-发送队列;RX_FIFO-接收队列
** Returned value:          发送队列中可操作的空间大小,或接收队列中可用数据个数
*********************************************************************************************************/
extern int32 uartFifoStatus (uint32 uiId,uint32 uiWhichFifo)
{
	if (uiId < __UART_MAX_NUM)
		return _uartQueueStatus(__GpuiUartInfoTab[uiId], uiWhichFifo);
	else
		return 0;
}

/*********************************************************************************************************
** Function name:           uartFifoFlush
** Descriptions:            清空接收或发送Queue队列
** input parameters:        uiId:        UART子设备号
**                          uiWhichFifo: TX_FIFO-发送队列;RX_FIFO-接收队列
** Output parameters:       NONE
** Returned value:			OPERATE_SUCCESS:    操作成功
**                          OPERATE_FAIL:       操作失败
*********************************************************************************************************/
extern int32 uartFifoFlush (uint32 uiId, uint32 uiWhichFifo)
{
#if defined(UART_SEMCONTROL)
	INT8U UartErr;
#endif // end #if defined(UART_SEMCONTROL)
	
		if (uiId >__UART_MAX_NUM) {
			return UART_NOK;
		}
		else if (uiWhichFifo == RX_FIFO) {
#if defined(UART_SEMCONTROL)
		OSSemPend(__GpuiUartInfoTab[uiId]->pUartSem, 0, &UartErr);
#else
		OS_ENTER_CRITICAL(); 
#endif // end #if defined(UART_SEMCONTROL)
			QueueFlush((void *)__GpuiUartInfoTab[uiId]->PdqReviceBuf);  /*  清空接收队列                */
#if defined(UART_SEMCONTROL)
		OSSemPost(__GpuiUartInfoTab[uiId]->pUartSem);
#else
		OS_EXIT_CRITICAL();
#endif // end #if defined(UART_SEMCONTROL)
		}
		else if (uiWhichFifo == TX_FIFO) {
#if defined(UART_SEMCONTROL)
		OSSemPend(__GpuiUartInfoTab[uiId]->pUartSem, 0, &UartErr);
#else
		OS_ENTER_CRITICAL(); 
#endif // end #if defined(UART_SEMCONTROL)			
			QueueFlush((void *)__GpuiUartInfoTab[uiId]->PdqSendBuf);/*  清空发送队列                */
#if defined(UART_SEMCONTROL)
		OSSemPost(__GpuiUartInfoTab[uiId]->pUartSem);
#else
		OS_EXIT_CRITICAL();
#endif // end #if defined(UART_SEMCONTROL)			
		}
		else { 
			return UART_NOK;
		}
		return UART_OK;
}
/*********************************************************************************************************
** Function name:			__uartGetch
** Descriptions:			无等待接收一个字节
** input parameters:		PUartInfo      : 指向uart信息结构体的指针
**                          uiRet       : 存储接收到的数据
** Returned value:			UART_OK   : 成功
**                          UART_EMPTY: 无数据
** Created by:				Chenmingji
** Created Date:			2006-09-08
*********************************************************************************************************/
static uint8 __uartGetch (__PUART_INFO PUartInfo, uint8 *uiRet)
{
    uint32 uiErr;

#if defined(UART_SEMCONTROL)
		//OSSemPend(PUartInfo->pUartSem, 0, &UartErr);
#else
		//OS_ENTER_CRITICAL();
#endif // end #if defined(UART_SEMCONTROL)
    
    uiErr = QueueRead(uiRet, PUartInfo->PdqReviceBuf);
    
#if defined(UART_SEMCONTROL)
		//OSSemPost(PUartInfo->pUartSem);
#else
		//OS_EXIT_CRITICAL();	 
#endif // end #if defined(UART_SEMCONTROL)
    
    return uiErr;
} 
/*********************************************************************************************************
** Function name:           uartRead
** Descriptions:            从串口设备读取数据
** input parameters:        uiId:        子设备号
**                          puiBuf:      保存返回数据的字符串指针地址
**                          uiNum:       读取的数据个数
**                          pRsv:        保留参数
** Output parameters:       puiBuf:      读到的数据首地址      
** Returned value:          实际读取的数据个数
*********************************************************************************************************/
extern int32   uartRead (uint32 uiId,
                         uint8 *puiBuf,
                         uint32 uiNum,
                         void *pRsv)
{
	uint32 i;
	uint32 uiReviceNum = 0;	
#if defined(UART_SEMCONTROL)
	INT8U UartErr;
#endif // end #if defined(UART_SEMCONTROL)

	if (uiId < __UART_MAX_NUM) {
	
#if defined(UART_SEMCONTROL)
		OSSemPend(__GpuiUartInfoTab[uiId]->pUartSem, 0, &UartErr);
#else
		OS_ENTER_CRITICAL(); 
#endif // end #if defined(UART_SEMCONTROL)
		
		for (i=0;i<uiNum;i++) {			
			if (QUEUE_OK == ( __uartGetch(__GpuiUartInfoTab[uiId],puiBuf++))) {
				uiReviceNum ++;
			}
			else
				break;
		}
		
#if defined(UART_SEMCONTROL)
		OSSemPost(__GpuiUartInfoTab[uiId]->pUartSem);
#else
		OS_EXIT_CRITICAL();	 
#endif // end #if defined(UART_SEMCONTROL)

	} 
	return uiReviceNum;	
}
/*********************************************************************************************************
** Function name:			__uartWrite
** Descriptions:			发送多个字节数据
** input parameters:		PUartInfo:      指向uart信息结构体的指针
**                          puiData:        要发送的数据的首地址
**                          uiNumByte:      发送数据的个数
** Output parameters:       NONE
** Returned value:			成功发送数据的个数
*********************************************************************************************************/
static uint16 __uartWrite (__PUART_INFO PUartInfo, uint8 *puiData, uint32 uiNumByte)
{
    volatile uint32 *puiAddrBase;
	volatile uint32  uiOffBase;
	
    uint32 uiSendNumbyte;
    
    uint8 uiTemp;
#if defined(UART_SEMCONTROL)
		INT8U UartErr;
#endif // end #if defined(UART_SEMCONTROL)

    uiSendNumbyte = uiNumByte;
    PUartInfo->uiUartFlag &= (~0x80);                                   /*  使用队列做为缓冲            */
    if((PUartInfo->uiUartFlag) < 0x80) { 

#if defined(UART_SEMCONTROL)
			OSSemPend(PUartInfo->pUartSem, 0, &UartErr);
#else
			OS_ENTER_CRITICAL(); 
#endif // end #if defined(UART_SEMCONTROL)
	    
	    while (uiSendNumbyte > 0) {
	        if (QueueWrite((void *)PUartInfo->PdqSendBuf, *puiData++) == QUEUE_FULL) {
	            break;
	        }
	        uiSendNumbyte--;
	    }
	    PUartInfo->uiUartFlag |= 0x10;                                  /*  使能发送队列                */
	    if (PUartInfo->uiUartFlag > 0x0f) {
	        puiAddrBase = PUartInfo->puiAddrBase;
	        uiOffBase = PUartInfo->uiOffBase;

	        if ((puiAddrBase[__B_UART_IER << uiOffBase] & 0x02) == 0) {
	            if (QUEUE_OK == QueueRead(&uiTemp, PUartInfo->PdqSendBuf)) {	
	            	puiAddrBase[__B_UART_IER << uiOffBase] |= 0x02;     /*  开发送中断                  */
	            	puiAddrBase[__B_UART_THR << uiOffBase] = uiTemp; 
	        	}
	        }
	    }	
	    
#if defined(UART_SEMCONTROL)
			OSSemPost(PUartInfo->pUartSem);
#else
			OS_EXIT_CRITICAL();	 
#endif // end #if defined(UART_SEMCONTROL)
	      	
    }	
    return (uiNumByte - uiSendNumbyte);
}

/*********************************************************************************************************
** Function name:			__uartDirectMultiWrite
** Descriptions:			祇癳

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -