📄 serial.h
字号:
//------------------------------------------------------------
// Data definitions for Veturcom RTX serial driver
//------------------------------------------------------------
#define DISABLE_INTERRUPTS _asm cli
#define ENABLE_INTERRUPTS _asm sti
#define RING_BUFFER_SIZE 8192 //default size of a ring buffer
#define COM1_PORT (PUCHAR(0x3f8)) //port address for COM1
#define COM2_PORT (PUCHAR(0x2f8)) //port address for COM1
#define COM_IO_RANGE 8 // 8 registers,including scratch
#define COM_MAX_PORTS 2 //support only COM1 and COM2
#define COM1_IRQ 0x4 //COM1 interrupt request level
#define COM2_IRQ 0x3 //COM2 interrupt request level
//-------------------------------------------------
// define COM port states
//-------------------------------------------------
#define COM_STATE_IDLE 0
#define COM_STATE_OPEN 1
//-------------------------------------------------
// define register offsets
//-------------------------------------------------
#define UART_XMIT_BUFFER 0 //transmit holding buffer
#define UART_RCVR_BUFFER 0 //receiver buffer
#define UART_DIV_LATCH_LOW 0 //divsor latch low byte when DLAB = 1
#define UART_IER 1 //interrupt enable register
#define UART_DIV_LATCH_HIGH 1 //divsor latch high byte when DLAB = 1
#define UART_IIR 2 //interrupt identification when read
#define UART_FCR 2 //FIFO control when written
#define UART_LCR 3 //line control
#define UART_MCR 4 //modem control
#define UART_LSR 5 //line status
#define UART_MSR 6 //modem status
#define UART_SCRATCH 7 //the scratch register
#define UART_ADDRESS_SPACE 8 //16550 address space in bytes
//---------------------------------------------------
// define UART interrupt types
//---------------------------------------------------
#define IIR_MODEM_STATUS 0x00
#define IIR_NO_INT_PENDING 0x01
#define IIR_XMIT_HOLDING_EMPTY 0x02
#define IIR_RECEIVED_DATA_READY 0x04
#define IIR_RECEIVER_LINE_STATUS 0x06
#define IIR_RECEIVE_DATA_TIMEOUT 0x0c
#define IIR_MASK 0xff
//-----------------------------------------------------
// define bit masks for LSR
//-----------------------------------------------------
#define LSR_DATA_READY 0x01
#define LSR_OVERRUN_ERROR 0x02
#define LSR_PARITY_ERROR 0x04
#define LSR_FRAMING_ERROR 0x08
#define LSR_BREAK 0x10
#define LSR_EMPTY_XMIT_HOLDING_REG 0x20
#define LSR_TRANSMITTER_EMPTY 0x40
#define LSR_FIFO_ERROR 0x80
//-----------------------------------------------------
// Define bit Mask for LCR
//-----------------------------------------------------
#define LCR_PARITY_NONE 0x00
#define LCR_PARITY_ODD 0x08
#define LCR_PARITY_EVEN 0x18
#define LCR_PARITY_HIGH 0x28
#define LCR_PARITY_LOW 0x38
#define LCR_STOPBITS_1 0x00
#define LCR_STOPBITS_2 0x04
#define LCR_WORDSIZE_5 0x00
#define LCR_WORDSIZE_6 0x01
#define LCR_WORDSIZE_7 0x02
#define LCR_WORDSIZE_8 0x03
#define LCR_DIVISOR_LATCH 0x80
//--------------------------------------------------------
// define IER bit masks
//--------------------------------------------------------
#define IER_RECEIVE_DATA 0x01
#define IER_TRANSMIT_DATA 0x02
#define IER_LINE_STATUS 0x04
#define IER_MODEM_STATUS 0x08
//-------------------------------------------------------
// FCR bit masks
//-------------------------------------------------------
#define FCR_TRIGGER_1 0x07
#define FCR_TRIGGER_4 0x47
#define FCR_TRIGGER_8 0x87
#define FCR_TRIGGER_14 0xC7
#define FCR_FIFO_DISABLED 0x00
//-------------------------------------------------------
// MCR bit mask
//-------------------------------------------------------
#define MCR_DTR 0x01
#define MCR_RTS 0x02
#define MCR_OUT1 0x04
#define MCR_OUT2 0x08
#define MCR_LOOPBACK 0x10
//--------------------------------------------------------
// define UART initialization defaults
//--------------------------------------------------------
#define DEFAULT_BAUD_RATE COM_BAUD_RATE_9600
#define DEFAULT_PARITY LCR_PARITY_NONE
#define DEFAULT_WORDSIZE LCR_WORDSIZE_8
#define DEFAULT_STOP_BITS LCR_STOPBITS_1
#define DEFAULT_FIFO_MASK FCR_TRIGGER_14
#define DEFAULT_FIFO_SIZE 14
//-----------------------------------------------
// define a ring buffer and a stat block
//-----------------------------------------------
typedef struct
{
BYTE buffer[RING_BUFFER_SIZE]; //pointer to a character buffer
WORD count; //count of characters in buffer
WORD nextSlotOut; //pointer to next input slot
WORD nextSlotIn; //pointer to last character read
}RINGBUFFER;
typedef struct
{
DWORD bytesRead; //total bytes read
DWORD bytesWritten; //total bytes written
DWORD readRequests; //number of read requests
DWORD writeRequests; //number of write requests
DWORD rcvInts; //receive interrupts
DWORD xmtInts; //transmit interupts
DWORD secondaryInts; //secondary interrupts
}COMSTATS;
//-----------------------------------------------
// define a UART Control Block
//-----------------------------------------------
typedef struct
{
WORD port; //port ID 0 = com1 1 = com2 etc
PUCHAR baseAddress; //base port address
HANDLE isrHandle; //handle to Rt interrupt routine
WORD intVector; //interruptVector
WORD irq; //IRQ level
RINGBUFFER *inBuffer; //pointer to input ringbuffer
RINGBUFFER *outBuffer; //pointer to output ringbuffer
BYTE fifoMask; //fifo register mask
BYTE fifoSize; //size of FIFO..default = 14
BYTE parity; //parity odd/even/none
BYTE stopBits; //0 1 or 2 stop bits
BYTE receiveISRActive:1; //receive interrupt pending
BYTE transmitISRActive:1; //transmit Interrupt pending
BYTE timeDelimitedRcv:1; //time delimited receive active
BYTE statsActive:1; //com statistics active
BYTE spare:4; //fill out the byte
BYTE state; //uart state
BYTE wordSize; //word size..5 - 8 bits
BYTE baudRate; //configured baud rate
BYTE flowControl; //flow control
DWORD rcvTimer; //receiver timeout
WORD lastError; //last reported error
WORD errorCount; //count of errors
}UCB;
//----------------------------------------------------------------
// define a structure for a baud rate table
//----------------------------------------------------------------
typedef struct
{
DWORD rate; //the actual rate
BYTE DLHigh; //divisor latch high
BYTE DLLow; //divisor latch low
}BAUDRATE;
//-----------------------------
// define a fifo structure
//-----------------------------
typedef struct
{
BYTE size;
BYTE mask;
}FIFO;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -