serial.c

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C语言 代码 · 共 194 行

C
194
字号
/* -*-C-*-
 *
 * $Revision: 1.3 $
 *   $Author: kwelton $
 *     $Date: 2000/09/07 18:00:19 $
 *
 *  Copyright (c) 1995 Microsoft Corporation
 *
 *   @doc EXTERNAL OEM
 *
 *   @module debug.c | OEM Debug Serial Monitor Routines
 *
 */

#include <windows.h>
#include "win_plat.h"

extern void WhereAmI(void);
extern void ShowValue(int value);
extern void outb(BYTE value, void *address);
extern DWORD inb(void *address);

// Common Serial port initialisation code
static void setupSerial(DWORD port)
{
    DWORD value;

    /* first, disable everything */
    IO_WRITE(port + AMBA_UARTCR, 0x0);

    /* Set baud rate */
    value = (DEFAULT_OS_BAUD & 0xf00) >> 8;
    IO_WRITE(port + AMBA_UARTLCR_M, value);
    IO_WRITE(port + AMBA_UARTLCR_L, (DEFAULT_OS_BAUD & 0xff) );

    /*
     * NOTE: MUST BE WRITTEN LAST (AFTER UARTLCR_M & UARTLCR_L)
     *
     * set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled
     */
    value = AMBA_UARTLCR_H_WLEN_8 | AMBA_UARTLCR_H_FEN;
    IO_WRITE(port + AMBA_UARTLCR_H, value);

    /* finally, enable the uart */
    IO_WRITE(port + AMBA_UARTCR, AMBA_UARTCR_UARTEN);
}

/*
 *   @func   void    |   OEMInitDebugSerial | Initialize debug monitor port.
 *
 *   NOTE: This function MUST NOT use any global variables!!!!!!
 */
void InitSerial(DWORD port)
{
    setupSerial(port);
}

// Pre MMU serial setup version..
void ARMInitSerial(void)
{
    setupSerial(OS_COMPORT);
}

static void sendByte(DWORD port, UCHAR ch)
{
    int status;

    do
    {
        status = GET_STATUS(port);
    } while(!TX_READY(status));

    if (ch == '\n')
    {
        PUT_CHAR(port, '\r');

        do
        {
            status = GET_STATUS(port);
        } while(!TX_READY(status));
    }

    PUT_CHAR(port, ch);
}

/*
 *   @func   void    |   OEMWriteDebugByte | Output byte to the monitor port.
 *
 *   @parm   unsigned char *| str |
 *           Points to the output buffer.
 */
void WriteByte(UCHAR ch)
{
    DWORD port;

    port = (OS_COMPORT == INTEGRATOR_UART0_BASE) ?
        VA_UART0_BASE : VA_UART1_BASE;

    sendByte(port, ch);
}

// Pre MMU serial write version..
void ARMWriteByte(UCHAR ch)
{
    sendByte(OS_COMPORT, ch);
}

/*
 *   @func   void | OEMWriteDebugString | Display string to the monitor port.
 *
 *   @parm   unsigned short * | str |
 *           Points to the receiving buffer.
 */
void WriteString(unsigned char *str)
{
    // Send message to serial port
    while (*str)
        WriteByte(*str++);
}

// Pre-MMU active serial string write
void ARMWriteString(unsigned char *str)
{
    // Send message to serial port
    while (*str)
        ARMWriteByte(*str++);
}

void PutHex(long value)
{
    unsigned char c;
    int i;

    WriteByte('0');
    WriteByte('x');
    for (i = 7; i >= 0; i--)
    {
        // Strip next nibble
        c = (value >> (i * 4)) & 0xf;

        if ((c >= 0) && (c <= 9))
            c = c + '0';
        else
            c = c - 10 + 'a';
        WriteByte(c);
    }

    WriteByte(' ');
}

void ARMPutHex(long value)
{
    unsigned char c;
    int i;

    ARMWriteByte('0');
    ARMWriteByte('x');

    for (i = 7; i >= 0; i--)
    {
        // Strip next nibble
        c = (value >> (i * 4)) & 0xf;
        if ((c >= 0) && (c <= 9))
            c = c + '0';
        else
            c = c - 10 + 'a';

        ARMWriteByte(c);
    }
}

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 + =
减小字号Ctrl + -
显示快捷键?