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

📄 uarts.c

📁 ARM9WindowsOS下电机控制 蜂鸣控制 需要高度自治的试验平台
💻 C
字号:
/******************************************************************************
**
**  COPYRIGHT (C) 2000, 2001 Intel Corporation.
**
**  This software as well as the software described in it is furnished under 
**  license and may only be used or copied in accordance with the terms of the 
**  license. The information in this file is furnished for informational use 
**  only, is subject to change without notice, and should not be construed as 
**  a commitment by Intel Corporation. Intel Corporation assumes no 
**  responsibility or liability for any errors or inaccuracies that may appear 
**  in this document or any software that may be provided in association with 
**  this document. 
**  Except as permitted by such license, no part of this document may be 
**  reproduced, stored in a retrieval system, or transmitted in any form or by 
**  any means without the express written consent of Intel Corporation. 
**
**  FILENAME:       UARTs.c
**
**  PURPOSE:        This module contains the test code for UARTs.
**
**  LAST MODIFIED:  $Modtime: 10/01/01 4:32p $
******************************************************************************/

/*
*******************************************************************************
*   HEADER FILES
*******************************************************************************
*/                                                                      

#include "post.h"
#include "xsuart.h"
#include "uarts.h"  
#include "xsffuart.h"

/*
*******************************************************************************
*   LOCAL DEFINITIONS
*******************************************************************************
*/
char sText[256];
#define WRITE_BUFFER_SIZE   256
#define CR                  '\r'

int PrintFlag=1;
int SerialPort=0;
// Default configuration structure used in the test
static UartCfgT testConfigUART = 
{
    FCR_TRFIFOE,                 // Enable FIFOs
    IER_UUE,                     // Disable DMA, no NRZ, disable IRQ, and enable UART unit
    LCR_WLS8,                    // One stop bit, no parity, 8 bits
    0,                           // No IRDA
    UartLoopbackOff,             // Disable loopback        
    38400                       // baud rate 38400
};

#ifdef temp
// DMA configuration structure to setup the transmit channel
static UartCfgDmaT ConfigBTTxDma = 
{
    NUM_BUF,        
    XFER_LEN,   
    MEMORY, 
    BTUART_DMA,
    lowPriority 
};

// DMA configuration structure to setup the receive channel
static UartCfgDmaT ConfigBTRxDma = 
{
    NUM_BUF,        
    XFER_LEN,   
    BTUART_DMA, 
    MEMORY,
    lowPriority 
};

// Single transaction loopback values. Attempt to spread out bits.
static int testValues16[] =
{
    0x0010, 0x8000, 0xAAAA, 0x5555, 0xC0C0, 0x0330, 0x9090, 0x6060,
    0x1111, 0x2222, 0x4444, 0x8888
};
static int testValues12[] =
{
    0x0010, 0x8000, 0xAAA0, 0x5550, 0xC0C0, 0x0330, 0x9090, 0x6060,
    0x1110, 0x2220, 0x4440, 0x8880
};

#endif
// Single transaction loopback values. Attempt to spread out bits.
static int testValues8[] =
{
    0x10, 0x80, 0xAA, 0x55, 0xC0, 0x03, 0x90, 0x60,
    0x11, 0x22, 0x44, 0x88                                                                
};                                                                                        

//static DMADescriptorElementsT * firstDescP;


// Print a string via the serial device.

void PrintSerial(char* s, int portId)
{
    UartContextT * ctxP;
    int len;
    char buff[WRITE_BUFFER_SIZE];

    
        ctxP = &FFUart;
    
  

    // Setup the Uart's hardware  
    ctxP->setupUartFnP(ctxP);
    WaitMs (100);

    strcpy(buff, s);
    len = strlen(s);

    if (buff[len-1] == '\n')
    {
        buff[len-1] = CR;
        buff[len++] = '\n';
    } 

    ctxP->writeUartFnP(ctxP, buff, len);
}

/*
*******************************************************************************
*
* FUNCTION:         UartConfigDma
*   
* DESCRIPTION: 
*   This function configures the DMA to service the UART's receive or transmit FIFOs. 
*
* INPUT PARAMETERS:
*   UartContextT * ctxP - Pointer the the UART Device Context Structure.
*
* RETURNS:
*   int error    -   It return an error code if the DMA channnel or memory 
*                       can not be allocated by the DMA service. It returns zero
*                       in case of success.                 
*
* GLOBAL EFFECTS:   none.
*
* ASSUMPTIONS:      none.
*
* CALLS:
*
* CALLED BY:
*   Menu system
*
* PROTOTYPE:
*   int UartConfigDma(UartDmaCfgT * dmaCfgP)
*
*******************************************************************************
*/
#ifdef temp
int UartConfigDma(UartDmaCfgT * dmaCfgP)
{
    DMADescriptorElementsT * nextDescP;
    int  error = 0;
    int  dmaChannel
    int i;

    // Configure the DMA channel to be used to service transmit FIFO:
    // Request a free channel from DMA service
    dmaChannel = DMAGetFreeChannel(dmaCfgP->priority, NULL);

    if (dmaChannel == -1)
    {
        // Error: no channel has been given
        return (error = -1);
    }

    // Setup the descriptor chain of N descriptors
    // and get the pointer that points to the chain of the descriptors
    firstDescP = DMASetupChain(dmaCfgP->descNum, 
                               dmaCfgP->xferLength,
                               dmaCfgP->sourceName,
                               dmaCfgP->targetName, 
                               dmaChannel);

    if (firstDescP == NULL)
    {
        // Error: in descriptor chain setup
        return (error = -1);
    }

    nextDescP = firstDescP;
    do {
            if (dmaCfgP->sourceName == MEMORY)
            {
                XmitBufP[i] = nextDescP->sourceVirtualAddr;
                nextDescP = nextDescP->nextVirtualAddr;
            }
            else if (dmaCfgP->targetName == MEMORY)
            {
                RcvBufP[i] = firstDescP->targetVirtualAddr;
                nextDescP = nextDescP->nextVirtualAddr;
            }

            i++;

    } while ((nextDecsP != NULL) && (i < dmaCfgP->descNum));
                        
    // Set the Run bit in the appropriate DMA channel and also
    // load the DDADR register with the address of the first descriptor to execute.
    DMAGo (dmaCfgP->dmaChannel, firstDescP);
    
    return error;
}
#endif


/*
*******************************************************************************
*
* FUNCTION:
*   UartLoopbackSingle
*
* DESCRIPTION: 
*   This is a simple loopback test where the single byte of data is sent
*   via loopback path.
*
* INPUT PARAMETERS:
*   UartContextT * ctxP - Pointer the the UART Device Context Structure.
*   int data            - Single byte of data, that is sent via loopback.
*
* RETURNS:
*   The single byte of data if success 
*   or (-1) if loopback timout time has expiered
*
* GLOBAL EFFECTS:
*
* ASSUMPTIONS:
*
* CALLS:
*
* CALLED BY:
*   UartStaticLoopTest
*
* PROTOTYPE:
*   int UartLoopbackSingle (UartContextT * ctxP, int data)
*
*******************************************************************************
*/

static
int UartLoopbackSingle (UartContextT * ctxP, int data)
{
    int x = ctxP->loopbackUartFnP (ctxP, data);
    if (x < 0)
    {
        POST_SerialPrintf ("Timeout on loopback write");
    }
    return x;
}

/*
*******************************************************************************
*
* FUNCTION:
*   UartStaticLoopTest
*   
* 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.
*   char* 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 UartStaticLoopTest (void * arg, char * param)
*
*******************************************************************************
*/

void UartStaticLoopTest (void * arg, char * param)
{
    UartContextT * ctxP = (UartContextT *)arg;
    int data;
    int done = 0;
    int i;
    int testCount = NUM_TEST_VALUES;
    int loopbackMode;
    int loggedError;
    int deviceType = 0;

    ctxP->uartCfgP = &testConfigUART;

    // Setup the display handler
    DM_StartTest ("UART Loopback Test");

    // Initialize the device and select loopback
    if (ctxP == NULL)
        POST_SerialPrintf ("UART device open failed");
    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
            POST_SerialPrintf ("UART device invalid number of param.");
        }
        
        // Set the loopback variable in UART's configuration structure
        ctxP->uartCfgP->loopback = loopbackMode;
        
        // Setup the Uart's hardware  
        ctxP->setupUartFnP (ctxP);

        WaitMs (500);

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

        // Make sure we can get a simple transmit back
        for (i = 0; i < testCount; i++)
        {
            if ((data = UartLoopbackSingle (ctxP, testValues8[i])) != testValues8[i])
            {
                sprintf (sText, "Mismatch, exp %x, rcv %x", testValues8[i], data);
                POST_SerialPrintf (sText);

                if (ctxP == &FFUart)
                {
                    deviceType = 0;
                }
               
               
            }
        }

        if (done)
        {
            return;
        }

        DM_Status ("Static loopback OK");

        // Cleanup the allocated resources
//        funcP->cleanupUartFnP (ctxP);
    }

    // Test done
    DM_FinishTest ("UART Loopback Test Complete");
}

#ifdef temp
static
void clearRxBuffer (char* rcvBufP)
{
    int i, j;
    char* rxP;

    for (j = 0; j < NUM_BUF; j++)
    {
        rxP = rcvBufP[j];
        for (i = 0; i < DMA_BUFSIZ; i++)
           *(rxP + i) = 0;
    }
}


static
void fillTxBuffer (char* xmitBufP)
{
    int i, j;
    char* txP;

    // Write a simple pattern to the transmit buffer and
    // clear the receive buffer
    for (j = 0; j < NUM_BUF; j++)
    {
        txP = rcvBufP[j];
        for (i = 0; i < DMA_BUFSIZ; i++)
           *(txP + i) = i & (i & 1 ? 0xff : 0xf0);
    }
        // Set a start data marker
        *(int *)  = DMA_DATA_MARKER;
    
}

static
void compareData (char* xmitBufP, char* rcvBufP)
{

    int i, j;

    for (j = 0; j < NUM_BUF; j++)
    {
        for (i = 0; i < DMA_BUFSIZ; i++)
        {
            rcvBufP[j][i] = 0;
        for (i = 0; i < DMA_BUFSIZ; i++)
            xmitBufP[j][i] = i & (i & 1 ? 0xff : 0xf0);


  /* Check for errors
   */
    if (retryCount == 0)
    {
        POST_SerialPrintf ("Timeout on DMA loopback");
    }
    else
    {
        i = checkData (rcvBufP[0], DMA_BUFSIZ);
        if (i < DMA_BUFSIZ)
        {
            sprintf (sText, "Mismatch at %d, exp %x, rcv %x",
                i, xmitBufP[0][i], rcvBufP[0][i]);
            POST_SerialPrintf (sText);
        }
        else
            DM_Status ("DMA loopback OK");
    }

}
/*
*******************************************************************************
*
* FUNCTION:
*   UartDmaLoopTest
*   
* DESCRIPTION: 
*   In this test the DMA is used to load and empty the receive and transmit FIFOs. 
*   
*
* INPUT PARAMETERS:
*   UartContextT * ctxP - Pointer the the UART Device Context Structure.
*   char* 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 UartDmaLoopTest (void * arg, char * param)
*
*******************************************************************************
*/

void UartDmaLoopTest (void * arg, char * param)
{
    UartContextT * ctxP = (UartContextT *)arg;
    UartCfgT *cfgP = (UartCfgT *)ctxP->uartCfgP;
    UartDmaCfgT * dmaTxCfgP = (UartDmaCfgT *)ctxP->dmaTxCfgP;
    UartDmaCfgT * dmaRxCfgP = (UartDmaCfgT *)ctxP->dmaRxCfgP;

    int loopbackMode;
    int  error;

    if (cfgP == NULL)
        cfgP = &testConfigUART;

    // Setup the display handler
    DM_StartTest ("UART DMA Loopback Test");

    // Initialize the device and select loopback
    if (ctxP == NULL)
        POST_SerialPrintf ("UART device open failed");
    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
            POST_SerialPrintf ("UART device invalid number of param.");
        }

        // Process input parameters and select the DMA config. structure
        // for the specific UART
        
        // Set the loopback variable in UART's configuration structure
        cfgP->loopback = loopbackMode;
        
        // Configure the DMA channel to be used to service transmit FIFO:
//        if (dmaTxCfgP == NULL)
//          dmaTxCfgP = &ConfigBTTxDma;

        error = UartConfigDma(dmaCfgP);
        if (error != 0)
        {
            // DMA channel or source memory can not be allocated
            POST_SerialPrintf ("UART DMA config failed");
        }
                
        // Configure the DMA channel to be used to service receive FIFO:
//        if (dmaRxCfgP == NULL)
//          dmaRxCfgP = &ConfigBTRxDma;

        error = UartConfigDma(dmaCfgP);
        if (error != 0)
        {
            // DMA channel or source memory can not be allocated
            POST_SerialPrintf ("UART DMA config failed");
        }

        // Fill transmit buffers with a known pattern 

        // Setup the Uart's hardware  
        ctxP->setupUartFnP (ctxP);

        WaitMs (500);

        // Run the actual test
        dmaTestBody (ctxP, 0);

        // Cleanup the allocated resources
//        funcP->cleanup_fnp (funcP);
    }

    // Test done
    DM_FinishTest ("DMA Loopback Test Complete");
}

#endif



void TestUARTs()
{

//    POST_Progress("Testing FFUART...");

    // Run Full Feature UART Loopback test.
    UartStaticLoopTest(&FFUart, "1");

 //   POST_Progress("Success!\n");


 
}

⌨️ 快捷键说明

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