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 <usb.h>
00019 #include "ApogeeUsb.h"
00020 #include "ApogeeUsbErr.h"
00021 #include "ApogeeLinux.h"
00022
00023
00024 #define HANDLE struct usb_dev_handle;
00025 #define ULONG unsigned int
00026 #define BOOLEAN unsigned int
00027 #define USHORT unsigned short
00028
00029 #define APOGEE_USB_DEVICE "/dev/usb/alta"
00030 #define INVALID_HANDLE_VALUE -1
00031
00032 #define VND_ANCHOR_LOAD_INTERNAL 0xA0
00033 #define VND_APOGEE_CMD_BASE 0xC0
00034 #define VND_APOGEE_STATUS ( VND_APOGEE_CMD_BASE + 0x0 )
00035 #define VND_APOGEE_CAMCON_REG ( VND_APOGEE_CMD_BASE + 0x2 )
00036 #define VND_APOGEE_BUFCON_REG ( VND_APOGEE_CMD_BASE + 0x3 )
00037 #define VND_APOGEE_SET_SERIAL ( VND_APOGEE_CMD_BASE + 0x4 )
00038 #define VND_APOGEE_SERIAL ( VND_APOGEE_CMD_BASE + 0x5 )
00039 #define VND_APOGEE_EEPROM ( VND_APOGEE_CMD_BASE + 0x6 )
00040 #define VND_APOGEE_SOFT_RESET ( VND_APOGEE_CMD_BASE + 0x8 )
00041 #define VND_APOGEE_GET_IMAGE ( VND_APOGEE_CMD_BASE + 0x9 )
00042 #define VND_APOGEE_STOP_IMAGE ( VND_APOGEE_CMD_BASE + 0xA )
00043
00044 #define USB_ALTA_VENDOR_ID 0x125c
00045 #define USB_ALTA_PRODUCT_ID 0x0010
00046 #define USB_DIR_IN USB_ENDPOINT_IN
00047 #define USB_DIR_OUT USB_ENDPOINT_OUT
00048
00049
00050
00051 struct usb_dev_handle *g_hSysDriver;
00052 ULONG g_UsbImgSizeBytes;
00053 char controlBuffer[1024];
00054
00055 #define IMAGE_BUFFER_SIZE 126976 // Number of requested bytes in a transfer
00056
00057
00058
00059
00060 APN_USB_TYPE ApnUsbOpen( unsigned short )
00061 {
00062
00063
00064 struct usb_bus *bus;
00065 struct usb_device *dev;
00066 struct usb_dev_handle *hDevice(NULL);
00067
00068 usb_init();
00069
00070 usb_find_busses();
00071 usb_find_devices();
00072
00073
00074
00075 int found = 0;
00076
00077
00078 for(bus = usb_busses; bus && !found; bus = bus->next) {
00079 for(dev = bus->devices; dev && !found; dev = dev->next) {
00080 if (dev->descriptor.idVendor == USB_ALTA_VENDOR_ID &&
00081 dev->descriptor.idProduct == USB_ALTA_PRODUCT_ID) {
00082 hDevice = usb_open(dev);
00083
00084 found = 1;
00085 if (hDevice) {
00086
00087
00088
00089
00090
00091
00092 }
00093 else return APN_USB_ERR_OPEN;
00094 }
00095 }
00096 }
00097
00098 if (!found) return APN_USB_ERR_OPEN;
00099
00100 if (!usb_claim_interface(hDevice, 0x0)) return APN_USB_ERR_OPEN;
00101
00102 g_hSysDriver = hDevice;
00103 g_UsbImgSizeBytes = 0;
00104
00105
00106 return APN_USB_SUCCESS;
00107 }
00108
00109
00110 APN_USB_TYPE ApnUsbClose( void )
00111 {
00112
00113 if ( g_hSysDriver != 0 )
00114 {
00115 usb_release_interface(g_hSysDriver, 0x0);
00116 usb_close(g_hSysDriver);
00117 g_hSysDriver = 0;
00118 }
00119
00120 return APN_USB_SUCCESS;
00121 }
00122
00123
00124
00125 APN_USB_TYPE ApnUsbReadReg( unsigned short FpgaReg, unsigned short *FpgaData )
00126 {
00127 int Success;
00128 unsigned short RegData;
00129
00130 Success = usb_control_msg((struct usb_dev_handle *)g_hSysDriver,
00131 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, VND_APOGEE_CAMCON_REG,
00132 FpgaReg, FpgaReg,(char *)&RegData, 2, 50);
00133 *FpgaData = RegData;
00134
00135
00136 if ( !Success )
00137 return APN_USB_ERR_WRITE;
00138 return APN_USB_SUCCESS;
00139 }
00140
00141 APN_USB_TYPE ApnUsbWriteReg( unsigned short FpgaReg, unsigned short FpgaData )
00142 {
00143 char *cbuf;
00144 int Success;
00145
00146 cbuf = (char *)&FpgaData;
00147 Success = usb_control_msg((struct usb_dev_handle *)g_hSysDriver,
00148 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, VND_APOGEE_CAMCON_REG,
00149 0, FpgaReg, cbuf, 2, 50);
00150
00151 if ( !Success )
00152 return APN_USB_ERR_WRITE;
00153 return APN_USB_SUCCESS;
00154
00155
00156 }
00157
00158
00159
00160
00161 APN_USB_TYPE ApnUsbWriteRegMulti( unsigned short FpgaReg, unsigned short FpgaData[], unsigned short RegCount )
00162 {
00163 unsigned short Counter;
00164
00165 for ( Counter=0; Counter<RegCount; Counter++ )
00166 {
00167 if ( ApnUsbWriteReg( FpgaReg, FpgaData[Counter] ) != APN_USB_SUCCESS )
00168 {
00169 return APN_USB_ERR_WRITE;
00170 }
00171 }
00172
00173 return APN_USB_SUCCESS;
00174 }
00175
00176 APN_USB_TYPE ApnUsbWriteRegMultiMRMD( unsigned short FpgaReg[],
00177 unsigned short FpgaData[],
00178 unsigned short RegCount )
00179 {
00180 unsigned short Counter;
00181
00182 for ( Counter=0; Counter<RegCount; Counter++ )
00183 {
00184 if ( ApnUsbWriteReg( FpgaReg[Counter],
00185 FpgaData[Counter] ) != APN_USB_SUCCESS )
00186 {
00187 return APN_USB_ERR_WRITE;
00188 }
00189 }
00190
00191 return APN_USB_SUCCESS;
00192 }
00193
00194
00195 APN_USB_TYPE ApnUsbReadStatusRegs( unsigned short *StatusReg,
00196 unsigned short *HeatsinkTempReg,
00197 unsigned short *CcdTempReg,
00198 unsigned short *CoolerDriveReg,
00199 unsigned short *VoltageReg,
00200 unsigned short *TdiCounter,
00201 unsigned short *SequenceCounter )
00202 {
00203 BOOLEAN Success;
00204
00205 unsigned short *Data;
00206 unsigned char StatusData[21];
00207
00208 Success = usb_control_msg(g_hSysDriver,
00209 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, VND_APOGEE_STATUS,
00210 0, 0, (char *)&StatusData, 21, 3000);
00211
00212
00213
00214 Data = (unsigned short *)StatusData;
00215
00216 *HeatsinkTempReg = Data[0];
00217 *CcdTempReg = Data[1];
00218 *CoolerDriveReg = Data[2];
00219 *VoltageReg = Data[3];
00220 *TdiCounter = Data[4];
00221 *SequenceCounter = Data[5];
00222 *StatusReg = Data[6];
00223
00224 if ( (StatusData[20] & 0x01) != 0 )
00225 {
00226 *StatusReg |= 0x8;
00227 }
00228
00229 return APN_USB_SUCCESS;
00230 }
00231
00232
00233 APN_USB_TYPE ApnUsbStartExp( unsigned short ImageWidth,
00234 unsigned short ImageHeight )
00235 {
00236 BOOLEAN Success;
00237 ULONG ImageSize;
00238 unsigned short BytesReceived;
00239
00240
00241
00242
00243
00244
00245
00246 g_UsbImgSizeBytes = ImageWidth * ImageHeight * 2;
00247 ImageSize = ImageWidth * ImageHeight;
00248
00249 if ( g_UsbImgSizeBytes == 0 )
00250 {
00251 return APN_USB_ERR_START_EXP;
00252 }
00253
00254 Success = usb_control_msg(g_hSysDriver,
00255 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, VND_APOGEE_GET_IMAGE,
00256 (ImageSize >> 16) & 0xFFFF,
00257 (ImageSize & 0xFFFF), (char *)&BytesReceived, 4, 3000);
00258
00259
00260
00261
00262
00263
00264
00265
00266 return APN_USB_SUCCESS;
00267 }
00268
00269
00270 APN_USB_TYPE ApnUsbStopExp( bool DigitizeData )
00271 {
00272 BOOLEAN Success;
00273 unsigned short BytesReceived;
00274
00275
00276
00277
00278
00279
00280
00281 if ( DigitizeData == false )
00282 {
00283 Success = usb_control_msg(g_hSysDriver,
00284 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, VND_APOGEE_STOP_IMAGE,
00285 0, 0,(char *)&BytesReceived, 2, 3000);
00286
00287
00288
00289
00290
00291 }
00292
00293 return APN_USB_SUCCESS;
00294 }
00295
00296 APN_USB_TYPE ApnUsbGetImage( unsigned short *pMem )
00297 {
00298 BOOLEAN Success;
00299 ULONG ImageBytesRemaining;
00300 ULONG ReceivedSize;
00301
00302 Success = 1;
00303
00304
00305
00306
00307
00308 ImageBytesRemaining = g_UsbImgSizeBytes;
00309
00310
00312 ULONG LoopCount = g_UsbImgSizeBytes / IMAGE_BUFFER_SIZE;
00313 ULONG Remainder = g_UsbImgSizeBytes - ( LoopCount * IMAGE_BUFFER_SIZE );
00314 ULONG MemIterator = IMAGE_BUFFER_SIZE / 2;
00315 ULONG Counter;
00316
00317
00318 for ( Counter=0; Counter<LoopCount; Counter++ )
00319 {
00320 ReceivedSize = usb_bulk_read(g_hSysDriver, 0x86,
00321 (char *)pMem, IMAGE_BUFFER_SIZE, 1000);
00322
00323
00324 if ( ReceivedSize != IMAGE_BUFFER_SIZE )
00325 {
00326 Success = 0;
00327 break;
00328 }
00329 else
00330 {
00331 pMem += MemIterator;
00332 printf(".");
00333 }
00334 }
00335
00336 if ( Remainder != 0 )
00337 {
00338 ReceivedSize = usb_bulk_read(g_hSysDriver, 0x86,
00339 (char *)pMem, Remainder, 1000);
00340
00341
00342 if ( ReceivedSize != Remainder )
00343 Success = 0;
00344 }
00345 printf("\n");
00346
00347 if ( !Success )
00348 return APN_USB_ERR_IMAGE_DOWNLOAD;
00349
00350 return APN_USB_SUCCESS;
00351 }
00352
00353
00354 APN_USB_TYPE ApnUsbReset()
00355 {
00356 BOOLEAN Success;
00357 unsigned short BytesReceived;
00358
00359
00360
00361
00362
00363
00364 Success = usb_control_msg(g_hSysDriver,
00365 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, VND_APOGEE_SOFT_RESET,
00366 0, 0, (char *)&BytesReceived, 2, 3000);
00367
00368
00369 if ( !Success )
00370 {
00371 return APN_USB_ERR_RESET;
00372 }
00373
00374 return APN_USB_SUCCESS;
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394