1 // CameraIO_PCI.cpp: implementation of the CCameraIO_PCI class.
3 // Copyright (c) 2000 Apogee Instruments Inc.
4 //////////////////////////////////////////////////////////////////////
9 #include "ApogeeLinux.h" // This defines the IOCTL constants.
10 #include "CameraIO_PCI.h"
14 //////////////////////////////////////////////////////////////////////
15 // Construction/Destruction
16 //////////////////////////////////////////////////////////////////////
18 CCameraIO_PCI::CCameraIO_PCI()
24 CCameraIO_PCI::~CCameraIO_PCI()
29 long CCameraIO_PCI::Read(unsigned short reg
, unsigned short& val
)
39 RegNumber
= RegPCI_CommandRead
;
42 RegNumber
= RegPCI_TimerRead
;
45 RegNumber
= RegPCI_VBinningRead
;
48 RegNumber
= RegPCI_AICCounterRead
;
50 case Reg_TempSetPoint
:
51 RegNumber
= RegPCI_TempSetPointRead
;
53 case Reg_PixelCounter
:
54 RegNumber
= RegPCI_PixelCounterRead
;
57 RegNumber
= RegPCI_LineCounterRead
;
60 RegNumber
= RegPCI_BICCounterRead
;
63 RegNumber
= RegPCI_ImageData
;
66 RegNumber
= RegPCI_TempData
;
69 RegNumber
= RegPCI_Status
;
71 case Reg_CommandReadback
:
72 RegNumber
= RegPCI_CommandReadback
;
75 _ASSERT( FALSE
); // Application program bug
82 IoctlResult
= DeviceIoControl(
83 m_hDriver
, // Handle to device
84 IOCTL_WDM_READ_PCI_USHORT
, // IO Control code for Read
85 &RegNumber
, // Buffer to driver.
86 sizeof(RegNumber
), // Length of buffer in bytes.
87 &ReadBuffer
, // Buffer from driver.
88 sizeof(ReadBuffer
), // Length of buffer in bytes.
89 &ReturnedLength
, // Bytes placed in DataBuffer.
90 NULL
// NULL means wait till op. completes.
95 IoctlResult
= DeviceIoControl(
96 m_hDriver
, // Handle to device
97 IOCTL_GPD_READ_PCI_USHORT
, // IO Control code for Read
98 &RegNumber
, // Buffer to driver.
99 sizeof(RegNumber
), // Length of buffer in bytes.
100 &ReadBuffer
, // Buffer from driver.
101 sizeof(ReadBuffer
), // Length of buffer in bytes.
102 &ReturnedLength
, // Bytes placed in DataBuffer.
103 NULL
// NULL means wait till op. completes.
107 if ( (ReturnedLength
!= 2) || (IoctlResult
== FALSE
) )
117 long CCameraIO_PCI::Write(unsigned short reg
, unsigned short val
)
121 ULONG ReturnedLength
;
127 RegNumber
= RegPCI_Command
;
130 RegNumber
= RegPCI_Timer
;
133 RegNumber
= RegPCI_VBinning
;
136 RegNumber
= RegPCI_AICCounter
;
138 case Reg_TempSetPoint
:
139 RegNumber
= RegPCI_TempSetPoint
;
141 case Reg_PixelCounter
:
142 RegNumber
= RegPCI_PixelCounter
;
144 case Reg_LineCounter
:
145 RegNumber
= RegPCI_LineCounter
;
148 RegNumber
= RegPCI_BICCounter
;
155 InBuffer
[0] = RegNumber
;
161 IoctlResult
= DeviceIoControl(
162 m_hDriver
, // Handle to device
163 IOCTL_WDM_WRITE_PCI_USHORT
, // IO Control code for Write
164 &InBuffer
, // Buffer to driver. Holds register/data.
165 sizeof ( InBuffer
), // Length of buffer in bytes.
166 NULL
, // Buffer from driver. Not used.
167 0, // Length of buffer in bytes.
168 &ReturnedLength
, // Bytes placed in outbuf. Should be 0.
169 NULL
// NULL means wait till I/O completes.
174 IoctlResult
= DeviceIoControl(
175 m_hDriver
, // Handle to device
176 IOCTL_GPD_WRITE_PCI_USHORT
, // IO Control code for Write
177 &InBuffer
, // Buffer to driver. Holds register/data.
178 sizeof ( InBuffer
), // Length of buffer in bytes.
179 NULL
, // Buffer from driver. Not used.
180 0, // Length of buffer in bytes.
181 &ReturnedLength
, // Bytes placed in outbuf. Should be 0.
182 NULL
// NULL means wait till I/O completes.
186 if ( (IoctlResult
== FALSE
) || (ReturnedLength
!= 0) )
195 long CCameraIO_PCI::ReadLine( long SkipPixels
, long Pixels
, unsigned short* pLineBuffer
)
199 ULONG ReturnedLength
; // Number of bytes returned in output buffer
203 InBuffer
[0] = RegPCI_ImageData
;
204 InBuffer
[1] = SkipPixels
; // Data points to skip
205 InBuffer
[2] = Pixels
; // Data points to keep
207 NumBytes
= Pixels
* sizeof( unsigned short );
208 DataBuffer
= pLineBuffer
;
212 /////////////////////////////////////
213 // Clock out the line
214 m_RegShadow
[ Reg_Command
] |= RegBit_StartNextLine
; // set bit to 1
215 Write( Reg_Command
, m_RegShadow
[ Reg_Command
] );
217 m_RegShadow
[ Reg_Command
] &= ~RegBit_StartNextLine
; // set bit to 0
218 Write( Reg_Command
, m_RegShadow
[ Reg_Command
] );
219 /////////////////////////////////////
224 IoctlResult
= DeviceIoControl(
225 m_hDriver
, // Handle to device
226 IOCTL_WDM_READ_PCI_LINE
, // IO Control code for Read line
227 &InBuffer
, // Buffer to driver.
228 sizeof(InBuffer
), // Length of buffer in bytes.
229 DataBuffer
, // Buffer from driver.
230 NumBytes
, // Length of buffer in bytes.
231 &ReturnedLength
, // Bytes placed in DataBuffer.
232 NULL
// NULL means wait till op. completes.
237 IoctlResult
= DeviceIoControl(
238 m_hDriver
, // Handle to device
239 IOCTL_GPD_READ_PCI_LINE
, // IO Control code for Read line
240 &InBuffer
, // Buffer to driver.
241 sizeof(InBuffer
), // Length of buffer in bytes.
242 DataBuffer
, // Buffer from driver.
243 NumBytes
, // Length of buffer in bytes.
244 &ReturnedLength
, // Bytes placed in DataBuffer.
245 NULL
// NULL means wait till op. completes.
249 if ( (ReturnedLength
!= NumBytes
) || (!IoctlResult
) )
251 return 1; // Failed to get line info
254 /////////////////////////////////////
255 // Assert done reading line
256 m_RegShadow
[ Reg_Command
] |= RegBit_DoneReading
; // set bit to 1
257 Write( Reg_Command
, m_RegShadow
[ Reg_Command
] );
259 m_RegShadow
[ Reg_Command
] &= ~RegBit_DoneReading
; // set bit to 0
260 Write( Reg_Command
, m_RegShadow
[ Reg_Command
] );
261 /////////////////////////////////////
265 /////////////////////////////////////
266 // Wait until camera is done
267 clock_t StopTime
= clock() + CLOCKS_PER_SEC
; // wait at most one second
270 unsigned short val
= 0;
271 Read( Reg_Status
, val
);
272 if ( ( val
& RegBit_LineDone
) != 0 ) break;// Line done
274 if ( clock() > StopTime
) return 1; // Timed out
281 bool CCameraIO_PCI::InitDriver()
283 OSVERSIONINFO OSVerInfo
;
284 BOOLEAN IsPostWin98OS
;
288 IsPostWin98OS
= false;
294 OSVerInfo
.dwOSVersionInfoSize
= sizeof ( OSVERSIONINFO
);
295 GetVersionEx( &OSVerInfo
);
297 // Check for Win9x versions. Pre-Win98 is unsupported.
298 if ( OSVerInfo
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
)
300 // Check for pre-Win98
301 if (( OSVerInfo
.dwMajorVersion
< 4 ) ||
302 (( OSVerInfo
.dwMajorVersion
== 4 ) && ( OSVerInfo
.dwMinorVersion
== 0 )))
304 return false; // Pre-Win98 not supported
308 IsPostWin98OS
= true;
312 else if ( OSVerInfo
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
315 if ( OSVerInfo
.dwMajorVersion
< 4 )
317 // NT3.51 is not supported. Right??
320 else if (OSVerInfo
.dwMajorVersion
== 4 )
324 else if (OSVerInfo
.dwMajorVersion
> 4 )
332 ULONG ReturnedLength
;
336 m_hDriver
= CreateFile(
338 GENERIC_WRITE
| GENERIC_READ
,
339 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
345 if ( m_hDriver
== INVALID_HANDLE_VALUE
)
351 BOOL IoctlResult
= DeviceIoControl(
352 m_hDriver
, // Handle to device
353 IOCTL_PCI_BUS_SCAN
, // IO Control code for PCI Bus Scan
354 NULL
, // Buffer to driver.
355 0, // Length of buffer in bytes.
356 DataBuffer
, // Buffer from driver.
357 sizeof( DataBuffer
), // Length of buffer in bytes.
358 &ReturnedLength
, // Bytes placed in DataBuffer.
359 NULL
// NULL means wait till op. completes.
362 if ( (!IoctlResult
) || (ReturnedLength
!= sizeof(DataBuffer
)) )
367 else if ( IsPostWin98OS
|| IsPostNT4OS
)
369 // Should be okay to use the WDM driver. Note that the kernel
370 // driver will actually check to see if WDM services are available
373 m_hDriver
= CreateFile(
375 GENERIC_WRITE
| GENERIC_READ
,
376 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
383 if ( m_hDriver
== INVALID_HANDLE_VALUE
)
389 // Safe to assume we're using the WDM driver at this point.
396 void CCameraIO_PCI::CloseDriver()
398 // Close the driver if it already exists
399 if ( m_hDriver
!= NULL
)
401 CloseHandle ( m_hDriver
);