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

📄 usb_drv.lst

📁 这是一个用C语言在VC6.0环境下编写的用于USB驱动的代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
 246   3                      {
 247   4                                  case GET_STATUS:
 248   4                           usb_get_status();
 249   4                           break;
 250   4                                      case CLEAR_FEATURE:
 251   4                           usb_clear_feature();
 252   4                           break;
 253   4                                      case SET_FEATURE:
 254   4                           usb_set_feature();
 255   4                           break;
 256   4                                      case SET_ADDRESS:
 257   4                           /*set and enable new address for endpoint 0, but set*/
 258   4                                   /*DEF too, so new address doesn't take effect until */
 259   4                                   /*the handshake completes                           */
 260   4                           write_usb(EPC0,DEF);   
 261   4                               write_usb(FAR,usb_buf[2] | AD_EN);  
 262   4                                               break;
 263   4                                  case GET_DESCRIPTOR:
 264   4                           usb_get_descriptor();
 265   4                           break;                             
 266   4                      case GET_CONFIGURATION:
 267   4                           write_usb(TXD0,usb_cfg);/*load the config value   */ 
 268   4                                               break;
 269   4                      
 270   4                      case SET_CONFIGURATION:
 271   4                           usb_set_configuration();
 272   4                                               break;
 273   4                      
 274   4                     
 275   4                      
 276   4                      case GET_MAX_LUN:
 277   4                           write_usb(TXD0,MS_MAX_LUN);
 278   4                           break;
 279   4                      case MASS_STORAGE_RESET:
 280   4                           usb_mass_storage_reset();
 281   4                           break;
 282   4                              default:                  /*unsupported standard req*/ 
 283   4                                       //write_usb(EPC0,STALL);
 284   4                                      break;
 285   4                  }
 286   3              }
 287   2              else                            /*if a non-standard req.  */   
 288   2              {
 289   3                          //write_usb(EPC0,STALL);
 290   3              }
 291   2      
 292   2                  /*the following is done for all setup packets.  Note that if*/  
 293   2                  /*no data was stuffed into the FIFO, the result of the fol- */  
 294   2                  /*lowing will be a zero-length response.                    */ 
 295   2              dta_pid0=1;  /*enable the TX (DATA1) */ 
 296   2                  TXEN0_PID;
 297   2          }
 298   1          /*if not a setup packet, it must be an OUT packet ***************/ 
 299   1          else   
 300   1          {
 301   2                  
 302   2                  if (data_to_transfer>0||zero_packet_flag==TRUE)             /*get_descr status stage? */
 303   2              {
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 6   

 304   3                  /*test for errors (zero length, correct PID)            */
 305   3                  if ((rxstat& 0x5F)!=0x10)   /*length error??          */
 306   3                  {
 307   4                  }
 308   3                          data_to_transfer=0;           /*exit get_descr mode     */
 309   3                  zero_packet_flag=FALSE;
 310   3                              FLUSHTX0;                   /*flush TX0 and disable   */ 
 311   3              }
 312   2                      FLUSHRX0;                     /*make sure the RX is off */
 313   2                      write_usb(RXC0,RX_EN);          /*re-enable the receiver  */   
 314   2          }
 315   1          //test_b= 0;
 316   1          /*we do this stuff for all rx_0 events **************************/  
 317   1      
 318   1      }
 319          
 320          void usb_get_descriptor()
 321          {
 322   1          uchar string_type;
 323   1              uchar descriptor_type;
 324   1              uchar  wLength;
 325   1          data_to_transfer=0;
 326   1              pbuffer=NULL;
 327   1              zero_packet_flag=FALSE;
 328   1              string_type = usb_buf[2];            /* read LSB of wValue */
 329   1          descriptor_type = usb_buf[3];        /* read MSB of wValue */ 
 330   1              wLength=usb_buf[6];
 331   1              switch (descriptor_type)
 332   1          {
 333   2               case DEVICE:
 334   2                    data_to_transfer = sizeof (usb_device_descriptor);
 335   2                    pbuffer = &(usb_device_descriptor.bLength);
 336   2                                break;
 337   2          
 338   2              case CONFIGURATION:
 339   2                   data_to_transfer = sizeof (usb_configuration);
 340   2                   pbuffer = &(usb_configuration.cfg.bLength);
 341   2                               break;
 342   2              case STRING:
 343   2                   switch (string_type)
 344   2                   {
 345   3                        case LANG_ID:
 346   3                             data_to_transfer = sizeof (usb_language);
 347   3                             pbuffer = &(usb_language.bLength);
 348   3                                                 break;
 349   3      
 350   3                        case MAN_INDEX:
 351   3                             data_to_transfer = sizeof (usb_manufacturer);
 352   3                             pbuffer = &(usb_manufacturer.bLength);
 353   3                                                 break;
 354   3              
 355   3                       case PROD_INDEX:
 356   3                            data_to_transfer = sizeof (usb_product);
 357   3                            pbuffer = &(usb_product.bLength);
 358   3                            break;
 359   3              
 360   3                      case SN_INDEX:
 361   3                           data_to_transfer = sizeof (usb_serial_number);
 362   3                           pbuffer = &(usb_serial_number.bLength);
 363   3                                               break;
 364   3              
 365   3                     default:
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 7   

 366   3                                      //write_usb(EPC0,STALL);
 367   3                                      break;
 368   3                           }
 369   2                 default:
 370   2                       //write_usb(EPC0,STALL);
 371   2                       break;
 372   2              }
 373   1          if(wLength>data_to_transfer)
 374   1              {
 375   2                  if((data_to_transfer % EP_CONTROL_LENGTH) == 0)
 376   2                      {
 377   3                          zero_packet_flag=TRUE;
 378   3                      }
 379   2                      else
 380   2                      {
 381   3                          zero_packet_flag=FALSE;
 382   3                      }
 383   2              }
 384   1              else
 385   1              {
 386   2                  data_to_transfer=wLength;
 387   2              }       
 388   1          send_desc_to_TXD0;
 389   1      
 390   1      }
 391          
 392          void usb_set_configuration()
 393          {   
 394   1          usb_cfg = usb_buf[2];               /*set the configuration # */  
 395   1              if (usb_cfg>0)                  /*set the configuration   */   
 396   1              {
 397   2                  dtapid = 0;                     /*FIRST PID is DATA0      */   
 398   2                  stalled = 0;                     /*nothing stalled         */
 399   2                      dta_pid1=1;
 400   2                  FLUSHTX1;                       /*flush  TX1 and disable  */   
 401   2                  write_usb(EPC1,EP_EN+01);       /*enable EP1 at adr 1     */
 402   2      
 403   2                  FLUSHRX1;                       /*flush  RX1 and disable  */ 
 404   2                  write_usb(EPC2,EP_EN+02);       /*enable EP2 at adr 2     */   
 405   2                  write_usb(RXC1,RX_EN);          /*enable RX1              */  
 406   2         
 407   2                      FLUSHTX2;                       /*flush  TX1 and disable  */   
 408   2                  write_usb(EPC3,EP_EN+03);       /*enable EP1 at adr 1     */
 409   2      
 410   2                      FLUSHRX2;                       /*flush  RX1 and disable  */ 
 411   2                  write_usb(EPC4,EP_EN+04);       /*enable EP2 at adr 2     */   
 412   2                  write_usb(RXC2,RX_EN);          /*enable RX1              */  
 413   2      
 414   2                      FLUSHTX3;                       /*flush  TX1 and disable  */   
 415   2                  write_usb(EPC5,EP_EN+05);       /*enable EP1 at adr 1     */
 416   2      
 417   2                      FLUSHRX3;                       /*flush  RX1 and disable  */ 
 418   2                  write_usb(EPC6,EP_EN+06);       /*enable EP2 at adr 2     */   
 419   2                  write_usb(RXC3,RX_EN);          /*enable RX1              */  
 420   2                      
 421   2                  
 422   2                }
 423   1              else                                /*unconfigure the device  */
 424   1                {
 425   2                  write_usb(EPC1,0);              /*disable EP1             */
 426   2                  write_usb(EPC2,0);              /*disable EP2             */   
 427   2                  write_usb(EPC3,0);              /*disable EP3             */
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 8   

 428   2                  write_usb(EPC4,0);              /*disable EP4             */
 429   2                  write_usb(EPC5,0);              /*disable EP5             */   
 430   2                  write_usb(EPC6,0);              /*disable EP6             */   
 431   2                }
 432   1      }
 433          
 434          void usb_clear_feature()
 435          {
 436   1              switch(usb_buf[0]&0x03)            /*find request target     */
 437   1              {
 438   2                  case 0:                         /*DEVICE                  */  
 439   2                    //write_usb(EPC0,STALL);
 440   2                    break;
 441   2      
 442   2                  case 1:                         /*INTERFACE               */  
 443   2                    //write_usb(EPC0,STALL);
 444   2                        break;
 445   2      
 446   2                  case 2:                         /*ENDPOINT                */  
 447   2                        switch (usb_buf[3]&0x07)           /*find specific endpoint  */
 448   2                            {
 449   3                                case 0:                                                 
 450   3                                      stall_ep0 = 0;
 451   3                                      break;
 452   3                                case 1:                                                 
 453   3                                      stall_ep1 = 0;
 454   3                                      break;
 455   3                                case 2:                                                 
 456   3                                      stall_ep2 = 0;
 457   3                                      break;
 458   3                                case 3:                                                 
 459   3                                      stall_ep3 = 0;
 460   3                                      break;
 461   3                                case 4:                                                 
 462   3                                      stall_ep4 = 0;
 463   3                                      break;
 464   3                                case 5:                                                 
 465   3                                      stall_ep5 = 0;
 466   3                                      break;
 467   3                                case 6:                                                 
 468   3                                      stall_ep6 = 0;
 469   3                                      break;
 470   3                                default:
 471   3                              //write_usb(EPC0,STALL);
 472   3                                      break;
 473   3                            }
 474   2                        break;
 475   2      
 476   2                  default:                        /*UNDEFINED               */ 
 477   2                   //write_usb(EPC0,STALL);
 478   2                       break;
 479   2              }
 480   1      
 481   1      }
 482          
 483          void usb_set_feature()
 484          {
 485   1              switch(usb_buf[0]&0x03)            /*find request target     */
 486   1              {
 487   2                  case 0:                         /*DEVICE                  */ 
 488   2                    //write_usb(EPC0,STALL); 
 489   2                    break;
C51 COMPILER V7.06   USB_DRV                                                               08/05/2007 09:42:58 PAGE 9   

 490   2      
 491   2                  case 1:                         /*INTERFACE               */ 
 492   2                    //write_usb(EPC0,STALL); 
 493   2                        break;
 494   2      
 495   2                  case 2:                         /*ENDPOINT                */  
 496   2                        switch (usb_buf[3])           /*find specific endpoint  */
 497   2                            {
 498   3                                case 0:                                                 
 499   3                                      stall_ep0 = 1;

⌨️ 快捷键说明

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