现在有个项目使用STM32F2做主机与 STM103或nxp系列的从USB设备进行自定义HID通信,从设备是根据鼠标更改的从USB设备,能和我们的上位机进行正常通信。
1.采用st官网的STM32_USB-Host-Device_Lib_V2.1.0_HID固件库,我选择的是hid 鼠标程序更改。 2.宏定义选择USE_USB_OTG_HS,USE_EMBEDDED_PHY。 3. static USBH_StatusUSBH_HID_InterfaceInit ( USB_OTG_CORE_HANDLE *pdev, void*phost) 函数更改了使用自定义的HID_USER_cb。 遇到的问题: 枚举正常,主机HOST 能发送数据到从机,从机也能够返回数据,我采用的是一问一答的通信方式,但是发现通信有10s左右,从机收不到主机发送的数据,主机就需要重新发送一次请求数据,从机还是不能正常返回,遇到这种情况我只能重连USB从机,重连需要3s时间,每次重连后都是正常通信10s左右就又不正常了。麻烦高手帮忙分析下原因。 USB HID处理函数。采用中断方式发送和接收
stastatic USBH_Status USBH_HID_Handle(USB_OTG_CORE_HANDLE *pdev , void *phost) { u16 buff_add; USBH_HOST *pphost = phost; USBH_Status status = USBH_OK;
switch (HID_Machine.state) { case HID_IDLE: HID_Machine.cb->Init(); HID_Machine.state = HID_SYNC; case HID_SYNC:
/* Sync with start of Even Frame */ if(USB_OTG_IsEvenFrame(pdev) == TRUE) { hid_error = 0; if(hostUsbSendEn == 1) HID_Machine.state = HID_SEND_DATA; else if(hostUsbSendEn == 2) { HID_Machine.timer = HCD_GetCurrentFrame(pdev); HID_Machine.state = HID_POLL; } else { HID_Machine.state = HID_IDLE; } } else { hid_error++; if(hid_error > 10) { hid_error = 0; bDeviceState = 2; } HID_Machine.state = HID_IDLE; } break; case HID_SEND_DATA: memcpy(HID_Machine.buff, &hostUsb_Transceive_buffer, 8); USBH_InterruptSendData(pdev, HID_Machine.buff, HID_Machine.length, HID_Machine.hc_num_out); start_toggle = 0; hostUsbSendEn = 2; RxTxUsbStatus = 15; hostSendNum++; memset(hostUsb_Receive_Buffer,0,192); HID_Machine.state = HID_IDLE; break; case HID_GET_DATA:
hostreciveNum++; if(start_toggle < 3) { / buff_add = start_toggle*64; USBH_InterruptReceiveData(pdev, hostUsb_Receive_Buffer+buff_add,//HID_Machine.buff HID_Machine.length,//64,// HID_Machine.hc_num_in); start_toggle++; HID_Machine.state = HID_POLL; HID_Machine.timer = HCD_GetCurrentFrame(pdev); } else { start_toggle = 0; HID_Machine.state = HID_IDLE; } break; case HID_POLL: if(( HCD_GetCurrentFrame(pdev) - HID_Machine.timer) >= HID_Machine.poll) { HID_Machine.state = HID_GET_DATA; hid_error = 0; if(start_toggle == 3) { hostUsbSendEn = 0; hid_error = 0; start_toggle = 0; HostUsbrxOk = 1; HID_Machine.state = HID_IDLE; } } else if(HCD_GetURB_State(pdev , HID_Machine.hc_num_in) == URB_DONE) { // HID_Machine.state = HID_GET_DATA; hid_error = 0; if(start_toggle == 3) { hid_error = 0; hostUsbSendEn = 0; start_toggle = 0; HostUsbrxOk = 1; HID_Machine.state = HID_IDLE; } } else if(HCD_GetURB_State(pdev, HID_Machine.hc_num_in) == URB_STALL) /* IN Endpoint Stalled */ { /* Issue Clear Feature on interrupt IN endpoint */ if( (USBH_ClrFeature(pdev, pphost, HID_Machine.ep_addr, HID_Machine.hc_num_in)) == USBH_OK) { /* Change state to issue next IN token */ HID_Machine.state = HID_IDLE; } } else { // hid_error++; // if(hid_error > 10) // { // hid_error = 0; // bDeviceState = 2; // HID_Machine.state = HID_IDLE; // } } break; default: break; } return status; }
|