void MainTask(void) { USBH_Init(); OS_SetPriority(OS_GetTaskID(), TASK_PRIO_APP); // This task has highest prio for real-time application OS_CREATETASK(&_TCBMain, "USBH_Task", USBH_Task, TASK_PRIO_USBH_MAIN, _StackMain); // Start USBH main task OS_CREATETASK(&_TCBIsr, "USBH_isr", USBH_ISRTask, TASK_PRIO_USBH_ISR, _StackIsr); // Start USBH main task USBH_HID_Init(); while (1) { BSP_ToggleLED(1); if (USBH_HID_GetNumDevices(NULL, 0)) { USBH_HID_HANDLE hDevice; USBH_HID_DEVICE_INFO aDevInfo[20]; unsigned NumItems = COUNTOF(_aReportInfo); USBH_HID_GetNumDevices(aDevInfo, COUNTOF(aDevInfo)); hDevice = USBH_HID_Open("hid000"); if (hDevice) { USBH_HID_GetReportDescriptorParsed(hDevice, &_aReportInfo[0], &NumItems); USBH_HID_GetReport(hDevice, _aReport, sizeof(_aReport), NULL, NULL); USBH_HID_Close(hDevice); } } OS_Delay(100); } }
/** * @brief USBH_HID_Process * The function is for managing state machine for HID data transfers * @param phost: Host handle * @retval USBH Status */ static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost) { USBH_StatusTypeDef status = USBH_OK; HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; switch (HID_Handle->state) { case HID_INIT: HID_Handle->Init(phost); case HID_IDLE: if(USBH_HID_GetReport (phost, 0x01, 0, HID_Handle->pData, HID_Handle->length) == USBH_OK) { fifo_write(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length); HID_Handle->state = HID_SYNC; } break; case HID_SYNC: /* Sync with start of Even Frame */ if(phost->Timer & 1) { HID_Handle->state = HID_GET_DATA; } #if (USBH_USE_OS == 1) osMessagePut ( phost->os_event, USBH_URB_EVENT, 0); #endif break; case HID_GET_DATA: USBH_InterruptReceiveData(phost, HID_Handle->pData, HID_Handle->length, HID_Handle->InPipe); HID_Handle->state = HID_POLL; HID_Handle->timer = phost->Timer; HID_Handle->DataReady = 0; break; case HID_POLL: if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_DONE) { if(HID_Handle->DataReady == 0) { fifo_write(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length); HID_Handle->DataReady = 1; USBH_HID_EventCallback(phost); #if (USBH_USE_OS == 1) osMessagePut ( phost->os_event, USBH_URB_EVENT, 0); #endif } } else if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_STALL) /* IN Endpoint Stalled */ { /* Issue Clear Feature on interrupt IN endpoint */ if(USBH_ClrFeature(phost, HID_Handle->ep_addr) == USBH_OK) { /* Change state to issue next IN token */ HID_Handle->state = HID_GET_DATA; } } break; default: break; } return status; }