chap_9.LST
上传用户:ids068
上传日期:2013-04-04
资源大小:639k
文件大小:71k
源码类别:

USB编程

开发平台:

C/C++

  1. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 1   
  2. C51 COMPILER V7.06, COMPILATION OF MODULE CHAP_9
  3. OBJECT MODULE PLACED IN chap_9.OBJ
  4. COMPILER INVOKED BY: C:KEILC51BINC51.EXE chap_9.c OPTIMIZE(2,SPEED) BROWSE DEBUG OBJECTEXTEND
  5. stmt level    source
  6.    1          /****************************************Copyright (c)**************************************************
  7.    2          **                               广州周立功单片机发展有限公司
  8.    3          **                                     研    究    所
  9.    4          **                                 http://www.zlgmcu.com
  10.    5          **--------------当前版本修订------------------------------------------------------------------------------
  11.    6          ** 修改人: 刘英斌
  12.    7          ** 日 期:2003-03-13
  13.    8          ** 描 述:ISP1581 V1.0
  14.    9          **------------------------------------------------------------------------------------------------------
  15.   10          ********************************************************************************************************/
  16.   11          
  17.   12          #include "standard.h"
  18.   13          #include "Kernel.h"
  19.   14          #include "D14.h"
  20.   15          #include "usb.h"
  21.   16          
  22.   17          extern volatile D14_CNTRL_REG xdata D14_Cntrl_Reg;
  23.   18          extern void Init_D14(void);
  24.   19          extern UC code Standard_Device_Descriptor[];
  25.   20          extern UC code Bulk_Interface_Descriptor[];
  26.   21          extern UC code Iso_Interface_Descriptor[];
  27.   22          extern UC code Standard_Config_Descriptor[];
  28.   23          extern UC code Standard_Manufacturer_String[];
  29.   24          extern UC code Standard_Product_String[];
  30.   25          extern UC code Standard_String_ID[];
  31.   26          extern UC code Bulk_In_Descriptor[];
  32.   27          extern UC code Bulk_Out_Descriptor[];
  33.   28          extern UC code Int_In_Descriptor[];
  34.   29          extern UC code Int_Out_Descriptor[];
  35.   30          extern UC code Iso_In_Descriptor[];
  36.   31          extern UC code Iso_Out_Descriptor[];
  37.   32          extern UC code Iso_In_Zero_Descriptor[];
  38.   33          extern UC code Iso_Out_Zero_Descriptor[];
  39.   34          extern UC code HS_Device_Descriptor[];
  40.   35          extern UC code HS_Device_Qualifier[];
  41.   36          extern UC code HS_Bulk_Interface_Descriptor[];
  42.   37          extern UC code HS_Bulk_In_Descriptor[];
  43.   38          extern UC code HS_Bulk_Out_Descriptor[];
  44.   39          extern UC code HS_Int_In_Descriptor[];
  45.   40          extern UC code HS_Int_Out_Descriptor[];
  46.   41          extern UC code Other_Speed_Config_Descriptor[];
  47.   42          extern UI count;
  48.   43          extern void Start_mSEC_Timer(Data);
  49.   44          extern void Start_SEC_Timer(Data);
  50.   45          extern void Check_Busy(void);
  51.   46          extern void TaskFile_Update(void);
  52.   47          extern UI Temp;
  53.   48          extern UC Device_Config_Value;
  54.   49          extern KERNEL Kernel_Flag;
  55.   50          extern DMA_INT_FLAG DMA_Int_Flag;
  56.   51          extern USB_INT_FLAG USB_Int_Flag;
  57.   52          extern USBCBW USB_CBW;
  58.   53          extern void Init_Device(void);
  59.   54          extern UC code Test_Packet[];
  60.   55          extern void Init_Endpoint(void);
  61. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 2   
  62.   56          extern void Init_Device(void);
  63.   57          extern void Set_USB(void);
  64.   58          
  65.   59          //***********************************************************************
  66.   60          //*                                                                                                                                                            *
  67.   61          //*                      Prototype Routine Definition                                              *
  68.   62          //*                                                                                                                                                            *
  69.   63          //***********************************************************************
  70.   64          
  71.   65          void USB_Setup(void);
  72.   66          void reserved(void);
  73.   67          void Send_Device_Status(void);
  74.   68          void Send_Interface_Status(void);
  75.   69          void Send_Endpoint_Status(void);
  76.   70          
  77.   71          void Vendor_Specific_Command(void);
  78.   72          void Send_Status(unsigned char tran_status);
  79.   73          
  80.   74          void Set_Interface_Value(void);
  81.   75          void Get_Interface_Value(void);
  82.   76          void Process_Synch_Frame(void);
  83.   77          
  84.   78          void get_status(void);
  85.   79          void clear_feature(void);
  86.   80          void set_feature(void);
  87.   81          void set_address(void);
  88.   82          void get_descriptor(void);
  89.   83          void get_config(void);
  90.   84          void set_config(void);
  91.   85          void get_interface(void);
  92.   86          void set_interface(void);
  93.   87          
  94.   88          void Set_Descriptor(void);
  95.   89          void reserved(void);
  96.   90          
  97.   91          void ep0in_ack(void);
  98.   92          void ep0out_ack(void);
  99.   93          void wait_ep0tx(void);
  100.   94          void wait_ep0rx(void);
  101.   95          unsigned int read_endpoint(unsigned char endp,unsigned int len,unsigned char *buf);
  102.   96          unsigned int write_endpoint(unsigned char endp,unsigned int len,unsigned char *buf);
  103.   97          
  104.   98          
  105.   99          
  106.  100          //***********************************************************************
  107.  101          //*                                                                                                                                                            *
  108.  102          //*                          Variable Definition                                                              *
  109.  103          //*                                                                                                                                                            *
  110.  104          //***********************************************************************
  111.  105          
  112.  106          USB_INT_FLAG  USB_Int_Flag;
  113.  107          USB_DEVICE_REQ  idata   USB_Device_Request;
  114.  108          USB_DEVICE              USB_Device;
  115.  109          
  116.  110          UC      code    *Descriptor_Ptr;
  117.  111          
  118.  112          UC      idata Endpt_FIFO[MAX_BUF];
  119.  113          UI      FIFO_Count;
  120.  114          UC VID[2],PID[2];
  121.  115          FILESIZE FileSize;
  122.  116          UI PIO_Count;
  123.  117          UC      File_Number;
  124. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 3   
  125.  118          UC      Current_File;
  126.  119          ULI bytecount;
  127.  120          
  128.  121          //USB标准请求
  129.  122          code void (*StandardDeviceRequest[])(void) =
  130.  123          {
  131.  124                  get_status,
  132.  125                  clear_feature,
  133.  126                  reserved,
  134.  127                  set_feature,
  135.  128                  reserved,
  136.  129                  set_address,
  137.  130                  get_descriptor,
  138.  131                  reserved,
  139.  132                  get_config,
  140.  133                  set_config,
  141.  134                  get_interface,
  142.  135                  set_interface,
  143.  136                  reserved,
  144.  137                  reserved,
  145.  138                  reserved,
  146.  139                  reserved
  147.  140          };
  148.  141          
  149.  142          
  150.  143          
  151.  144          //***********************************************************************
  152.  145          //*                                                                                                                                                            *
  153.  146          //*                          Routine Definition                                                            *
  154.  147          //*                                                                                                                                                            *
  155.  148          //***********************************************************************
  156.  149          
  157.  150          //***********************************************************************
  158.  151          //*                                                                                                                                                    *
  159.  152          //*     Routine         :  USB Debug Mode                                                  *
  160.  153          //*     Input           :  USB Interrupt Register                                                                        *
  161.  154          //*     Output  :  None                                                                    *
  162.  155          //*     Function        :  Branch to respective ISR                                        *
  163.  156          //*                                                                                                                                                            *
  164.  157          //***********************************************************************
  165.  158          
  166.  159          void USB_Debug_Mode(void)
  167.  160          {
  168.  161   1              //端点0接收到setup包处理      
  169.  162   1              if(USB_Int_Flag.BITS.EP0SETUP)
  170.  163   1              {
  171.  164   2                      EA_DISABLE;
  172.  165   2                      USB_Setup();
  173.  166   2                      EA_ENABLE;
  174.  167   2              }
  175.  168   1              
  176.  169   1              //挂起处理
  177.  170   1              if(USB_Int_Flag.BITS.SUSP)
  178.  171   1              {               
  179.  172   2                      //清除挂起标志
  180.  173   2                      EA_DISABLE;
  181.  174   2                      USB_Int_Flag.BITS.SUSP = 0;
  182.  175   2                      EA_ENABLE;
  183.  176   2                      //ISP1581进入挂起状态           
  184.  177   2                      D14_Cntrl_Reg.D14_MODE.BITS.GOSUSP = 1;
  185.  178   2                      D14_Cntrl_Reg.D14_MODE.BITS.GOSUSP = 0;
  186.  179   2              }
  187. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 4   
  188.  180   1              
  189.  181   1              //挂起恢复处理
  190.  182   1              if(USB_Int_Flag.BITS.RESUME)
  191.  183   1              {
  192.  184   2                      //清除恢复标志
  193.  185   2                      EA_DISABLE;
  194.  186   2                      USB_Int_Flag.BITS.RESUME = 0;         
  195.  187   2                      EA_ENABLE;
  196.  188   2                      //产生一个恢复信号
  197.  189   2                      D14_Cntrl_Reg.D14_MODE.BITS.SNDRSU = 1;
  198.  190   2                      D14_Cntrl_Reg.D14_MODE.BITS.SNDRSU = 0;
  199.  191   2              }
  200.  192   1              
  201.  193   1              //高速状态
  202.  194   1              if(USB_Int_Flag.BITS.HS_STAT)
  203.  195   1              {
  204.  196   2                      //清除高速状态标志
  205.  197   2                      EA_DISABLE;
  206.  198   2                      USB_Int_Flag.BITS.HS_STAT = 0;
  207.  199   2                      EA_ENABLE;
  208.  200   2                      if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
  209.  201   2                      {
  210.  202   3                              //切换高速指示
  211.  203   3                              HS_FS_LED = HIGH_SPEED_LED;
  212.  204   3                              //设置高速模式状态
  213.  205   3                              Kernel_Flag.BITS.HS_FS_State = HIGH_SPEED;
  214.  206   3                              //重新配置高速端点
  215.  207   3                              Init_Endpoint();
  216.  208   3      
  217.  209   3      //                      PIO_Count = 64;
  218.  210   3                              PIO_Count = 512;
  219.  211   3                      }
  220.  212   2              }
  221.  213   1      }
  222.  214          
  223.  215          
  224.  216          //***********************************************************************
  225.  217          //*                                                                                                                                     *
  226.  218          //*     Routine         :                                                               *
  227.  219          //*     Input           : None                                                                                  *
  228.  220          //*     Output          : None                                                                  *
  229.  221          //*     Function        :                                                               *
  230.  222          //*                                                                                                                                             *
  231.  223          //***********************************************************************
  232.  224          //建立包处理
  233.  225          void USB_Setup(void)
  234.  226          {
  235.  227   1              unsigned char type, req;
  236.  228   1      
  237.  229   1              //清除EP0SETUP中断标志
  238.  230   1              USB_Int_Flag.BITS.EP0SETUP = 0;   
  239.  231   1              //清除总线复位标志
  240.  232   1              Kernel_Flag.BITS.Bus_Reset = 0;
  241.  233   1              //选择端点0SETUP缓冲区
  242.  234   1              D14_Cntrl_Reg.D14_ENDPT_INDEX = 0x20;
  243.  235   1              
  244.  236   1              //读取USB请求
  245.  237   1              //读取请求类型
  246.  238   1              USB_Device_Request.bmRequestType =  D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  247.  239   1              //读取请求号
  248.  240   1              USB_Device_Request.bRequest = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  249.  241   1              //读取16bit请求数据
  250. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 5   
  251.  242   1              USB_Device_Request.wValue = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  252.  243   1              USB_Device_Request.wValue <<= 8;
  253.  244   1              USB_Device_Request.wValue |= D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  254.  245   1              //读取16bit请求索引
  255.  246   1              USB_Device_Request.wIndex = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  256.  247   1              USB_Device_Request.wIndex <<= 8;
  257.  248   1              USB_Device_Request.wIndex |= D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  258.  249   1              //读取16bit长度
  259.  250   1              USB_Device_Request.wLength = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  260.  251   1              USB_Device_Request.wLength <<= 8;
  261.  252   1              USB_Device_Request.wLength |= D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  262.  253   1              
  263.  254   1              //长度处理(高低8位对调)
  264.  255   1              Temp = USB_Device_Request.wLength;
  265.  256   1              USB_Device_Request.wLength = 0;
  266.  257   1              USB_Device_Request.wLength = (UC) Temp;
  267.  258   1              USB_Device_Request.wLength <<= 8;
  268.  259   1              Temp >>= 8;
  269.  260   1              USB_Device_Request.wLength |= Temp;
  270.  261   1              
  271.  262   1              type = USB_Device_Request.bmRequestType & 0x60;
  272.  263   1              req = USB_Device_Request.bRequest & 0x0f;
  273.  264   1      
  274.  265   1              //厂商请求
  275.  266   1              if (type == 0x40)
  276.  267   1              {
  277.  268   2                      Vendor_Specific_Command();//厂商请求处理
  278.  269   2              }
  279.  270   1              else if (type == 0x00)
  280.  271   1                      (*StandardDeviceRequest[req])();//调用标准请求
  281.  272   1      }
  282.  273          
  283.  274          //***********************************************************************
  284.  275          //*                                                                                                                                     *
  285.  276          //*     Routine         :                                                               *
  286.  277          //*     Input           : None                                                                                  *
  287.  278          //*     Output          : None                                                                  *
  288.  279          //*     Function        :                                                               *
  289.  280          //*                                                                                                                                             *
  290.  281          //***********************************************************************
  291.  282          //厂商请求处理
  292.  283          void Vendor_Specific_Command(void)
  293.  284          {
  294.  285   1              if(USB_Device_Request.bRequest == 0x0C)
  295.  286   1              {
  296.  287   2                      switch (USB_Device_Request.wIndex)
  297.  288   2                      {
  298.  289   3                      case GET_FIRMWARE_VERSION  ://获取固件版本
  299.  290   3                              if(USB_Device_Request.wLength == 1)
  300.  291   3                              {
  301.  292   4                                      //select endpoint
  302.  293   4                                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  303.  294   4                                      //set the data length to 1 byte
  304.  295   4                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 1;
  305.  296   4                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  306.  297   4                                      //sent 0x21 as data
  307.  298   4                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = 0x22;//版本号
  308.  299   4                                      wait_ep0tx();
  309.  300   4                                      ep0out_ack();
  310.  301   4      //                              ep0in_ack();
  311.  302   4                                      break;
  312.  303   4                              }
  313. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 6   
  314.  304   3                              
  315.  305   3                      case SETUP_DMA_REQUEST :
  316.  306   3                              //Out Token Ack detected
  317.  307   3                              wait_ep0rx();
  318.  308   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
  319.  309   3                              //
  320.  310   3                              FileSize.Size.Address[0] = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  321.  311   3                              FileSize.Size.Address[1] = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  322.  312   3                              FileSize.Size.Address[2] = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  323.  313   3                              //读取传输字节数
  324.  314   3                              Temp = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  325.  315   3                              bytecount = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  326.  316   3                              bytecount <<= 8;
  327.  317   3                              bytecount |= Temp;
  328.  318   3                              //读取传输方式
  329.  319   3                              FileSize.Size.DIR = D14_Cntrl_Reg.D14_DATA_PORT_LSB;
  330.  320   3                              ep0in_ack();
  331.  321   3                              Kernel_Flag.BITS.Transfer_Start = 1;//标志传输数据开始
  332.  322   3                              break;
  333.  323   3                              
  334.  324   3                      default :
  335.  325   3                              if(USB_Device_Request.bmRequestType == 0xC0)
  336.  326   3                              {
  337.  327   4                                      ep0in_ack();
  338.  328   4                                      break;                          
  339.  329   4                              }
  340.  330   3                              if(USB_Device_Request.bmRequestType == 0x40)
  341.  331   3                              {       
  342.  332   4                                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
  343.  333   4                                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  344.  334   4                                      while(!USB_Int_Flag.BITS.EP0SETUP)
  345.  335   4                                      {
  346.  336   5                                              //读取ISP1581中断寄存器
  347.  337   5                                              USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  348.  338   5                                              //回写ISP1581中断寄存器
  349.  339   5                                              D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  350.  340   5                                              if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  351.  341   5                                                      return;
  352.  342   5                                      }
  353.  343   4                                      USB_Int_Flag.BITS.EP0TX = 0;
  354.  344   4                                      break;                          
  355.  345   4                              }
  356.  346   3                      }
  357.  347   2              }
  358.  348   1      }
  359.  349          
  360.  350          //***********************************************************************
  361.  351          //*                                                                                                                                     *
  362.  352          //*     Routine         :                                                               *
  363.  353          //*     Input           : None                                                                                  *
  364.  354          //*     Output          : None                                                                  *
  365.  355          //*     Function        :                                                               *
  366.  356          //*                                                                                                                                             *
  367.  357          //***********************************************************************
  368.  358          
  369.  359          void    reserved(void)
  370.  360          {
  371.  361   1      }
  372.  362          
  373.  363          
  374.  364          //Get_Descriptor()获取描述符
  375.  365          void get_descriptor(void)
  376. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 7   
  377.  366          {
  378.  367   1      
  379.  368   1              switch(USB_Device_Request.wValue)
  380.  369   1              {
  381.  370   2              case  1://设备描述符
  382.  371   2                      //选择描述符
  383.  372   2                      if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
  384.  373   2                              Descriptor_Ptr = &Standard_Device_Descriptor[0];        //set pointer to device descriptor
  385.  374   2                      if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
  386.  375   2                              Descriptor_Ptr = &HS_Device_Descriptor[0];      //set pointer to device descriptor
  387.  376   2                      count = *Descriptor_Ptr;                                                        //read in descriptor length
  388.  377   2                      //选择端点号
  389.  378   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  390.  379   2                      //写下发送字节数
  391.  380   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 0x12;
  392.  381   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  393.  382   2                      //send descriptor
  394.  383   2                      //发送描述符
  395.  384   2                      while(count != 0)
  396.  385   2                      {
  397.  386   3                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  398.  387   3                              count--;
  399.  388   3                              Descriptor_Ptr++;
  400.  389   3                      }
  401.  390   2                      wait_ep0tx();
  402.  391   2                      ep0out_ack();
  403.  392   2                      break;
  404.  393   2                      
  405.  394   2              case  2        :  //获取配置描述符
  406.  395   2                      USB_Int_Flag.BITS.EP0RX = 0;
  407.  396   2                      Temp = 64;
  408.  397   2                      Descriptor_Ptr = &Standard_Config_Descriptor[0];        //set pointer to configuration descriptor
  409.  398   2                      count = *Descriptor_Ptr;                                                        //get configuration Descriptor length
  410.  399   2                      //选择端点号1
  411.  400   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  412.  401   2                      //check if only configuration descriptor required
  413.  402   2                      //检查描述符长度,决定是只发送配置描述符还是描述符集合
  414.  403   2                      if(count >= USB_Device_Request.wLength)                         //Send only Configuration descriptor
  415.  404   2                      {
  416.  405   3                              //写入发送字节数
  417.  406   3                              D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) USB_Device_Request.wLength;
  418.  407   3                              D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (USB_Device_Request.wLength >> 8);
  419.  408   3                              //send descriptor
  420.  409   3                              //发送描述符
  421.  410   3                              for(count = 0 ; count < USB_Device_Request.wLength ; count++)
  422.  411   3                              {
  423.  412   4                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  424.  413   4                                      Descriptor_Ptr++;
  425.  414   4                              }
  426.  415   3                              wait_ep0tx();
  427.  416   3                              ep0out_ack();
  428.  417   3                      }
  429.  418   2                      else //send Configuration,Interface and Endpoint descriptor
  430.  419   2                      {
  431.  420   3                              //发送描述符集合(配置+接口+端点)
  432.  421   3                              if(USB_Device_Request.wLength >= 32)
  433.  422   3                              {
  434.  423   4                                      //写入配置描述符
  435.  424   4                                      while(count != 0)
  436.  425   4                                      {
  437.  426   5                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  438.  427   5                                              count--;
  439. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 8   
  440.  428   5                                              Descriptor_Ptr++;
  441.  429   5                                      }
  442.  430   4                                      //选择高速还是全速接口描述符
  443.  431   4                                      if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
  444.  432   4                                              Descriptor_Ptr = &Bulk_Interface_Descriptor[0]; //set pointer to interface descriptor
  445.  433   4                                      if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
  446.  434   4                                              Descriptor_Ptr = &HS_Bulk_Interface_Descriptor[0]; //set pointer to interface descriptor
  447.  435   4                                      count = *Descriptor_Ptr;        //get the descriptor count
  448.  436   4                                      //写入接口描述符
  449.  437   4                                      while(count != 0)
  450.  438   4                                      {
  451.  439   5                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  452.  440   5                                              count--;
  453.  441   5                                              Descriptor_Ptr++;
  454.  442   5                                      }
  455.  443   4                                      //选择高速还是全速端点1IN描述符
  456.  444   4                                      if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
  457.  445   4                                              Descriptor_Ptr = &Int_In_Descriptor[0]; //set pointer to interface descriptor
  458.  446   4                                      if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
  459.  447   4                                              Descriptor_Ptr = &HS_Int_In_Descriptor[0]; //set pointer to interface descriptor
  460.  448   4                                      count = *Descriptor_Ptr;        //read in descriptor length
  461.  449   4                                      //写入端点1IN描述符
  462.  450   4                                      while(count != 0)
  463.  451   4                                      {
  464.  452   5                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  465.  453   5                                              count--;
  466.  454   5                                              Descriptor_Ptr++;
  467.  455   5                                      }
  468.  456   4                                      //选择高速还是全速端点1OUT描述符
  469.  457   4                                      if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
  470.  458   4                                              Descriptor_Ptr = &Int_Out_Descriptor[0]; //set pointer to interface descriptor
  471.  459   4                                      if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
  472.  460   4                                              Descriptor_Ptr = &HS_Int_Out_Descriptor[0]; //set pointer to interface descriptor
  473.  461   4                                      count = *Descriptor_Ptr;        //read in descriptor length
  474.  462   4                                      //写入端点1OUT描述符
  475.  463   4                                      while(count != 0)
  476.  464   4                                      {
  477.  465   5                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  478.  466   5                                              count--;
  479.  467   5                                              Descriptor_Ptr++;
  480.  468   5                                      }
  481.  469   4                                      //选择高速还是全速端点2IN描述符
  482.  470   4                                      if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
  483.  471   4                                              Descriptor_Ptr = &Bulk_In_Descriptor[0]; //set pointer to interface descriptor
  484.  472   4                                      if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
  485.  473   4                                              Descriptor_Ptr = &HS_Bulk_In_Descriptor[0]; //set pointer to interface descriptor
  486.  474   4                                      count = *Descriptor_Ptr;        //read in descriptor length
  487.  475   4                                      //写入端点2IN描述符
  488.  476   4                                      while(count != 0)
  489.  477   4                                      {
  490.  478   5                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  491.  479   5                                              count--;
  492.  480   5                                              Descriptor_Ptr++;
  493.  481   5                                      }
  494.  482   4                                      //选择高速还是全速端点2OUT描述符
  495.  483   4                                      if(Kernel_Flag.BITS.HS_FS_State == FULL_SPEED)
  496.  484   4                                              Descriptor_Ptr = &Bulk_Out_Descriptor[0]; //set pointer to interface descriptor
  497.  485   4                                      if(Kernel_Flag.BITS.HS_FS_State == HIGH_SPEED)
  498.  486   4                                              Descriptor_Ptr = &HS_Bulk_Out_Descriptor[0]; //set pointer to interface descriptor
  499.  487   4                                      count = *Descriptor_Ptr;        //read in descriptor length
  500.  488   4                                      //写入端点2OUT描述符
  501.  489   4                                      while(count != 0)
  502. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 9   
  503.  490   4                                      {
  504.  491   5                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  505.  492   5                                              count--;
  506.  493   5                                              Descriptor_Ptr++;
  507.  494   5                                      }
  508.  495   4                              }
  509.  496   3                              //使端点有效
  510.  497   3                              D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.VENDP = 1;      //validate FIFO
  511.  498   3                              wait_ep0tx();
  512.  499   3                              ep0out_ack();
  513.  500   3                      }
  514.  501   2                      break;
  515.  502   2                      
  516.  503   2                      case  3:  //获取字符串描述符
  517.  504   2                              Descriptor_Ptr = &Standard_String_ID[0];                //set pointer to interface descriptor
  518.  505   2                              count = *Descriptor_Ptr;                //get length
  519.  506   2                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  520.  507   2                              if(USB_Device_Request.wLength < count)
  521.  508   2                                      count = USB_Device_Request.wLength;                     //get the remaining length
  522.  509   2                              D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = count;
  523.  510   2                              D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  524.  511   2                              //send string ID
  525.  512   2                              while(count != 0)
  526.  513   2                              {
  527.  514   3                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;      //fill up FIFO
  528.  515   3                                      Descriptor_Ptr++;
  529.  516   3                                      count--;
  530.  517   3                              }
  531.  518   2                              wait_ep0tx();
  532.  519   2                              ep0out_ack();
  533.  520   2                              break;
  534.  521   2                              
  535.  522   2                              
  536.  523   2                      case  4:  
  537.  524   2                              reserved();
  538.  525   2                              break;
  539.  526   2                              
  540.  527   2                      case  5:  //获取端点描述符
  541.  528   2                              Temp = 0;
  542.  529   2                              if((USB_Device_Request.wValue & 0x00ff) > 0x0001)
  543.  530   2                              {
  544.  531   3                                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  545.  532   3                                      while(!USB_Int_Flag.BITS.EP0SETUP)
  546.  533   3                                      {
  547.  534   4                                              //读取ISP1581中断寄存器
  548.  535   4                                              USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  549.  536   4                                              //回写ISP1581中断寄存器
  550.  537   4                                              D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  551.  538   4                                              if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  552.  539   4                                                      return;
  553.  540   4                                      }
  554.  541   3                                      USB_Int_Flag.BITS.EP0TX = 0;
  555.  542   3                              }
  556.  543   2                              else
  557.  544   2                              {
  558.  545   3                                      Descriptor_Ptr = &Bulk_Out_Descriptor[0]; //set pointer to Bulk Out endpoint descriptor
  559.  546   3                                      count = *Descriptor_Ptr;        //read in descriptor length
  560.  547   3                                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  561.  548   3                                      if(count >= USB_Device_Request.wLength)
  562.  549   3                                      {
  563.  550   4                                              Temp = USB_Device_Request.wLength;
  564.  551   4                                              //send endpoint descriptor
  565. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 10  
  566.  552   4                                              D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) Temp;
  567.  553   4                                              D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (Temp >> 8);
  568.  554   4                                              for(count = 0 ; count < USB_Device_Request.wLength ; count++)
  569.  555   4                                              {
  570.  556   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  571.  557   5                                                      Descriptor_Ptr++;
  572.  558   5                                              }
  573.  559   4                                              wait_ep0tx();
  574.  560   4                                              ep0out_ack();
  575.  561   4                                      }
  576.  562   3                                      else
  577.  563   3                                      {
  578.  564   4                                              USB_Device_Request.wLength -= count; //send Endpoint descriptor
  579.  565   4                                              Temp += count;
  580.  566   4                                              while(count != 0)
  581.  567   4                                              {
  582.  568   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  583.  569   5                                                      count--;
  584.  570   5                                                      Descriptor_Ptr++;
  585.  571   5                                              }
  586.  572   4                                              Descriptor_Ptr = &Bulk_In_Descriptor[0]; //set pointer to Bulk In endpoint descriptor
  587.  573   4                                              count = *Descriptor_Ptr;        //read in descriptor length
  588.  574   4                                              if(count >= USB_Device_Request.wLength)
  589.  575   4                                              {
  590.  576   5                                                      Temp += USB_Device_Request.wLength;
  591.  577   5                                                      //send endpoint descriptor
  592.  578   5                                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) Temp;
  593.  579   5                                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (Temp >> 8);
  594.  580   5                                                      for(count = 0 ; count < USB_Device_Request.wLength ; count++)
  595.  581   5                                                      {
  596.  582   6                                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  597.  583   6                                                              Descriptor_Ptr++;
  598.  584   6                                                      }
  599.  585   5                                                      wait_ep0tx();
  600.  586   5                                                      ep0out_ack();
  601.  587   5                                              }
  602.  588   4                                              else
  603.  589   4                                              {
  604.  590   5                                                      USB_Device_Request.wLength -= count; //send Endpoint descriptor
  605.  591   5                                                      Temp += count;
  606.  592   5                                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) Temp;
  607.  593   5                                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (Temp >> 8);
  608.  594   5                                                      while(count != 0)
  609.  595   5                                                      {
  610.  596   6                                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  611.  597   6                                                              count--;
  612.  598   6                                                              Descriptor_Ptr++;
  613.  599   6                                                      }
  614.  600   5                                                      wait_ep0tx();
  615.  601   5                                                      ep0out_ack();
  616.  602   5                                              }       
  617.  603   4                                      }                                       
  618.  604   3                              }
  619.  605   2                              break;
  620.  606   2                              
  621.  607   2                      case  6:  //获取设备限制描述符
  622.  608   2                              Descriptor_Ptr = &HS_Device_Qualifier[0];               //set pointer to High Speed device descriptor
  623.  609   2                              count = *Descriptor_Ptr;                                                        //read in descriptor length
  624.  610   2                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  625.  611   2                              if(USB_Device_Request.wLength < count)
  626.  612   2                              {
  627.  613   3                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) USB_Device_Request.wLength;
  628. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 11  
  629.  614   3                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  630.  615   3                                      count = USB_Device_Request.wLength;
  631.  616   3                              }
  632.  617   2                              else
  633.  618   2                              {
  634.  619   3                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = count;
  635.  620   3                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  636.  621   3                              }
  637.  622   2                              while(count != 0)
  638.  623   2                              {
  639.  624   3                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;      //fill up FIFO
  640.  625   3                                      Descriptor_Ptr++;
  641.  626   3                                      count--;
  642.  627   3                              };
  643.  628   2                              wait_ep0tx();
  644.  629   2                              ep0out_ack();
  645.  630   2                              break;
  646.  631   2                              
  647.  632   2                      case  7  ://获取速度配置描述符
  648.  633   2                              Temp = 64;
  649.  634   2                              Descriptor_Ptr = &Other_Speed_Config_Descriptor[0];     //set pointer to configuration descriptor
  650.  635   2                              count = *Descriptor_Ptr;                                                        //get configuration Descriptor length
  651.  636   2                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  652.  637   2                              if(count >= USB_Device_Request.wLength)                         //Send only Configuration descriptor
  653.  638   2                              {
  654.  639   3                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (UC) USB_Device_Request.wLength;
  655.  640   3                                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (UC) (USB_Device_Request.wLength >> 8);
  656.  641   3                                      for(count = 0 ; count < USB_Device_Request.wLength ; count++)
  657.  642   3                                      {
  658.  643   4                                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  659.  644   4                                              Descriptor_Ptr++;
  660.  645   4                                      }
  661.  646   3                                      wait_ep0tx();
  662.  647   3                                      ep0out_ack();
  663.  648   3                              }
  664.  649   2                              else //send Configuration,Interface and Endpoint descriptor
  665.  650   2                              {
  666.  651   3                                      if(USB_Device_Request.wLength >= 32)
  667.  652   3                                      {
  668.  653   4                                              while(count != 0)
  669.  654   4                                              {
  670.  655   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  671.  656   5                                                      count--;
  672.  657   5                                                      Descriptor_Ptr++;
  673.  658   5                                              }
  674.  659   4                                              Descriptor_Ptr = &Bulk_Interface_Descriptor[0]; //set pointer to interface descriptor
  675.  660   4                                              count = *Descriptor_Ptr;        //get the descriptor count
  676.  661   4                                              while(count != 0)
  677.  662   4                                              {
  678.  663   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  679.  664   5                                                      count--;
  680.  665   5                                                      Descriptor_Ptr++;
  681.  666   5                                              }
  682.  667   4                                              Descriptor_Ptr = &Int_In_Descriptor[0]; //set pointer to interface descriptor
  683.  668   4                                              count = *Descriptor_Ptr;        //read in descriptor length
  684.  669   4                                              while(count != 0)
  685.  670   4                                              {
  686.  671   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  687.  672   5                                                      count--;
  688.  673   5                                                      Descriptor_Ptr++;
  689.  674   5                                              }
  690.  675   4                                              Descriptor_Ptr = &Int_Out_Descriptor[0]; //set pointer to interface descriptor
  691. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 12  
  692.  676   4                                              count = *Descriptor_Ptr;        //read in descriptor length
  693.  677   4                                              while(count != 0)
  694.  678   4                                              {
  695.  679   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  696.  680   5                                                      count--;
  697.  681   5                                                      Descriptor_Ptr++;
  698.  682   5                                              }
  699.  683   4                                              Descriptor_Ptr = &Bulk_In_Descriptor[0]; //set pointer to interface descriptor
  700.  684   4                                              count = *Descriptor_Ptr;        //read in descriptor length
  701.  685   4                                              while(count != 0)
  702.  686   4                                              {
  703.  687   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  704.  688   5                                                      count--;
  705.  689   5                                                      Descriptor_Ptr++;
  706.  690   5                                              }
  707.  691   4                                              Descriptor_Ptr = &Bulk_Out_Descriptor[0]; //set pointer to interface descriptor
  708.  692   4                                              count = *Descriptor_Ptr;        //read in descriptor length
  709.  693   4                                              while(count != 0)
  710.  694   4                                              {
  711.  695   5                                                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *Descriptor_Ptr;
  712.  696   5                                                      count--;
  713.  697   5                                                      Descriptor_Ptr++;
  714.  698   5                                              }
  715.  699   4                                      }                                       
  716.  700   3                                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.VENDP = 1;      //validate FIFO
  717.  701   3                                      wait_ep0tx();
  718.  702   3                                      ep0out_ack();
  719.  703   3                              }
  720.  704   2                              break;
  721.  705   2                              
  722.  706   2                      case  8:  
  723.  707   2                              break;
  724.  708   2                              
  725.  709   2                      default  :
  726.  710   2                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  727.  711   2                              D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  728.  712   2                              while(!USB_Int_Flag.BITS.EP0SETUP)
  729.  713   2                              {
  730.  714   3                                      //读取ISP1581中断寄存器
  731.  715   3                                      USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  732.  716   3                                      //回写ISP1581中断寄存器
  733.  717   3                                      D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  734.  718   3                                      if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  735.  719   3                                              return;
  736.  720   3                              }
  737.  721   2                              USB_Int_Flag.BITS.EP0TX = 0;
  738.  722   2                              break;
  739.  723   2              }
  740.  724   1      }       
  741.  725          
  742.  726          //Set_Address设置地址
  743.  727          void set_address(void)
  744.  728          {
  745.  729   1              USB_Int_Flag.BITS.EP0RX = 0;
  746.  730   1              D14_Cntrl_Reg.D14_ADDRESS.BITS.DEVADDR = (USB_Device_Request.wValue >> 8);//设置地址
  747.  731   1              //状态响应
  748.  732   1              ep0in_ack();
  749.  733   1      }
  750.  734          
  751.  735          //Set_Config设置配置
  752.  736          void set_config(void)
  753.  737          {
  754. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 13  
  755.  738   1              USB_Int_Flag.BITS.EP0RX = 0;
  756.  739   1              
  757.  740   1              if(USB_Device_Request.wValue == 0)
  758.  741   1              {
  759.  742   2                      Device_Config_Value = 0;
  760.  743   2                      ep0in_ack();
  761.  744   2              }
  762.  745   1              else if (USB_Device_Request.wValue ==  0x0100)
  763.  746   1          {
  764.  747   2                      //配置设备
  765.  748   2                      Device_Config_Value = (UC) (USB_Device_Request.wValue >> 8);
  766.  749   2      //              USB_Device.BITS.State = USB_Configured;
  767.  750   2                      ep0in_ack();
  768.  751   2              }
  769.  752   1              else
  770.  753   1              {
  771.  754   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  772.  755   2                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  773.  756   2                      while(!USB_Int_Flag.BITS.EP0SETUP)
  774.  757   2                      {
  775.  758   3                              //读取ISP1581中断寄存器
  776.  759   3                              USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  777.  760   3                              //回写ISP1581中断寄存器
  778.  761   3                              D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  779.  762   3                              if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  780.  763   3                                      return;
  781.  764   3                      }
  782.  765   2                      USB_Int_Flag.BITS.EP0TX = 0;
  783.  766   2              }
  784.  767   1      }
  785.  768          
  786.  769          
  787.  770          //Get_Config获取配置
  788.  771          void get_config(void)
  789.  772          {
  790.  773   1              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  791.  774   1              D14_Cntrl_Reg.D14_DATA_PORT_LSB = Device_Config_Value;
  792.  775   1              D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.VENDP = 1;
  793.  776   1              wait_ep0tx();
  794.  777   1              ep0out_ack();
  795.  778   1      }       
  796.  779          
  797.  780          //Clear_Feature特性清除
  798.  781          void clear_feature(void)
  799.  782          {
  800.  783   1              unsigned char bRecipient = USB_Device_Request.bmRequestType & 0x0f;
  801.  784   1              //端点
  802.  785   1              if(bRecipient == 0x02 &&USB_Device_Request.wValue == 0x0000)
  803.  786   1              {
  804.  787   2                      switch(USB_Device_Request.wIndex)
  805.  788   2                      {
  806.  789   3                      case  0x8000   :
  807.  790   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  808.  791   3                              break;
  809.  792   3                      case  0x0000   :
  810.  793   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
  811.  794   3                              break;
  812.  795   3                      case  0x8100   :
  813.  796   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 3;
  814.  797   3                              break;
  815.  798   3                      case  0x0100   :
  816.  799   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 2;
  817. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 14  
  818.  800   3                              break;
  819.  801   3                      case  0x8200   :
  820.  802   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 5;
  821.  803   3                              break;
  822.  804   3                      case  0x0200   :
  823.  805   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 4;
  824.  806   3                              break;
  825.  807   3                      case  0x8300   :
  826.  808   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 7;
  827.  809   3                              break;
  828.  810   3                      case  0x0300   :
  829.  811   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 6;
  830.  812   3                              break;
  831.  813   3                      case  0x8400   :
  832.  814   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 9;
  833.  815   3                              break;
  834.  816   3                      case  0x0400   :
  835.  817   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 8;
  836.  818   3                              break;
  837.  819   3                      case  0x8500   :
  838.  820   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 11;
  839.  821   3                              break;
  840.  822   3                      case  0x0500   :
  841.  823   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 10;
  842.  824   3                              break;
  843.  825   3                      case  0x8600   :
  844.  826   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 13;
  845.  827   3                              break;
  846.  828   3                      case  0x0600   :
  847.  829   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 12;
  848.  830   3                              break;
  849.  831   3                      case  0x8700   :
  850.  832   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 15;
  851.  833   3                              break;
  852.  834   3                      case  0x0700   :
  853.  835   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 14;
  854.  836   3                              break;
  855.  837   3                      default        :  
  856.  838   3                              break;
  857.  839   3                      }                                                                                                      
  858.  840   2                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 0;
  859.  841   2                      Kernel_Flag.BITS.Endpt_Stall = 0;
  860.  842   2                      D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = OFF;                 //disable FIFO
  861.  843   2                      D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = ON;                  //disable FIFO
  862.  844   2                      ep0in_ack();
  863.  845   2                      USB_Device.BITS.Halt = 0;
  864.  846   2              }                                                                                       
  865.  847   1              else if(bRecipient == 0x00 && USB_Device_Request.wValue == 0x0100)
  866.  848   1              {       //远程唤醒
  867.  849   2                      USB_Device.BITS.Remote_Wakeup = 0;
  868.  850   2                      ep0in_ack();
  869.  851   2              }
  870.  852   1              else
  871.  853   1              {
  872.  854   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  873.  855   2                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  874.  856   2                      while(!USB_Int_Flag.BITS.EP0SETUP)
  875.  857   2                      {
  876.  858   3                              //读取ISP1581中断寄存器
  877.  859   3                              USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  878.  860   3                              //回写ISP1581中断寄存器
  879.  861   3                              D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  880. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 15  
  881.  862   3                              if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  882.  863   3                                      return;
  883.  864   3                      }
  884.  865   2                      USB_Int_Flag.BITS.EP0TX = 0;
  885.  866   2              }
  886.  867   1      }       
  887.  868          
  888.  869          //Get_Status获取设备状态
  889.  870          void get_status(void)
  890.  871          {
  891.  872   1              if(USB_Device_Request.bmRequestType == 0x80)
  892.  873   1              {
  893.  874   2                      //端点0IN返回0x01,0x00两字节
  894.  875   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  895.  876   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 2;
  896.  877   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  897.  878   2                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = 1;
  898.  879   2                      D14_Cntrl_Reg.D14_DATA_PORT_MSB = 0;
  899.  880   2                      //等待发送完毕
  900.  881   2                      wait_ep0tx();
  901.  882   2                      ep0out_ack();
  902.  883   2              }
  903.  884   1      
  904.  885   1              if(USB_Device_Request.bmRequestType == 0x81)
  905.  886   1              {
  906.  887   2                      //端点0IN返回0x00,0x00两字节
  907.  888   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  908.  889   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 2;
  909.  890   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  910.  891   2                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = 0;
  911.  892   2                      D14_Cntrl_Reg.D14_DATA_PORT_MSB = 0;
  912.  893   2                      wait_ep0tx();
  913.  894   2                      ep0out_ack();
  914.  895   2              }
  915.  896   1      
  916.  897   1              if(USB_Device_Request.bmRequestType == 0x82)
  917.  898   1              {
  918.  899   2                      Temp = 0;
  919.  900   2                      Temp = USB_Device.BITS.Halt;
  920.  901   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  921.  902   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 2;
  922.  903   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  923.  904   2                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = (UC) Temp;
  924.  905   2                      D14_Cntrl_Reg.D14_DATA_PORT_MSB = (UC) (Temp >> 8);
  925.  906   2                      wait_ep0tx();
  926.  907   2                      ep0out_ack();
  927.  908   2              }
  928.  909   1      }       
  929.  910          
  930.  911          
  931.  912          
  932.  913          //Set_Feature特性设置
  933.  914          //不支持测试模式
  934.  915          void set_feature(void)
  935.  916          {
  936.  917   1              unsigned char bRecipient = USB_Device_Request.bmRequestType & 0x0f;
  937.  918   1              //端点
  938.  919   1              if(bRecipient == 0x02 && USB_Device_Request.wValue == 0x0000)
  939.  920   1              {
  940.  921   2                      switch(USB_Device_Request.wIndex)
  941.  922   2                      {
  942.  923   3                      case  0x8000   :
  943. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 16  
  944.  924   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  945.  925   3                              break;
  946.  926   3                      case  0x0000   :
  947.  927   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
  948.  928   3                              break;
  949.  929   3                      case  0x8100   :
  950.  930   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 3;
  951.  931   3                              break;
  952.  932   3                      case  0x0100   :
  953.  933   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 2;
  954.  934   3                              break;
  955.  935   3                      case  0x8200   :
  956.  936   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 5;
  957.  937   3                              break;
  958.  938   3                      case  0x0200   :
  959.  939   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 4;
  960.  940   3                              break;
  961.  941   3                      case  0x8300   :
  962.  942   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 7;
  963.  943   3                              break;
  964.  944   3                      case  0x0300   :
  965.  945   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 6;
  966.  946   3                              break;
  967.  947   3                      case  0x8400   :
  968.  948   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 9;
  969.  949   3                              break;
  970.  950   3                      case  0x0400   :
  971.  951   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 8;
  972.  952   3                              break;
  973.  953   3                      case  0x8500   :
  974.  954   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 11;
  975.  955   3                              break;
  976.  956   3                      case  0x0500   :
  977.  957   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 10;
  978.  958   3                              break;
  979.  959   3                      case  0x8600   :
  980.  960   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 13;
  981.  961   3                              break;
  982.  962   3                      case  0x0600   :
  983.  963   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 12;
  984.  964   3                              break;
  985.  965   3                      case  0x8700   :
  986.  966   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 15;
  987.  967   3                              break;
  988.  968   3                      case  0x0700   :
  989.  969   3                              D14_Cntrl_Reg.D14_ENDPT_INDEX = 14;
  990.  970   3                              break;
  991.  971   3                      default        :  
  992.  972   3                              break;
  993.  973   3                      }                                                                                                      
  994.  974   2                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  995.  975   2                      Kernel_Flag.BITS.Endpt_Stall = 1;
  996.  976   2                      D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = OFF;                 //disable FIFO
  997.  977   2                      //D14_Cntrl_Reg.D14_ENDPT_TYPE.BITS.ENABLE = ON;                        //disable FIFO
  998.  978   2                      ep0in_ack();
  999.  979   2                      USB_Device.BITS.Halt = 0;
  1000.  980   2              }                                                                                       
  1001.  981   1              else if(bRecipient == 0x00 && USB_Device_Request.wValue == 0x0100)
  1002.  982   1              {       //远程唤醒
  1003.  983   2                      USB_Device.BITS.Remote_Wakeup = 1;
  1004.  984   2                      ep0in_ack();
  1005.  985   2              }
  1006. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 17  
  1007.  986   1              else
  1008.  987   1              {
  1009.  988   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  1010.  989   2                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  1011.  990   2                      while(!USB_Int_Flag.BITS.EP0SETUP)
  1012.  991   2                      {
  1013.  992   3                              //读取ISP1581中断寄存器
  1014.  993   3                              USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  1015.  994   3                              //回写ISP1581中断寄存器
  1016.  995   3                              D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  1017.  996   3                              if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  1018.  997   3                                      return;
  1019.  998   3                      }
  1020.  999   2                      USB_Int_Flag.BITS.EP0TX = 0;
  1021. 1000   2              }
  1022. 1001   1      }
  1023. 1002          
  1024. 1003          
  1025. 1004          //Get_Interface 获取接口状态
  1026. 1005          void get_interface(void)
  1027. 1006          {
  1028. 1007   1              if(USB_Device_Request.wValue != 0)
  1029. 1008   1              {
  1030. 1009   2                      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STALL = 1;
  1031. 1010   2                      while(!USB_Int_Flag.BITS.EP0SETUP)
  1032. 1011   2                      {
  1033. 1012   3                              //读取ISP1581中断寄存器
  1034. 1013   3                              USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  1035. 1014   3                              //回写ISP1581中断寄存器
  1036. 1015   3                              D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  1037. 1016   3                              if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  1038. 1017   3                                      return;
  1039. 1018   3                      }
  1040. 1019   2                      USB_Int_Flag.BITS.EP0TX = 0;
  1041. 1020   2              }
  1042. 1021   1              else
  1043. 1022   1              {
  1044. 1023   2                      D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  1045. 1024   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = 1;
  1046. 1025   2                      D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = 0;
  1047. 1026   2                      if(!USB_Device.BITS.Alter_Interface)
  1048. 1027   2                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = 0;
  1049. 1028   2                      else
  1050. 1029   2                              D14_Cntrl_Reg.D14_DATA_PORT_LSB = 1;
  1051. 1030   2                      wait_ep0tx();
  1052. 1031   2                      ep0in_ack();
  1053. 1032   2              }
  1054. 1033   1      }
  1055. 1034          
  1056. 1035          
  1057. 1036          //Set_Interface 设置接口状态
  1058. 1037          void set_interface(void)
  1059. 1038          {
  1060. 1039   1              if(USB_Device_Request.wIndex == 0 && USB_Device_Request.wValue == 0x0100)
  1061. 1040   1                      USB_Device.BITS.Alter_Interface = 1;
  1062. 1041   1              
  1063. 1042   1              if(USB_Device_Request.wIndex == 0x0100 && USB_Device_Request.wValue == 0)
  1064. 1043   1                      USB_Device.BITS.Alter_Interface = 0;
  1065. 1044   1              
  1066. 1045   1              if(USB_Device_Request.wIndex == 0 && USB_Device_Request.wValue == 0)
  1067. 1046   1                      USB_Device.BITS.Alter_Interface = 0;
  1068. 1047   1      
  1069. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 18  
  1070. 1048   1              ep0in_ack();
  1071. 1049   1      }
  1072. 1050          
  1073. 1051          
  1074. 1052          
  1075. 1053          //端点0IN状态响应
  1076. 1054          void ep0in_ack(void)
  1077. 1055          {
  1078. 1056   1              D14_Cntrl_Reg.D14_ENDPT_INDEX = 1;
  1079. 1057   1              D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STATUS = 1;
  1080. 1058   1              wait_ep0tx();
  1081. 1059   1      }
  1082. 1060          
  1083. 1061          //端点0IN状态响应
  1084. 1062          void wait_ep0tx(void)
  1085. 1063          {
  1086. 1064   1              //end of status stage
  1087. 1065   1              while(!USB_Int_Flag.BITS.EP0TX)
  1088. 1066   1              {
  1089. 1067   2                      //读取ISP1581中断寄存器
  1090. 1068   2                      USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  1091. 1069   2                      //回写ISP1581中断寄存器
  1092. 1070   2                      D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  1093. 1071   2                      if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  1094. 1072   2                              return;
  1095. 1073   2              }
  1096. 1074   1              USB_Int_Flag.BITS.EP0TX = 0;
  1097. 1075   1      }
  1098. 1076          
  1099. 1077          //端点0OUT状态响应
  1100. 1078          void ep0out_ack(void)
  1101. 1079          {
  1102. 1080   1              //设置状态响应
  1103. 1081   1              //acknowledge status stage
  1104. 1082   1              D14_Cntrl_Reg.D14_ENDPT_INDEX = 0;
  1105. 1083   1              D14_Cntrl_Reg.D14_CONTROL_FUNCTION.BITS.STATUS = 1;
  1106. 1084   1      //      D14_Cntrl_Reg.D14_CONTROL_FUNCTION.VALUE = 2;
  1107. 1085   1              wait_ep0rx();
  1108. 1086   1      }
  1109. 1087          
  1110. 1088          //端点0OUT状态响应
  1111. 1089          void wait_ep0rx(void)
  1112. 1090          {
  1113. 1091   1              while(!USB_Int_Flag.BITS.EP0RX)
  1114. 1092   1              {
  1115. 1093   2                      //读取ISP1581中断寄存器
  1116. 1094   2                      USB_Int_Flag.VALUE |= D14_Cntrl_Reg.D14_INT.VALUE;
  1117. 1095   2                      //回写ISP1581中断寄存器
  1118. 1096   2                      D14_Cntrl_Reg.D14_INT.VALUE = USB_Int_Flag.VALUE;
  1119. 1097   2                      if(USB_Int_Flag.BITS.SUSP || USB_Int_Flag.BITS.RESET)
  1120. 1098   2                              return;
  1121. 1099   2              }               
  1122. 1100   1              USB_Int_Flag.BITS.EP0RX = 0;
  1123. 1101   1      }
  1124. 1102          
  1125. 1103          #pragma disable
  1126. 1104          unsigned int read_endpoint(unsigned char endp,unsigned int len,unsigned char *buf)
  1127. 1105          {
  1128. 1106   1              unsigned int datalen;
  1129. 1107   1              D14_Cntrl_Reg.D14_ENDPT_INDEX = endp;
  1130. 1108   1              len = D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB;
  1131. 1109   1              len <<= 8;
  1132. C51 COMPILER V7.06   CHAP_9                                                                07/02/2003 10:27:50 PAGE 19  
  1133. 1110   1              len += D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB;
  1134. 1111   1              datalen=len;
  1135. 1112   1              while(datalen-- != 0)
  1136. 1113   1              {
  1137. 1114   2                      *buf = D14_Cntrl_Reg.D14_DATA_PORT_LSB; //fill up FIFO
  1138. 1115   2                      buf++;
  1139. 1116   2              }
  1140. 1117   1              return len;
  1141. 1118   1      
  1142. 1119   1      }
  1143. 1120          
  1144. 1121          #pragma disable
  1145. 1122          unsigned int write_endpoint(unsigned char endp,unsigned int len,unsigned char *buf)
  1146. 1123          {
  1147. 1124   1              unsigned int datalen;
  1148. 1125   1              D14_Cntrl_Reg.D14_ENDPT_INDEX = endp;
  1149. 1126   1              D14_Cntrl_Reg.D14_BUFFER_LENGTH_LSB = (unsigned char) len;
  1150. 1127   1              D14_Cntrl_Reg.D14_BUFFER_LENGTH_MSB = (unsigned char) (len>>8);
  1151. 1128   1              datalen=len;
  1152. 1129   1              while(datalen-- != 0)
  1153. 1130   1              {
  1154. 1131   2                      D14_Cntrl_Reg.D14_DATA_PORT_LSB = *buf; //fill up FIFO
  1155. 1132   2                      buf++;
  1156. 1133   2              }
  1157. 1134   1              return len;
  1158. 1135   1      }
  1159. 1136          
  1160. 1137          
  1161. 1138          
  1162. MODULE INFORMATION:   STATIC OVERLAYABLE
  1163.    CODE SIZE        =   5047    ----
  1164.    CONSTANT SIZE    =     48    ----
  1165.    XDATA SIZE       =   ----    ----
  1166.    PDATA SIZE       =   ----    ----
  1167.    DATA SIZE        =     32      20
  1168.    IDATA SIZE       =     72    ----
  1169.    BIT SIZE         =   ----    ----
  1170. END OF MODULE INFORMATION.
  1171. C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)