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

📄 usb.lst

📁 基于at89c51snd1c的硬盘MP3播放器
💻 LST
📖 第 1 页 / 共 3 页
字号:
 278   1       usb_flags_Register=0;
 279   1       SCSI_Command=1;
 280   1       SCSI_Data=0;
 281   1       csw.dCSWSignature=0x55534253;
 282   1      
 283   1       USBIEN=0x00;                                    //不允许USB中断
 284   1       USBCLK=0x03;                                   
 285   1       USBCON=0x80;
 286   1       set_endpoint_enable();
 287   1       UEPRST=0x07;                                    //端点FIFO复位
 288   1       UEPRST=0x00;
 289   1      }
 290          
 291          void usb_bus_reset(void)                         //USB总线复位处理
 292          {
 293   1       usb_flags_Register=0;
 294   1       SCSI_Command=1;
 295   1       SCSI_Data=0;
 296   1       csw.dCSWSignature=0x55534253;
 297   1       USBADDR=0x80;
 298   1       LCD_go_home();
C51 COMPILER V8.02   USB                                                                   08/03/2008 09:58:47 PAGE 6   

 299   1       prints("               ");
 300   1       LCD_line_feed();
 301   1       prints("               ");
 302   1       LCD_go_home();
 303   1       prints("移动硬盘模式");
 304   1       LCD_line_feed();
 305   1       prints("运行中......");
 306   1       remove_enable=0;
 307   1      }
 308          
 309          void usb_bus_suspend(void)                       //USB总线挂起处理
 310          {
 311   1       LCD_go_home();
 312   1       prints("现在可以安全的 ");
 313   1       LCD_line_feed();
 314   1       prints("拔下USB 连线了 ");
 315   1       remove_enable=1;
 316   1      }
 317          
 318          void set_usb_address(void)                       //设置地址
 319          {
 320   1       USB_set_addr=1;
 321   1       write_endpoint_buff(0,0,0);
 322   1       USBADDR=0x80 | (Control_Data.DeviceRequest.wValue);
 323   1       USBCON=USBCON | 0x01;
 324   1       USB_usb_endp0_in=0;
 325   1       USB_setup_packet_out=0;
 326   1      }
 327          
 328          void get_status(unsigned char receiver)           //获取状态响应
 329          {
 330   1       unsigned char status[2];
 331   1       switch (receiver)
 332   1        {
 333   2         case 0:                        //获取设备状态
 334   2          status[0]=0x00;
 335   2          status[1]=0x00;
 336   2          break;
 337   2         case 1:                        //获取接口状态
 338   2          status[0]=0x00;
 339   2          status[0]=0x00;
 340   2          break;
 341   2         case 2:                        //获取端点状态
 342   2          status[0]=0x00;
 343   2          status[1]=0x00;
 344   2          break;
 345   2         }
 346   1        UEPNUM=0;
 347   1        UEPSTAX=(UEPSTAX | 0x80);       //数据阶段的输入
 348   1        write_endpoint_buff(0,2,status);
 349   1        UEPSTAX=(UEPSTAX & (~0x80));
 350   1        USB_usb_endp0_in=0;
 351   1      }
 352          
 353          void clear_feature(unsigned char receiver)    
 354          {
 355   1       receiver=0;
 356   1       write_endpoint_buff(0,0,0);
 357   1       USB_usb_endp0_in=0;
 358   1       USB_setup_packet_out=0;
 359   1      }
 360          
C51 COMPILER V8.02   USB                                                                   08/03/2008 09:58:47 PAGE 7   

 361          void set_feature(unsigned char receiver)
 362          {
 363   1       receiver=0;
 364   1       write_endpoint_buff(0,0,0);
 365   1       USB_usb_endp0_in=0;
 366   1       USB_setup_packet_out=0;
 367   1      }
 368          
 369          void set_descriptor(void)
 370          {
 371   1       write_endpoint_buff(0,0,0);
 372   1       USB_usb_endp0_in=0;
 373   1       USB_setup_packet_out=0;
 374   1      }
 375          
 376          void set_configuration(void)
 377          {
 378   1       write_endpoint_buff(0,0,0);
 379   1       USBCON=USBCON | 0x02;
 380   1       USB_usb_endp0_in=0;
 381   1       USB_setup_packet_out=0;
 382   1       UEPNUM=1;
 383   1       UEPCONX = 0x86 ;
 384   1       UEPNUM=2;
 385   1       UEPCONX = 0x82;
 386   1       LCD_go_home();
 387   1       prints("               ");
 388   1       LCD_line_feed();
 389   1       prints("               ");
 390   1       LCD_go_home();
 391   1       prints("移动硬盘模式");
 392   1       LCD_line_feed();
 393   1       prints("运行中......");
 394   1      }
 395          
 396          void get_configuration(void)               //获取配置状态
 397          {
 398   1       unsigned char value=0x01;
 399   1       UEPNUM=0;
 400   1       UEPSTAX=(UEPSTAX | 0x80);       //数据阶段的输入
 401   1       write_endpoint_buff(0,1,&value);
 402   1       UEPSTAX=(UEPSTAX & (~0x80));
 403   1       USB_usb_endp0_in=0;
 404   1      }
 405          
 406          void set_interface(void)                   //设置接口
 407          {
 408   1       write_endpoint_buff(0,0,0);
 409   1       USB_usb_endp0_in=0;
 410   1       USB_setup_packet_out=0;
 411   1      }
 412          
 413          void get_interface(void)                  //获取接口状态
 414          {
 415   1       unsigned char value=0x01;
 416   1       UEPNUM=0;
 417   1       UEPSTAX=(UEPSTAX | 0x80);                //数据阶段的输入
 418   1       write_endpoint_buff(0,1,&value);
 419   1       UEPSTAX=(UEPSTAX & (~0x80));
 420   1       USB_usb_endp0_in=0;
 421   1      }
 422          
C51 COMPILER V8.02   USB                                                                   08/03/2008 09:58:47 PAGE 8   

 423          void get_max_LUN(void)                   //获取磁盘最大逻辑单元号
 424          {
 425   1       unsigned char max_LUN=MAX_LUN;          //只有一个逻辑单元
 426   1       UEPNUM=0;
 427   1       UEPSTAX=(UEPSTAX | 0x80);               //数据阶段的输入
 428   1       write_endpoint_buff(0,1,&(max_LUN));
 429   1       UEPSTAX=(UEPSTAX & (~0x80));
 430   1       USB_usb_endp0_in=0;
 431   1      }
 432          
 433          void mass_storage_reset(void)            //USB大容量存储设备复位
 434          {
 435   1       write_endpoint_buff(0,0,0);
 436   1       USB_usb_endp0_in=0;
 437   1       USB_setup_packet_out=0;
 438   1       SCSI_Command=1;
 439   1       SCSI_Data=0;
 440   1      }
 441          
 442          
 443          void get_descriptor(void)                    //获取描述符
 444          {
 445   1       if(!USB_not_end)
 446   1        {
 447   2         switch(MSB(Control_Data.DeviceRequest.wValue))
 448   2          {
 449   3           case DEVICE_DESCRIPTOR:
 450   3                Control_Data.wCount=sizeof(DEVICE_DESCRIPTOR_STRUCT);
 451   3                Control_Data.pData=(unsigned char *)(&device_descriptor);
 452   3                break;
 453   3           case CONFIGURATION_DESCRIPTOR:
 454   3           Control_Data.wCount=SWAP(con_int_endp_descriptor.configuration_descriptor.wTotalLength);
 455   3           Control_Data.pData=(unsigned char *)(&con_int_endp_descriptor);
 456   3           if(Control_Data.wLength<Control_Data.wCount)Control_Data.wCount=Control_Data.wLength;
 457   3           break;
 458   3           case STRING_DESCRIPTOR:
 459   3                if(LSB(Control_Data.DeviceRequest.wValue)==0)
 460   3                 {
 461   4                   Control_Data.wCount=LANGUAGE_ID[0];
 462   4                   Control_Data.pData=LANGUAGE_ID;
 463   4                 }
 464   3                if(LSB(Control_Data.DeviceRequest.wValue)==2)
 465   3                 {
 466   4                  Control_Data.wCount=device_serial_number[0];
 467   4                          Control_Data.pData=device_serial_number;
 468   4                         }
 469   3                    break;
 470   3          }
 471   2         if(Control_Data.wLength<Control_Data.wCount)Control_Data.wCount=Control_Data.wLength;
 472   2        }
 473   1       if(Control_Data.wCount>=MAX_CONTROL_DATA_SIZE)
 474   1        {
 475   2         UEPNUM=0;
 476   2         UEPSTAX=(UEPSTAX | 0x80);       //数据阶段的输入
 477   2         write_endpoint_buff(0,MAX_CONTROL_DATA_SIZE,Control_Data.pData);
 478   2         UEPSTAX=(UEPSTAX & (~0x80));
 479   2         Control_Data.pData+=MAX_CONTROL_DATA_SIZE;
 480   2         Control_Data.wCount-=MAX_CONTROL_DATA_SIZE;
 481   2         if(USB_set_addr)USB_not_end=1;
 482   2         else USB_usb_endp0_in=0;
 483   2         return;
 484   2        }
C51 COMPILER V8.02   USB                                                                   08/03/2008 09:58:47 PAGE 9   

 485   1       else
 486   1        {
 487   2         UEPNUM=0;
 488   2         UEPSTAX=(UEPSTAX | 0x80);       //数据阶段的输入
 489   2         write_endpoint_buff(0,Control_Data.wCount,Control_Data.pData);
 490   2         UEPSTAX=(UEPSTAX & (~0x80));
 491   2         USB_setup_packet_in=0;
 492   2         USB_usb_endp0_in=0;
 493   2         return;
 494   2        }      
 495   1      }
 496          
 497          void endp0_out(void)                            //终端点0输出中断处理
 498          {
 499   1       if(status_setup_packet)
 500   1        {
 501   2         Control_Data.wLength=0;
 502   2         Control_Data.wCount=0;
 503   2         if(read_endpoint_buff(0,sizeof(Control_Data.DeviceRequest),(unsigned char *)(&(Control_Data.DeviceReque
             -st)))!=sizeof(REQUESTCMD))
 504   2          {
 505   3           error(1);
 506   3               return;
 507   3              }
 508   2        UEPNUM=0;
 509   2        UEPSTAX= UEPSTAX & (~0x04);  //清除SETUP标记
 510   2        Control_Data.DeviceRequest.wValue=SWAP(Control_Data.DeviceRequest.wValue);
 511   2        Control_Data.DeviceRequest.wIndex=SWAP(Control_Data.DeviceRequest.wIndex);
 512   2        Control_Data.DeviceRequest.wLength=SWAP(Control_Data.DeviceRequest.wLength);
 513   2        Control_Data.wLength=Control_Data.DeviceRequest.wLength;
 514   2        USB_not_end=0;
 515   2        USB_usb_endp0_in=1;
 516   2        USB_setup_packet_in=0;
 517   2        USB_setup_packet_out=0;
 518   2        if(Control_Data.DeviceRequest.bmRequestType&0x80){USB_setup_packet_in=1;return;}
 519   2        else {USB_setup_packet_out=1;return;}
 520   2       }
 521   1       else
 522   1        {
 523   2         read_endpoint_buff(0,0,0);
 524   2        }
 525   1      }
 526          
 527          void endp0_in(void)                                        //终端点0输入处理
 528          {
 529   1       if(USB_setup_packet_in||USB_not_end)
 530   1        {
 531   2         if((Control_Data.DeviceRequest.bmRequestType==0xA1)&&(Control_Data.DeviceRequest.bRequest==0xFE))get_ma
             -x_LUN();
 532   2         switch(Control_Data.DeviceRequest.bmRequestType&0x7B)
 533   2          {
 534   3               case 0:
 535   3                 switch (Control_Data.DeviceRequest.bRequest)
 536   3                  {
 537   4                   case 0: get_status(0);break;
 538   4                   case 6: get_descriptor();break;
 539   4                   case 8: get_configuration();break;
 540   4                       default:break;
 541   4                  }
 542   3               case 1:
 543   3                 switch (Control_Data.DeviceRequest.bRequest)
 544   3                  {
C51 COMPILER V8.02   USB                                                                   08/03/2008 09:58:47 PAGE 10  

 545   4               case  0: get_status(1);break;
 546   4                   case 10: get_interface();break;
 547   4               default: break;
 548   4                      }
 549   3               case 2:
 550   3                {
 551   4             if(Control_Data.DeviceRequest.bRequest==0)get_status(2);  
 552   4                }
 553   3               default: break;
 554   3          }
 555   2        return;
 556   2        }
 557   1       if(USB_setup_packet_out)
 558   1        {
 559   2         if((Control_Data.DeviceRequest.bmRequestType==0x21)&&(Control_Data.DeviceRequest.bRequest==0xFF))
 560   2          mass_storage_reset();
 561   2      
 562   2         switch(Control_Data.DeviceRequest.bmRequestType&0x03)
 563   2          {
 564   3               case 0:

⌨️ 快捷键说明

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