📄 dbgserial.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//------------------------------------------------------------------------------
//
// File: dbgserial.c
//
// This module is provides the interface to the serial port.
//
#include <bsp.h>
#include <nkintr.h>
//------------------------------------------------------------------------------
// Defines
//
#define DEBUG_BAUD_38400 0x00000018
#define DEBUG_BAUD_115200 0x00000008
//------------------------------------------------------------------------------
// Externs
//
//------------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------------
// Local Variables
//
static volatile BULVERDE_UART_REG *g_pDebugUARTPort = NULL;
static volatile UINT16 *g_pDbgLED = NULL;
//------------------------------------------------------------------------------
// Local Functions
//
//------------------------------------------------------------------------------
//
// Function: OEMInitDebugSerial
//
// Initializes the debug serial port
//
VOID InitDebugSerial(UINT32 DbgSerPhysAddr)
{
UINT32 logMask = 0;
volatile BULVERDE_GPIO_REG *pGPIO_REGS = NULL;
volatile BULVERDE_CLKMGR_REG *pCLKMGR_REGS = NULL;
#ifdef DEBUG
// At this moment we must suppress logging.
//
logMask = dpCurSettings.ulZoneMask| 0x80000001;
dpCurSettings.ulZoneMask = 0;
#endif
g_pDebugUARTPort = (volatile BULVERDE_UART_REG *) OALPAtoVA(DbgSerPhysAddr, FALSE);
pGPIO_REGS = (volatile BULVERDE_GPIO_REG *) OALPAtoVA(BULVERDE_BASE_REG_PA_GPIO, FALSE);
pCLKMGR_REGS = (volatile BULVERDE_CLKMGR_REG *) OALPAtoVA(BULVERDE_BASE_REG_PA_CLKMGR, FALSE);
g_pDbgLED = (volatile UINT16 *) OALPAtoVA(PLATO_BASE_REG_PA_SMSC_ETHERNET, FALSE);
if (g_pDbgLED)
{
g_pDbgLED += 8;
}
// Ensure that UART interrupts are turned off.
//
g_pDebugUARTPort->lcr = 0x0; // Clear DLAB.
g_pDebugUARTPort->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.
//
g_pDebugUARTPort->lcr = 0x80; // Access Divisor.
g_pDebugUARTPort->thr_rbr_dll = DEBUG_BAUD_38400; // Low byte divisor.
g_pDebugUARTPort->ier_dlh = 0x00; // High byte divisor.
g_pDebugUARTPort->lcr = 0x0; // Clear DLAB.
//Setting UART properties to 8N1
//
g_pDebugUARTPort->lcr = 0x3; // 8 bits, 1 stop, no parity. Also LCR DLAB bit = 0.
g_pDebugUARTPort->iir_fcr = 0x01; // Enable the FIFO.
g_pDebugUARTPort->iir_fcr = 0x07; // Clear Rx,Tx FIFOs.
// Don't enable UART interrupts - we'll poll for the data.
//
g_pDebugUARTPort->ier_dlh = 0x0;
// Ensure loop-back test mode is off even though MCR reset value is 0x0.
//
g_pDebugUARTPort->mcr = 0x0; // UART is in normal mode.
// Configure GPIO pins for FFUART
//
if (DbgSerPhysAddr == BULVERDE_BASE_REG_PA_FFUART)
{
// Initialize GPIO pins.
// For Plato, it only uses pin 34(in:RXD), 39(out:TXD), 41(out:RTS).
// Write 0 on GPIO pins 39,41 before configuring it as output.
//
pGPIO_REGS->GPCR1 = (XLLP_GPIO_BIT_FFTXD | XLLP_GPIO_BIT_FFRTS);
// Configure direction of GPIO pins 34 as input
// and GPIO pins 39,41 as output
//
pGPIO_REGS->GPDR1 &= ~XLLP_GPIO_BIT_FFRXD;
pGPIO_REGS->GPDR1 |= XLLP_GPIO_BIT_FFTXD | XLLP_GPIO_BIT_FFRTS;
// Configure GPIO pin 34 for Alt_fn1. And pins 39 and 41 for Alt_fn2.
//
pGPIO_REGS->GAFR1_L |= ( XLLP_GPIO_AF_BIT_FFRXD | XLLP_GPIO_AF_BIT_FFTXD | XLLP_GPIO_AF_BIT_FFRTS );
// Enable the FFUART clock.
//
pCLKMGR_REGS->cken |= XLLP_CLKEN_FFUART ;
}
else if (DbgSerPhysAddr == BULVERDE_BASE_REG_PA_BTUART)
{
// Initialize GPIO pins.
// Write 0 on GPIO pins 43 and 45 before configuring them as outputs.
//
pGPIO_REGS->GPCR1 = (XLLP_GPIO_BIT_BTTXD | XLLP_GPIO_BIT_BTRTS);
// Configure direction of GPIO pins 42 and 44 as input
// and GPIO pins 43 and 45 as output.
//
pGPIO_REGS->GPDR1 &= ~( XLLP_GPIO_BIT_BTRXD | XLLP_GPIO_BIT_BTCTS);
pGPIO_REGS->GPDR1 |= ( XLLP_GPIO_BIT_BTTXD | XLLP_GPIO_BIT_BTRTS);
// Configure GPIO pins 42 and 44 for Alt_fn1. And pins 43 and 45 for Alt_fn2.
//
pGPIO_REGS->GAFR1_L |= ( XLLP_GPIO_AF_BIT_BTRXD | XLLP_GPIO_AF_BIT_BTCTS |
XLLP_GPIO_AF_BIT_BTTXD | XLLP_GPIO_AF_BIT_BTRTS );
// Enable the BTUART clock.
//
pCLKMGR_REGS->cken |= XLLP_CLKEN_BTUART ;
}
// Enable the UART.
//
g_pDebugUARTPort->ier_dlh = 0x40;
#ifdef DEBUG
// Restore the logging mask.
//
dpCurSettings.ulZoneMask = logMask;
#endif
}
//------------------------------------------------------------------------------
//
// Function: OEMWriteDebugByte
//
// Transmits a character out the debug serial port.
//
VOID OEMWriteDebugByte(UINT8 ch)
{
if (!g_pDebugUARTPort)
{
return;
}
// Spin if FIFO has more than half data.
//
while(!(g_pDebugUARTPort->lsr & 0x020));
// Write a character byte to the FIFO.
//
g_pDebugUARTPort->thr_rbr_dll = (unsigned char)ch;
}
//------------------------------------------------------------------------------
//
// Function: OEMReadDebugByte
//
// Reads a byte from the debug serial port. Does not wait for a character.
// If a character is not available function returns "OEM_DEBUG_READ_NODATA".
//
int OEMReadDebugByte()
{
UINT32 data = OEM_DEBUG_READ_NODATA;
UINT32 LSR;
if (!g_pDebugUARTPort)
{
return(data);
}
// Read LSR.
//
LSR = g_pDebugUARTPort->lsr;
// Return if no data.
//
if(!(LSR & 0x1))
{
return(data);
}
// Read data.
//
data = g_pDebugUARTPort->thr_rbr_dll;
// Signal error if PE or FE was set.
// Do nothing if BI or OE was set.
//
if(LSR & 0x0c)
{
data = OEM_DEBUG_COM_ERROR;
}
return(data);
}
//------------------------------------------------------------------------------
//
// Function: OEMWriteDebugLED
//
// Writes a pattern to the on-board hex LEDs.
//
void OEMWriteDebugLED(UINT16 Index, DWORD Pattern)
{
if (g_pDbgLED)
{
*(volatile UINT16 *)g_pDbgLED = (UINT16)Pattern;
}
}
//------------------------------------------------------------------------------
//
// Function: OEMClearDebugCommError
//
// Clears communications errors (flushes the serial FIFO).
//
void OEMClearDebugCommError(void)
{
while(OEMReadDebugByte() == OEM_DEBUG_COM_ERROR);
}
//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -