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.

309 lines
8.1 KiB

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: LgDeviceGPIOLPT.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgDeviceGPIOLPT.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgDeviceGpioLPT.h"
  16. #include "..\KernelIO\KernelIO.h"
  17. DWORD CLgDeviceGPIOLPT::s_bControlBits[13] =
  18. {
  19. GPIO_LPT_I_ASK,
  20. GPIO_LPT_I_BUZY,
  21. GPIO_LPT_I_PAPEREND,
  22. GPIO_LPT_I_SELECT,
  23. GPIO_LPT_I_ERROR,
  24. GPIO_LPT_IO_DATA_0,
  25. GPIO_LPT_IO_DATA_1,
  26. GPIO_LPT_IO_DATA_2,
  27. GPIO_LPT_IO_DATA_3,
  28. GPIO_LPT_IO_DATA_4,
  29. GPIO_LPT_IO_DATA_5,
  30. GPIO_LPT_IO_DATA_6,
  31. GPIO_LPT_IO_DATA_7,
  32. };
  33. BOOL CLgDeviceGPIOLPT::s_bInvertBits[13] =
  34. {
  35. TRUE,
  36. FALSE,
  37. TRUE,
  38. TRUE,
  39. TRUE,
  40. FALSE,
  41. FALSE,
  42. FALSE,
  43. FALSE,
  44. FALSE,
  45. FALSE,
  46. FALSE,
  47. FALSE
  48. };
  49. CLgDeviceGPIOLPT::CLgDeviceGPIOLPT()
  50. {
  51. m_BaseAddress = 0;
  52. m_dwInputState = 0;
  53. m_dwOutputState = 0;
  54. m_bDataOutput = TRUE;
  55. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDeviceGPIOLPT::CLgDeviceGPIOLPT" );
  56. }
  57. CLgDeviceGPIOLPT::~CLgDeviceGPIOLPT()
  58. {
  59. Destroy();
  60. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgDeviceGPIOLPT::~CLgDeviceGPIOLPT" );
  61. }
  62. BOOL CLgDeviceGPIOLPT::Create( LPCTSTR pPort, BOOL bDataOutput )
  63. {
  64. // store port direction
  65. m_bDataOutput = bDataOutput;
  66. if( m_bDataOutput )
  67. {
  68. if( !CLgDeviceGPIO::Create( 5, 8 ) )
  69. return FALSE;
  70. }
  71. else
  72. {
  73. if( !CLgDeviceGPIO::Create( 13, 0 ) )
  74. return FALSE;
  75. }
  76. // initialize driver
  77. if( !InitializeWinIo() )
  78. {
  79. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CLgDeviceGPIOLPT::Create", "Can't initialize WinIO, probably no drivers" );
  80. return FALSE;
  81. }
  82. // set base address
  83. if( !_tcsnicmp( pPort, _T("LPT1"), 4 ) )
  84. m_BaseAddress = 0x378;
  85. else
  86. {
  87. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "Only LPT1 is implemented" );
  88. return FALSE;
  89. }
  90. // store current input state
  91. if( !GetPortVal( m_BaseAddress, &m_dwSavedState, 4 ) )
  92. {
  93. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  94. }
  95. m_dwOutputState = 0;
  96. // initialize parallel port
  97. if( m_bDataOutput )
  98. {
  99. DWORD dwValue;
  100. // set data as output
  101. if( !GetPortVal( m_BaseAddress + 2, &dwValue, 1 ) )
  102. {
  103. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  104. }
  105. if( !SetPortVal( m_BaseAddress + 2, dwValue & ~0x20, 1 ) )
  106. {
  107. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  108. }
  109. // store initial state of inputs
  110. if( !GetPortVal( m_BaseAddress + 1, &dwValue, 1 ) )
  111. {
  112. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  113. }
  114. m_dwInputState = dwValue;
  115. // clear all outputs
  116. if( !SetPortVal( m_BaseAddress, m_dwOutputState, 1 ) )
  117. {
  118. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  119. }
  120. }
  121. else
  122. {
  123. DWORD dwValue;
  124. // set data as input
  125. if( !GetPortVal( m_BaseAddress + 2, &dwValue, 1 ) )
  126. {
  127. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  128. }
  129. if( !SetPortVal( m_BaseAddress + 2, dwValue | 0x20, 1 ) )
  130. {
  131. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  132. }
  133. // store initial state of data inputs
  134. if( !GetPortVal( m_BaseAddress + 1, &dwValue, 1 ) )
  135. {
  136. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  137. }
  138. m_dwInputState = dwValue;
  139. if( !GetPortVal( m_BaseAddress, &dwValue, 1 ) )
  140. {
  141. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "GetPortVal failed" );
  142. }
  143. m_dwInputState |= (dwValue << 8);
  144. }
  145. // init gisteresis
  146. for( INT i = 0 ; i < 13; i++ )
  147. m_pGisterezis[i].bSavedOn = ( (m_dwInputState & s_bControlBits[i]) == s_bControlBits[i] );
  148. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgDeviceGPIOLPT::Create", "%d,%d,%d,%d,%d,"
  149. "%d,%d,%d,%d,%d,"
  150. "%d,%d,%d",
  151. m_pGisterezis[0].bSavedOn, m_pGisterezis[1].bSavedOn, m_pGisterezis[2].bSavedOn, m_pGisterezis[3].bSavedOn, m_pGisterezis[4].bSavedOn,
  152. m_pGisterezis[5].bSavedOn, m_pGisterezis[6].bSavedOn, m_pGisterezis[7].bSavedOn, m_pGisterezis[8].bSavedOn, m_pGisterezis[9].bSavedOn,
  153. m_pGisterezis[10].bSavedOn, m_pGisterezis[11].bSavedOn, m_pGisterezis[12].bSavedOn );
  154. /*
  155. if( !CLgDeviceGPIO::InitInputState( 0, m_dwInputState ) )
  156. {
  157. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::Create", "CLgDeviceGPIO::InitInputState failed" );
  158. return FALSE;
  159. }
  160. */
  161. return TRUE;
  162. }
  163. void CLgDeviceGPIOLPT::Destroy()
  164. {
  165. // deitialize driver
  166. ShutdownWinIo();
  167. }
  168. BOOL CLgDeviceGPIOLPT::ReadWriteProc()
  169. {
  170. INT nCmd = GetCmd();
  171. switch( nCmd )
  172. {
  173. case CMD_GET_INPUT:
  174. // avalible for new commands
  175. ReleaseCmd();
  176. // call handler
  177. OnEvtGetInput( m_Input, s_bInvertBits[m_Input] ? !m_pGisterezis[m_Input].bSavedOn : m_pGisterezis[m_Input].bSavedOn );
  178. break;
  179. case CMD_GET_INPUT_ALL:
  180. {
  181. // avalible for new commands
  182. ReleaseCmd();
  183. // prepare input list
  184. for( INT i = 0; i < GetInputCount(); i++ )
  185. GetInputPins()->pPin[i].bOn = ( s_bInvertBits[i] ? !m_pGisterezis[i].bSavedOn : m_pGisterezis[i].bSavedOn );
  186. // call handler
  187. OnEvtGetInputAll( GetInputPins() );
  188. }
  189. break;
  190. case CMD_SET_OUTPUT:
  191. {
  192. ReleaseCmd();
  193. if( m_Output.bOn ) m_dwOutputState |= ( 1 << m_Output.nIndex );
  194. else m_dwOutputState &= ~( 1 << m_Output.nIndex );
  195. SetPortVal( m_BaseAddress, m_dwOutputState, 1 );
  196. OnEvtSetOutput( m_Output.nIndex, m_Output.bOn );
  197. }
  198. break;
  199. case CMD_GET_OUTPUT_ALL:
  200. {
  201. // avalible for new commands
  202. ReleaseCmd();
  203. // prepare input list
  204. for( INT i = 0; i < GetOutputCount(); i++ )
  205. {
  206. DWORD dwBit = 1 << i;
  207. GetOutputPins()->pPin[i].bOn = (m_dwOutputState & dwBit ) == dwBit;
  208. }
  209. // call handler
  210. OnEvtGetOutputAll( GetOutputPins() );
  211. }
  212. break;
  213. case CMD_NONE:
  214. break;
  215. default:
  216. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CLgDeviceGPIOLPT::ReadWriteProc", "received unsupported command %d", nCmd );
  217. ReleaseCmd();
  218. OnEvtCommandError( nCmd, _T("UNSUPPORTED") );
  219. break;
  220. }
  221. // get current time
  222. LARGE_INTEGER liTime;
  223. ::GetSystemTimeAsFileTime( (LPFILETIME)&liTime );
  224. // read state of control bits
  225. DWORD dwInputState = 0;
  226. INT nBitCount = 5;
  227. if( !GetPortVal( m_BaseAddress+1, &dwInputState, 1 ) )
  228. {
  229. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::ReadWriteProc", "GetPortVal failed" );
  230. }
  231. if( !m_bDataOutput )
  232. {
  233. DWORD dwDataPort = 0;
  234. if( !GetPortVal( m_BaseAddress, &dwDataPort, 4 ) )
  235. {
  236. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgDeviceGPIOLPT::ReadWriteProc", "GetPortVal failed" );
  237. }
  238. dwInputState |= ( dwDataPort << 8 );
  239. nBitCount += 8;
  240. }
  241. // check changes
  242. if( dwInputState != m_dwInputState )
  243. {
  244. DWORD dwChangedBits = dwInputState ^ m_dwInputState;
  245. for( INT i = 0; i < nBitCount; i++ )
  246. {
  247. DWORD dwBit = s_bControlBits[i];
  248. if( dwChangedBits & dwBit )
  249. {
  250. // update times
  251. m_pGisterezis[i].bCurrentOn = (dwInputState & dwBit) == dwBit;
  252. m_pGisterezis[i].liTime.QuadPart = liTime.QuadPart + GPIO_GISTERESIS_TIME;
  253. }
  254. }
  255. // store new state
  256. m_dwInputState = dwInputState;
  257. }
  258. // resolve gisteresis times
  259. for( INT i = 0; i < nBitCount; i++ )
  260. {
  261. if( m_pGisterezis[i].liTime.QuadPart > liTime.QuadPart )
  262. {
  263. if( m_pGisterezis[i].bCurrentOn != m_pGisterezis[i].bSavedOn )
  264. {
  265. m_pGisterezis[i].bSavedOn = m_pGisterezis[i].bCurrentOn;
  266. // notify about performation
  267. OnEvtInputChange( i, s_bInvertBits[i] ? !m_pGisterezis[i].bSavedOn : m_pGisterezis[i].bSavedOn );
  268. }
  269. }
  270. }
  271. return TRUE;
  272. }