//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: Comm485.h // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Programmable Interrupt Controller (PIC). //---------------------------------------------------------------------------- #ifndef __COMM__485__H__INCLUDED__ #define __COMM__485__H__INCLUDED__ // These are the port addresses of the 8259 // Programmable Interrupt Controller (PIC). #define ICR_REGISTER 0x20 #define IMR_REGISTER 0x21 // An end of interrupt needs to be sent to the Control Port of // the 8259 when a hardware interrupt ends. #define END_OF_INTERRUPT 0x20 #define INTERRUPT_ENABLE 0x08 // Registers of UART 16550A #define THR_REGISTER 0 // Transmit Holding Register, write only #define RBR_REGISTER 0 // Receiver Buffer Register, read omly #define DLL_REGISTER 0 // Divisor Latch LSB, r/w #define DLM_REGISTER 1 // Divisor Latch MSB, r/w #define IER_REGISTER 1 // Interrupt Enable Register, r/w #define IIR_REGISTER 2 // Interrupt Identification Register, read only #define FCR_REGISTER 2 // FIFO Control Register, write only #define LCR_REGISTER 3 // Line Control Register, r/w #define MCR_REGISTER 4 // Modem Control Register, r/w #define LSR_REGISTER 5 // Line Status Register, r/w #define MSR_REGISTER 6 // Modem Status Register, r/w #define SPR_REGISTER 7 // Scratch Pad Register(Is this ever used?), r/w // Interrupt Enable Register, offset 1, r/w // Bits [7:4] = 0, unuse #define IER_EDSSI 0x08 // Enable Delta Status Signals Interrupt #define IER_ELSI 0x04 // Enable Line Status Interrupt #define IER_ETBEI 0x02 // Enable Transmitter Buffer Empty Interrupt #define IER_ERBFI 0x01 // Receiving char, in fifo mode - interrupt on timeout // Interrupt Identification Register, offset 2, read only // Bits [5:4] unuse #define IIR__16550A 0xC0 // fifo mode 16550A #define IIR__16550 0x80 // fifo mode 16550 // Identification interrupts in fifo mode, IIR[3:1] bits #define IIR_TIMEOUT 0x0C//0x06 // To reset - reading RBR_REGISTER #define IIR_BREAK_OR_ERROR 0x06//0x03 // To reset - reading LSR_REGISTER #define IIR_RECEIVER_READY 0x04//0x02 // Received char, to reset - reading RBR_REGISTER #define IIR_TRANSMITER_EMPTY 0x02//0x01 // Holding register empty, to reset - writing THR_REGISTER #define IIR_MODEM_CHANGE_STATUS 0x00 /* Signals CTS, DSR, RI or DCD changes state. To reset - reading MSR_REGISTER */ #define IIR_IID2 0x04 // Interrupt IDentification 2 #define IIR_IID1 0x02 // Interrupt IDentification 1 #define IIR_IID0 0x01 // Interrupt IDentification 0 // Identification interrupts in common mode, IIR[2:1] bits #define IIR_TIMEOUT_COMMON 0x08 #define IIR_PENDING 0x01 // An interrupt is pending if '0' // FIFO Control Register, offset 2, write only // Interrupt Trigger Level, [7:6] bits #define FCR_ITL14 0xC0 // 14 bytes in fifo #define FCR_ITL8 0x80 // 8 bytes in fifo #define FCR_ITL4 0x40 // 4 bytes in fifo #define FCR_ITL1 0x00 // 1 byte, default // Bits [5:4] reserved #define FCR_DMA_ENABLE 0x08 // DMA mode select #define FCR_RESETTF 0x04 // Transmitter FIFO reset #define FCR_RESETRF 0x02 // Receiver FIFO reset #define FCR_FIFO_ENABLE 0x01 // Transmitter and Reciever FIFO Enable // Line Control Register, offset 3, r/w #define LCR_DLAB 0x80 // Divisor Latch Access Bit #define LCR_SBR 0x40 // Set BReak #define LCR_STICPAR 0x20 // Sticky Parity #define LCR_EVENPAR 0x10 // Even Parity Select, if = 0 Odd, = 1 Even #define LCR_PAREN 0x08 // Parity Enable #define LCR_STOPB 0x04 /* Stop Bits, if = 0 is 1 stop bit, = 1 2 stop bits. For 5 bits code 1.5 bits */ #define LCR_SERIALDB8 0x03 // Serial Data Bits #define LCR_SERIALDB7 0x02 #define LCR_SERIALDB6 0x01 #define LCR_SERIALDB5 0x00 // Modem Control Register, offset 4, r/w // Bits [7:5] = 0 reserved #define MCR_LME 0x10 // Loopback mode enable #define MCR_IE 0x08 // Interrupt Enable #define MCR_OUT1C 0x04 // OUT1 Bit control #define MCR_RTSC 0x02 // Request To Send Control #define MCR_DTRC 0x01 // Data Terminal Ready Control // Line Status Register, offset 5, r/w #define LSR_FIFOE 0x80 // At least one error is pending in the RX FIFO chain #define LSR_TEMPT 0x40 // Transmitter Empty (last byte has been sent) #define LSR_THRE 0x20 // Transmitter Holding Register Empty (new data can be written to THR) #define LSR_BI 0x10 // Break Interrupt, Reset to read LSR #define LSR_BD LSR_BI #define LSR_FE 0x08 // Framing Error, Reset to read LSR #define LSR_PE 0x04 // Parity Error, Reset to read LSR #define LSR_OE 0x02 // Overrun Error, Reset to read LSR #define LSR_DR 0x01 // Receiver Buffer Full (Data Ready), Reset to read RBR // Modem Status Register, offset 6, r/w #define MSR_DCD 0x80 // Data Carrier Detect #define MSR_RI 0x40 // Ring Indicator #define MSR_DSR 0x20 // Data Set Ready #define MSR_CTS 0x10 // Clear To Send #define MSR_DDCD 0x08 // Delta Data Carrier Detect #define MSR_TERI 0x04 // Trailing Edge Of Ring Indicator #define MSR_DDSR 0x02 // Delta Data Set Ready #define MSR_DCTS 0x01 // Delta Clear To Send // Bit Masks #define IIR_INTERRUPT_MASK 0x0F//0x07 #define LSR_ERROR_MASK 0x9E #include "..\lgs\stdafx.h" #include "..\lgs\Isr.h" #include "..\lgs\Stream.h" #include "..\lglib\comm\connect.h" class CComm485 : public CIsr, public CConnection { public: CComm485( PISRPROC ); ~CComm485(); public: typedef enum { _COM1 = 0, _COM2, _COM3, _COM4 } PORTS; typedef enum { _NOPARITY = 0x00, _ODDPARITY = 0x08, _EVENPARITY = 0x18, _MARKPARITY = 0x28, _SPACEPARITY = 0x38 } PARITY; BOOL Create( PORTS nPort, LONG nBaudRate, INT nBitCount, PARITY eParity ); void Destroy(); BOOL SetParameters( LONG nBaudRate, INT nBitCount, PARITY eParity ); LONG GetBaudRate() { return m_nBaudRate; } void Flush(); public: #ifdef COMM_WITHOUT_INTERRUPTS BOOL Invalidate(); #endif virtual DWORD Read( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ); virtual DWORD Write( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ); virtual BOOL SetRTS( BOOL bOn ); virtual BOOL SetDTR( BOOL bOn ); public: virtual void OnISR(); #ifdef DEBUG_SHOW_COMMUNICATION public: void ShowCommunication( BOOL bShow ) { m_bShowCommunication = bShow; } private: BOOL m_bShowCommunication; WORD m_nShowCommunicationWrite; WORD m_nShowCommunicationRead; BOOL m_bShowCommunicationWritten; #endif private: short m_pBaseAddress; PISRPROC m_pOldIsrProc; short m_nBase; private: #ifdef COMM_RS485_PREVENT_ECHO volatile BOOL m_bWrite; #endif private: CFastStream m_InputStream; CFastStream m_OutputStream; LONG m_nBaudRate; INT m_nBitCount; PARITY m_eParity; private: static const BYTE c_acPort[]; static const BYTE c_acMask[]; }; #endif