void USBD_IRQHandler(void) { uint32_t u32IntSts = USBD_GET_INT_FLAG(); uint32_t u32State = USBD_GET_BUS_STATE(); //------------------------------------------------------------------ if (u32IntSts & USBD_INTSTS_FLDET) { // Floating detect USBD_CLR_INT_FLAG(USBD_INTSTS_FLDET); if (USBD_IS_ATTACHED()) { /* USB Plug In */ USBD_ENABLE_USB(); } else { /* USB Un-plug */ USBD_DISABLE_USB(); } } //------------------------------------------------------------------ if (u32IntSts & USBD_INTSTS_BUS) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_BUS); if (u32State & USBD_STATE_USBRST) { /* Bus reset */ USBD_ENABLE_USB(); USBD_SwReset(); } if (u32State & USBD_STATE_SUSPEND) { /* Enable USB but disable PHY */ USBD_DISABLE_PHY(); } if (u32State & USBD_STATE_RESUME) { /* Enable USB and enable PHY */ USBD_ENABLE_USB(); } } //------------------------------------------------------------------ if(u32IntSts & USBD_INTSTS_WAKEUP) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_WAKEUP); } if (u32IntSts & USBD_INTSTS_USB) { extern uint8_t g_usbd_SetupPacket[]; // USB event if (u32IntSts & USBD_INTSTS_SETUP) { // Setup packet /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_SETUP); /* Clear the data IN/OUT ready flag of control end-points */ USBD_STOP_TRANSACTION(EP0); USBD_STOP_TRANSACTION(EP1); USBD_ProcessSetupPacket(); } // EP events if (u32IntSts & USBD_INTSTS_EP0) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_EP0); // control IN USBD_CtrlIn(); } if (u32IntSts & USBD_INTSTS_EP1) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_EP1); // control OUT USBD_CtrlOut(); if(g_usbd_SetupPacket[1] == SET_LINE_CODE) { if(g_usbd_SetupPacket[4] == 0) /* VCOM-1 */ VCOM_LineCoding(0); /* Apply UART settings */ } } if (u32IntSts & USBD_INTSTS_EP2) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_EP2); // Bulk IN EP2_Handler(); } if (u32IntSts & USBD_INTSTS_EP3) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_EP3); // Bulk OUT EP3_Handler(); } if (u32IntSts & USBD_INTSTS_EP4) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_EP4); } if (u32IntSts & USBD_INTSTS_EP5) { /* Clear event flag */ USBD_CLR_INT_FLAG(USBD_INTSTS_EP5); // Interrupt IN EP5_Handler(); } } }
void HID_ClassRequest(void) { uint8_t buf[8]; USBD_GetSetupPacket(buf); if (buf[0] & 0x80) { /* request data transfer direction */ // Device to host switch (buf[1]) { case GET_LINE_CODE: { if (buf[4] == 0) { /* VCOM-1 */ USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0)), (uint8_t *)&gLineCoding, 7); } /* Data stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, 7); /* Status stage */ USBD_PrepareCtrlOut(0,0); break; } case GET_REPORT: case GET_IDLE: case GET_PROTOCOL: default: { /* Setup error, stall the device */ USBD_SetStall(0); break; } } } else { // Host to device switch (buf[1]) { case SET_CONTROL_LINE_STATE: { if (buf[4] == 0) { /* VCOM-1 */ gCtrlSignal = buf[3]; gCtrlSignal = (gCtrlSignal << 8) | buf[2]; //printf("RTS=%d DTR=%d\n", (gCtrlSignal0 >> 1) & 1, gCtrlSignal0 & 1); } /* Status stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, 0); break; } case SET_LINE_CODE: { //g_usbd_UsbConfig = 0100; if (buf[4] == 0) /* VCOM-1 */ USBD_PrepareCtrlOut((uint8_t *)&gLineCoding, 7); /* Status stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, 0); /* UART setting */ if (buf[4] == 0) /* VCOM-1 */ VCOM_LineCoding(0); break; } case SET_REPORT: { if(buf[3] == 2) { /* Request Type = Output */ USBD_SET_DATA1(EP1); USBD_SET_PAYLOAD_LEN(EP1, buf[6]); /* Trigger for HID Int in */ USBD_SET_PAYLOAD_LEN(EP5, 0); /* Status stage */ USBD_PrepareCtrlIn(0, 0); } break; } case SET_IDLE: { /* Status stage */ USBD_SET_DATA1(EP0); USBD_SET_PAYLOAD_LEN(EP0, 0); break; } case SET_PROTOCOL: default: { // Stall /* Setup error, stall the device */ USBD_SetStall(0); break; } } } }