📄 serial.c
字号:
/* The content of this file or document is CONFIDENTIAL and PROPRIETARY
* to Jade Technologies Co., Ltd. It is subjected to the terms of a
* License Agreement between Licensee and Jade Technologies Co., Ltd.
* restricting among other things, the use, reproduction, distribution
* and transfer. Each of the embodiments, including this information
* and any derivative work shall retain this copyright notice.
*
* Copyright (c) 2004 - 2005 Jade Technologies Co., Ltd.
* All rights reserved.
* ----------------------------------------------------------------
* File: serial.c,v
* Revision: 1.0
* ----------------------------------------------------------------
* $
*
* @doc EXTERNAL OEM
*
* @module debug.c | OEM Debug Serial Monitor Routines
*
*/
#include <windows.h>
#include "win_plat.h"
#include "PL011.h"
/* Common serial port initialisation code */
static void setupSerial(ULONG pRegBase)
{
ULONG value;
/*
//close pin_multi/UART2
//GPIO5_6/7
WRITE_REGISTER_ULONG(0x2002C420, 0x000000C0);
//GPIO6_6/7
WRITE_REGISTER_ULONG(0x2002D420, 0x000000C0);
*/
//close pin_multi/UART0
//GPIO4_0/1
if (pRegBase == 0x20030000)
{
WRITE_REGISTER_ULONG(PHYS_GPIO4_BASE + 0x420, 0x0000000F);
}else{
WRITE_REGISTER_ULONG(VA_GPIO4_BASE + 0x420, 0x0000000F);
}
/* First we disable the UART before changing its settings */
/* Read the current Control register value */
value = READ_REGISTER_ULONG(pRegBase + PL011_CR);
/* Disable the UART */
value = value & ~PL011_CR_UARTEN;
WRITE_REGISTER_ULONG(pRegBase + PL011_CR, value);
/* Set baud rate */
/* Write out integer baud part first */
WRITE_REGISTER_ULONG(pRegBase + PL011_IBRD,
(ARMVPB_BAUD_38400 >> BAUD_BRDI_SHIFTRIGHT));
/* Now write out fractional baud part */
WRITE_REGISTER_ULONG(pRegBase + PL011_FBRD,
(ARMVPB_BAUD_38400 & BAUD_BRDF_MASK));
/* set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
value = PL011_LCR_H_WLEN_8 | PL011_LCR_H_FEN;
WRITE_REGISTER_ULONG(pRegBase + PL011_LCR_H, value);
/* Set the UART to no h/w flow control, transmit and receive enabled,
* assert RTS and DTR
*/
value = PL011_CR_RTS | PL011_CR_DTR | PL011_CR_RXE | PL011_CR_TXE;
WRITE_REGISTER_ULONG(pRegBase + PL011_CR, value);
/* Ensure all interrupts are masked off */
WRITE_REGISTER_ULONG(pRegBase + PL011_IMSC, 0);
/* All DMA configuration disabled */
WRITE_REGISTER_ULONG(pRegBase + PL011_DMACR, 0);
/* Clear any errors */
WRITE_REGISTER_ULONG(pRegBase + PL011_ECR, PL011_ECR_ALLERRORS);
/* Finally, enable the uart */
value = READ_REGISTER_ULONG(pRegBase + PL011_CR);
value = value | PL011_CR_UARTEN;
WRITE_REGISTER_ULONG(pRegBase + PL011_CR, value);
}
void InitSerial(ULONG pRegBase)
{
setupSerial(pRegBase);
}
static void sendByte(DWORD port, UCHAR ch)
{
/* Wait until the flag register indicates the UART has room for another
* character
*/
while ((READ_REGISTER_ULONG(port + PL011_FR) & PL011_FR_TXFF) != 0)
;
if (ch == '\n')
{
WRITE_REGISTER_ULONG(port + PL011_DR, '\r');
/* Wait until the flag register indicates the UART has room for
* another character
*/
while ((READ_REGISTER_ULONG(port + PL011_FR) & PL011_FR_TXFF) != 0)
;
}
WRITE_REGISTER_ULONG(port + PL011_DR, ch);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Pre MMU serial setup versions
void ARMInitSerial(void)
{
setupSerial(DEBUG_COMPORT);
}
void ARMWriteByte(UCHAR ch)
{
sendByte(DEBUG_COMPORT, ch);
}
void ARMWriteString(unsigned char *str)
{
/* Send message to serial port */
while (*str)
ARMWriteByte(*str++);
}
void ARMPutHex(long value)
{
unsigned char c;
int i;
ARMWriteByte('0');
ARMWriteByte('x');
for (i = 7; i >= 0; i--)
{
/* Strip next nibble */
c = (unsigned char)((value >> (i * 4)) & 0xf);
if ((c >= 0) && (c <= 9))
c = c + '0';
else
c = c - 10 + 'a';
ARMWriteByte(c);
}
}
// End of pre MMU serial setup versions
/////////////////////////////////////////////////////////////////////////////////////////
void WriteByte(UCHAR ch)
{
sendByte(VA_DEBUG_COMPORT, ch);
}
void WriteString(unsigned char *str)
{
/* Send message to serial port */
while (*str)
WriteByte(*str++);
}
void PutHex(long value)
{
unsigned char c;
int i;
WriteByte('0');
WriteByte('x');
for (i = 7; i >= 0; i--)
{
/* Strip next nibble */
c = (unsigned char)((value >> (i * 4)) & 0xf);
if ((c >= 0) && (c <= 9))
c = c + '0';
else
c = c - 10 + 'a';
WriteByte(c);
}
WriteByte(' ');
}
void WriteHex(long *ptr, int length)
{
int count;
for (count = 0; count < length; ++count)
{
if ((count & 7) == 0)
{
WriteByte('\r');
WriteByte('\n');
PutHex((long)ptr);
WriteByte(':');
}
WriteByte(' ');
PutHex(*ptr++);
}
WriteByte('\r');
WriteByte('\n');
}
/* EOF serial.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -