For DOS Protected Mode version of sensor core software
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.

563 lines
13 KiB

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