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.

484 lines
12 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of Comm485.cpp
  13. //----------------------------------------------------------------------------
  14. #include <dos.h>
  15. #include <stdio.h>
  16. #include "..\lgs\comm485.h"
  17. #include "..\LgLib\version.h"
  18. const BYTE CComm485::c_acPort[] = {0x0C, 0x0B, 0x0C, 0x0B};
  19. const BYTE CComm485::c_acMask[] = {0xEF, 0xF7, 0xEF, 0xF7};
  20. CComm485::CComm485( PISRPROC pIsrProc ) : CIsr( pIsrProc )
  21. {
  22. m_pBaseAddress = 0;
  23. m_nBase = 0;
  24. m_pOldIsrProc = NULL;
  25. #ifdef COMM_RS485_PREVENT_ECHO
  26. m_bWrite = FALSE;
  27. #endif
  28. m_nBaudRate = 0;
  29. m_nBitCount = 0;
  30. m_eParity = _NOPARITY;
  31. #ifdef DEBUG_SHOW_COMMUNICATION
  32. m_bShowCommunication = FALSE;
  33. m_nShowCommunicationWrite = 0;
  34. m_nShowCommunicationRead = 0;
  35. m_bShowCommunicationWritten = FALSE;
  36. #endif
  37. }
  38. CComm485::~CComm485()
  39. {
  40. Destroy();
  41. }
  42. BOOL CComm485::SetParameters( LONG nBaudRate, INT nBitCount, PARITY eParity )
  43. {
  44. if( !m_pBaseAddress )
  45. return FALSE;
  46. if( m_nBaudRate == nBaudRate && m_nBitCount == nBitCount && m_eParity == eParity )
  47. return TRUE;
  48. // finish transmition!!!
  49. Flush();
  50. // prepare configuration
  51. int config;
  52. switch( nBitCount )
  53. {
  54. case 5: config = LCR_SERIALDB5; break;
  55. case 6: config = LCR_SERIALDB6; break;
  56. case 7: config = LCR_SERIALDB7; break;
  57. case 8: config = LCR_SERIALDB8; break;
  58. default:
  59. return FALSE;
  60. }
  61. // add parity
  62. config |= (BYTE)eParity;
  63. // Setup baud rate;
  64. outportb( m_pBaseAddress + LCR_REGISTER, LCR_DLAB );
  65. // remember baudrate for future use
  66. m_nBaudRate = nBaudRate;
  67. m_nBitCount = nBitCount;
  68. m_eParity = eParity;
  69. // initialize baudrate generator
  70. outportb( m_pBaseAddress + DLL_REGISTER,
  71. static_cast<BYTE> (115200L / nBaudRate) );
  72. outportb( m_pBaseAddress + DLM_REGISTER, 0 );
  73. // Setup configuration
  74. outportb( m_pBaseAddress + LCR_REGISTER, config );
  75. return TRUE;
  76. }
  77. BOOL CComm485::Create( PORTS nPort, LONG nBaudRate, INT nBitCount, PARITY eParity )
  78. {
  79. Destroy();
  80. if( !m_InputStream.Create() )
  81. return FALSE;
  82. if( !m_OutputStream.Create() )
  83. return FALSE;
  84. m_nBase = nPort;
  85. m_pBaseAddress = *((short far*)MK_FP(0x0040, 2 * m_nBase ));
  86. if(!m_pBaseAddress)
  87. {
  88. printf( "Comm %d is inaccessable\n", nPort );
  89. return FALSE;
  90. }
  91. /*
  92. int config;
  93. switch( nBitCount )
  94. {
  95. case 5: config = LCR_SERIALDB5; break;
  96. case 6: config = LCR_SERIALDB6; break;
  97. case 7: config = LCR_SERIALDB7; break;
  98. case 8: config = LCR_SERIALDB8; break;
  99. default:
  100. return FALSE;
  101. }
  102. // Setup baud rate;
  103. outportb( m_pBaseAddress + LCR_REGISTER, LCR_DLAB );
  104. // remember baudrate for future use
  105. m_nBaudRate = nBaudRate;
  106. outportb(m_pBaseAddress + DLL_REGISTER,
  107. static_cast<BYTE> (115200L / nBaudRate));
  108. outportb( m_pBaseAddress + DLM_REGISTER, 0 );
  109. // Setup configuration
  110. outportb( m_pBaseAddress + LCR_REGISTER, config );
  111. */
  112. if( !SetParameters( nBaudRate, nBitCount, eParity ) )
  113. return FALSE;
  114. // Use FIFO
  115. outportb(m_pBaseAddress + FCR_REGISTER,
  116. FCR_ITL14 | FCR_RESETTF | FCR_RESETRF | FCR_FIFO_ENABLE );
  117. #ifndef COMM_WITHOUT_INTERRUPTS
  118. // First thing is to store the old interrupt
  119. m_pOldIsrProc = _dos_getvect( c_acPort[m_nBase] );
  120. _dos_setvect( c_acPort[m_nBase], m_pIsrProc );
  121. // Set OUT2 bit to enable interrupts
  122. outportb(m_pBaseAddress + MCR_REGISTER, MCR_IE);
  123. // Enable the interrupts we want
  124. outportb(m_pBaseAddress + IER_REGISTER,
  125. IER_EDSSI | IER_ELSI | IER_ERBFI );
  126. // Last thing to do is unmask the int in the ICU
  127. outportb(IMR_REGISTER, c_acMask[m_nBase] & inp(IMR_REGISTER));
  128. #else
  129. // Disable all interrupts
  130. outportb(m_pBaseAddress + IER_REGISTER, 0 );
  131. #endif
  132. SetRTS( FALSE );
  133. // clear RTS
  134. // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
  135. // if( MCR_Status & MCR_RTSC )
  136. // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC );
  137. return TRUE;
  138. }
  139. void CComm485::Flush()
  140. {
  141. #ifdef COMM_WITHOUT_INTERRUPTS
  142. while( !Invalidate() );
  143. #else
  144. // output remaining bytes
  145. while( m_OutputStream.GetCount() );
  146. // wait until output shift register become empty
  147. while( !(inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT) );
  148. #endif
  149. }
  150. void CComm485::Destroy()
  151. {
  152. if( m_pBaseAddress )
  153. {
  154. // finish transmition!!!
  155. Flush();
  156. // Disable FIFO
  157. outportb(m_pBaseAddress + FCR_REGISTER, 0);
  158. #ifndef COMM_WITHOUT_INTERRUPTS
  159. // Disable interrupt
  160. outportb(m_pBaseAddress + MCR_REGISTER, 0);
  161. outportb(m_pBaseAddress + IER_REGISTER, 0);
  162. // disable in interrupt controller
  163. outportb(IMR_REGISTER,
  164. ~c_acMask[m_nBase] | inp(IMR_REGISTER));
  165. // return old vector
  166. _dos_setvect(c_acPort[m_nBase], m_pOldIsrProc );
  167. #endif
  168. m_pBaseAddress = 0;
  169. #ifdef COMM_RS485_PREVENT_ECHO
  170. m_bWrite = FALSE;
  171. #endif
  172. m_nBase = 0;
  173. }
  174. m_nBaudRate = 0;
  175. m_nBitCount = 0;
  176. m_eParity = _NOPARITY;
  177. }
  178. void CComm485::OnISR()
  179. {
  180. // First thing to do in interrupt routines is tell
  181. // the ICU about the service being done. This avoids lock-up
  182. outportb( ICR_REGISTER, END_OF_INTERRUPT );
  183. #ifndef COMM_WITHOUT_INTERRUPTS
  184. while( TRUE )
  185. {
  186. BYTE byte;
  187. int LSR_Status;
  188. int IIR_Status = inportb( m_pBaseAddress + IIR_REGISTER );
  189. if( IIR_Status & IIR_PENDING )
  190. break;
  191. IIR_Status &= IIR_INTERRUPT_MASK; // Equal 0x0F
  192. switch( IIR_Status )
  193. {
  194. case IIR_BREAK_OR_ERROR:
  195. // In order of priority
  196. // It's just important to read the LSR_REGISTER
  197. LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER);
  198. if( LSR_Status & LSR_ERROR_MASK )
  199. {
  200. // read error byte from receiver buffer
  201. inportb(m_pBaseAddress + RBR_REGISTER);
  202. }
  203. break;
  204. case IIR_RECEIVER_READY:
  205. case IIR_TIMEOUT:
  206. while( TRUE )
  207. {
  208. // read byte from receiver buffer
  209. byte = inportb(m_pBaseAddress + RBR_REGISTER);
  210. #ifdef COMM_RS485_PREVENT_ECHO
  211. // check for echo
  212. if( !m_bWrite )
  213. #endif
  214. m_InputStream.Put( byte );
  215. // Check when receiver data ready
  216. LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER);
  217. if(!(LSR_Status & LSR_DR))
  218. break;
  219. }
  220. break;
  221. case IIR_TRANSMITER_EMPTY:
  222. // if there are not bytes for transmit
  223. if( !m_OutputStream.GetCount() )
  224. {
  225. #ifdef COMM_RS485_PREVENT_ECHO
  226. // check for echo
  227. m_bWrite = FALSE;
  228. #endif
  229. SetRTS( FALSE );
  230. // clear RTS
  231. // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
  232. // if( MCR_Status & MCR_RTSC )
  233. // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC );
  234. // enable receiver and disable transmitter
  235. outportb(m_pBaseAddress + IER_REGISTER,
  236. IER_EDSSI | IER_ELSI | IER_ERBFI );
  237. break;
  238. }
  239. #ifdef COMM_RS485_PREVENT_ECHO
  240. // check for echo
  241. m_bWrite = TRUE;
  242. #endif
  243. // while there are bytes in output stream
  244. while(1)
  245. {
  246. // check for transmitter buffer overflow
  247. LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER);
  248. if(!(LSR_Status & LSR_THRE))
  249. break;
  250. // get new byte for transmit
  251. if( !m_OutputStream.Get( &byte ) )
  252. break;
  253. // write byte into FIFO
  254. outportb(m_pBaseAddress + THR_REGISTER, byte );
  255. }
  256. break;
  257. case IIR_MODEM_CHANGE_STATUS:
  258. // Must read the MSR_REGISTER or will lock up the system!
  259. inportb(m_pBaseAddress + MSR_REGISTER);
  260. break;
  261. }
  262. }
  263. #endif // COMM_WITHOUT_INTERRUPTS
  264. }
  265. #ifdef COMM_WITHOUT_INTERRUPTS
  266. BOOL CComm485::Invalidate()
  267. {
  268. BYTE byte;
  269. if( !m_pBaseAddress )
  270. return TRUE;
  271. // check errors
  272. // while( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_ERROR_MASK )
  273. while( inportb(m_pBaseAddress + LSR_REGISTER) & ( LSR_FIFOE | LSR_FE | LSR_PE | LSR_OE ) )
  274. {
  275. // read error byte from receiver buffer
  276. inportb(m_pBaseAddress + RBR_REGISTER);
  277. }
  278. // check receiver
  279. while( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_DR )
  280. {
  281. // read byte from receiver buffer
  282. byte = inportb(m_pBaseAddress + RBR_REGISTER);
  283. #ifdef COMM_RS485_PREVENT_ECHO
  284. // check for echo
  285. if( !m_bWrite )
  286. #endif
  287. m_InputStream.Put( byte );
  288. }
  289. // if there are not bytes for transmit
  290. if( m_OutputStream.GetCount() )
  291. {
  292. // set RTS
  293. SetRTS( TRUE );
  294. // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
  295. // if( !(MCR_Status & MCR_RTSC) )
  296. // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC );
  297. #ifdef COMM_RS485_PREVENT_ECHO
  298. // check for echo
  299. m_bWrite = FALSE;
  300. #endif
  301. // if FIFO empty
  302. if( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE )
  303. {
  304. // while there are bytes in output stream
  305. for( INT i = 0; i < 16; i++ )
  306. {
  307. // get new byte for transmit
  308. if( !m_OutputStream.Get( &byte ) )
  309. break;
  310. // write byte into FIFO
  311. outportb(m_pBaseAddress + THR_REGISTER, byte );
  312. }
  313. }
  314. }
  315. // check for transmit end
  316. if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE ) )
  317. return FALSE;
  318. if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT ) )
  319. return FALSE;
  320. SetRTS( FALSE );
  321. // clear RTS
  322. // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
  323. // if( MCR_Status & MCR_RTSC )
  324. // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC );
  325. #ifdef COMM_RS485_PREVENT_ECHO
  326. // check for echo
  327. m_bWrite = FALSE;
  328. #endif
  329. return TRUE;
  330. }
  331. #endif
  332. DWORD CComm485::Write( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout )
  333. {
  334. // unused parameter
  335. dwTimeout;
  336. INT nWritten = 0;
  337. if ( dwSize > 0 ) {
  338. // write bytes to transmit stream
  339. // Sending 0xFF after Data
  340. pBuffer[dwSize]=0xFF;
  341. dwSize++;
  342. // Remove previews FF from queue
  343. m_OutputStream.RemoveLast();
  344. // write bytes to transmit stream
  345. nWritten = m_OutputStream.Put( pBuffer, dwSize );
  346. nWritten -- ; // decrease because +FF
  347. };
  348. #ifdef DEBUG_SHOW_COMMUNICATION
  349. if( m_bShowCommunication )
  350. {
  351. printf( "\nW%d: ", m_nShowCommunicationWrite++ );
  352. for( DWORD i = 0; i < nWritten; i++ )
  353. printf( "%02X", (BYTE)pBuffer[i] );
  354. m_bShowCommunicationWritten = TRUE;
  355. }
  356. #endif
  357. #ifndef COMM_WITHOUT_INTERRUPTS
  358. SetRTS( TRUE );
  359. // set RTS
  360. // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
  361. // if( !(MCR_Status & MCR_RTSC) )
  362. // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC );
  363. // enable transmitter and disable receiver
  364. outportb(m_pBaseAddress + IER_REGISTER,
  365. IER_EDSSI | IER_ELSI | IER_ETBEI | IER_ERBFI );
  366. #endif
  367. return nWritten;
  368. }
  369. DWORD CComm485::Read( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout )
  370. {
  371. // unused parameter
  372. dwTimeout;
  373. DWORD dwRead = m_InputStream.Get( pBuffer, dwSize );
  374. #ifdef DEBUG_SHOW_COMMUNICATION
  375. if( m_bShowCommunication )
  376. {
  377. if( dwRead && m_bShowCommunicationWritten )
  378. {
  379. printf( "\nR%d: ", m_nShowCommunicationRead++ );
  380. m_bShowCommunicationWritten = FALSE;
  381. }
  382. for( DWORD i = 0; i < dwRead; i++ )
  383. printf( "%02X", (BYTE)pBuffer[i] );
  384. }
  385. #endif
  386. // read bytes from receive stream
  387. return dwRead;
  388. }
  389. BOOL CComm485::SetRTS( BOOL bOn )
  390. {
  391. // set/clear RTS
  392. BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
  393. if( bOn ) { if( !(MCR_Status & MCR_RTSC) ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC ); }
  394. else { if( MCR_Status & MCR_RTSC ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC ); }
  395. }
  396. BOOL CComm485::SetDTR( BOOL bOn )
  397. {
  398. // set/clear RTS
  399. BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
  400. if( bOn ) { if( !(MCR_Status & MCR_DTRC) ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_DTRC ); }
  401. else { if( MCR_Status & MCR_DTRC ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_DTRC ); }
  402. }