00001
00002
00003
00004 #include <assert.h>
00005 #include <sys/time.h>
00006 #include <sys/resource.h>
00007 #include <sys/ioctl.h>
00008 #include <string.h>
00009 #include <sched.h>
00010 #include <unistd.h>
00011 #include <fcntl.h>
00012 #include <sys/types.h>
00013 #include <sys/stat.h>
00014 #include <fcntl.h>
00015 #include <stdio.h>
00016
00017
00018 #include "ApogeeUsb.h"
00019 #include "ApogeeUsbErr.h"
00020 #include "ApogeeLinux.h"
00021
00022
00023 #define HANDLE unsigned int
00024 #define ULONG unsigned int
00025 #define BOOLEAN unsigned int
00026 #define USHORT unsigned short
00027
00028 #define APOGEE_USB_DEVICE "/dev/usb/alta"
00029 #define INVALID_HANDLE_VALUE -1
00030
00031
00032
00033 HANDLE g_hSysDriver;
00034 ULONG g_UsbImgSizeBytes;
00035
00036
00037
00038
00039
00040
00041
00042
00043 #define IMAGE_BUFFER_SIZE 126976 // Number of requested bytes in a transfer
00044
00045
00046
00047
00048 APN_USB_TYPE ApnUsbOpen( unsigned short DevNumber )
00049 {
00050
00051 char deviceName[128];
00052
00053 g_hSysDriver = 0;
00054 g_UsbImgSizeBytes = 0;
00055
00056
00057 sprintf(deviceName,"%s%d",APOGEE_USB_DEVICE,DevNumber);
00058 g_hSysDriver = ::open(deviceName,O_RDONLY);
00059
00060 if ( g_hSysDriver == INVALID_HANDLE_VALUE )
00061 {
00062 return APN_USB_ERR_OPEN;
00063 }
00064
00065 return APN_USB_SUCCESS;
00066 }
00067
00068
00069 APN_USB_TYPE ApnUsbClose( void )
00070 {
00071 if ( (g_hSysDriver != INVALID_HANDLE_VALUE ) && (g_hSysDriver != 0) )
00072 {
00073 ::close( g_hSysDriver );
00074 g_hSysDriver = 0;
00075 }
00076
00077 return APN_USB_SUCCESS;
00078 }
00079
00080
00081 APN_USB_TYPE ApnUsbDiscovery( unsigned short *UsbCamCount,
00082 APN_USB_CAMINFO UsbCamInfo[] )
00083 {
00084 HANDLE hDriver;
00085 char deviceName[64];
00086 unsigned short RegNumber;
00087 unsigned short retval;
00088 struct apIOparam request;
00089 USHORT RegData;
00090 *UsbCamCount = 0;
00091
00092
00093 for ( int i=0; i<APN_USB_MAXCAMERAS; i++ )
00094 {
00095 NULL,
00096 NULL,
00097
00098
00099 sprintf(deviceName,"%s%d",APOGEE_USB_DEVICE,i);
00100 hDriver = ::open(deviceName,O_RDONLY);
00101
00102 if ( hDriver != INVALID_HANDLE_VALUE )
00103 {
00104
00105 UsbCamInfo[*UsbCamCount].CamNumber = i;
00106
00107
00108 BOOLEAN Success;
00109 USHORT FpgaReg;
00110 USHORT RegData;
00111 ULONG BytesReceived;
00112
00113 FpgaReg = 100;
00114 request.reg = FpgaReg;
00115 request.param1=(unsigned long)&retval;
00116 Success=ioctl(hDriver,APUSB_READ_USHORT,(unsigned long)&request);
00117 RegData = (unsigned short)retval;
00118
00119 if ( Success )
00120 {
00121 UsbCamInfo[*UsbCamCount].CamModel = RegData & 0x00FF;
00122 (*UsbCamCount)++;
00123 }
00124 }
00125
00126 }
00127
00128 return APN_USB_SUCCESS;
00129 }
00130
00131
00132 APN_USB_TYPE ApnUsbReadReg( unsigned short FpgaReg, unsigned short *FpgaData )
00133 {
00134 BOOLEAN Success;
00135 USHORT RegData;
00136 unsigned short RegNumber;
00137 unsigned short retval;
00138 struct apIOparam request;
00139
00140 if ( (g_hSysDriver == INVALID_HANDLE_VALUE) || (g_hSysDriver == 0) )
00141 {
00142 return APN_USB_ERR_OPEN;
00143 }
00144
00145
00146 request.reg = FpgaReg;
00147 request.param1=(unsigned long)&retval;
00148 Success=ioctl(g_hSysDriver,APUSB_READ_USHORT,(unsigned long)&request);
00149 RegData = (unsigned short)retval;
00150
00151 if ( (!Success) )
00152 {
00153 return APN_USB_ERR_READ;
00154 }
00155
00156 *FpgaData = RegData;
00157
00158 return APN_USB_SUCCESS;
00159 }
00160
00161
00162 APN_USB_TYPE ApnUsbWriteReg( unsigned short FpgaReg, unsigned short FpgaData )
00163 {
00164 BOOLEAN Success;
00165 unsigned short RegNumber;
00166 struct apIOparam request;
00167
00168 if ( (g_hSysDriver == INVALID_HANDLE_VALUE) || (g_hSysDriver == 0) )
00169 {
00170 return APN_USB_ERR_OPEN;
00171 }
00172
00173 request.reg = FpgaReg;
00174 request.param1=(int)FpgaData;
00175 Success=ioctl(g_hSysDriver,APUSB_WRITE_USHORT,(unsigned long)&request);
00176 if ( !Success )
00177 return APN_USB_ERR_WRITE;
00178
00179 return APN_USB_SUCCESS;
00180 }
00181
00182
00183 APN_USB_TYPE ApnUsbWriteRegMulti( unsigned short FpgaReg, unsigned short FpgaData[], unsigned short RegCount )
00184 {
00185 unsigned short Counter;
00186
00187 for ( Counter=0; Counter<RegCount; Counter++ )
00188 {
00189 if ( ApnUsbWriteReg( FpgaReg, FpgaData[Counter] ) != APN_USB_SUCCESS )
00190 {
00191 return APN_USB_ERR_WRITE;
00192 }
00193 }
00194
00195 return APN_USB_SUCCESS;
00196 }
00197
00198 APN_USB_TYPE ApnUsbWriteRegMultiMRMD( unsigned short FpgaReg[],
00199 unsigned short FpgaData[],
00200 unsigned short RegCount )
00201 {
00202 unsigned short Counter;
00203
00204 for ( Counter=0; Counter<RegCount; Counter++ )
00205 {
00206 if ( ApnUsbWriteReg( FpgaReg[Counter], FpgaData[Counter] ) != APN_USB_SUCCESS )
00207 {
00208 return APN_USB_ERR_WRITE;
00209 }
00210 }
00211
00212 return APN_USB_SUCCESS;
00213 }
00214
00215
00216 APN_USB_TYPE ApnUsbReadStatusRegs( unsigned short *StatusReg,
00217 unsigned short *HeatsinkTempReg,
00218 unsigned short *CcdTempReg,
00219 unsigned short *CoolerDriveReg,
00220 unsigned short *VoltageReg,
00221 unsigned short *TdiCounter,
00222 unsigned short *SequenceCounter )
00223 {
00224 BOOLEAN Success;
00225 unsigned int BytesReceived;
00226 unsigned short RegNumber;
00227 struct apIOparam request;
00228 unsigned short *Data;
00229 unsigned char StatusData[21];
00230
00231 request.reg = 0;
00232 request.param1=(unsigned long)&StatusData;
00233 Success=ioctl(g_hSysDriver,APUSB_READ_STATUS,(unsigned long)&request);
00234
00235
00236
00237 Data = (unsigned short *)StatusData;
00238
00239 *HeatsinkTempReg = Data[0];
00240 *CcdTempReg = Data[1];
00241 *CoolerDriveReg = Data[2];
00242 *VoltageReg = Data[3];
00243 *TdiCounter = Data[4];
00244 *SequenceCounter = Data[5];
00245 *StatusReg = Data[6];
00246
00247 if ( (StatusData[20] & 0x01) != 0 )
00248 {
00249 *StatusReg |= 0x8;
00250 }
00251
00252 return APN_USB_SUCCESS;
00253 }
00254
00255
00256 APN_USB_TYPE ApnUsbStartExp( unsigned short ImageWidth,
00257 unsigned short ImageHeight )
00258 {
00259 BOOLEAN Success;
00260 ULONG ImageSize;
00261 ULONG BytesReceived;
00262 unsigned short RegNumber;
00263 struct apIOparam request;
00264
00265
00266 if ( (g_hSysDriver == INVALID_HANDLE_VALUE) || (g_hSysDriver == 0) )
00267 {
00268 return APN_USB_ERR_OPEN;
00269 }
00270
00271 g_UsbImgSizeBytes = ImageWidth * ImageHeight * 2;
00272 ImageSize = ImageWidth * ImageHeight;
00273
00274 if ( g_UsbImgSizeBytes == 0 )
00275 {
00276 return APN_USB_ERR_START_EXP;
00277 }
00278
00279 request.reg = (int)ImageSize;
00280 request.param1= 0;
00281 Success=ioctl(g_hSysDriver,APUSB_PRIME_USB_DOWNLOAD,(unsigned long)&request);
00282
00283 if ( !Success )
00284 {
00285 return APN_USB_ERR_START_EXP;
00286 }
00287
00288 return APN_USB_SUCCESS;
00289 }
00290
00291
00292 APN_USB_TYPE ApnUsbStopExp( bool DigitizeData )
00293 {
00294 BOOLEAN Success;
00295 ULONG BytesReceived;
00296 unsigned short RegNumber;
00297 struct apIOparam request;
00298
00299
00300 if ( (g_hSysDriver == INVALID_HANDLE_VALUE) || (g_hSysDriver == 0) )
00301 {
00302 return APN_USB_ERR_OPEN;
00303 }
00304
00305 if ( DigitizeData == false )
00306 {
00307 request.reg = 0;
00308 request.param1 = 0;
00309 Success=ioctl(g_hSysDriver,APUSB_STOP_USB_IMAGE,(unsigned long)&request);
00310
00311 if ( !Success )
00312 {
00313 return APN_USB_ERR_STOP_EXP;
00314 }
00315 }
00316
00317 return APN_USB_SUCCESS;
00318 }
00319
00320
00321 APN_USB_TYPE OrigApnUsbGetImage( unsigned short *pMem )
00322 {
00323 BOOLEAN Success;
00324 ULONG ImageBytesRemaining;
00325 ULONG ReceivedSize;
00326 ULONG retval;
00327 unsigned char *pRequestData;
00328 unsigned short RegNumber;
00329 struct apIOparam request;
00330
00331
00332 if ( (g_hSysDriver == INVALID_HANDLE_VALUE) || (g_hSysDriver == 0) )
00333 {
00334 return APN_USB_ERR_OPEN;
00335 }
00336
00337 ImageBytesRemaining = g_UsbImgSizeBytes;
00338
00339
00340
00342 ULONG LoopCount = g_UsbImgSizeBytes / IMAGE_BUFFER_SIZE;
00343 ULONG Remainder = g_UsbImgSizeBytes - ( LoopCount * IMAGE_BUFFER_SIZE );
00344 ULONG MemIterator = IMAGE_BUFFER_SIZE / 2;
00345 ULONG Counter;
00346
00347
00348 for ( Counter=0; Counter<LoopCount; Counter++ )
00349 {
00350 request.reg = 0;
00351 request.param1= *pMem;
00352 request.param2=IMAGE_BUFFER_SIZE;
00353 Success=ioctl(g_hSysDriver,APUSB_READ_USB_IMAGE,(unsigned long)&request);
00354 ReceivedSize = (unsigned short)retval;
00355
00356 if ( (!Success) || (ReceivedSize != IMAGE_BUFFER_SIZE) )
00357 {
00358 Success = 0;
00359 break;
00360 }
00361 else
00362 {
00363 pMem += MemIterator;
00364 }
00365 }
00366
00367 if ( (Success) && (Remainder != 0) )
00368 {
00369 request.reg = 0;
00370 request.param1= *pMem;
00371 request.param2=Remainder;
00372 Success=ioctl(g_hSysDriver,APUSB_READ_USB_IMAGE,(unsigned long)&request);
00373 ReceivedSize = (unsigned short)retval;
00374
00375 if ( ReceivedSize != Remainder )
00376 Success = 0;
00377 }
00378
00379
00380
00381 if ( !Success )
00382 return APN_USB_ERR_IMAGE_DOWNLOAD;
00383
00384 return APN_USB_SUCCESS;
00385 }
00386
00387 APN_USB_TYPE ApnUsbGetImage( unsigned short *pMem )
00388 {
00389 BOOLEAN Success;
00390 ULONG ImageBytesRemaining;
00391 ULONG ReceivedSize;
00392 ULONG retval;
00393 unsigned char *pRequestData;
00394 unsigned short RegNumber;
00395 struct apIOparam request;
00396
00397 Success = 1;
00398 if ( (g_hSysDriver == INVALID_HANDLE_VALUE) || (g_hSysDriver == 0) )
00399 {
00400 return APN_USB_ERR_OPEN;
00401 }
00402
00403 ImageBytesRemaining = g_UsbImgSizeBytes;
00404
00405
00407 ULONG LoopCount = g_UsbImgSizeBytes / IMAGE_BUFFER_SIZE;
00408 ULONG Remainder = g_UsbImgSizeBytes - ( LoopCount * IMAGE_BUFFER_SIZE );
00409 ULONG MemIterator = IMAGE_BUFFER_SIZE / 2;
00410 ULONG Counter;
00411
00412
00413 for ( Counter=0; Counter<LoopCount; Counter++ )
00414 {
00415 ReceivedSize = read(g_hSysDriver,pMem,IMAGE_BUFFER_SIZE);
00416
00417 if ( ReceivedSize != IMAGE_BUFFER_SIZE )
00418 {
00419 Success = 0;
00420 break;
00421 }
00422 else
00423 {
00424 pMem += MemIterator;
00425 printf(".");
00426 }
00427 }
00428
00429 if ( Remainder != 0 )
00430 {
00431 ReceivedSize = read(g_hSysDriver,pMem,Remainder);
00432
00433 if ( ReceivedSize != Remainder )
00434 Success = 0;
00435 }
00436 printf("\n");
00437
00438 if ( !Success )
00439 return APN_USB_ERR_IMAGE_DOWNLOAD;
00440
00441 return APN_USB_SUCCESS;
00442 }
00443
00444
00445 APN_USB_TYPE ApnUsbReset()
00446 {
00447 BOOLEAN Success;
00448 ULONG BytesReceived;
00449 unsigned short RegNumber;
00450 struct apIOparam request;
00451
00452
00453 if ( (g_hSysDriver == INVALID_HANDLE_VALUE) || (g_hSysDriver == 0) )
00454 {
00455 return APN_USB_ERR_OPEN;
00456 }
00457
00458 request.reg = 0;
00459 request.param1 = 0;
00460 Success=ioctl(g_hSysDriver,APUSB_USB_RESET,(unsigned long)&request);
00461
00462 if ( !Success )
00463 {
00464 return APN_USB_ERR_RESET;
00465 }
00466
00467 return APN_USB_SUCCESS;
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486