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

📄 usbcore.lst

📁 此源代码为双接口法USB键盘鼠标开发源代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
 745          /********************************************************************
 746          函数功能:USB断开连接函数。
 747          入口参数:无。
 748          返    回:无。
 749          备    注:无。
 750          ********************************************************************/
 751          void UsbDisconnect(void)
 752          {
 753   1      #ifdef DEBUG0
 754   1       Prints("断开USB连接。\r\n");
 755   1      #endif
 756   1       D12WriteCommand(D12_SET_MODE);  //写设置模式命令
 757   1       D12WriteByte(0x06); //设置模式的第一字节
 758   1       D12WriteByte(0x47); //设置模式的第二字节
 759   1       DelayXms(1000);  //延迟1秒
 760   1      }
 761          ////////////////////////End of function//////////////////////////////
 762          
 763          /********************************************************************
 764          函数功能:USB连接函数。
 765          入口参数:无。
 766          返    回:无。
 767          备    注:无。
 768          ********************************************************************/
 769          void UsbConnect(void)
 770          {
 771   1      #ifdef DEBUG0
 772   1       Prints("连接USB。\r\n");
 773   1      #endif
 774   1       D12WriteCommand(D12_SET_MODE);  //写设置模式命令
 775   1       D12WriteByte(0x16); //设置模式的第一字节
 776   1       D12WriteByte(0x47); //设置模式的第二字节
 777   1      }
 778          ////////////////////////End of function//////////////////////////////
 779          
 780          /********************************************************************
 781          函数功能:总线挂起中断处理函数。
 782          入口参数:无。
 783          返    回:无。
 784          备    注:无。
 785          ********************************************************************/
 786          void UsbBusSuspend(void)
 787          {
 788   1      #ifdef DEBUG0
 789   1       Prints("USB总线挂起。\r\n");
 790   1      #endif
 791   1      }
 792          ////////////////////////End of function//////////////////////////////
 793          
 794          /********************************************************************
 795          函数功能:总线复位中断处理函数。
 796          入口参数:无。
 797          返    回:无。
 798          备    注:无。
 799          ********************************************************************/
C51 COMPILER V7.06   USBCORE                                                               11/16/2008 15:59:36 PAGE 14  

 800          void UsbBusReset(void)
 801          {
 802   1      #ifdef DEBUG0
 803   1       Prints("USB总线复位。\r\n");
 804   1      #endif
 805   1       Ep1InIsBusy=0; //复位后端点1输入缓冲区空闲。
 806   1       Ep2InIsBusy=0; //复位后端点2输入缓冲区空闲。
 807   1      }
 808          ////////////////////////End of function//////////////////////////////
 809          
 810          /********************************************************************
 811          函数功能:根据pData和SendLength将数据发送到端点0的函数。
 812          入口参数:无。
 813          返    回:无。
 814          备    注:无。
 815          ********************************************************************/
 816          void UsbEp0SendData(void)
 817          {
 818   1       //将数据写到端点中去准备发送
 819   1       //写之前要先判断一下需要发送的数据是否比端点0
 820   1       //最大长度大,如果超过端点大小,则一次只能发送
 821   1       //最大包长的数据。端点0的最大包长在DeviceDescriptor[7]
 822   1       if(SendLength>DeviceDescriptor[7])
 823   1       {
 824   2        //按最大包长度发送
 825   2        D12WriteEndpointBuffer(1,DeviceDescriptor[7],pSendData);
 826   2        //发送后剩余字节数减少最大包长
 827   2        SendLength-=DeviceDescriptor[7];
 828   2        //发送一次后指针位置要调整
 829   2        pSendData+= DeviceDescriptor[7];
 830   2       }
 831   1       else
 832   1       {
 833   2        if(SendLength!=0)
 834   2        {
 835   3         //不够最大包长,可以直接发送
 836   3         D12WriteEndpointBuffer(1,SendLength,pSendData);
 837   3         //发送完毕后,SendLength长度变为0
 838   3         SendLength=0;
 839   3        }
 840   2        else //如果要发送的数据包长度为0
 841   2        {
 842   3         if(NeedZeroPacket==1) //如果需要发送0长度数据
 843   3         {
 844   4          D12WriteEndpointBuffer(1,0,pSendData); //发送0长度数据包
 845   4          NeedZeroPacket=0; //清需要发送0长度数据包标志
 846   4         }
 847   3        }
 848   2       }
 849   1      }
 850          ////////////////////////End of function//////////////////////////////
 851          
 852          /********************************************************************
 853          函数功能:端点0输出中断处理函数。
 854          入口参数:无。
 855          返    回:无。
 856          备    注:无。
 857          ********************************************************************/
 858          void UsbEp0Out(void)
 859          {
 860   1      #ifdef DEBUG0
 861   1       Prints("USB端点0输出中断。\r\n");
C51 COMPILER V7.06   USBCORE                                                               11/16/2008 15:59:36 PAGE 15  

 862   1      #endif
 863   1       //读取端点0输出最后传输状态,该操作清除中断标志
 864   1       //并判断第5位是否为1,如果是,则说明是建立包
 865   1       if(D12ReadEndpointLastStatus(0)&0x20)
 866   1       {
 867   2        D12ReadEndpointBuffer(0,16,Buffer); //读建立过程数据
 868   2        D12AcknowledgeSetup(); //应答建立包
 869   2        D12ClearBuffer(); //清缓冲区
 870   2        //将缓冲数据填到设备请求的各字段中
 871   2        bmRequestType=Buffer[0];
 872   2        bRequest=Buffer[1];
 873   2        wValue=Buffer[2]+(((uint16)Buffer[3])<<8);
 874   2        wIndex=Buffer[4]+(((uint16)Buffer[5])<<8);
 875   2        wLength=Buffer[6]+(((uint16)Buffer[7])<<8);
 876   2        //下面的代码判断具体的请求,并根据不同的请求进行相关操作
 877   2        //如果D7位为1,则说明是输入请求
 878   2        if((bmRequestType&0x80)==0x80)
 879   2        {
 880   3         //根据bmRequestType的D6~5位散转,D6~5位表示请求的类型
 881   3         //0为标准请求,1为类请求,2为厂商请求。
 882   3         switch((bmRequestType>>5)&0x03)
 883   3         {
 884   4          case 0:  //标准请求
 885   4           #ifdef DEBUG0
 886   4            Prints("USB标准输入请求:");
 887   4           #endif
 888   4           //USB协议定义了几个标准输入请求,我们实现这些标准请求即可
 889   4           //请求的代码在bRequest中,对不同的请求代码进行散转
 890   4           //事实上,我们还需要对接收者进行散转,因为不同的请求接收者
 891   4           //是不一样的。接收者在bmRequestType的D4~D0位中定义。
 892   4           //我们这里为了简化操作,有些就省略了对接收者的判断。
 893   4           //例如获取描述符的请求,只根据描述符的类型来区别。
 894   4           switch(bRequest)
 895   4           {
 896   5            case GET_CONFIGURATION: //获取配置
 897   5             #ifdef DEBUG0
 898   5              Prints("获取配置。\r\n");
 899   5             #endif
 900   5            break;
 901   5            
 902   5            case GET_DESCRIPTOR:  //获取描述符
 903   5             #ifdef DEBUG0
 904   5              Prints("获取描述符——");
 905   5             #endif
 906   5             //对描述符类型进行散转,对于全速设备,
 907   5             //标准请求只支持发送到设备的设备、配置、字符串三种描述符
 908   5             switch((wValue>>8)&0xFF)
 909   5              {
 910   6               case DEVICE_DESCRIPTOR: //设备描述符
 911   6                #ifdef DEBUG0
 912   6                 Prints("设备描述符。\r\n");
 913   6                #endif
 914   6                pSendData=DeviceDescriptor;  //需要发送的数据
 915   6                //判断请求的字节数是否比实际需要发送的字节数多
 916   6                //这里请求的是设备描述符,因此数据长度就是
 917   6                //DeviceDescriptor[0]。如果请求的比实际的长,
 918   6                //那么只返回实际长度的数据
 919   6                if(wLength>DeviceDescriptor[0])
 920   6                {
 921   7                 SendLength=DeviceDescriptor[0];
 922   7                 if(SendLength%DeviceDescriptor[7]==0) //并且刚好是整数个数据包时
 923   7                 {
C51 COMPILER V7.06   USBCORE                                                               11/16/2008 15:59:36 PAGE 16  

 924   8                  NeedZeroPacket=1; //需要返回0长度的数据包
 925   8                 }
 926   7                }
 927   6                else
 928   6                {
 929   7                 SendLength=wLength;
 930   7                }
 931   6                //将数据通过EP0返回
 932   6                UsbEp0SendData();
 933   6               break;
 934   6               
 935   6               case CONFIGURATION_DESCRIPTOR:  //配置描述符
 936   6                #ifdef DEBUG0
 937   6                 Prints("配置描述符。\r\n");
 938   6                #endif
 939   6                pSendData=ConfigurationDescriptor; //需要发送的数据为配置描述符
 940   6                //判断请求的字节数是否比实际需要发送的字节数多
 941   6                //这里请求的是配置描述符集合,因此数据长度就是
 942   6                //ConfigurationDescriptor[3]*256+ConfigurationDescriptor[2]。
 943   6                //如果请求的比实际的长,那么只返回实际长度的数据
 944   6                SendLength=ConfigurationDescriptor[3];
 945   6                SendLength=SendLength*256+ConfigurationDescriptor[2];
 946   6                if(wLength>SendLength)
 947   6                {
 948   7                 if(SendLength%DeviceDescriptor[7]==0) //并且刚好是整数个数据包时
 949   7                 {
 950   8                  NeedZeroPacket=1; //需要返回0长度的数据包
 951   8                 }
 952   7                }
 953   6                else
 954   6                {
 955   7                 SendLength=wLength;
 956   7                }
 957   6                //将数据通过EP0返回
 958   6                UsbEp0SendData();
 959   6               break;
 960   6               
 961   6               case STRING_DESCRIPTOR:  //字符串描述符
 962   6                #ifdef DEBUG0
 963   6                 Prints("字符串描述符");
 964   6                #endif
 965   6                switch(wValue&0xFF)  //根据wValue的低字节(索引值)散转
 966   6                {
 967   7                 case 0:  //获取语言ID
 968   7                  #ifdef DEBUG0
 969   7                   Prints("(语言ID)。\r\n");
 970   7                  #endif
 971   7                  pSendData=LanguageId;
 972   7                  SendLength=LanguageId[0];
 973   7                 break;
 974   7                 
 975   7                 case 1:  //厂商字符串的索引值为1,所以这里为厂商字符串
 976   7                 #ifdef DEBUG0
 977   7                   Prints("(厂商描述)。\r\n");
 978   7                  #endif
 979   7                  pSendData=ManufacturerStringDescriptor;
 980   7                  SendLength=ManufacturerStringDescriptor[0];
 981   7                 break;
 982   7                 
 983   7                 case 2:  //产品字符串的索引值为2,所以这里为产品字符串
 984   7                 #ifdef DEBUG0
 985   7                   Prints("(产品描述)。\r\n");
C51 COMPILER V7.06   USBCORE                                                               11/16/2008 15:59:36 PAGE 17  

 986   7                  #endif
 987   7                  pSendData=ProductStringDescriptor;
 988   7                  SendLength=ProductStringDescriptor[0];
 989   7                 break;
 990   7                 
 991   7                 case 3:  //产品序列号的索引值为3,所以这里为序列号
 992   7                 #ifdef DEBUG0
 993   7                   Prints("(产品序列号)。\r\n");
 994   7                  #endif
 995   7                  pSendData=SerialNumberStringDescriptor;

⌨️ 快捷键说明

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