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

📄 uarts.c

📁 优龙YLP270的串口裸机测试程序
💻 C
📖 第 1 页 / 共 3 页
字号:
      return 0;

    // Print a standard message on what UART is about to be tested
    UartStartTesting (ctxP);

    PostDisplayProgress(UartType, ERR_TS_UART_LOOPSTATIC, 1);

  	// Initialize the device and select loopback
    if (ctxP == NULL)
    {
        //printf (" Error! UART device open failed\r\n");
        status = ERRORCODE(UartType, 
                           ERR_S_UART_INV_DEV,
                           ERR_T_NODEVICE);
        XllpUtilityOutputError(status);
        return ++errorCount;
    }
    else
    {
		// Setup the test config. structure.   
		UartSetConfiguration (ctxP);

		// Set the loopback variable in UART's configuration structure
        // Note: When STUART is used the loopback mode would not be set to
        // allow STUART function in full duplex mode
        PostDisplayProgress(UartType, ERR_TS_UART_LOOPSTATIC, 2);
        if ((ctxP->type == FFUartType) || (ctxP->type == BTUartType))
        {
		    ctxP->uartCfgP->loopback = loopbackMode;
            PostDisplayProgress(UartType, ERR_TS_UART_LOOPSTATIC, 3);
        }
        else if (ctxP->type == STUartType)
        {
	        ctxP->uartCfgP->loopback = loopbackMode;
            if (loopbackMode == UartLoopbackOff)
            {
                // Set Sir polarity to negative
                ctxP->uartCfgP->maskIRDA |= ISR_RXPL;
            }
        }
                
        // Setup the Uart's hardware  
        PostDisplayProgress(UartType, ERR_TS_UART_LOOPSTATIC, 4);
        ctxP->setupUartFnP (ctxP);

        DM_WaitMs (500);

        // Run the actual test
		// Clear the receiver
    	ctxP->clearRxUartFnP (ctxP);

        PostDisplayProgress(UartType, ERR_TS_UART_LOOPSTATIC, 5);

        // Make sure we can get a simple transmit back
 	    for (i = 0; i < testCount; i++)
		{
        	if ((data = UartLoopbackSingle (ctxP, testValues8[i])) != testValues8[i])
        	{
                // Output mismatch error
                status = ERRORCODE(UartType, 
                                   ERR_S_UART_RECEIVE,
                                   ERR_T_RECEIVE_MISMATCH);
                XllpUtilityOutputError(status);
            	DM_CwDbgPrintf(DM_CW_UART_0,
            	            " Error! Mismatch, exp %x, rcv %x\r\n", testValues8[i], data);
            	testFailed = TRUE;
                ++errorCount;
                break;
        	}
            else
            {
                PostDisplayProgress(UartType, ERR_TS_UART_LOOPSTATIC, 6);
            }
        }
    }

	// Restore Uart's default configuration 
	UartRestoreConfiguration (ctxP);

    // Shutdown the UART if not the DM System Uart
    if(!((ctxP->type & SystemUart) == SystemUart))
      ctxP->shutdownUartFnP (ctxP);

    // Report result of the test
	if (testFailed)
	{
    	return errorCount;
	}
    else
    {
        printf (" Success!\r\n");
        return errorCount;
    }
}

/*
*******************************************************************************
*
* FUNCTION:
*   PostUartStaticLoopTest
*	
* DESCRIPTION: 
*	This is a simple loopback test where the number of bytes are sent
*	via loopback path. This procedure can be used by POST or by static loopback 
*	test for basic check of the UART's health. 
*
* INPUT PARAMETERS:
*   UartContextT * ctxP - Pointer the the UART Device Context Structure.
*   PCHAR param - Pointer to the parameter string in UART's command structure.
*	              It controlls the mode of UART's operation (internal loopback or normal) 
*
* RETURNS:			none.
*
* GLOBAL EFFECTS:	none.
*
* ASSUMPTIONS:		none.
*
* CALLS:
*	UartLoopbackSingle
*
* CALLED BY:
*	Menu system
*
* PROTOTYPE:
*    void PostUartStaticLoopTest (void * arg, CHAR * param)
*
*******************************************************************************
*/
void PostUartStaticLoopTest (void * arg, CHAR * param)
{
    UartContextT * ctxP = (UartContextT *)arg;
	UINT32 status;
	INT loopbackMode;

    // Configure UART to operate either in normal or internal loopback mode
    if (sscanf(param, "%d", &loopbackMode) != 1)
	{
		// Bad number of the parameters have been passed to the function
		// printf (" Error! Invalid number of param\r\n");
        status = ERRORCODE(UartType, 
                        ERR_S_UART_STATIC_LOOPBACK,
                        ERR_T_ILLPARAM);
        XllpUtilityOutputError(status);
	}
    PostUartStaticLoop (ctxP, loopbackMode);
}

/*
*******************************************************************************
*
* FUNCTION:
*   UartDmaInterruptTest
*	
* DESCRIPTION: 
*	In this test the DMA is used to load and empty the receive and transmit FIFOs.
*	The interrupts will be generated on the completion of the Tx and Rx transfers. 
*
* INPUT PARAMETERS:
*   UartContextT * ctxP - Pointer the the UART Device Context Structure.
*   PCHAR param - Pointer to the parameter string in UART's command structure.
*	              It controlls the mode of UART's operation (internal loopback or normal) 
*
* RETURNS:			none.
*
* GLOBAL EFFECTS:	none.
*
* ASSUMPTIONS:		none.
*
* CALLS:
*	
*
* CALLED BY:
*	Menu system
*
* PROTOTYPE:
*   void UartDmaInterruptTest (void * arg, CHAR * param)
*
*******************************************************************************
*/
VOID UartDmaInterruptTest (PVOID arg, CHAR * param)
{
    UartContextT * ctxP = (UartContextT *)arg;
	INT loopbackMode;
	INT retryTxCount = DMA_TIMEOUT;
	INT retryRxCount = DMA_TIMEOUT;
	UINT32  error, status = 0;

    DM_ControlWordsDebugPrintPutBit (DM_CW_UART_0, 1);
    DM_ControlWordsDebugPrintPutBit (DM_CW_DMA_0, 1);
    DM_ControlWordsDebugPrintPutBit (DM_CW_XS_INT_CTRL, 1);

    // Check if we are going to test the DM System Uart
    if (UartReturnFromTestIfSystem (ctxP) != 0)
      return;

    // Print a standard message on what UART is about to be tested
    UartStartTesting (ctxP);

    PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 1);
  	// Initialize the device and select loopback
    if (ctxP == NULL)
    {
        // printf (" Error! UART device open failed\r\n");
        status = ERRORCODE(UartType, 
                           ERR_S_UART_INV_DEV,
                           ERR_T_NODEVICE);
        XllpUtilityOutputError(status);
        return;
    }
    else
    {
        // Configure UART to operate either in normal or internal loopback mode
		if (sscanf(param, "%d", &loopbackMode) != 1)
		{
			// Bad number of the parameters have been passed to the function
			// printf (" Error! UART device invalid number of param\r\n");
            status = ERRORCODE(UartType, 
                               ERR_TS_UART_DMA_LOOPBACK,
                               ERR_T_ILLPARAM);
            XllpUtilityOutputError(status);
			return;
		}

        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 2);

		// Setup the test config. structure for the specific Uart.   
		UartSetConfiguration (ctxP);
 
        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 3);

        // Process input parameters and select the DMA config. structure
		// for the specific UART
		UartSetDmaConfiguration (ctxP);
		
		// Set the loopback variable in UART's configuration structure
		ctxP->uartCfgP->loopback = loopbackMode;

		// Enable DMA in UART's configuration structure
		ctxP->uartCfgP->maskInt |= IER_DMAE;

		// Set the receive DMA request triger level to 32 bytes
		ctxP->uartCfgP->maskFIFO |= FCR_ITL32;

		// Configure the DMA channel to be used to service transmit FIFO
		// Enable an interrupt on the completion of the transfer
        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 4);
		error = XsDmaConfigureDevice (XSDMA_CH_PR_LOW,
							ctxP->dmaTxCfgP->targetName, 
							TRUE,
							&firstDescTxVtP,
							ctxP->dmaTxCfgP->descNum,
							ctxP->dmaTxCfgP->bufferSize,
							ctxP->intHandlerDmaTxFnP,
							ctxP,
							DCSR_STOPINTR,
							(PINT)&ctxP->dmaTxChannel); 

		if (error != 0)
		{
	        // DMA channel or source memory can not be allocated
	        // printf (" Error! UART DMA Tx config failed\r\n");
            status = ERRORCODE(UartType, 
                               ERR_S_UART_TRANSMIT,
                               ERR_T_DMA_NOCHAN);
            XllpUtilityOutputError(status);
		    // Cleanup the allocated resources
		    XsDmaFreeChannel (ctxP->dmaTxChannel);
		    XsDmaDestroyChain (firstDescTxVtP);
	        // Restore an original Uart configuration
		    UartRestoreDmaConfiguration (ctxP);
		}

		// Configure the DMA channel to be used to service receive FIFO
		// Enable an interrupt on the completion of the transfer
        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 5);
		error = XsDmaConfigureDevice (XSDMA_CH_PR_LOW,
							ctxP->dmaRxCfgP->sourceName, 
							FALSE,
							&firstDescRxVtP,
							ctxP->dmaRxCfgP->descNum,
							ctxP->dmaRxCfgP->bufferSize,
							ctxP->intHandlerDmaRxFnP,
							ctxP,
							DCSR_STOPINTR,
							(PINT)&ctxP->dmaRxChannel); 

		if (error != 0)
		{
	        // DMA channel or source memory can not be allocated
	        // printf (" Error! UART DMA Rx config failed\r\n");
            status = ERRORCODE(UartType, 
                               ERR_S_UART_RECEIVE,
                               ERR_T_DMA_NOCHAN);
            XllpUtilityOutputError(status);
		    // Cleanup the allocated resources
		    XsDmaFreeChannel (ctxP->dmaRxChannel);
		    XsDmaDestroyChain (firstDescRxVtP);
	        // Restore an original Uart configuration
		    UartRestoreDmaConfiguration (ctxP);
		}

        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 6);

		// Fill transmit buffers with a known pattern
		FillTxBufferSerial (firstDescTxVtP, ctxP->dmaTxCfgP->descNum, ctxP->dmaTxCfgP->bufferSize, 0);

        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 7);

		// Clear the receive buffer
		ClearRxBufferSerial (firstDescRxVtP, ctxP->dmaRxCfgP->descNum, ctxP->dmaRxCfgP->bufferSize);

        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 8);

		// Fill receive buffers with a known pattern
		FillRxBufferSerial (firstDescRxVtP, ctxP->dmaRxCfgP->descNum, ctxP->dmaRxCfgP->bufferSize, 0x55);

        // Clear the Rx and Tx transfer completion flag 
        // These flags will be set by the DMA interrupt handler
		ctxP->xferTxComplete = 0;
        ctxP->xferRxComplete = 0;
		ctxP->dmaTxIntStatusP->stopIntCount = 0;
		ctxP->dmaRxIntStatusP->stopIntCount = 0;

		// Clear the receiver's FIFO
    	ctxP->clearRxUartFnP (ctxP);

        // Setup the Uart's hardware  						
        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 9);
        ctxP->setupUartFnP (ctxP);							
        DM_WaitMs (500);

        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 0xA);

		// Wait for the completion of the Tx transfer
		while (!ctxP->xferTxComplete && --retryTxCount > 0)
            DM_WaitMs (1);
		if (retryTxCount <= 0)
		{
	        // Time out on Tx DMA channel
	        // printf (" Error! UART Tx DMA Timeout\r\n");
            status = ERRORCODE(UartType, 
                               ERR_S_UART_TRANSMIT,
                               ERR_T_TIMEOUT);
            XllpUtilityOutputError(status);
		    // Cleanup the allocated resources
		    XsDmaFreeChannel (ctxP->dmaTxChannel);
		    XsDmaFreeChannel (ctxP->dmaRxChannel);
		    XsDmaDestroyChain (firstDescTxVtP);
		    XsDmaDestroyChain (firstDescRxVtP);
	        // Restore an original Uart configuration
		    UartRestoreDmaConfiguration (ctxP);
            // Shutdown the UART
            ctxP->shutdownUartFnP (ctxP);
            return;
		}

        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 0xB);

		// Wait for the completion of the Rx transfer
		while (!ctxP->xferRxComplete && --retryRxCount > 0)
            DM_WaitMs (1);
		if (retryRxCount <= 0)
		{
	        // Time out on Rx DMA channel
	        // printf (" Error! UART Rx DMA Timeout\r\n");
            status = ERRORCODE(UartType, 
                               ERR_S_UART_RECEIVE,
                               ERR_T_TIMEOUT);
            XllpUtilityOutputError(status);
		    // Cleanup the allocated resources
		    XsDmaFreeChannel (ctxP->dmaTxChannel);
		    XsDmaFreeChannel (ctxP->dmaRxChannel);
		    XsDmaDestroyChain (firstDescTxVtP);
		    XsDmaDestroyChain (firstDescRxVtP);
	        // Restore an original Uart configuration
		    UartRestoreDmaConfiguration (ctxP);
            // Shutdown the UART
            ctxP->shutdownUartFnP (ctxP);
            return;
		}

		// Compare the data between receive and transmit buffers
        PostDisplayProgress(UartType, ERR_TS_UART_DMA_LOOPBACK, 0xC);
		status = CompareDataDmaXferSerial (firstDescTxVtP,  firstDescRxVtP,
				                           ctxP->dmaTxCfgP->descNum, 
				                           ctxP->dmaTxCfgP->bufferSize,
				                           ctxP->dmaTxCfgP->xferLength);
        if (status)
        {
            // Report data mismatch error
            status = ERRORCODE(UartType, 
                               ERR_S_UART_RECEIVE,
                               ERR_T_RECEIVE_MISMATCH);
            XllpUtilityOutputError(status);
        }

		// Cleanup the allocated resources
		XsDmaFreeChannel (ctxP->dmaTxChannel);
		XsDmaFreeChannel (ctxP->dmaRxChannel);
		XsDmaDestroyChain (firstDescTxVtP);
		XsDmaDestroyChain (firstDescRxVtP);

⌨️ 快捷键说明

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