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

📄 xsstuart.c

📁 优龙YLP270开发板 光盘自带的BIOS和实验例程源码 强烈推荐
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
**
**  COPYRIGHT (C) 2000 Intel Corporation
**
**  FILENAME:      xsstuart.c
**
**  PURPOSE:	   This file contains an initialization code of the STUART
**				   used to support the Slow Infrared interface (SIR).		       
**
**  LAST MODIFIED: 01/02/2001
******************************************************************************/

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

#include "systypes.h"
#include "DM_Debug.h"
#include "XsClkMgr.h"
#include "XsGpioApi.h"
#include "DM_Debug.h"
#include "XsDmaApi.h"
#include "xsuart.h"
#define STUART_GLOBALS  1
#include "xsstuart.h"
#include "timedelays.h"
#include "boardControl.h"

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

/*
*******************************************************************************
*   LOCAL DEFINITIONS
*******************************************************************************
*/
static UartCfgT defaultSTUartCfg = {
	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		
	115200,					     // baud rate 115200 
};

// DMA configuration structure to setup the transmit channel
static UartDmaCfgT defaultDmaTxCfg = 
{
	NUM_BUF_DEFAULT,
	BUFF_SIZE_DEFAULT,
	XFER_LEN_DEFAULT,	
	XSDMA_DN_MEMORY,	
	XSDMA_DN_STUART,
	XSDMA_CH_PR_LOW	
};

// DMA configuration structure to setup the receive channel
static UartDmaCfgT defaultDmaRxCfg = 
{
	NUM_BUF_DEFAULT,		
	BUFF_SIZE_DEFAULT,
	XFER_LEN_DEFAULT,	
	XSDMA_DN_STUART,	
	XSDMA_DN_MEMORY,
	XSDMA_CH_PR_LOW	
};

static UartDmaStatusT defaultDmaTxStatus = {0,0,0,0};
static UartDmaStatusT defaultDmaRxStatus = {0,0,0,0};

/*
******************************************************************************************
*
* FUNCTION:             XsSTUartHWSetup         
*
* DESCRIPTION:          This function is used for hardware initialization of UART.
*                       It uses the uart configuration structure for coniguring UART's
*                       modes of operation.
*                       If STUART is used to support SIR, than maskIRDA in UART's 
*                       configuration structure will be used to set SIR polarity,
*                       transmit pulse width and enable SIR receiver and transmitter.
*
* INPUT PARAMETERS:     ctxP is a pointer to UART's context structure
*
* RETURNS:              UINT32
*
* GLOBAL EFFECTS:       none.
*
* ASSUMPTIONS:          GPIO registers need to be programmed before this function
*                       can be called.
*                       If STUART is used to support SIR than: 
*                       On the Sandgate board:
*                           Board Control Register bit BCR[2] = 0 (SIR mode) and 
*                                                  bits BCR[4:3] = 00 (IRDA Max. power)
*                           were set on early board initialization as the default settings. 
*                       On the Lubbock board:
*                           MISC_WR register bit 4, IRDA MODE = 0 (SIR mode) and
*                           IRDA power level can be set by the switches S5 and S6.                       
*
*******************************************************************************************
*/

static
UINT32 XsSTUartHWSetup(UartContextT * ctxP)
{   
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;
    UartCfgT * cfgP = (UartCfgT *)ctxP->uartCfgP;
    UINT divisor;
    UINT value;
    UINT rate;
    
    // Clear all the registers
	uartP->IER = 0;	
	uartP->FCR = 0;	
	uartP->LCR = 0;	
	uartP->MCR = 0;	
	uartP->ISR = 0;	

    // Clear the Rx FIFO 
    uartP->FCR = ctxP->uartCfgP->maskFIFO | FCR_RESETRF;

    // Clear the Tx FIFO 
    uartP->FCR = ctxP->uartCfgP->maskFIFO | FCR_RESETTF;

	// Disable the peripheral clock bit for the ICP
	xsCMDisableClock(CK_ICP);

	// Configure GPIOs 
	XsGpioSetStuart ();

	// Select the peripheral clock bit for the UART
	xsCMEnableClock (CK_STUART);

    // Set Serial Line Control Register (LCR) 
    // DLAB = 1
    uartP->LCR = LCR_DLAB1 | cfgP->maskSerial;

    // 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
    
    // Check the default rate
    if (cfgP->rate == 0)
        rate = 115200;
    else
        rate = cfgP->rate;

    // To Configure the baud rate generators for UART:
    // DLAB = 1 to access DLL and DLH
    // Load DLL and DLH with divisor (divisor = 8 for 115200)
    // DLAB = 0
    divisor = 14745600 / (16 * rate);
    if (divisor < FFDIVISOR_MIN )
        divisor = FFDIVISOR_MIN;

    uartP->UDATA = divisor & 0xff;
	uartP->IER = (divisor & 0xff00) >> 8; 
    uartP->LCR &= ~LCR_DLAB1;

    // Set loopback
    // LOOP = 1 Test mode
    if (cfgP->loopback != 0)
        uartP->MCR |= MCR_LOOP;
    else
    {    
        uartP->MCR &= ~MCR_LOOP;
        // Read MSR once to clear delta bits (bits 3:0)
        value = uartP->MSR;
    }

    // Enable FIFOs, reset FIFOs and set interrupt triger level
    if ((cfgP->maskFIFO & FCR_TRFIFOE) != 0)
    {        
        // Enable FIFOs	and reset FIFOs and set interrupt trigger level
        uartP->FCR = cfgP->maskFIFO | FCR_TRFIFOE| FCR_RESETRF| FCR_RESETTF;
    }
    else
    {
        // non-FIFO mode
        uartP->FCR = FCR_TRFIFOD;
    }

    // Configure IRDA operation
    // This must be done before enabling STUART
    if (cfgP->maskIRDA != 0)
    {
        // Enable SIR receiver    
        uartP->ISR = cfgP->maskIRDA | ISR_RCVEIR;
    }
    
    // Configure UART to operate via programmed I/O or DMA
    if ((cfgP->maskInt & IER_DMAE) == 0)
    { 
        // Register an interrupt handler if required

        // Disable DMA, configure NRZ, IRQ, and enable UART unit
        // Just make sure that DMA is disabled and UART is enabled
        uartP->IER = (cfgP->maskInt & ~IER_DMAE | IER_UUE);
    }
    else if ((cfgP->maskFIFO & FCR_TRFIFOE) && (cfgP->maskInt & IER_DMAE))
    {    
        // Setup the DMA channels

        // Enable DMA and enable UART unit
        uartP->IER = (cfgP->maskInt | IER_UUE);
    }

    return 0;
}
#ifdef temp
static
void enableReceivIR(UartContextT * ctxP, INT param)
{
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;

    // Disable UART unit
    uartP->IER &= ~IER_UUE;

    if (param == 0)
    {    
        // Disable SIR receiver
        uartP->ISR &= ~ISR_RCVEIR;
    }        
    else
    {    
        // Enable SIR receiver
        uartP->ISR |= ISR_RCVEIR;
    }
            
    // Enable UART unit
    uartP->IER |= IER_UUE;
}
#endif

static
void enableTransmIR(UartContextT * ctxP, INT param)
{
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;

    (void) XsGpioSetAlternateFunction ( XS_GPIO_ID_ICPSTUART_TX, XS_GPIO_ALT_FUNC_GPIO);
	(void) XsGpioSetDirection (XS_GPIO_ID_ICPSTUART_TX, XS_GPIO_DIR_IN, XS_GPIO_PIN_LEVEL_0);

    // Disable UART unit
    uartP->IER &= ~IER_UUE;

    if (param == 0)
    {    
        // Disable SIR transmitter
        uartP->ISR &= ~ISR_XMITIR;
        // Enable SIR receiver
        uartP->ISR |= ISR_RCVEIR;
    }        
    else
    {    
        // Disable SIR receiver
        uartP->ISR &= ~ISR_RCVEIR;
        // Enable SIR transmitter
        uartP->ISR |= ISR_XMITIR;
    }
            
    // Enable UART unit
    uartP->IER |= IER_UUE;

	(void) XsGpioSetDirection (XS_GPIO_ID_ICPSTUART_TX, XS_GPIO_DIR_OUT, XS_GPIO_PIN_LEVEL_0);
    (void) XsGpioSetAlternateFunction ( XS_GPIO_ID_ICPSTUART_TX, XS_GPIO_ALT_FUNC_1);
}



/*
*******************************************************************************
*
* FUNCTION:         loopbackSTUart                  
*
* DESCRIPTION:      This function is used to test UART in polled mode operation.
*                   The function checks if loopback test mode is enabled.
*                   It disables infrared operation, sends and receives a single 
*                   byte via internal loopback.
*
* 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 loopbackSTUart(UartContextT * ctxP, INT data)
{
    volatile UartRegsT * uartP = (UartRegsT *)ctxP->regsP;
    INT retry = RETRY;

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

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

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

    if (retry > 0)
        return uartP->UDATA;

    return (-1);
}

/*
*******************************************************************************
*
* FUNCTION:         writeSTUart                      
*
* 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.
*

⌨️ 快捷键说明

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