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

📄 usb.lst

📁 usb接口的移动硬盘
💻 LST
📖 第 1 页 / 共 3 页
字号:
 638   2      
 639   2        ack_setup(0);
 640   2        ack_setup(1);
 641   2        Control_Data.wLength=Control_Data.DeviceRequest.wLength;
 642   2        USB_not_end=0;
 643   2        USB_usb_endp0_in=1;
 644   2        USB_setup_packet_in=0;
 645   2        USB_setup_packet_out=0;
 646   2        if(Control_Data.DeviceRequest.bmRequestType&0x80){USB_setup_packet_in=1;return;}
 647   2        else {USB_setup_packet_out=1;return;}
 648   2       }
 649   1       else
 650   1        {
 651   2         select_endpoint(0);
 652   2         clear_buffer();
 653   2        }
 654   1      }
 655          
 656          void endp0_in(void)                                        //终端点0输入处理
 657          {
 658   1       read_last_status(1);
 659   1       if(USB_setup_packet_in||USB_not_end)
 660   1        {
 661   2         if((Control_Data.DeviceRequest.bmRequestType==0xA1)&&(Control_Data.DeviceRequest.bRequest==0xFE))
 662   2          get_max_LUN();
 663   2         switch(Control_Data.DeviceRequest.bmRequestType&0x7B)
 664   2          {
 665   3               case 0:
 666   3                 switch (Control_Data.DeviceRequest.bRequest)
 667   3                  {
 668   4                   case 0: get_status(0);break;
 669   4                   case 6: get_descriptor();break;
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 12  

 670   4                   case 8: get_configuration();break;
 671   4                       default:break;
 672   4                  }
 673   3               case 1:
 674   3                 switch (Control_Data.DeviceRequest.bRequest)
 675   3                  {
 676   4               case  0: get_status(1);break;
 677   4                   case 10: get_interface();break;
 678   4               default: break;
 679   4                      }
 680   3               case 2:
 681   3                {
 682   4             if(Control_Data.DeviceRequest.bRequest==0)get_status(2);  
 683   4                }
 684   3               default: break;
 685   3          }
 686   2        return;
 687   2        }
 688   1       if(USB_setup_packet_out) 
 689   1        {
 690   2         if((Control_Data.DeviceRequest.bmRequestType==0x21)&&(Control_Data.DeviceRequest.bRequest==0xFF))
 691   2          mass_storage_reset();
 692   2      
 693   2         switch(Control_Data.DeviceRequest.bmRequestType&0x03)
 694   2          {
 695   3               case 0:
 696   3           switch (Control_Data.DeviceRequest.bRequest)
 697   3            {
 698   4                 case     CLEAR_FEATURE: clear_feature(0);break;
 699   4                 case       SET_FEATURE: set_feature(0);break;
 700   4                 case       SET_ADDRESS: set_usb_address();break;                  //set_address                 
 701   4                 case    SET_DESCRIPTOR: set_descriptor();break;                //set_descriptor
 702   4                 case SET_CONFIGURATION: set_configuration(); break;         //set_configurat                           
 703   4                 default: break;
 704   4                }break;
 705   3               case 1:
 706   3            switch (Control_Data.DeviceRequest.bRequest)
 707   3                 {
 708   4                  case CLEAR_FEATURE:  clear_feature(1);break;
 709   4                  case   SET_FEATURE:  set_feature(1); break;
 710   4              case SET_INTERFACE: set_interface();break;
 711   4                  default: break;
 712   4              }break;
 713   3               case 2:
 714   3                switch (Control_Data.DeviceRequest.bRequest)
 715   3                 {
 716   4                  case CLEAR_FEATURE: clear_feature(2);break;
 717   4                  case   SET_FEATURE: set_feature(2);break;
 718   4              default: break;      
 719   4                 }break;
 720   3               default: break;
 721   3          }
 722   2        }
 723   1      }
 724          
 725          /*void endp1_out(void)
 726          {
 727           printc(0xcc);
 728          }*/
 729          
 730          
 731          /*void endp1_in(void)
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 13  

 732          {
 733           printc(0xcc);
 734          }*/
 735          
 736          void Return_CSW(unsigned long int DataResidue,unsigned char status)          //返回CSW数据包
 737          {
 738   1        
 739   1        csw.dCSWTag=cbw.dCBWTag;
 740   1        csw.dCSWDataResidue=DataResidue;
 741   1        csw.bCSWStatus=status;
 742   1        while(select_endpoint(5)&0x01);
 743   1        write_endpoint_buff(5,sizeof(CSW),(unsigned char *)(&csw));
 744   1        USB_usb_endp2_in=0;
 745   1        USB_usb_endp2_out=0;
 746   1        SCSI_Command=1;
 747   1        SCSI_Data=0;
 748   1      }
 749          
 750          
 751          void write_10(void)                                                         //SCSI写操作
 752          {
 753   1       unsigned long int LBA,Byte_Count;
 754   1       unsigned char i;
 755   1        if(SCSI_Data)
 756   1        {
 757   2         ((unsigned char*)&LBA)[0]=cbw.CBWCB[2];
 758   2         ((unsigned char*)&LBA)[1]=cbw.CBWCB[3];
 759   2         ((unsigned char*)&LBA)[2]=cbw.CBWCB[4];
 760   2         ((unsigned char*)&LBA)[3]=cbw.CBWCB[5];
 761   2      
 762   2         ((unsigned char*)&Byte_Count)[0]=0;
 763   2         ((unsigned char*)&Byte_Count)[1]=cbw.CBWCB[7];
 764   2         ((unsigned char*)&Byte_Count)[2]=cbw.CBWCB[8];
 765   2         ((unsigned char*)&Byte_Count)[3]=0;
 766   2         Byte_Count<<=1;
 767   2         while(USB_usb_endp2_out)
 768   2          { 
 769   3           if((((unsigned char *)&Byte_Count)[3]==0)&&((((unsigned char *)&Byte_Count)[2]&0x01)==0))
 770   3            {
 771   4             write_IDE_LBA0(((unsigned char*)&LBA)[0]);
 772   4             write_IDE_LBA1(((unsigned char*)&LBA)[1]);
 773   4             write_IDE_LBA2(((unsigned char*)&LBA)[2]);
 774   4             write_IDE_LBA3(((unsigned char*)&LBA)[3]);
 775   4             LBA++;
 776   4             write_IDE_sector_count(1);
 777   4             wait_IDE_busy();
 778   4             write_IDE_sector();
 779   4             IDE_Address=IDE_Data;
 780   4             set_IDE_address();
 781   4            }
 782   3           while(!(select_endpoint(4)&0x01));
 783   3               if(Byte_Count>64)
 784   3                {
 785   4                 read_endpoint_buff(4,64,buffer);      
 786   4                 Byte_Count-=64;
 787   4                }
 788   3               else 
 789   3                {
 790   4             read_endpoint_buff(4,Byte_Count,buffer);
 791   4             Return_CSW(0x00,SUCCESS);
 792   4                }
 793   3           for(i=0;i<64;i++)
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 14  

 794   3            {
 795   4             while(!IDE_IORDY);
 796   4      
 797   4             IDE_DIOW=0;
 798   4             IDE_DATA_L=buffer[i];
 799   4             i++;
 800   4             IDE_DATA_H=buffer[i];
 801   4             IDE_DIOW=1;
 802   4            }
 803   3          } 
 804   2         wait_IDE_busy();
 805   2         IDE_Address=IDE_Bus_Not_Use;
 806   2         set_IDE_address();
 807   2        }
 808   1      }
 809          
 810          
 811          void endp2_out(void)                                                     //主端点输出处理
 812          {
 813   1       if(SCSI_Command)
 814   1        {
 815   2         if(read_endpoint_buff(4,sizeof(CBW),(unsigned char *)(&cbw))!=sizeof(CBW)){error(2);return;}
 816   2         if(cbw.dCBWSignature!=0x55534243){error(3);return;}
 817   2         SCSI_Command=0;
 818   2         SCSI_Data=1;
 819   2         if(cbw.bmCBWFlags&0x80)
 820   2          {
 821   3               USB_usb_endp2_in=1;
 822   3               USB_usb_endp2_out=0;
 823   3              }
 824   2         else
 825   2          {
 826   3               USB_usb_endp2_in=0;
 827   3               USB_usb_endp2_out=1;
 828   3           switch(cbw.CBWCB[0])
 829   3            {
 830   4             case        Write_10: write_10();break;
 831   4             case Test_Unit_Ready: Return_CSW(0x00,SUCCESS);break; 
 832   4             case          Verify: Return_CSW(0x00,SUCCESS);break;
 833   4                 default             : Return_CSW(cbw.dCBWDataTransgerLength,FAIL);break;
 834   4                }
 835   3              }
 836   2        }
 837   1       else
 838   1        {
 839   2         read_last_status(4);
 840   2         select_endpoint(4);
 841   2         clear_buffer();
 842   2        }
 843   1      }
 844          
 845          
 846          void read_10(void)                                                              //SCSI读处理
 847          {
 848   1       unsigned long int LBA=0,Byte_Count;
 849   1       unsigned char i;
 850   1       if(SCSI_Data)
 851   1        {
 852   2      
 853   2         ((unsigned char*)&LBA)[0]=cbw.CBWCB[2];
 854   2         ((unsigned char*)&LBA)[1]=cbw.CBWCB[3];
 855   2         ((unsigned char*)&LBA)[2]=cbw.CBWCB[4];
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 15  

 856   2         ((unsigned char*)&LBA)[3]=cbw.CBWCB[5];
 857   2      
 858   2         ((unsigned char*)&Byte_Count)[0]=0;
 859   2         ((unsigned char*)&Byte_Count)[1]=cbw.CBWCB[7];
 860   2         ((unsigned char*)&Byte_Count)[2]=cbw.CBWCB[8];
 861   2         ((unsigned char*)&Byte_Count)[3]=0;
 862   2         Byte_Count<<=1;
 863   2         pData=buffer;
 864   2         while(USB_usb_endp2_in)
 865   2          {
 866   3           if((((unsigned char *)&Byte_Count)[3]==0)&&((((unsigned char *)&Byte_Count)[2]&0x01)==0))
 867   3            {
 868   4             write_IDE_LBA0(((unsigned char*)&LBA)[0]);
 869   4             write_IDE_LBA1(((unsigned char*)&LBA)[1]);
 870   4             write_IDE_LBA2(((unsigned char*)&LBA)[2]);
 871   4             write_IDE_LBA3(((unsigned char*)&LBA)[3]);
 872   4             LBA++;
 873   4             write_IDE_sector_count(1);
 874   4             read_IDE_sector();
 875   4             IDE_Address=IDE_Data;
 876   4             set_IDE_address();
 877   4            }
 878   3           IDE_DATA_H=0xFF;
 879   3           for(i=0;i<64;i++)
 880   3            {
 881   4             while(!IDE_IORDY);
 882   4             IDE_DIOR=0;
 883   4             buffer[i]=IDE_DATA_L;
 884   4             i++;
 885   4             buffer[i]=IDE_DATA_H;
 886   4             IDE_DIOR=1;
 887   4            }
 888   3           while(select_endpoint(5)&0x01);
 889   3           if(Byte_Count>MAX_BULK_DATA_SIZE)
 890   3            {
 891   4             write_endpoint_buff(5,MAX_BULK_DATA_SIZE,pData);
 892   4             Byte_Count-=MAX_BULK_DATA_SIZE;
 893   4            }
 894   3           else
 895   3            {
 896   4             write_endpoint_buff(5,Byte_Count,pData);
 897   4             Return_CSW(0x00,SUCCESS);
 898   4             IDE_Address=IDE_Bus_Not_Use;
 899   4             set_IDE_address();
 900   4            }
 901   3          }
 902   2        }
 903   1       else error(2);return;
 904   1      }
 905          
 906          void endp2_in(void)                                      //主端点输入处理
 907          {
 908   1          switch(cbw.CBWCB[0])
 909   1          {
 910   2           case              Read_10: read_10();break;
 911   2           case              Inquiry: write_endpoint_buff(5,0x24,DISK_INF);Return_CSW(0x00,SUCCESS); break;
 912   2           case        Read_Capacity: write_endpoint_buff(5,0x08,DISK_CAPACITY);Return_CSW(0x00,SUCCESS);break;
 913   2           case Read_Format_capacity: write_endpoint_buff(5,0x00,0x00);Return_CSW(cbw.dCBWDataTransgerLength,FAI
             -L);break;
 914   2           case        Request_Sense: write_endpoint_buff(5,0x12,SENSE);Return_CSW(0x00,SUCCESS);break;
 915   2               case       Medium_Removal: Return_CSW(0x00,SUCCESS);break;
 916   2               case                 0x1a: write_endpoint_buff(5,0x00,0x00);Return_CSW(cbw.dCBWDataTransgerLength,FAIL);
C51 COMPILER V7.06   USB                                                                   03/02/2006 23:51:19 PAGE 16  

             -break;
 917   2           default                  : write_endpoint_buff(5,0x00,0x00);Return_CSW(cbw.dCBWDataTransgerLength,FAI
             -L);break;
 918   2              }
 919   1      }
 920          
 921          void main(void)
 922          {
 923   1      
 924   1       disconnect_usb();
 925   1      
 926   1       system_initial();
 927   1       init_usb();
 928   1       connect_usb();
 929   1        
 930   1       while(1)
 931   1        { 
 932   2         if(!USB_INT)
 933   2          {
 934   3               read_interrupt_register();
 935   3      
 936   3               if(Interrupt_bus_reset){usb_bus_reset();continue;}
 937   3               if(Interrupt_suspend_change){usb_bus_suspend();continue;}
 938   3           if(Interrupt_control_out_port){endp0_out();continue;}
 939   3               if(Interrupt_control_in_port&&USB_usb_endp0_in){endp0_in();continue;}
 940   3               if(Interrupt_main_out_port){endp2_out();continue;}
 941   3               if(Interrupt_main_in_port&&USB_usb_endp2_in){endp2_in();continue;}
 942   3          }
 943   2        }
 944   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   2151    ----
   CONSTANT SIZE    =    126    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     17      35
   IDATA SIZE       =    139    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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