?? uart.c
字號:
// Copyright (c) David Vescovi. All rights reserved.
// Part of Project DrumStix
// Windows Embedded Developers Interest Group (WE-DIG) community project.
// http://www.we-dig.org
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//
// File: uart.c
//
//------------------------------------------------------------------------------
#include <bsp.h>
static volatile UART_REG_T *pUARTRegs;
//------------------------------------------------------------------------------
//
// Function: UARTInit
//
// Initialize kitl serial port
//
//------------------------------------------------------------------------------
BOOL UARTInit(KITL_SERIAL_INFO *pInfo)
{
BOOL rc = FALSE;
pUARTRegs = (volatile UART_REG_T *)pInfo->pAddress;
// Check if config paramters are supportable
if (
pInfo->baudRate < 9600 || pInfo->baudRate > 115200 ||
pInfo->dataBits != 8 || pInfo->stopBits == 0 || pInfo->parity > 2
) goto cleanUp;
// Ensure that UART interrupts are turned off.
//
pUARTRegs->LCR = 0x0; // Clear DLAB.
pUARTRegs->IER_DLH = 0x0; // IER_DLH = 0x0.
// Set the Baud Rate.
// The divisor latches are at offsets 0 and 1, which are
// receive/transmit data and ier registers.
//
pUARTRegs->LCR = 0x80; // Access Divisor.
pUARTRegs->THR_RBR_DLL = (14745600/pInfo->baudRate)/16; // Low byte divisor.
pUARTRegs->IER_DLH = 0x00; // High byte divisor.
pUARTRegs->LCR = 0x0; // Clear DLAB.
//Setting UART properties to 8N1
//
pUARTRegs->LCR = 0x3; // 8 bits, 1 stop, no parity. Also LCR DLAB bit = 0.
pUARTRegs->IIR_FCR = 0x01; // Enable the FIFO.
pUARTRegs->IIR_FCR = 0x07; // Clear Rx,Tx FIFOs.
// Don't enable UART interrupts - we'll poll for the data.
//
pUARTRegs->IER_DLH = 0x0;
// Ensure loop-back test mode is off even though MCR reset value is 0x0.
//
pUARTRegs->MCR = 0x0; // UART is in normal mode.
//UART in auto handshake mode ... if it supports it
// Enable the UART.
//
pUARTRegs->IER_DLH = 0x40;
pInfo->bestSize = 1; // read it one by one
// Done
rc = TRUE;
cleanUp:
return rc;
}
//------------------------------------------------------------------------------
//
// Function: UARTSend
//
//------------------------------------------------------------------------------
UINT16 UARTSend(UINT8 *pData, UINT16 size)
{
// This should not happen, but to be sure
if (size == 0) return 0;
// Spin if FIFO has more than half data.
//
while(!(pUARTRegs->LSR & UART_LSR_TDRQ));
// Write a character byte to the FIFO.
//
pUARTRegs->THR_RBR_DLL = *pData;
// We send only one char per call
return 1;
}
//------------------------------------------------------------------------------
//
// Function: UARTFlowControl
//
//------------------------------------------------------------------------------
VOID UARTFlowControl(BOOL fOn)
{
UINT32 uCtrl = (pUARTRegs->MCR & ~UART_MCR_RTS);
pUARTRegs->MCR = (uCtrl | (fOn ? UART_MCR_RTS : 0));
if (fOn) {
// clear interrupts, if applicable
uCtrl = pUARTRegs->IIR_FCR;
}
}
//------------------------------------------------------------------------------
//
// Function: UARTRecv
//
//------------------------------------------------------------------------------
UINT16 UARTRecv(UINT8 *pData, UINT16 size)
{
UINT32 lsr;
UINT16 count = 0;
count = 0;
// Read LSR.
lsr = pUARTRegs->LSR;
if(lsr & UART_LSR_DR) {
if (lsr & (UART_LSR_PE | UART_LSR_FE | UART_LSR_OE)) {
KITL_RETAILMSG(ZONE_INIT, ("E\r\n"));
pUARTRegs->IIR_FCR = (UART_FCR_RESETTF | UART_FCR_RESETRF | UART_FCR_TRFIFOE);
} else {
*pData = (UINT8)pUARTRegs->THR_RBR_DLL;
count = 1;
}
}
return count;
}
//------------------------------------------------------------------------------
//
// Function: UARTEnableInts
//
//------------------------------------------------------------------------------
void UARTEnableInts()
{
}
//------------------------------------------------------------------------------
//
// Function: UARTDisableInts
//
//------------------------------------------------------------------------------
void UARTDisableInts()
{
}
//------------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -