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

📄 uart.c

📁 Windows CE 6.0 BSP for the Beagle Board.
💻 C
字号:
//
// Copyright (c) Special Computing.  All rights reserved. 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//------------------------------------------------------------------------------
//
//  File: uart.c
//
//------------------------------------------------------------------------------
#include "bsp.h"


static OMAP3_UART_REGS *g_pUartRegs;

VOID UARTFlowControl(BOOL fOn);
//------------------------------------------------------------------------------
//
//  Function:   UARTInit
//
//  Initialize kitl serial port
//
//------------------------------------------------------------------------------
BOOL UARTInit(KITL_SERIAL_INFO *pInfo)
{
    BOOL rc = FALSE;
	
	g_pUartRegs = (OMAP3_UART_REGS *)pInfo->pAddress;

	// Check if config paramters are supportable
	if (
		pInfo->baudRate < 9600 || //pInfo->baudRate > 115200 ||
		pInfo->dataBits != DATABITS_8 || pInfo->stopBits == STOPBITS_10 || pInfo->parity > PARITY_NONE
	) 
	{
		OALMSG(1, (L"Error: UARTInit parameter!\r\n"));
		goto cleanUp;
	}

    // Reset UART & wait until it completes
    OUTREG8(&g_pUartRegs->SYSC, UART_SYSC_RST);
    while ((INREG8(&g_pUartRegs->SYSS) & UART_SYSS_RST_DONE) == 0);
    
    // Set baud rate
    OUTREG8(&g_pUartRegs->LCR, UART_LCR_DLAB);
	if (pInfo->baudRate > 230400)
		OUTREG8(&g_pUartRegs->DLL, (48000000/pInfo->baudRate)/13);
	else
		OUTREG8(&g_pUartRegs->DLL, (48000000/pInfo->baudRate)/16);		// 1A = 115200

    OUTREG8(&g_pUartRegs->DLH, BSP_UART_DSIUDLH);

    // 8 bit, 1 stop bit, no parity
    OUTREG8(&g_pUartRegs->LCR, BSP_UART_LCR);

    // Enable FIFO
    OUTREG8(&g_pUartRegs->FCR, UART_FCR_FIFO_EN);

    // Pool
    OUTREG8(&g_pUartRegs->IER, 0);

    // Set DTR/RTS signals
    OUTREG8(&g_pUartRegs->MCR, UART_MCR_DTR|UART_MCR_RTS);

    // Configuration complete so select UART clock sampling mode
	if (pInfo->baudRate > 230400)
	    OUTREG8(&g_pUartRegs->MDR1, UART_MDR1_UART13);
	else
	    OUTREG8(&g_pUartRegs->MDR1, UART_MDR1_UART16);

	pInfo->bestSize = UART_FIFO_DEPTH;

	// Done
    rc = TRUE;
    
cleanUp:
    return rc;
}

//------------------------------------------------------------------------------
//
//  Function:  UARTSend
//
//------------------------------------------------------------------------------
UINT16 UARTSend(UINT8 *pData, UINT16 size)
{
	UINT16 count;

    // This should not happen, but to be sure
    if (size == 0) return 0;

	for (count=0;count<size;count++)
	{
		// Wait if FIFO is full
		while ((INREG8(&g_pUartRegs->SSR) & UART_SSR_TX_FIFO_FULL) != 0)
			;
		// Send
		OUTREG8(&g_pUartRegs->THR, *pData);
		pData++;
	}

	// We send only one char per call
	return count;
}


//------------------------------------------------------------------------------
//
//  Function:  UARTFlowControl
//
//------------------------------------------------------------------------------
VOID UARTFlowControl(BOOL fOn)
{
	UINT8 uCtrl = INREG8(&g_pUartRegs->MCR) & ~UART_MCR_RTS;

	OUTREG8(&g_pUartRegs->MCR, (uCtrl | (fOn ? UART_MCR_RTS : 0)));

	if (fOn) {
		// clear interrupts, if applicable
		INREG8(&g_pUartRegs->IIR);
	}
}


//------------------------------------------------------------------------------
//
//  Function:  UARTRecv
//
//------------------------------------------------------------------------------
UINT16 UARTRecv(UINT8 *pData, UINT16 size)
{
    UINT8 status;
    UINT16 count;
    UINT8 ch = OEM_DEBUG_READ_NODATA;

	// read until buffer size is reached or an error occurs
	for (count=0;count<size;count++) 
	{
		status = INREG8(&g_pUartRegs->LSR);
		if ((status & UART_LSR_RX_FIFO_E) != 0)
 		{	// read data
			ch = INREG8(&g_pUartRegs->RHR);
			if ((status & UART_LSR_RX_ERROR) != 0)
			{
				ch = OEM_DEBUG_COM_ERROR;
				OALMSG(1, (L"E %02x\r\n",status));
				INREG8(&g_pUartRegs->RESUME);
				count = 0;
				break;
			}
		}
		else
		{	// no data
			break;
		}
		*pData = ch;
	}
	return count;
}

//------------------------------------------------------------------------------
//
//  Function:  UARTEnableInts
//
//------------------------------------------------------------------------------
void UARTEnableInts()
{
}

//------------------------------------------------------------------------------
//
//  Function:  UARTDisableInts
//
//------------------------------------------------------------------------------
void UARTDisableInts()
{
}

//------------------------------------------------------------------------------

⌨️ 快捷键说明

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