⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 host_811.lst

📁 sl811 c8051F驱动程序 保证绝对可以使用 USB通信协议USB1.1
💻 LST
📖 第 1 页 / 共 3 页
字号:
 359          {
 360   1               BYTE   pid  = PID_IN;
 361   1               WORD   wLen = WordSwap(setup->wLength);                                        // swap back for correct length
 362   1               BYTE   ep0 = 0;                                                                                        // always endpoint zero
 363   1              ////////////////////////////////////////////////////////////////////////
 364   1              //----------------------------------------------------
C51 COMPILER V8.08   HOST_811                                                              04/25/2009 23:03:20 PAGE 7   

 365   1              // SETUP token with 8-byte request on endpoint 0
 366   1              //----------------------------------------------------
 367   1              if (!usbXfer(usbaddr, ep0, PID_SETUP,payload, 8, (BYTE*)setup)) 
 368   1                      return FALSE;
 369   1      
 370   1              //----------------------------------------------------
 371   1              // IN or OUT data stage on endpoint 0   
 372   1              //----------------------------------------------------
 373   1              if (wLen)                                                                                                       // if there are data for transfer
 374   1              {
 375   2                      if (setup->bmRequest & 0x80)                                                    // host-to-device : IN token
 376   2                      {
 377   3                              pid  = PID_OUT; 
 378   3                              if(!usbXfer(usbaddr, ep0, PID_IN,payload, wLen, pData))
 379   3                                      return FALSE;
 380   3                              payload = 0;
 381   3                      }
 382   2                      else                                                                                                    // device-to-host : OUT token
 383   2                      {                                                       
 384   3                              if(!usbXfer(usbaddr, ep0, PID_OUT,payload, wLen, pData))
 385   3                                      return FALSE;
 386   3                      }
 387   2              }
 388   1      
 389   1              //----------------------------------------------------
 390   1              // Status stage IN or OUT zero-length data packet
 391   1              //----------------------------------------------------
 392   1              if(!usbXfer(usbaddr, ep0, pid,payload, 0, NULL))
 393   1                      return FALSE;
 394   1      
 395   1              return TRUE;                                                                            
 396   1      }       
 397          
 398          //*****************************************************************************************
 399          // Control endpoint
 400          //*****************************************************************************************
 401          unsigned  char VendorCmd(BYTE usbaddr,BYTE bReq,BYTE bCmd,WORD wValue,WORD wIndex,WORD wLen,BYTE *pData)
 402          { 
 403   1          SetupPKG   setup;
 404   1          setup.bmRequest  = bReq;
 405   1          setup.bRequest   = bCmd;
 406   1          setup.wValue     = wValue;
 407   1          setup.wIndex     = wIndex;
 408   1          setup.wLength    = WordSwap(wLen);
 409   1              return ep0Xfer(usbaddr, uDev.wPayLoad[0], &setup, pData);
 410   1      }
 411          
 412          //*****************************************************************************************
 413          // Set Device Address : 
 414          //*****************************************************************************************
 415          unsigned  char  SetAddress(WORD addr)
 416          {
 417   1              return VendorCmd(0,0,SET_ADDRESS, WordSwap(addr), 0, 0, NULL);
 418   1      }
 419          
 420          //*****************************************************************************************
 421          // Set Device Configuration : 
 422          //*****************************************************************************************
 423          unsigned  char  SetConfiguration(BYTE usbaddr, WORD wVal)
 424          {
 425   1              return VendorCmd(usbaddr, 0, SET_CONFIG, WordSwap(wVal), 0, 0, NULL);
 426   1      }
C51 COMPILER V8.08   HOST_811                                                              04/25/2009 23:03:20 PAGE 8   

 427          
 428          //*****************************************************************************************
 429          // Get Device Descriptor : Device, Configuration, String
 430          //*****************************************************************************************
 431          unsigned  char  GetDesc(BYTE usbaddr, WORD wValue,      WORD wIndex, WORD wLen, BYTE *desc)
 432          { 
 433   1              return VendorCmd(usbaddr, 0x80, GET_DESCRIPTOR, wValue, wIndex, wLen, desc);
 434   1      }
 435          //*****************************************************************************************
 436          // USB Device Enumeration Process
 437          // Support 1 confguration and interface #0 and alternate setting #0 only
 438          // Support up to 1 control endpoint + 4 data endpoint only
 439          //*****************************************************************************************
 440          unsigned  char  EnumUsbDev(BYTE usbaddr)//地址总是1;
 441          {  
 442   1              BYTE i;                                                                                 // always reset USB transfer address 
 443   1              BYTE uAddr = 0;                                                                 // for enumeration to Address #0
 444   1              BYTE epLen;
 445   1              pDevDesc  pDev; //四个描述符;
 446   1              pCfgDesc pCfg;
 447   1              pIntfDesc pIfc;
 448   1              pEPDesc pEnp;
 449   1              ////////////////////////////////////////////////////////////////////////枚举在端点0,地址0发生;
 450   1              uDev.wPayLoad[0] = 64;                                                  // default 64-byte payload of Endpoint 0, address #0
 451   1              if(usbaddr == 1)                                                                // bus reset for the device attached to SL811HS only
 452   1                      USBReset();     // that will always have the USB address = 0x01 (for a hub)
 453   1              pDev =(pDevDesc)bBUF;                                                   // bBUF是单片机上的缓冲去;
 454   1              if (!GetDesc(uAddr,DEVICE,0,18,bBUF))
 455   1                              // and determine the wPayload size
 456   1                      return FALSE;                                                           // get correct wPayload of Endpoint 0
 457   1              uDev.wPayLoad[0]=pDev->bMaxPacketSize0;                 // on current non-zero USB address
 458   1              if (!SetAddress(usbaddr))                                               // set to specific USB address
 459   1                      return FALSE;                                                           //
 460   1              uAddr = usbaddr;                                                                // transfer using this new address
 461   1              if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),bBUF))      
 462   1                      return FALSE;                                                           // For this current device:
 463   1              uDev.wVID        = pDev->idVendor;                                      // save VID
 464   1              uDev.wPID        = pDev->idProduct;                                     // save PID
 465   1              uDev.iMfg        = pDev->iManufacturer;                         // save Mfg Index
 466   1              uDev.iPdt        = pDev->iProduct;                                      // save Product Index
 467   1              pCfg = (pCfgDesc)bBUF;                                                                  
 468   1              if (!GetDesc(uAddr,CONFIGURATION,0,8,bBUF))             
 469   1                      return FALSE;                                                                           
 470   1              if (!GetDesc(uAddr,CONFIGURATION,0,WordSwap(pCfg->wLength),bBUF))       
 471   1                      return FALSE;           
 472   1      
 473   1              pIfc = (pIntfDesc)(bBUF + 9);                                   // point to Interface Descp
 474   1              uDev.bClass     = pIfc->iClass;                                 // update to class type
 475   1              uDev.bNumOfEPs = (pIfc->bEndPoints<=MAX_EP) ? pIfc->bEndPoints : MAX_EP;
 476   1                      if (!SetConfiguration(uAddr,DEVICE))            // connected directly to SL811HS
 477   1                                      return FALSE;
 478   1              if(uDev.bClass==8)
 479   1              {       
 480   2                      USB_Control.bMassDevice=TRUE;
 481   2              }
 482   1              epLen = 0;
 483   1              for (i=1; i<=uDev.bNumOfEPs; i++)                               // For each data endpoint
 484   1              {
 485   2                      pEnp = (pEPDesc)(bBUF + 9 + 9 + epLen);         // point to Endpoint Descp(non-HID)
 486   2                      uDev.bEPAddr[i]         = pEnp->bEPAdd;                 // Ep address and direction
 487   2                      uDev.bAttr[i]           = pEnp->bAttr;                  // Attribute of Endpoint
 488   2                      uDev.wPayLoad[i]        = pEnp->wPayLoad;               // Payload of Endpoint
C51 COMPILER V8.08   HOST_811                                                              04/25/2009 23:03:20 PAGE 9   

 489   2                      uDev.bInterval[i]       = pEnp->bInterval;              // Polling interval
 490   2                  uDev.bData1[i] = 0;                                 // init data toggleu
 491   2                      epLen += 7;
 492   2                      if(uDev.bAttr[i]==0x2)
 493   2                      {
 494   3                          if(uDev.bEPAddr[i]&0x80)
 495   3                          uDev.bEpin=uDev.bEPAddr[i];
 496   3                          else
 497   3                          uDev.bEpOut=uDev.bEPAddr[i];
 498   3                      }
 499   2              }
 500   1              return TRUE;
 501   1      }
 502          //--------------------------------------------------------------------------
 503          //SL811H variables initialization
 504          //--------------------------------------------------------------------------
 505          
 506                  
 507          
 508          void sl811h_init(void)
 509          {       USB_Control.SLAVE_FOUND = FALSE;
 510   1              USB_Control.SLAVE_ENUMERATED = FALSE;
 511   1              USB_Control.SLAVE_ONLINE = FALSE;
 512   1              USB_Control.SLAVE_REMOVED=FALSE;
 513   1              USB_Control.DATA_STOP=FALSE;
 514   1              USB_Control.TIMEOUT_ERR=FALSE;
 515   1              USB_Control.bMassDevice = FALSE;                
 516   1              SL811Write(IntEna,0x20); //只允许设备移入和移除;
 517   1              SL811Write(IntStatus,INT_CLEAR);
 518   1              SL811Write(cSOFcnt,0xae); 
 519   1              DelayMs(10); 
 520   1              SL811Write(IntStatus,INT_CLEAR);
 521   1      
 522   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   2318    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =    259      84
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      1    ----
   IDATA SIZE       =     45       1
   BIT SIZE         =   ----    ----
   EDATA SIZE       =   ----    ----
   HDATA SIZE       =   ----    ----
   XDATA CONST SIZE =   ----    ----
   FAR CONST SIZE   =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -