📄 serial.c
字号:
/*++
Copyright (c) 1998-2001 Klaus P. Gerlicher
Module Name:
serial.c
Abstract:
serial debugger connection
Environment:
LINUX 2.2.X
Kernel mode only
Author:
Klaus P. Gerlicher
Revision History:
19-Aug-2000: created
15-Nov-2000: general cleanup of source files
Copyright notice:
This file may be distributed under the terms of the GNU Public License.
--*/
#include "remods.h"
#include "precomp.h"
#include "serial_port.h"
BOOLEAN SerialReadByte(PUCHAR px);
// used for SERIAL window creation
// NB: at the moment the terminal is 60 lines high.
WINDOW wWindowSerial[4]=
{
{1,3,1,0,FALSE},
{5,8,1,0,FALSE},
{14,26,1,0,FALSE},
{41,18,1,0,FALSE}
};
PUCHAR pScreenBufferSerial;
USHORT usSerialPortBase;
UCHAR packet[_PAGE_SIZE];
UCHAR assemble_packet[_PAGE_SIZE];
UCHAR flush_buffer[_PAGE_SIZE],g_x,g_y;
ULONG ulFlushBufferPos = 0;
UCHAR ucLastKeyRead;
ECOLORS eForegroundColor=WHITE,eBackgroundColor=BLACK;
///************************************************************************
// SerialSetSpeed()
//
///************************************************************************
void SerialSetSpeed(ULONG baudrate)
{
UCHAR c;
ULONG divisor;
divisor = (ULONG) (115200L/baudrate);
c = inportb((USHORT)(usSerialPortBase + LCR));
outportb((USHORT)(usSerialPortBase + LCR), (UCHAR)(c | 0x80)); // Set DLAB
outportb((USHORT)(usSerialPortBase + DLL), (UCHAR)(divisor & 0x00FF));
outportb((USHORT)(usSerialPortBase + DLH), (UCHAR)((divisor >> 8) & 0x00FF));
outportb((USHORT)(usSerialPortBase + LCR), c); // Reset DLAB
}
///************************************************************************
// SerialSetOthers()
//
// Set other communications parameters
//************************************************************************
void SerialSetOthers(ULONG Parity, ULONG Bits, ULONG StopBit)
{
ULONG setting;
UCHAR c;
if (usSerialPortBase == 0) return ;
if (Bits < 5 || Bits > 8) return ;
if (StopBit != 1 && StopBit != 2) return ;
if (Parity != NO_PARITY && Parity != ODD_PARITY && Parity != EVEN_PARITY)
return;
setting = Bits-5;
setting |= ((StopBit == 1) ? 0x00 : 0x04);
setting |= Parity;
c = inportb((USHORT)(usSerialPortBase + LCR));
outportb((USHORT)(usSerialPortBase + LCR), (UCHAR)(c & ~0x80)); // Reset DLAB
// no ints
outportb((USHORT)(usSerialPortBase + IER), (UCHAR)0);
// clear FIFO and disable them
outportb((USHORT)(usSerialPortBase + FCR), (UCHAR)0);
outportb((USHORT)(usSerialPortBase + LCR), (UCHAR)setting);
outportb((USHORT)(usSerialPortBase + MCR), DTR | RTS);
return ;
}
///************************************************************************
// FlushSerialBuffer()
//
///************************************************************************
void FlushSerialBuffer(void)
{
UCHAR c;
while(SerialReadByte(&c));
}
///************************************************************************
// SetupSerial()
//
///************************************************************************
void SetupSerial(ULONG port,ULONG baudrate)
{
USHORT ports[]={COM1BASE,COM2BASE,COM3BASE,COM4BASE};
usSerialPortBase = ports[port-1];
SerialSetOthers(NO_PARITY,8,1);
SerialSetSpeed(baudrate);
// clear out received bytes
// else we would think there's a terminal connected
FlushSerialBuffer();
}
///************************************************************************
// SerialReadByte()
//
// Output a character to the serial port
//************************************************************************
BOOLEAN SerialReadByte(PUCHAR px)
{
ULONG timeout;
timeout = 0x00FFFFL;
// Wait for transmitter to clear
while ((inportb((USHORT)(usSerialPortBase + LSR)) & RCVRDY) == 0)
if (!(--timeout))
{
return FALSE;
}
*px = inportb((USHORT)(usSerialPortBase + RXR));
return TRUE;
}
///************************************************************************
// SerialSendByte()
//
// Output a character to the serial port
//************************************************************************
BOOLEAN SerialSendByte(UCHAR x)
{
ULONG timeout;
timeout = 0x00FFFFL;
// Wait for transmitter to clear
while ((inportb((USHORT)(usSerialPortBase + LSR)) & XMTRDY) == 0)
if (!(--timeout))
{
return FALSE;
}
outportb((USHORT)(usSerialPortBase + TXR), x);
return TRUE;
}
//************************************************************************
// CheckSum()
//
//************************************************************************
UCHAR CheckSum(LPSTR p,ULONG Len)
{
UCHAR ucCheckSum = 0;
ULONG i;
for(i=0;i<Len;i++)
{
ucCheckSum ^= *p++;
ucCheckSum += 1;
}
return ucCheckSum;
}
///************************************************************************
// ReadPacket()
//
///************************************************************************
BOOLEAN ReadPacket(PSERIAL_PACKET p)
{
return TRUE;
}
///************************************************************************
// SendPacket()
//
///************************************************************************
BOOLEAN SendPacket(PSERIAL_PACKET p)
{
PUCHAR pHeader = (PUCHAR)&p->header;
ULONG i;
UCHAR c;
ULONG timeout;
do
{
timeout = 10;
pHeader = (PUCHAR)&p->header;
for(i=0;i<(sizeof(SERIAL_PACKET_HEADER)+p->header.packet_size);i++)
{
if(!SerialSendByte(*pHeader++))
{
return FALSE;
}
}
do
{
c = 0;
SerialReadByte(&c);
if(c != ACK)
ucLastKeyRead = c;
}while(c != ACK && timeout--);
}while(c != ACK);
return TRUE;
}
///************************************************************************
// SendPacketTimeout()
//
///************************************************************************
BOOLEAN SendPacketTimeout(PSERIAL_PACKET p)
{
PUCHAR pHeader = (PUCHAR)&p->header;
ULONG i;
UCHAR c;
ULONG timeout = 20;
BOOLEAN bResult = TRUE;
pHeader = (PUCHAR)&p->header;
for(i=0;i<(sizeof(SERIAL_PACKET_HEADER)+p->header.packet_size);i++)
{
if(!SerialSendByte(*pHeader++))
{
return FALSE;
}
}
do
{
c = 0xFF;
SerialReadByte(&c);
}while(c != ACK && timeout--);
if(c != ACK)
bResult = FALSE;
return bResult;
}
//************************************************************************
// AssemblePacket()
//
//************************************************************************
PSERIAL_PACKET AssemblePacket(PUCHAR pData,ULONG ulSize)
{
PSERIAL_PACKET p;
ULONG ulCheckSum;
p = (PSERIAL_PACKET)assemble_packet;
// fill in header
p->header.packet_chksum = CheckSum(pData,ulSize);
p->header.packet_size = ulSize;
p->header.packet_header_chksum = 0;
ulCheckSum = (ULONG)CheckSum((PUCHAR)p,sizeof(SERIAL_PACKET_HEADER));
p->header.packet_header_chksum = ulCheckSum;
// attach data to packet
PICE_memcpy(p->data,pData,ulSize);
return p;
}
// OUTPUT handlers
//*************************************************************************
// SetForegroundColorVga()
//
//*************************************************************************
void SetForegroundColorSerial(ECOLORS col)
{
eForegroundColor = col;
}
//*************************************************************************
// SetBackgroundColorVga()
//
//*************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -