Chap_9.LST
上传用户:lyfy_2008
上传日期:2014-07-13
资源大小:3016k
文件大小:13k
源码类别:

USB编程

开发平台:

Visual C++

  1. C51 COMPILER V7.02b   CHAP_9                                                               09/28/2006 22:04:32 PAGE 1   
  2. C51 COMPILER V7.02b, COMPILATION OF MODULE CHAP_9
  3. OBJECT MODULE PLACED IN Chap_9.OBJ
  4. COMPILER INVOKED BY: C:KeilC51BINC51.EXE Chap_9.c BROWSE DEBUG OBJECTEXTEND
  5. stmt level    source
  6.    1          #include <stdio.h>
  7.    2          #include <string.h>
  8.    3          
  9.    4          #include <reg51.h>                /* special function register declarations   */
  10.    5          
  11.    6          //#include "epphal.h"
  12.    7          #include "d12ci.h"
  13.    8          #include "mainloop.h"
  14.    9          #include "usb100.h"
  15.   10          #include "chap_9.h"
  16.   11          
  17.   12          #define NUM_ENDPOINTS   4
  18.   13          
  19.   14          #define CONFIG_DESCRIPTOR_LENGTH    sizeof(USB_CONFIGURATION_DESCRIPTOR) 
  20.   15                                                                                  + sizeof(USB_INTERFACE_DESCRIPTOR) 
  21.   16                                                                                  + (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR))
  22.   17          
  23.   18          extern CONTROL_XFER ControlData;
  24.   19          extern IO_REQUEST idata ioRequest;
  25.   20          extern EPPFLAGS bEPPflags;
  26.   21          
  27.   22          //设备描述符
  28.   23          code USB_DEVICE_DESCRIPTOR DeviceDescr =
  29.   24          {
  30.   25                  sizeof(USB_DEVICE_DESCRIPTOR),
  31.   26              USB_DEVICE_DESCRIPTOR_TYPE,
  32.   27              SWAP(0x0110),
  33.   28              USB_CLASS_CODE_TEST_CLASS_DEVICE,
  34.   29              0, 0,
  35.   30                  EP0_PACKET_SIZE,
  36.   31                  SWAP(0x0471),
  37.   32                  SWAP(0x0677),
  38.   33                  SWAP(0x0100),
  39.   34              0, 0, 0,
  40.   35                  1
  41.   36          };
  42.   37          
  43.   38          //配置描述符
  44.   39          code USB_CONFIGURATION_DESCRIPTOR ConfigDescr =
  45.   40          {
  46.   41              sizeof(USB_CONFIGURATION_DESCRIPTOR),
  47.   42              USB_CONFIGURATION_DESCRIPTOR_TYPE,
  48.   43              SWAP(CONFIG_DESCRIPTOR_LENGTH),
  49.   44                  1,
  50.   45                  1,
  51.   46              0,
  52.   47                  0x60,//自供电
  53.   48                  0x32
  54.   49          };
  55.   50          
  56.   51          //接口描述符
  57.   52          code USB_INTERFACE_DESCRIPTOR InterfaceDescr =
  58.   53          {
  59.   54              sizeof(USB_INTERFACE_DESCRIPTOR),
  60.   55              USB_INTERFACE_DESCRIPTOR_TYPE,
  61. C51 COMPILER V7.02b   CHAP_9                                                               09/28/2006 22:04:32 PAGE 2   
  62.   56              0,
  63.   57              0,
  64.   58                  NUM_ENDPOINTS,
  65.   59                  USB_CLASS_CODE_TEST_CLASS_DEVICE,
  66.   60                  USB_SUBCLASS_CODE_TEST_CLASS_D12,
  67.   61                  USB_PROTOCOL_CODE_TEST_CLASS_D12,
  68.   62                  0
  69.   63          };
  70.   64          
  71.   65          //端点描述符
  72.   66          code USB_ENDPOINT_DESCRIPTOR EP1_TXDescr =
  73.   67          {
  74.   68                  sizeof(USB_ENDPOINT_DESCRIPTOR),
  75.   69                  USB_ENDPOINT_DESCRIPTOR_TYPE,
  76.   70                  0x81,
  77.   71                  USB_ENDPOINT_TYPE_INTERRUPT,
  78.   72                  SWAP(EP1_PACKET_SIZE),
  79.   73                  1
  80.   74          };
  81.   75          
  82.   76          code USB_ENDPOINT_DESCRIPTOR EP1_RXDescr =
  83.   77          {
  84.   78                  sizeof(USB_ENDPOINT_DESCRIPTOR),
  85.   79                  USB_ENDPOINT_DESCRIPTOR_TYPE,
  86.   80                  0x1,
  87.   81                  USB_ENDPOINT_TYPE_INTERRUPT,
  88.   82                  SWAP(EP1_PACKET_SIZE),
  89.   83                  1
  90.   84          };
  91.   85          
  92.   86          code USB_ENDPOINT_DESCRIPTOR EP2_TXDescr =
  93.   87          {
  94.   88                  sizeof(USB_ENDPOINT_DESCRIPTOR),
  95.   89                  USB_ENDPOINT_DESCRIPTOR_TYPE,
  96.   90                  0x82,
  97.   91                  USB_ENDPOINT_TYPE_BULK,
  98.   92                  SWAP(EP2_PACKET_SIZE),
  99.   93                  10
  100.   94          };
  101.   95          
  102.   96          code USB_ENDPOINT_DESCRIPTOR EP2_RXDescr =
  103.   97          {
  104.   98                  sizeof(USB_ENDPOINT_DESCRIPTOR),
  105.   99                  USB_ENDPOINT_DESCRIPTOR_TYPE,
  106.  100                  0x2,
  107.  101                  USB_ENDPOINT_TYPE_BULK,
  108.  102                  SWAP(EP2_PACKET_SIZE),
  109.  103                  10
  110.  104          };
  111.  105          
  112.  106          /*
  113.  107             //*************************************************************************
  114.  108             // USB Protocol Layer
  115.  109             //*************************************************************************
  116.  110          */
  117.  111          
  118.  112          void reserved(void)
  119.  113          {
  120.  114   1              stall_ep0();
  121.  115   1      }
  122.  116          
  123.  117          /*
  124. C51 COMPILER V7.02b   CHAP_9                                                               09/28/2006 22:04:32 PAGE 3   
  125.  118             //*************************************************************************
  126.  119             // USB standard device requests
  127.  120             //*************************************************************************
  128.  121          */
  129.  122          
  130.  123          //获取设备状态
  131.  124          void get_status(void)
  132.  125          {
  133.  126   1              unsigned char endp, txdat[2];
  134.  127   1              unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
  135.  128   1              unsigned char c;
  136.  129   1      
  137.  130   1              if (bRecipient == USB_RECIPIENT_DEVICE) {
  138.  131   2                      if(bEPPflags.bits.remote_wakeup == 1)//获取远程唤醒状态
  139.  132   2                              txdat[0] = 3;
  140.  133   2                      else
  141.  134   2                              txdat[0] = 1;
  142.  135   2                      txdat[1]=0;
  143.  136   2                      single_transmit(txdat, 2);
  144.  137   2              } else if (bRecipient == USB_RECIPIENT_INTERFACE) {//获取接口状态
  145.  138   2                      txdat[0]=0;
  146.  139   2                      txdat[1]=0;
  147.  140   2                      single_transmit(txdat, 2);
  148.  141   2              } else if (bRecipient == USB_RECIPIENT_ENDPOINT) {//获取端点状态
  149.  142   2                      endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
  150.  143   2                      if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
  151.  144   2                              c = D12_SelectEndpoint(endp*2 + 1);     /* Control-in */
  152.  145   2                      else
  153.  146   2                              c = D12_SelectEndpoint(endp*2); /* Control-out */
  154.  147   2                      if(c & D12_STALL)
  155.  148   2                              txdat[0] = 1;
  156.  149   2                      else
  157.  150   2                              txdat[0] = 0;
  158.  151   2                      txdat[1] = 0;
  159.  152   2                      single_transmit(txdat, 2);
  160.  153   2              } else
  161.  154   1                      stall_ep0();
  162.  155   1      }
  163.  156          
  164.  157          //特性清除
  165.  158          void clear_feature(void)
  166.  159          {
  167.  160   1              unsigned char endp;
  168.  161   1              unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
  169.  162   1      
  170.  163   1              if (bRecipient == USB_RECIPIENT_DEVICE
  171.  164   1                      && ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {//清除远程唤醒功能
  172.  165   2                      DISABLE;
  173.  166   2                      bEPPflags.bits.remote_wakeup = 0;
  174.  167   2                      ENABLE;
  175.  168   2                      single_transmit(0, 0);
  176.  169   2              }
  177.  170   1              else if (bRecipient == USB_RECIPIENT_ENDPOINT
  178.  171   1                      && ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {//清除端点stall
  179.  172   2                      endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
  180.  173   2                      if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
  181.  174   2                              /* clear TX stall for IN on EPn. */
  182.  175   2                              D12_SetEndpointStatus(endp*2 + 1, 0);
  183.  176   2                      else
  184.  177   2                              /* clear RX stall for OUT on EPn. */
  185.  178   2                              D12_SetEndpointStatus(endp*2, 0);
  186.  179   2                      single_transmit(0, 0);
  187. C51 COMPILER V7.02b   CHAP_9                                                               09/28/2006 22:04:32 PAGE 4   
  188.  180   2              } else
  189.  181   1                      stall_ep0();
  190.  182   1      }
  191.  183          
  192.  184          //特性设置
  193.  185          void set_feature(void)
  194.  186          {
  195.  187   1              unsigned char endp;
  196.  188   1              unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
  197.  189   1      
  198.  190   1              if (bRecipient == USB_RECIPIENT_DEVICE
  199.  191   1                      && ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {//设置远程唤醒功能
  200.  192   2                      DISABLE;
  201.  193   2                      bEPPflags.bits.remote_wakeup = 1;
  202.  194   2                      ENABLE;
  203.  195   2                      single_transmit(0, 0);
  204.  196   2              }
  205.  197   1              else if (bRecipient == USB_RECIPIENT_ENDPOINT
  206.  198   1                      && ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {//设置端点stall
  207.  199   2                      endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
  208.  200   2                      if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
  209.  201   2                              /* clear TX stall for IN on EPn. */
  210.  202   2                              D12_SetEndpointStatus(endp*2 + 1, 1);
  211.  203   2                      else
  212.  204   2                              /* clear RX stall for OUT on EPn. */
  213.  205   2                              D12_SetEndpointStatus(endp*2, 1);
  214.  206   2                      single_transmit(0, 0);
  215.  207   2              } else
  216.  208   1                      stall_ep0();
  217.  209   1      }
  218.  210          
  219.  211          //设置地址
  220.  212          void set_address(void)
  221.  213          {
  222.  214   1              D12_SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue &
  223.  215   1                      DEVICE_ADDRESS_MASK), 1);
  224.  216   1              single_transmit(0, 0);
  225.  217   1      }
  226.  218          
  227.  219          //获取描述符
  228.  220          void get_descriptor(void)
  229.  221          {
  230.  222   1              unsigned char bDescriptor = MSB(ControlData.DeviceRequest.wValue);
  231.  223   1      
  232.  224   1              if (bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE) {//获取设备描述符
  233.  225   2                      code_transmit((unsigned char code *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
  234.  226   2              } else if (bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE) {//获取配置描述符
  235.  227   2                      code_transmit((unsigned char code *)&ConfigDescr, CONFIG_DESCRIPTOR_LENGTH);
  236.  228   2              } else
  237.  229   1                      stall_ep0();
  238.  230   1      }
  239.  231          
  240.  232          //获取配置状态
  241.  233          void get_configuration(void)
  242.  234          {
  243.  235   1              unsigned char c = bEPPflags.bits.configuration;
  244.  236   1              single_transmit(&c, 1);
  245.  237   1      }
  246.  238          
  247.  239          //设置配置状态
  248.  240          void set_configuration(void)
  249.  241          {
  250. C51 COMPILER V7.02b   CHAP_9                                                               09/28/2006 22:04:32 PAGE 5   
  251.  242   1              if (ControlData.DeviceRequest.wValue == 0) {
  252.  243   2                      //设备进入未配置状态
  253.  244   2                      single_transmit(0, 0);
  254.  245   2                      DISABLE;
  255.  246   2                      bEPPflags.bits.configuration = 0;
  256.  247   2                      ENABLE;
  257.  248   2                      init_unconfig();
  258.  249   2              } else if (ControlData.DeviceRequest.wValue == 1) {
  259.  250   2                      //配置设备
  260.  251   2                      single_transmit(0, 0);
  261.  252   2      
  262.  253   2                      init_unconfig();
  263.  254   2                      init_config();
  264.  255   2                      DISABLE;
  265.  256   2                      bEPPflags.bits.configuration = 1;
  266.  257   2                      ENABLE;
  267.  258   2              } else
  268.  259   1                      stall_ep0();
  269.  260   1      }
  270.  261          
  271.  262          //获取接口状态
  272.  263          void get_interface(void)
  273.  264          {
  274.  265   1              unsigned char txdat = 0;        /* Only/Current interface = 0 */
  275.  266   1              single_transmit(&txdat, 1);
  276.  267   1      }
  277.  268          
  278.  269          //设置接口状态
  279.  270          void set_interface(void)
  280.  271          {
  281.  272   1              if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
  282.  273   1                      single_transmit(0, 0);
  283.  274   1              else
  284.  275   1                      stall_ep0();
  285.  276   1      }
  286.  277          
  287. MODULE INFORMATION:   STATIC OVERLAYABLE
  288.    CODE SIZE        =    448    ----
  289.    CONSTANT SIZE    =     64    ----
  290.    XDATA SIZE       =   ----    ----
  291.    PDATA SIZE       =   ----    ----
  292.    DATA SIZE        =   ----      12
  293.    IDATA SIZE       =   ----    ----
  294.    BIT SIZE         =   ----    ----
  295. END OF MODULE INFORMATION.
  296. C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)