完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好, 最近在使用rtthread USB HOST , 要做一个可识别U盘和HID设备, 使用U盘没什么问题, 而插入鼠标时,在取全部设备描述符时失败, 调试打印显示:get full device descriptor failed, 如图,获取全部描述符应该是一个标准的发送接收, U盘可以, HID为什么不行呢, 是什么问题导致,百思不得解, 是什么原因麻烦知道的人帮助解答一下,感谢:handshake. |
|
相关推荐
2个回答
|
|
大家好,通过调试打印输出已经找到问题的所在了,主要是在第一次读取端点0数据返回的wMaxPacketSize = 8, rtthread这个表达式,
send_size = (remain_size > pipe->ep.wMaxPacketSize) ? pipe->ep.wMaxPacketSize : remain_size; 实际赋值 8 = (18 > 8)? 8:18 ,因此发送的只是请求返回前8个字节的设备描述符,使得出现返回的只有8个字节, 和读取18个字节 的设备描述符对不上,打印出get full device descriptor failed, 而U盘之所以能正常是因为第一次读取端点0数据返回wMaxPacketSize = 64, 实际赋值 18 = (18> 64)? 64:18,即send_size = 18, 因此能正常读回完整的设备描述符. 通过读读取全部设备描述符时增加测试debug_dat代码人为更改send_size = 18, 可以读取 全部设备描述符了., 官方的USB HOST U盘程序 只能枚举wMaxPacketSize > 18字节的,小于的话,像鼠标类8字节会枚举失败。:) if(debug_dat) { send_size = 18; } int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout) { rt_size_t remain_size; rt_size_t send_size; remain_size = nbytes; rt_uint8_t * pbuffer = (rt_uint8_t *)buffer; do { RT_DEBUG_LOG(RT_DEBUG_USB,("pipe transform remain size,: %dn", remain_size)); send_size = (remain_size > pipe->ep.wMaxPacketSize) ? pipe->ep.wMaxPacketSize : remain_size; RT_DEBUG_LOG(RT_DEBUG_USB,("pipe->ep.wMaxPacketSize = %drn", pipe->ep.wMaxPacketSize)); RT_DEBUG_LOG(RT_DEBUG_USB,("send_size = %drn", send_size)); if(debug_dat) { send_size = 18; } if(hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, pbuffer, send_size, timeout) == send_size) { remain_size -= send_size; pbuffer += send_size; } else { return 0; } }while(remain_size > 0); return nbytes; } |
|
|
|
rt_err_t rt_usbh_attatch_instance(uinst_t device)
{ int i = 0; rt_err_t ret = RT_EOK; struct uconfig_descriptor cfg_desc; udev_desc_t dev_desc; uintf_desc_t intf_desc; uep_desc_t ep_desc; rt_uint8_t ep_index; upipe_t pipe; ucd_t drv; RT_ASSERT(device != RT_NULL); rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor)); dev_desc = &device->dev_desc; /* alloc address 0 ep0 pipe*/ ep0_out_desc.wMaxPacketSize = 8; ep0_in_desc.wMaxPacketSize = 8; rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc); rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc); RT_DEBUG_LOG(RT_DEBUG_USB, ("start enumnationn")); /* get device descriptor head */ ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8); if(ret != RT_EOK) { rt_kprintf("get device descriptor head failedn"); return ret; } /* reset bus */ rt_usbh_hub_reset_port(device->parent_hub, device->port); rt_thread_delay(2); rt_usbh_hub_clear_port_feature(device->parent_hub, i + 1, PORT_FEAT_C_CONNECTION); /* set device address */ ret = rt_usbh_set_address(device); if(ret != RT_EOK) { rt_kprintf("set device address failedn"); return ret; } /* free address 0 ep0 pipe*/ rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out); rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in); /* set device max packet size */ ep0_out_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0; ep0_in_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0; /* alloc true address ep0 pipe*/ rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc); rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc); RT_DEBUG_LOG(RT_DEBUG_USB, ("get device descriptor length %dn", dev_desc->bLength)); /* get full device descriptor again */ ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength); if(ret != RT_EOK) { rt_kprintf("get full device descriptor failedn"); return ret; } RT_DEBUG_LOG(RT_DEBUG_USB, ("Vendor ID 0x%xn", dev_desc->idVendor)); RT_DEBUG_LOG(RT_DEBUG_USB, ("Product ID 0x%xn", dev_desc->idProduct)); /* get configuration descriptor head */ ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 18); if(ret != RT_EOK) { rt_kprintf("get configuration descriptor head failedn"); return ret; } /* alloc memory for configuration descriptor */ device->cfg_desc = (ucfg_desc_t)rt_malloc(cfg_desc.wTotalLength); rt_memset(device->cfg_desc, 0, cfg_desc.wTotalLength); /* get full configuration descriptor */ ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, device->cfg_desc, cfg_desc.wTotalLength); if(ret != RT_EOK) { rt_kprintf("get full configuration descriptor failedn"); return ret; } /* set configuration */ ret = rt_usbh_set_configure(device, 1); if(ret != RT_EOK) { return ret; } for(i=0; i { /* get interface descriptor through configuration descriptor */ ret = rt_usbh_get_interface_descriptor(device->cfg_desc, i, &intf_desc); if(ret != RT_EOK) { rt_kprintf("rt_usb_get_interface_descriptor errorn"); return -RT_ERROR; } RT_DEBUG_LOG(RT_DEBUG_USB, ("interface class 0x%x, subclass 0x%xn", intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass)); /* alloc pipe*/ for(ep_index = 0; ep_index < intf_desc->bNumEndpoints; ep_index++) { rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc); if(ep_desc != RT_NULL) { if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK) { rt_kprintf("alloc pipe failedn"); return RT_ERROR; } rt_usb_instance_add_pipe(device,pipe); } else { rt_kprintf("get endpoint desc failedn"); return RT_ERROR; } } /* find driver by class code found in interface descriptor */ drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass); if(drv != RT_NULL) { /* allocate memory for interface device */ device->intf[i] = (struct uhintf*)rt_malloc(sizeof(struct uhintf)); device->intf[i]->drv = drv; device->intf[i]->device = device; device->intf[i]->intf_desc = intf_desc; device->intf[i]->user_data = RT_NULL; /* open usb class driver */ ret = rt_usbh_class_driver_enable(drv, (void*)device->intf[i]); if(ret != RT_EOK) { rt_kprintf("interface %d run class driver errorn", i); } } else { rt_kprintf("find usb device driver failedn"); continue; } } return RT_EOK; } |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2024 RT-Thread全球巡回线下培训火热来袭!报名提问有奖!
463 浏览 0 评论
1184 浏览 0 评论
IaaS+on+DPU(IoD)+下一代高性能算力底座技术白皮书
2296 浏览 0 评论
飞凌嵌入式-ELFBOARD 常用的USB接口及其不同版本介绍第1期
1095 浏览 0 评论
【Vision Board创客营连载体验】RA8D1-Vision Board上OSPI-Flash实践
1564 浏览 0 评论
65682 浏览 21 评论
嵌入式热门DIY项目:智能机器人开源资料合集(原理图、代码、论文)
67931 浏览 22 评论
58055 浏览 32 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-22 06:37 , Processed in 0.547892 second(s), Total 40, Slave 34 queries .
Powered by 电子发烧友网
© 2015 www.ws-dc.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号