• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

CameraIO_PCI.cpp

Go to the documentation of this file.
00001 // CameraIO_PCI.cpp: implementation of the CCameraIO_PCI class.
00002 //
00003 // Copyright (c) 2000 Apogee Instruments Inc.
00005 
00006 #include "stdafx.h"
00007 #include <winioctl.h>
00008 
00009 #include "ApogeeLinux.h"        // This defines the IOCTL constants.
00010 #include "CameraIO_PCI.h"
00011 #include "time.h"
00012 
00013 
00015 // Construction/Destruction
00017 
00018 CCameraIO_PCI::CCameraIO_PCI()
00019 {
00020     m_IsWDM     = false;
00021     m_hDriver   = NULL;
00022 }
00023 
00024 CCameraIO_PCI::~CCameraIO_PCI()
00025 {
00026     CloseDriver();
00027 }
00028 
00029 long CCameraIO_PCI::Read(unsigned short reg, unsigned short& val)
00030 {
00031     BOOLEAN IoctlResult;
00032     ULONG   ReturnedLength;
00033     USHORT  RegNumber;
00034     USHORT  ReadBuffer;
00035 
00036     switch ( reg )
00037     {
00038     case Reg_Command:
00039         RegNumber = RegPCI_CommandRead;
00040         break;
00041     case Reg_Timer:
00042         RegNumber = RegPCI_TimerRead;
00043         break;
00044     case Reg_VBinning:
00045         RegNumber = RegPCI_VBinningRead;
00046         break;
00047     case Reg_AICCounter:
00048         RegNumber = RegPCI_AICCounterRead;
00049         break;
00050     case Reg_TempSetPoint:
00051         RegNumber = RegPCI_TempSetPointRead;
00052         break;
00053     case Reg_PixelCounter:
00054         RegNumber = RegPCI_PixelCounterRead;
00055         break;
00056     case Reg_LineCounter:
00057         RegNumber = RegPCI_LineCounterRead;
00058         break;
00059     case Reg_BICCounter:
00060         RegNumber = RegPCI_BICCounterRead;
00061         break;
00062     case Reg_ImageData:
00063         RegNumber = RegPCI_ImageData;
00064         break;
00065     case Reg_TempData:
00066         RegNumber = RegPCI_TempData;
00067         break;
00068     case Reg_Status:
00069         RegNumber = RegPCI_Status;
00070         break;
00071     case Reg_CommandReadback:
00072         RegNumber = RegPCI_CommandReadback;
00073         break;
00074     default:
00075         _ASSERT( FALSE );   // Application program bug
00076         val = 0;
00077         return 0;
00078     }
00079 
00080     if ( m_IsWDM )
00081     {
00082         IoctlResult = DeviceIoControl(
00083             m_hDriver,                  // Handle to device
00084             IOCTL_WDM_READ_PCI_USHORT,  // IO Control code for Read
00085             &RegNumber,                 // Buffer to driver.
00086             sizeof(RegNumber),          // Length of buffer in bytes.
00087             &ReadBuffer,                // Buffer from driver.
00088             sizeof(ReadBuffer),         // Length of buffer in bytes.
00089             &ReturnedLength,            // Bytes placed in DataBuffer.
00090             NULL                        // NULL means wait till op. completes.
00091             );
00092     }
00093     else
00094     {
00095         IoctlResult = DeviceIoControl(
00096             m_hDriver,                  // Handle to device
00097             IOCTL_GPD_READ_PCI_USHORT,  // IO Control code for Read
00098             &RegNumber,                 // Buffer to driver.
00099             sizeof(RegNumber),          // Length of buffer in bytes.
00100             &ReadBuffer,                // Buffer from driver.
00101             sizeof(ReadBuffer),         // Length of buffer in bytes.
00102             &ReturnedLength,            // Bytes placed in DataBuffer.
00103             NULL                        // NULL means wait till op. completes.
00104             );
00105     }
00106 
00107     if ( (ReturnedLength != 2) || (IoctlResult == FALSE) )
00108     {
00109         return 1;
00110     }
00111 
00112     val = ReadBuffer;
00113     
00114     return 0;
00115 }
00116 
00117 long CCameraIO_PCI::Write(unsigned short reg, unsigned short val)
00118 {
00119     BOOLEAN IoctlResult;
00120     ULONG   InBuffer[2];
00121     ULONG   ReturnedLength;
00122     USHORT  RegNumber;
00123 
00124     switch ( reg )
00125     {
00126     case Reg_Command:
00127         RegNumber = RegPCI_Command;
00128         break;
00129     case Reg_Timer:
00130         RegNumber = RegPCI_Timer;
00131         break;
00132     case Reg_VBinning:
00133         RegNumber = RegPCI_VBinning;
00134         break;
00135     case Reg_AICCounter:
00136         RegNumber = RegPCI_AICCounter;
00137         break;
00138     case Reg_TempSetPoint:
00139         RegNumber = RegPCI_TempSetPoint;
00140         break;
00141     case Reg_PixelCounter:
00142         RegNumber = RegPCI_PixelCounter;
00143         break;
00144     case Reg_LineCounter:
00145         RegNumber = RegPCI_LineCounter;
00146         break;
00147     case Reg_BICCounter:
00148         RegNumber = RegPCI_BICCounter;
00149         break;
00150     default:
00151         _ASSERT ( false );
00152         return 0;
00153     }
00154 
00155     InBuffer[0] = RegNumber;
00156     InBuffer[1] = val;
00157 
00158     // Do an I/O write      
00159     if ( m_IsWDM )
00160     {
00161         IoctlResult = DeviceIoControl(
00162             m_hDriver,                  // Handle to device
00163             IOCTL_WDM_WRITE_PCI_USHORT, // IO Control code for Write
00164             &InBuffer,                  // Buffer to driver.  Holds register/data.
00165             sizeof ( InBuffer ),        // Length of buffer in bytes.
00166             NULL,                       // Buffer from driver.   Not used.
00167             0,                          // Length of buffer in bytes.
00168             &ReturnedLength,            // Bytes placed in outbuf.  Should be 0.
00169             NULL                        // NULL means wait till I/O completes.
00170             );
00171     }
00172     else
00173     {
00174         IoctlResult = DeviceIoControl(
00175             m_hDriver,                  // Handle to device
00176             IOCTL_GPD_WRITE_PCI_USHORT, // IO Control code for Write
00177             &InBuffer,                  // Buffer to driver.  Holds register/data.
00178             sizeof ( InBuffer ),        // Length of buffer in bytes.
00179             NULL,                       // Buffer from driver.   Not used.
00180             0,                          // Length of buffer in bytes.
00181             &ReturnedLength,            // Bytes placed in outbuf.  Should be 0.
00182             NULL                        // NULL means wait till I/O completes.
00183             );
00184     }
00185 
00186     if ( (IoctlResult == FALSE) || (ReturnedLength != 0) )
00187     {
00188         return 1;
00189     }
00190 
00191     return 0;
00192 
00193 }
00194 
00195 long CCameraIO_PCI::ReadLine( long SkipPixels, long Pixels, unsigned short* pLineBuffer )
00196 {
00197     BOOLEAN IoctlResult;
00198     ULONG   InBuffer[3];
00199     ULONG   ReturnedLength; // Number of bytes returned in output buffer
00200     ULONG   NumBytes;
00201     USHORT* DataBuffer;
00202 
00203     InBuffer[0] = RegPCI_ImageData;
00204     InBuffer[1] = SkipPixels;           // Data points to skip
00205     InBuffer[2] = Pixels;               // Data points to keep
00206     
00207     NumBytes    = Pixels * sizeof( unsigned short );
00208     DataBuffer  = pLineBuffer;
00209 
00210     if ( !m_TDI )
00211     {
00213         // Clock out the line
00214         m_RegShadow[ Reg_Command ] |= RegBit_StartNextLine;     // set bit to 1
00215         Write( Reg_Command, m_RegShadow[ Reg_Command ] );
00216         
00217         m_RegShadow[ Reg_Command ] &= ~RegBit_StartNextLine;    // set bit to 0
00218         Write( Reg_Command, m_RegShadow[ Reg_Command ] );
00220     }
00221 
00222     if ( m_IsWDM )
00223     {
00224         IoctlResult = DeviceIoControl(
00225             m_hDriver,                  // Handle to device
00226             IOCTL_WDM_READ_PCI_LINE,    // IO Control code for Read line
00227             &InBuffer,                  // Buffer to driver.
00228             sizeof(InBuffer),           // Length of buffer in bytes.
00229             DataBuffer,                 // Buffer from driver.
00230             NumBytes,                   // Length of buffer in bytes.
00231             &ReturnedLength,            // Bytes placed in DataBuffer.
00232             NULL                        // NULL means wait till op. completes.
00233             );
00234     }
00235     else
00236     {
00237         IoctlResult = DeviceIoControl(
00238             m_hDriver,                  // Handle to device
00239             IOCTL_GPD_READ_PCI_LINE,    // IO Control code for Read line
00240             &InBuffer,                  // Buffer to driver.
00241             sizeof(InBuffer),           // Length of buffer in bytes.
00242             DataBuffer,                 // Buffer from driver.
00243             NumBytes,                   // Length of buffer in bytes.
00244             &ReturnedLength,            // Bytes placed in DataBuffer.
00245             NULL                        // NULL means wait till op. completes.
00246             );
00247     }
00248 
00249     if ( (ReturnedLength != NumBytes) || (!IoctlResult) )
00250     {
00251         return 1;                   // Failed to get line info
00252     }
00253 
00255     // Assert done reading line
00256     m_RegShadow[ Reg_Command ] |= RegBit_DoneReading;   // set bit to 1
00257     Write( Reg_Command, m_RegShadow[ Reg_Command ] );
00258     
00259     m_RegShadow[ Reg_Command ] &= ~RegBit_DoneReading;  // set bit to 0
00260     Write( Reg_Command, m_RegShadow[ Reg_Command ] );
00262 
00263     if ( !m_TDI )
00264     {
00266         // Wait until camera is done
00267         clock_t StopTime = clock() + CLOCKS_PER_SEC;    // wait at most one second
00268         while ( true )
00269         {
00270             unsigned short val = 0;
00271             Read( Reg_Status, val );
00272             if ( ( val & RegBit_LineDone ) != 0 ) break;// Line done
00273             
00274             if ( clock() > StopTime ) return 1;     // Timed out
00275         }
00276     }
00277 
00278     return 0;
00279 }
00280 
00281 bool CCameraIO_PCI::InitDriver()
00282 {
00283     OSVERSIONINFO   OSVerInfo;
00284     BOOLEAN         IsPostWin98OS;
00285     BOOLEAN         IsNT4OS;
00286     BOOLEAN         IsPostNT4OS;
00287 
00288     IsPostWin98OS   = false;
00289     IsNT4OS         = false;
00290     IsPostNT4OS     = false;
00291 
00292     CloseDriver();
00293 
00294     OSVerInfo.dwOSVersionInfoSize  = sizeof ( OSVERSIONINFO );
00295     GetVersionEx( &OSVerInfo );
00296 
00297     // Check for Win9x versions.  Pre-Win98 is unsupported.
00298     if ( OSVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) 
00299     {
00300         // Check for pre-Win98
00301         if (( OSVerInfo.dwMajorVersion < 4 ) ||
00302             (( OSVerInfo.dwMajorVersion == 4 ) && ( OSVerInfo.dwMinorVersion == 0 )))
00303         {
00304             return false;       // Pre-Win98 not supported
00305         }
00306         else
00307         {
00308             IsPostWin98OS = true;
00309         }
00310 
00311     }
00312     else if ( OSVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT )
00313     {
00314         // Check if NT4
00315         if ( OSVerInfo.dwMajorVersion < 4 )
00316         {
00317             // NT3.51 is not supported.  Right??
00318             return false;
00319         }
00320         else if (OSVerInfo.dwMajorVersion == 4 )
00321         {
00322             IsNT4OS = true;
00323         }
00324         else if (OSVerInfo.dwMajorVersion > 4 )
00325         {
00326             IsPostNT4OS = true;
00327         }
00328     }
00329 
00330     if ( IsNT4OS )
00331     {
00332         ULONG ReturnedLength;
00333         ULONG DataBuffer[2];
00334 
00335         // Open the driver
00336         m_hDriver = CreateFile(
00337             "\\\\.\\ApogeeIO",
00338             GENERIC_WRITE | GENERIC_READ,
00339             FILE_SHARE_WRITE | FILE_SHARE_READ,
00340             NULL,
00341             OPEN_EXISTING,
00342             0,
00343             NULL);
00344 
00345         if ( m_hDriver == INVALID_HANDLE_VALUE )
00346         {
00347             m_hDriver = NULL;
00348             return false;
00349         }
00350         
00351         BOOL IoctlResult = DeviceIoControl(
00352             m_hDriver,              // Handle to device
00353             IOCTL_PCI_BUS_SCAN,     // IO Control code for PCI Bus Scan
00354             NULL,                   // Buffer to driver.
00355             0,                      // Length of buffer in bytes.
00356             DataBuffer,             // Buffer from driver.
00357             sizeof( DataBuffer ),   // Length of buffer in bytes.
00358             &ReturnedLength,        // Bytes placed in DataBuffer.
00359             NULL                    // NULL means wait till op. completes.
00360             );
00361 
00362         if ( (!IoctlResult) || (ReturnedLength != sizeof(DataBuffer)) )
00363         {
00364             return false;
00365         }
00366     }
00367     else if ( IsPostWin98OS || IsPostNT4OS )
00368     {
00369         // Should be okay to use the WDM driver.  Note that the kernel 
00370         // driver will actually check to see if WDM services are available
00371 
00372         // Open the driver
00373         m_hDriver = CreateFile(
00374             "\\\\.\\ApPCI",
00375             GENERIC_WRITE | GENERIC_READ,
00376             FILE_SHARE_WRITE | FILE_SHARE_READ,
00377             NULL,
00378             OPEN_EXISTING,
00379             0,
00380             NULL
00381             );
00382 
00383         if ( m_hDriver == INVALID_HANDLE_VALUE )
00384         {
00385             m_hDriver = NULL;
00386             return false;
00387         }
00388 
00389         // Safe to assume we're using the WDM driver at this point.
00390         m_IsWDM = true;
00391     }
00392 
00393     return true;
00394 }
00395 
00396 void CCameraIO_PCI::CloseDriver()
00397 {    
00398     // Close the driver if it already exists
00399     if ( m_hDriver != NULL ) 
00400     {
00401         CloseHandle ( m_hDriver );
00402     }
00403 
00404     m_hDriver = NULL;
00405 }

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal