Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6457030
  • 博文数量: 579
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 16635
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:29
个人简介

http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/

文章分类

全部博文(579)

文章存档

2018年(18)

2015年(91)

2014年(159)

2013年(231)

2012年(80)

分类: 嵌入式

2013-10-31 20:44:19

开发环境:win7
开发板    :51单片机 + pdiusbd12 芯片


    5,对接收到的主机的数据的解析的实现

点击(此处)折叠或打开

  1. int32_t        get_device_discriptor(void)
  2. {
  3.     int32_t        ret = 0;
  4.      buffer_t    device_buf;
  5.     standard_request_t    *req;
  6.     device_buf = get_global_buf();
  7.     req    = (standard_request_t*)device_buf.buffer;

  8.     req->wValue = device_buf.buffer[2] + (((uint16_t)device_buf.buffer[3])<<8);
  9.     req->wIndex = device_buf.buffer[4] + (((uint16_t)device_buf.buffer[5])<<8);
  10.     req->wLength = device_buf.buffer[6] + (((uint16_t)device_buf.buffer[7])<<8);

  11.     // 判断传输方向,输入还是输出
  12.     if((req->bmRequestType & 0x80) == 0x80)
  13.     {// 输入方向
  14.         uint8_t        type = (req->bmRequestType>>5)&0x03;
  15.         switch(type)
  16.         {
  17.             case 0:             // 标准
  18.                 Prints("USB标准输入请求 -> ");

  19.                 switch(req->bRequest)
  20.                 {
  21.                     case GET_STATUS:
  22.                         Prints("获取状态.\r\n");
  23.                         break;

  24.                     case GET_DESCRIPTOR:
  25.                         Prints("获取描述符 -> ");
  26.                         packet_device_discriptor(req);
  27.                         break;

  28.                     case GET_CONFIGURATION:
  29.                         Prints("获取配置.\r\n");
  30.                         break;

  31.                     case GET_INTERFACE:
  32.                         Prints("获取接口.\r\n");
  33.                         break;

  34.                     case SYNCH_FRAME:
  35.                         Prints("同步帧.\r\n");
  36.                         break;

  37.                     default:
  38.                         Prints("错误:为定义的标准输入请求.\r\n");
  39.                         break;
  40.                 }

  41.                 break;

  42.             case 1:             // 类型
  43.                 Prints("USB类输入请求.\r\n");
  44.                 break;

  45.             case 2:            // 供应商
  46.                 Prints("USB用户输入请求.\r\n");
  47.                 break;

  48.             default:        // 保留
  49.                 Prints("错误:未定义的输入请求.\r\n");
  50.                 break;        
  51.         }
  52.     }
  53.     else
  54.     {// 输出方向
  55.         uint8_t        type = (req->bmRequestType>>5)&0x03;
  56.         switch(type)
  57.         {
  58.             case 0:             // 标准
  59.                 Prints("USB标准输出请求.\r\n");
  60.                 switch(req->bRequest)
  61.                 {
  62.                     case CLEAR_FEATURE:
  63.                         Prints("清除特性.\r\n");
  64.                         break;

  65.                     case SET_FEATURE:
  66.                         Prints("设置特性.\r\n");
  67.                         break;

  68.                     case SET_ADDRESS:
  69.                         Prints("设置地址.\r\n");
  70.                         break;

  71.                     case SET_DESCRIPTOR:
  72.                         Prints("设置描述符.\r\n");
  73.                         break;

  74.                     case SET_CONFIGURATION:
  75.                         Prints("设置配置.\r\n");
  76.                         break;

  77.                     case SET_INTERFACE:
  78.                         Prints("设置接口.\r\n");
  79.                         break;

  80.                     default:
  81.                         Prints("错误:为定义的标准输出请求.\r\n");
  82.                         break;
  83.                 }

  84.                 break;

  85.             case 1:             // 类型
  86.                 Prints("USB类输出请求.\r\n");
  87.                 break;

  88.             case 2:            // 供应商
  89.                 Prints("USB用户输出请求.\r\n");
  90.                 break;

  91.             default:        // 保留
  92.                 Prints("错误:为定义的输出请求.\r\n");
  93.                 break;        
  94.         }
  95.     }
  96.      


  97.     return ret;
  98. }

点击(此处)折叠或打开

  1. int32_t        packet_device_discriptor(standard_request_t* req)
  2. {
  3.     int32_t        ret = 0;
  4.     uint8_t        desc_type = (req->wValue >> 8) & 0xFF;

  5.      switch(desc_type)
  6.     {
  7.         case DESC_TYPE_DEVICE:
  8.             Prints("设备描述符.\r\n");
  9.             {
  10.                 device_descriptor_t    mouse;
  11.                 init_device_descriptor(&mouse);

  12.                 send_buffer2host(ENDPOINT_CONTROL_IN,(uint8_t*)&mouse,mouse.bLength);                
  13.             }
  14.             break;

  15.         case DESC_TYPE_CONFIGURATION:
  16.             Prints("配置描述符.\r\n");
  17.             break;

  18.         case DESC_TYPE_STRING:
  19.             Prints("字符串描述符.\r\n");
  20.             break;

  21.         case DESC_TYPE_INTERFACE:
  22.             Prints("接口描述符.\r\n");
  23.             break;

  24.         case DESC_TYPE_ENDPOINT:
  25.             Prints("端点描述符.\r\n");
  26.             break;

  27.         default:
  28.             Prints("其他描述符.\r\n");
  29.             break;
  30.     }        

  31.     return ret;
  32. }


点击(此处)折叠或打开

  1. //高低字节交换
  2. #define SWAP16(x) ((((uint16_t)(x))<<8)|(((uint16_t)(x))>>8))

  3. int32_t        init_device_descriptor(device_descriptor_t * dev)
  4. {
  5.     int32_t        ret = 0;

  6.     dev->bLength            = sizeof(device_descriptor_t);     //设备描述符的字节数大小
  7.      dev->bDescriptorType    = DESC_TYPE_DEVICE;         //设备描述符类型编号
  8.      dev->bcdUSB                = SWAP16(0x0110); //USB版本号
  9.      dev->bDeviceClass        = 0x00;     //USB分配的设备类代码
  10.      dev->bDeviceSubClass    = 0x00;     //USB分配的子类代码
  11.      dev->bDeviceProtocol    = 0x00;     //USB分配的设备协议代码
  12.      dev->bMaxPacketSize0    = PACKET_SIZE_MAX;     //端点0的最大包大小
  13.      dev->idVendor            = SWAP16(0x8888); //厂商编号
  14.      dev->idProduct            = SWAP16(0x0001);     //产品编号
  15.      dev->bcdDevice            = SWAP16(0x0100);     //设备出厂编号
  16.      dev->iManufacturer        = 0x01; //设备厂商字符串的索引
  17.      dev->iProduct            = 0x02;     //描述产品字符串的索引
  18.      dev->iSerialNumber        = 0x03; //描述设备序列号字符串的索引
  19.      dev->bNumConfigurations    = 0x01;         //可能的配置数量

  20.     return ret;    
  21. }

点击(此处)折叠或打开

  1. int32_t        send_buffer2host(uint8_t endpoint,uint8_t *buffer,int8_t len)
  2. {
  3.     int32_t        ret = 0;
  4.     uint8_t        index = 0;

  5.     static        uint8_t    *buf_ptr = NULL;
  6.     static        int8_t    sended_len = 0;
  7.     int8_t        packet_len = 0;
  8.     static int8_t        is_end = 0;

  9.      if(!is_end)
  10.     {
  11.         buf_ptr = buffer;
  12.     
  13.         if((len-sended_len) > PACKET_SIZE_MAX)
  14.         {
  15.             packet_len = PACKET_SIZE_MAX;
  16.         }
  17.         else
  18.         {
  19.             packet_len = len - sended_len;
  20.             is_end = 1;    
  21.         }
  22.         
  23.     
  24.         write_endpoint_buffer(endpoint,&buffer[sended_len],packet_len);
  25.     
  26.         sended_len += packet_len;
  27.      }
  28.     else
  29.     {
  30.         buf_ptr = NULL;
  31.         sended_len = 0;
  32.         is_end = 0;
  33.     }

  34.     return ret;    
  35. }


    6,运行结果



    7,总结
        Acknowledge Setup 命令对控制输入和控制输出都要发送。

    8,疑问
        对设置地址的实现






欲了解以下内容,请看下回分解。。。



阅读(4363) | 评论(0) | 转发(4) |
给主人留下些什么吧!~~