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

📄 xsffuart.c

📁 ARM9WindowsOS下电机控制 蜂鸣控制 需要高度自治的试验平台
💻 C
字号:
/******************************************************************************
**
**  COPYRIGHT (C) 2000 Intel Corporation
**
**  FILENAME:      xsffuart.c
**
**  PURPOSE:       
**
**  LAST MODIFIED: 12/26/2000
******************************************************************************/

/*
*******************************************************************************
*   HEADER FILES
*******************************************************************************
*/                                                                      
#include "post.h"
#include "XsClkMgr.h"
#include "xsuart.h"
#define FFUART_GLOBALS  1
#include "xsffuart.h"

/*
*******************************************************************************
*   GLOBAL DEFINITIONS
*******************************************************************************
*/

/*
*******************************************************************************
*   LOCAL DEFINITIONS
*******************************************************************************
*/
#define DEBUG_BAUD_38400      0x00000018

static UartCfgT defaultFFUartCfg = {
	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 
};

/*
******************************************************************************************
*
* FUNCTION:             XsFFUartHWSetup         
*
* DESCRIPTION:          This function is used for hardware initialization of UART.
*                       It uses the uart configuration structure for coniguring UART's
*                       modes of operation.
*
* INPUT PARAMETERS:     ctxP  is a pointer to UART's context structure
*
* RETURNS:               0    if successful
*                        int error   if no memory have been allocated for DMA transfer
*
* GLOBAL EFFECTS:       none.
*
* ASSUMPTIONS:          GPIO registers need to be programmed before this function
*                       can be called.

*                             Board Control Register bit BCR[9] = 1 (RS232 power on)
*                             was set on early board initialization as a default. 
*                       
*******************************************************************************************
*/

static
int XsFFUartHWSetup(UartContextT * ctxP)
{   
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;
    UartCfgT * cfgP = (UartCfgT *)ctxP->uartCfgP;
    int divisor;
    int value;
    int rate;
    
	// Select the peripheral clock bit for the UART
	

    // Set Serial Line Control Register (LCR) 
    // DLAB = 1
    uartP->LCR = 0;
    uartP->IER = 0;
    // Configure the baud rate generator
    // Base rate is 14.7456 MHz
    // Baud Rate = base rate/(16*divisor)
    // 230.4 kbps = divisor = 4 (min.)
    // 115.2 kbps = divisor = 8
    // 9.6   kpbs = divisor = 96
    uartP->LCR = 0x80;
   uartP->UDATA = DEBUG_BAUD_38400;
   uartP->IER = 0x00;
   uartP->LCR = 0;
   
   uartP->LCR = 0x3;
   uartP->FCR = 0x01;
   uartP->FCR = 0x07;
   
   uartP->IER = 0x00;
   uartP->MCR = 0;
   

   
   uartP->IER = 0x40;
   
    return 0;
}


/*
*******************************************************************************
*
* FUNCTION:         loopbackFFUart
*
* DESCRIPTION:      This function is used to test UART in polled mode operation.
*
* INPUT PARAMETERS: ctxP    is a pointer to UART context structure. 
*                   data    is a single character sent via loopback path.
* RETURNS:          int   the byte of data.
*                   -1      if timeout expired and no character has been received.
*
* GLOBAL EFFECTS:   none.
*
* ASSUMPTIONS:      UART has been setup to operate in FIFO or non-FIFO polled mode
*                   and loopback test mode is enabled
*
*******************************************************************************
*/

static
int loopbackFFUart(UartContextT * ctxP, int data)
{
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;
    int retry = RETRY;

    while((uartP->LSR & LSR_TEMT) == 0);
	WaitMs(10);

    // Write data
    uartP->UDATA = data;   
	WaitMs(10);

    // Wait for the loopback data to arrive
    while (((uartP->LSR & LSR_DR) == 0) && (--retry > 0))
     	WaitMs(1);
            
    if (retry > 0)
        return uartP->UDATA;

    return (-1);
}

/*
*******************************************************************************
*
* FUNCTION:         writeFFUart                      
*
* DESCRIPTION:      This function is used to transmit data via UART in polled mode
*                   operation
*
* INPUT PARAMETERS: ctxP    is a pointer to UART context structure 
*                   txbufP  is a pointer to the buffer where the data is going 
*                           to be taken from
*                   len     is number of bytes to be sent
*
* RETURNS:          none.
*
* GLOBAL EFFECTS:   none.
*
* ASSUMPTIONS:      none.
*
*******************************************************************************
*/

static
void writeFFUart(UartContextT * ctxP, char * txbufP, int len)
{
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;
    int i;
  
 /* while(1){
  
  uartP->UDATA = 'a';   

        // Wait for UART to complete transmition
        while((uartP->LSR & LSR_TEMT) == 0)
            {;}
  }*/
    for (i=0; i < len; i++)
    {
        // Write data
        uartP->UDATA = *txbufP++;   

        // Wait for UART to complete transmition
        while((uartP->LSR & LSR_TEMT) == 0)
            {;}
    }
}

/*
*******************************************************************************
*
* FUNCTION:         readFFUart        
*
* DESCRIPTION:      This function is used to receive data via UART in polled mode 
*                   operation
*
* INPUT PARAMETERS: ctxP    is a pointer to UART context structure 
*                   rxbufP  is a pointer to the buffer where received data is
*                           going to be placed
*                   len     is a specified number of bytes to read.
*
* RETURNS:          int an actual number of bytes have been read        
*                   
* GLOBAL EFFECTS:   none.
*                  
* ASSUMPTIONS:      none.
*                  
*******************************************************************************
*/                  

static
int readFFUart(UartContextT * ctxP, char * rxbufP, int len)
{
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;
    int retry = RETRY;
    int i;
  
    for (i=0; i < len; i++) 
    {
        // Wait for data to be available
        while (((uartP->LSR & LSR_DR) == 0) && (--retry > 0))
            WaitMs(1);
            
        // Read data   
        if (retry > 0)
            *rxbufP++ = uartP->UDATA;
        else
            break;
    }
    return i;
}


/*
*******************************************************************************
*
* FUNCTION:         clearRxFFUart
*
* DESCRIPTION:      This function is used to clear receive FIFO of the UART
*
* INPUT PARAMETERS: ctxP    is a pointer to UART context structure
*
* RETURNS:          int     total number of bytes read
*
* GLOBAL EFFECTS:   none.
*
* ASSUMPTIONS:      none.
*
*******************************************************************************
*/

static
int clearRxFFUart(UartContextT * ctxP)
{
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;
    int data;
    int total = 0;
    
    // Read data from FIFO until none is left
    while ((uartP->LSR & LSR_DR) != 0)
    {
        data = uartP->UDATA;
        total++;
    }
    
    return total;
}



/*
*******************************************************************************
*
* FUNCTION:         XsFFUartSWInit         
*
* DESCRIPTION:      This function is used to initialize FFUART's context structure. 
*                   No hardware setup is done at this point.
*
* INPUT PARAMETERS: none.
*
* RETURNS:          none.
*
* GLOBAL EFFECTS:   none.
*
* ASSUMPTIONS:      none.
*
*******************************************************************************
*/

void XsFFUartSWInit (void)
{
    // Initialize the UART
    UartContextT * ctxP = &FFUart;

    // Initialize the UART's register base
    UartRegsT * regsP = (UartRegsT *)FFUARTREG_BASE;
    ctxP->regsP = regsP;

    // Initialize the UART's setup configuration
    ctxP->uartCfgP = &defaultFFUartCfg;

    // Set the context structures functions pointers 
    ctxP->setupUartFnP = (UartSetupT)XsFFUartHWSetup;
    ctxP->loopbackUartFnP = (UartLoopbackT)loopbackFFUart;
    ctxP->writeUartFnP = (UartWriteT)writeFFUart;
    ctxP->readUartFnP = (UartReadT)readFFUart;
//    ctxP->clearTxUartFnP = (UartClearTxT)clearTxFFUart;
    ctxP->clearRxUartFnP = (UartClearRxT)clearRxFFUart;
}

⌨️ 快捷键说明

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