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

USB编程

开发平台:

C/C++

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