Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
7.1 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: Comm485.h
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Programmable Interrupt Controller (PIC).
  13. //----------------------------------------------------------------------------
  14. #ifndef __COMM__485__H__INCLUDED__
  15. #define __COMM__485__H__INCLUDED__
  16. // These are the port addresses of the 8259
  17. // Programmable Interrupt Controller (PIC).
  18. #define ICR_REGISTER 0x20
  19. #define IMR_REGISTER 0x21
  20. // An end of interrupt needs to be sent to the Control Port of
  21. // the 8259 when a hardware interrupt ends.
  22. #define END_OF_INTERRUPT 0x20
  23. #define INTERRUPT_ENABLE 0x08
  24. // Registers of UART 16550A
  25. #define THR_REGISTER 0 // Transmit Holding Register, write only
  26. #define RBR_REGISTER 0 // Receiver Buffer Register, read omly
  27. #define DLL_REGISTER 0 // Divisor Latch LSB, r/w
  28. #define DLM_REGISTER 1 // Divisor Latch MSB, r/w
  29. #define IER_REGISTER 1 // Interrupt Enable Register, r/w
  30. #define IIR_REGISTER 2 // Interrupt Identification Register, read only
  31. #define FCR_REGISTER 2 // FIFO Control Register, write only
  32. #define LCR_REGISTER 3 // Line Control Register, r/w
  33. #define MCR_REGISTER 4 // Modem Control Register, r/w
  34. #define LSR_REGISTER 5 // Line Status Register, r/w
  35. #define MSR_REGISTER 6 // Modem Status Register, r/w
  36. #define SPR_REGISTER 7 // Scratch Pad Register(Is this ever used?), r/w
  37. // Interrupt Enable Register, offset 1, r/w
  38. // Bits [7:4] = 0, unuse
  39. #define IER_EDSSI 0x08 // Enable Delta Status Signals Interrupt
  40. #define IER_ELSI 0x04 // Enable Line Status Interrupt
  41. #define IER_ETBEI 0x02 // Enable Transmitter Buffer Empty Interrupt
  42. #define IER_ERBFI 0x01 // Receiving char, in fifo mode - interrupt on timeout
  43. // Interrupt Identification Register, offset 2, read only
  44. // Bits [5:4] unuse
  45. #define IIR__16550A 0xC0 // fifo mode 16550A
  46. #define IIR__16550 0x80 // fifo mode 16550
  47. // Identification interrupts in fifo mode, IIR[3:1] bits
  48. #define IIR_TIMEOUT 0x0C//0x06 // To reset - reading RBR_REGISTER
  49. #define IIR_BREAK_OR_ERROR 0x06//0x03 // To reset - reading LSR_REGISTER
  50. #define IIR_RECEIVER_READY 0x04//0x02 // Received char, to reset - reading RBR_REGISTER
  51. #define IIR_TRANSMITER_EMPTY 0x02//0x01 // Holding register empty, to reset - writing THR_REGISTER
  52. #define IIR_MODEM_CHANGE_STATUS 0x00 /* Signals CTS, DSR, RI or DCD changes state.
  53. To reset - reading MSR_REGISTER */
  54. #define IIR_IID2 0x04 // Interrupt IDentification 2
  55. #define IIR_IID1 0x02 // Interrupt IDentification 1
  56. #define IIR_IID0 0x01 // Interrupt IDentification 0
  57. // Identification interrupts in common mode, IIR[2:1] bits
  58. #define IIR_TIMEOUT_COMMON 0x08
  59. #define IIR_PENDING 0x01 // An interrupt is pending if '0'
  60. // FIFO Control Register, offset 2, write only
  61. // Interrupt Trigger Level, [7:6] bits
  62. #define FCR_ITL14 0xC0 // 14 bytes in fifo
  63. #define FCR_ITL8 0x80 // 8 bytes in fifo
  64. #define FCR_ITL4 0x40 // 4 bytes in fifo
  65. #define FCR_ITL1 0x00 // 1 byte, default
  66. // Bits [5:4] reserved
  67. #define FCR_DMA_ENABLE 0x08 // DMA mode select
  68. #define FCR_RESETTF 0x04 // Transmitter FIFO reset
  69. #define FCR_RESETRF 0x02 // Receiver FIFO reset
  70. #define FCR_FIFO_ENABLE 0x01 // Transmitter and Reciever FIFO Enable
  71. // Line Control Register, offset 3, r/w
  72. #define LCR_DLAB 0x80 // Divisor Latch Access Bit
  73. #define LCR_SBR 0x40 // Set BReak
  74. #define LCR_STICPAR 0x20 // Sticky Parity
  75. #define LCR_EVENPAR 0x10 // Even Parity Select, if = 0 Odd, = 1 Even
  76. #define LCR_PAREN 0x08 // Parity Enable
  77. #define LCR_STOPB 0x04 /* Stop Bits, if = 0 is 1 stop bit, = 1 2 stop bits.
  78. For 5 bits code 1.5 bits */
  79. #define LCR_SERIALDB8 0x03 // Serial Data Bits
  80. #define LCR_SERIALDB7 0x02
  81. #define LCR_SERIALDB6 0x01
  82. #define LCR_SERIALDB5 0x00
  83. // Modem Control Register, offset 4, r/w
  84. // Bits [7:5] = 0 reserved
  85. #define MCR_LME 0x10 // Loopback mode enable
  86. #define MCR_IE 0x08 // Interrupt Enable
  87. #define MCR_OUT1C 0x04 // OUT1 Bit control
  88. #define MCR_RTSC 0x02 // Request To Send Control
  89. #define MCR_DTRC 0x01 // Data Terminal Ready Control
  90. // Line Status Register, offset 5, r/w
  91. #define LSR_FIFOE 0x80 // At least one error is pending in the RX FIFO chain
  92. #define LSR_TEMPT 0x40 // Transmitter Empty (last byte has been sent)
  93. #define LSR_THRE 0x20 // Transmitter Holding Register Empty (new data can be written to THR)
  94. #define LSR_BI 0x10 // Break Interrupt, Reset to read LSR
  95. #define LSR_BD LSR_BI
  96. #define LSR_FE 0x08 // Framing Error, Reset to read LSR
  97. #define LSR_PE 0x04 // Parity Error, Reset to read LSR
  98. #define LSR_OE 0x02 // Overrun Error, Reset to read LSR
  99. #define LSR_DR 0x01 // Receiver Buffer Full (Data Ready), Reset to read RBR
  100. // Modem Status Register, offset 6, r/w
  101. #define MSR_DCD 0x80 // Data Carrier Detect
  102. #define MSR_RI 0x40 // Ring Indicator
  103. #define MSR_DSR 0x20 // Data Set Ready
  104. #define MSR_CTS 0x10 // Clear To Send
  105. #define MSR_DDCD 0x08 // Delta Data Carrier Detect
  106. #define MSR_TERI 0x04 // Trailing Edge Of Ring Indicator
  107. #define MSR_DDSR 0x02 // Delta Data Set Ready
  108. #define MSR_DCTS 0x01 // Delta Clear To Send
  109. // Bit Masks
  110. #define IIR_INTERRUPT_MASK 0x0F//0x07
  111. #define LSR_ERROR_MASK 0x9E
  112. #include "..\lgs\stdafx.h"
  113. #include "..\lgs\Isr.h"
  114. #include "..\lgs\Stream.h"
  115. #include "..\lglib\comm\connect.h"
  116. class CComm485 :
  117. public CIsr,
  118. public CConnection
  119. {
  120. public:
  121. CComm485( PISRPROC );
  122. ~CComm485();
  123. public:
  124. typedef enum { _COM1 = 0, _COM2, _COM3, _COM4 } PORTS;
  125. typedef enum { _NOPARITY = 0x00, _ODDPARITY = 0x08, _EVENPARITY = 0x18, _MARKPARITY = 0x28, _SPACEPARITY = 0x38 } PARITY;
  126. BOOL Create( PORTS nPort, LONG nBaudRate, INT nBitCount, PARITY eParity );
  127. void Destroy();
  128. BOOL SetParameters( LONG nBaudRate, INT nBitCount, PARITY eParity );
  129. LONG GetBaudRate() { return m_nBaudRate; }
  130. void Flush();
  131. public:
  132. #ifdef COMM_WITHOUT_INTERRUPTS
  133. BOOL Invalidate();
  134. #endif
  135. virtual DWORD Read( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout );
  136. virtual DWORD Write( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout );
  137. virtual BOOL SetRTS( BOOL bOn );
  138. virtual BOOL SetDTR( BOOL bOn );
  139. public:
  140. virtual void OnISR();
  141. #ifdef DEBUG_SHOW_COMMUNICATION
  142. public:
  143. void ShowCommunication( BOOL bShow ) { m_bShowCommunication = bShow; }
  144. private:
  145. BOOL m_bShowCommunication;
  146. WORD m_nShowCommunicationWrite;
  147. WORD m_nShowCommunicationRead;
  148. BOOL m_bShowCommunicationWritten;
  149. #endif
  150. private:
  151. short m_pBaseAddress;
  152. PISRPROC m_pOldIsrProc;
  153. short m_nBase;
  154. private:
  155. #ifdef COMM_RS485_PREVENT_ECHO
  156. volatile BOOL m_bWrite;
  157. #endif
  158. private:
  159. CFastStream<BYTE,7> m_InputStream;
  160. CFastStream<BYTE,7> m_OutputStream;
  161. LONG m_nBaudRate;
  162. INT m_nBitCount;
  163. PARITY m_eParity;
  164. private:
  165. static const BYTE c_acPort[];
  166. static const BYTE c_acMask[];
  167. };
  168. #endif