usbp_cyusb.cpp

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00032 #include <stdio.h>
00033 #include <string>
00034 #include <cassert>
00035 #include "usbp_cyusb.h"
00036 #include "upl_err.h"
00037 
00038 using namespace std;
00039 
00050 int CUsbpCyUSB::deviceInfo()
00051 {
00052   int err = UPL_OK;
00053   //String s;
00054   
00055   if(USBDevice == NULL){
00056     err = UPL_USB_ERR;
00057     printf("OPEN First!\n");
00058   }
00059   else{
00060         printf("USBDevice ConfigCount\n", USBDevice->ConfigCount()); 
00061     for(int c=0; c<USBDevice->ConfigCount(); c++){
00062       CCyUSBConfig cfg = USBDevice->GetUSBConfig(c);
00063       
00064       // \todo Write to the Status report, not directly print.
00065       printf("bLength : 0x%x\n", cfg.bLength);
00066       printf("bDescriptorType: %d (0x%x)\n", cfg.wTotalLength,cfg.wTotalLength);
00067       printf("bLength: 0x%x\n",cfg.bLength); 
00068       printf("bDescriptorType: %d\n",cfg.bDescriptorType);   
00069       printf("wTotalLength: %d (0x%x)\n",cfg.wTotalLength,cfg.wTotalLength);   
00070       printf("bNumInterfaces: %d\n",cfg.bNumInterfaces);   
00071       printf("bConfigurationValue: %d\n",cfg.bConfigurationValue);   
00072       printf("iConfiguration: %d\n",cfg.iConfiguration);   
00073       printf("bmAttributes: 0x%x\n",cfg.bmAttributes); 
00074       printf("MaxPower: %d\n",cfg.MaxPower); 
00075       printf("**********************************\n");
00076       
00077       for (int i=0; i<cfg.AltInterfaces; i++) {
00078         CCyUSBInterface *ifc = cfg.Interfaces[i];
00079         //printf("Interface Descriptor:" + String(i+1));
00080         printf("--------------------------------\n");
00081         printf("bLength: 0x%x\n",ifc->bLength); 
00082         printf("bDescriptorType: %d\n",ifc->bDescriptorType); 
00083         printf("bInterfaceNumber: %d\n",ifc->bInterfaceNumber);   
00084         printf("bAlternateSetting: %d\n",ifc->bAlternateSetting); 
00085         printf("bNumEndpoints: %d\n",ifc->bNumEndpoints); 
00086         printf("bInterfaceClass: %d\n",ifc->bInterfaceClass); 
00087         
00088         for (int e=0; e<ifc->bNumEndpoints; e++) {
00089           CCyUSBEndPoint *ept = ifc->EndPoints[e+1];
00090           //printf("EndPoint Descriptor: " + String(e+1));
00091           printf("--------------------------------\n");
00092           printf("bLength: 0x%x\n",ept->DscLen); 
00093           printf("bDescriptorType: %d\n",ept->DscType); 
00094           printf("bEndpointAddress: 0x%x\n",ept->Address); 
00095           printf("bmAttributes: 0x%x\n",ept->Attributes); 
00096           printf("wMaxPacketSize: %d\n",ept->MaxPktSize); 
00097           printf("bInterval: %d\n",ept->Interval); 
00098           printf("**********************************\n");
00099         }
00100       }
00101     }
00102   }
00103   
00104   return err;
00105 }
00106 
00107 
00112 int CUsbpCyUSB::deviceInfo(int vid, int pid)
00113 {
00114   int err = UPL_OK;
00115   
00116   return err;
00117 }
00118 
00119 
00124 int CUsbpCyUSB::deviceOpen(int vid, int pid)
00125 {
00126   int err = UPL_OK;
00127   
00128   // Don't really know what m_hWnd is??
00129   USBDevice = new CCyUSBDevice(); // m_hWnd, handle to the main window
00130   
00131   int devices = USBDevice->DeviceCount();
00132   
00133   printf("%d Devices Present\n", devices);
00134   
00135   int myDevice = -1;
00136   int vID, pID;
00137   
00138   for(int i=0; i<devices; i++){
00139     USBDevice->Open(i); // Automatically calls Close() ??
00140     vID = USBDevice->VendorID;
00141     pID = USBDevice->ProductID;
00142     printf("USBDevice[%d] vid %04X pid %04X\n", i, vID, pID);
00143     
00144     if(vID == vid && pID == pid){
00145       printf("Found Device!!\n");
00146       myDevice = i;
00147 
00148           assignEpt();
00149 
00150           break;
00151     }
00152   }
00153   
00154   if(myDevice == -1){
00155     printf("No Devices Found\n");
00156     USBDevice->Open(myDevice);
00157     err = UPL_ERR;
00158   }
00159   
00160   return err;
00161 }
00162 
00163 
00168 int CUsbpCyUSB::deviceStatus()
00169 {
00170   int err = UPL_OK;
00171   PCHAR status = NULL;
00172   LONG stat = NULL;
00173   
00174   USBDevice->UsbdStatusString(stat,status);
00175   
00176   printf("%s (%d)\n", status, stat);
00177   
00178   return err;
00179 }
00180 
00181 
00186 int CUsbpCyUSB::deviceCheck()
00187 {
00188   int err = UPL_OK;
00189   
00190   return err;
00191 }
00192 
00193 
00198 int CUsbpCyUSB::deviceClose(int vid, int pid)
00199 {
00200   int err = UPL_OK;
00201   
00202   if(USBDevice){
00203     delete USBDevice;
00204     USBDevice = NULL;
00205   }
00206 
00207   BulkOutEpt2 = NULL;
00208   BulkInEpt6  = NULL;
00209   BulkOutEpt4 = NULL;
00210   BulkInEpt8  = NULL;
00211 
00212   return err;
00213 }
00214 
00215 
00220 int CUsbpCyUSB::controlWrite(
00221                             byte reqCode,
00222                             unsigned short value,
00223                             unsigned short index,
00224                             byte *b, int len, int timeout)
00225 {
00226   
00227   int err = UPL_OK;
00228   int xferOk = 1;
00229   LONG lenr = len;
00230   
00231   CCyControlEndPoint *ept = USBDevice->ControlEndPt;
00232   
00233   if(ept){
00234     ept->Target    = TGT_DEVICE;    // ?? Examples Usually 
00235     ept->ReqType   = REQ_VENDOR;    // For these purposes control always vendor
00236     ept->Direction = DIR_TO_DEVICE; //
00237     ept->ReqCode   = reqCode;       // 
00238     ept->Value     = value;         //
00239     ept->Index     = index;         //
00240     
00241     ept->TimeOut = timeout*1000; // 4 seconds, more than enough time.
00242     xferOk = ept->XferData(b, lenr);
00243 
00245         if(!xferOk && (reqCode != 0x02)) {
00246                 printf("ControlWrite USB Error 0x%x\n", xferOk);
00247                 err = UPL_ERR;
00248         }
00249   }
00250   else{
00251     err = UPL_USB_ERR;
00252   }
00253   
00254   if((len != lenr) && (reqCode != 0x02)){
00255     err = UPL_ERR;
00256     printf("Control Write bad len %d %d\n", len, lenr);
00257   }
00258   
00259   return err;
00260 }
00261 
00262 
00267 int CUsbpCyUSB::bulkWrite(int ep, int size, byte *b)
00268 {
00269   bool xferOk;
00270   int err = UPL_OK;
00271   CCyBulkEndPoint *ept;
00272   LONG lenr = size;
00273 
00274   if(ep == 2)
00275     ept = BulkOutEpt2; //(CCyBulkEndPoint *)USBDevice->EndPoints[0];
00276   else if(ep == 4)
00277     ept = BulkOutEpt4; //(CCyBulkEndPoint *)USBDevice->EndPoints[1];
00278   else
00279     ept = 0;
00280   
00281   if(ept){ 
00282     xferOk = ept->XferData(b, lenr);
00283     
00284     if(!xferOk){
00285       fprintf(stderr, "-- XferData(%x, %d) FAILED == %d\n",(unsigned int)b,lenr,xferOk);
00286       deviceInfo();
00287       err = UPL_ERR;
00288     }
00289   }
00290   else{
00291     fprintf(stderr, "-- Not a Valid Endpoint!");
00292     err = UPL_ERR;
00293   }
00294 
00295   return err;
00296 }
00297 
00302 int CUsbpCyUSB::bulkRead(int ep, int size, byte *b)
00303 {
00304   bool xferOk;
00305   int err = UPL_OK;
00306   CCyBulkEndPoint *ept;
00307   LONG lenr = size;
00308 
00309   if(ep == 6)
00310     ept = BulkInEpt6; //(CCyBulkEndPoint *)USBDevice->EndPoints[0];
00311   else if(ep == 8)
00312     ept = BulkInEpt8; //(CCyBulkEndPoint *)USBDevice->EndPoints[1];
00313   else
00314     ept = 0;
00315   
00316   if(ept){ 
00317     xferOk = ept->XferData(b, lenr);
00318     
00319     if(!xferOk){
00320       fprintf(stderr, "-- XferData(%x, %d) FAILED == %d\n",(unsigned int)b,lenr,xferOk);
00321       deviceInfo();
00322       err = UPL_ERR;
00323     }
00324   }
00325   else{
00326     fprintf(stderr, "-- Not a Valid Endpoint!");
00327     err = UPL_ERR;
00328   }
00329 
00330   return err;
00331 }
00332 
00337 int CUsbpCyUSB::bulkWriteRead(int ep_out, int ep_in, int size, byte *b)
00338 {
00339   int err = UPL_OK;
00340   
00341   return err;
00342 }
00343 
00344 
00349 int CUsbpCyUSB::assignEpt()
00350 {
00351   
00352   int eptCount = USBDevice->EndPointCount();
00353   printf("Number of Endpoints present %d\n", eptCount);
00354   for (int i=1; i<eptCount; i++) {
00355     byte addr = USBDevice->EndPoints[i]->Address & 0x7F;
00356     bool bIn =   ((USBDevice->EndPoints[i]->Address & 0x80) == 0x80);
00357     bool bBulk = (USBDevice->EndPoints[i]->Attributes == 2);
00358     
00359     printf("Assign Endpoint 0x%x", addr);
00360     if(bIn) printf(" Bulk In"); else printf(" Bulk Out");
00361     switch(addr){
00362     case 0x02: BulkOutEpt2 = (CCyBulkEndPoint *) USBDevice->EndPoints[i];  printf(" to BulkOutEpt2\n"); break;
00363     case 0x06: BulkInEpt6  = (CCyBulkEndPoint *) USBDevice->EndPoints[i];  printf(" to BulkInEpt6\n"); break;
00364     case 0x04: BulkOutEpt4 = (CCyBulkEndPoint *) USBDevice->EndPoints[i];  printf(" to BulkOutEpt4\n"); break;
00365     case 0x08: BulkInEpt8  = (CCyBulkEndPoint *) USBDevice->EndPoints[i];  printf(" to BulkInEpt8\n"); break;
00366     default: printf(" to no EPT\n");
00367     }
00368   }
00369   
00370   return UPL_OK;
00371 }
00372 
00373 

Generated on Mon Mar 5 20:44:19 2007 for usbp by  doxygen 1.5.1-p1