void HID_Task(void) { ring_buffer * _tx_buffer = &tx_buffer_cdc; /* Device must be connected and configured for the task to run */ //if (USB_DeviceState != DEVICE_STATE_Configured) // return; Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM); /* Check to see if a packet has been sent from the host */ if (Endpoint_IsOUTReceived()) { /* Check to see if the packet contains data */ if (Endpoint_IsReadWriteAllowed()) { /* Create a temporary buffer to hold the read in report from the host */ uint8_t GenericData[GENERIC_REPORT_SIZE]; /* Read Generic Report Data */ Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData), NULL); /* Process Generic Report Data */ ProcessGenericHIDReport(GenericData); dataReceived = 1; } /* Finalize the stream transfer to send the last packet */ Endpoint_ClearOUT(); } Endpoint_SelectEndpoint(GENERIC_IN_EPNUM); /* Check to see if the host is ready to accept another packet */ //if (Endpoint_IsINReady() && dataReceived!=0) // && (_tx_buffer->head != _tx_buffer->tail) if (Endpoint_IsINReady()) { /* Create a temporary buffer to hold the report to send to the host */ uint8_t GenericData[GENERIC_REPORT_SIZE]; /* Create Generic Report Data */ CreateGenericHIDReport(GenericData); /* Write Generic Report Data */ Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData), NULL); /* Finalize the stream transfer to send the last packet */ Endpoint_ClearIN(); dataReceived = 0; } }
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to * the device from the USB host before passing along unhandled control requests to the library for processing * internally. */ void EVENT_USB_Device_ControlRequest(void) { /* Handle HID Class specific requests */ switch (USB_ControlRequest.bRequest) { case HID_REQ_GetReport: if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { uint8_t GenericData[GENERIC_REPORT_SIZE]; CreateGenericHIDReport(GenericData); Endpoint_ClearSETUP(); /* Write the report data to the control endpoint */ Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData)); Endpoint_ClearOUT(); } break; case HID_REQ_SetReport: if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { uint8_t GenericData[GENERIC_REPORT_SIZE]; Endpoint_ClearSETUP(); /* Read the report data from the control endpoint */ Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData)); Endpoint_ClearIN(); ProcessGenericHIDReport(GenericData); } break; } }
/** ISR for the general Pipe/Endpoint interrupt vector. This ISR fires when an endpoint's status changes (such as * a packet has been received) on an endpoint with its corresponding ISR enabling bits set. This is used to send * HID packets to the host each time the HID interrupt endpoints polling period elapses, as managed by the USB * controller. */ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) { /* Save previously selected endpoint before selecting a new endpoint */ uint8_t PrevSelectedEndpoint = Endpoint_GetCurrentEndpoint(); #if defined(INTERRUPT_CONTROL_ENDPOINT) /* Check if the control endpoint has received a request */ if (Endpoint_HasEndpointInterrupted(ENDPOINT_CONTROLEP)) { /* Clear the endpoint interrupt */ Endpoint_ClearEndpointInterrupt(ENDPOINT_CONTROLEP); /* Process the control request */ USB_USBTask(); /* Handshake the endpoint setup interrupt - must be after the call to USB_USBTask() */ USB_INT_Clear(ENDPOINT_INT_SETUP); } #endif #if defined(INTERRUPT_DATA_ENDPOINT) /* Check if Generic IN endpoint has interrupted */ if (Endpoint_HasEndpointInterrupted(GENERIC_IN_EPNUM)) { /* Select the Generic IN Report Endpoint */ Endpoint_SelectEndpoint(GENERIC_IN_EPNUM); /* Clear the endpoint IN interrupt flag */ USB_INT_Clear(ENDPOINT_INT_IN); /* Clear the Generic IN Report endpoint interrupt and select the endpoint */ Endpoint_ClearEndpointInterrupt(GENERIC_IN_EPNUM); /* Create a temporary buffer to hold the report to send to the host */ uint8_t GenericData[GENERIC_REPORT_SIZE]; /* Create Generic Report Data */ CreateGenericHIDReport(GenericData); /* Write Generic Report Data */ Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData)); /* Finalize the stream transfer to send the last packet */ Endpoint_ClearIN(); } /* Check if Generic OUT endpoint has interrupted */ if (Endpoint_HasEndpointInterrupted(GENERIC_OUT_EPNUM)) { /* Select the Generic OUT Report Endpoint */ Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM); /* Clear the endpoint OUT Interrupt flag */ USB_INT_Clear(ENDPOINT_INT_OUT); /* Clear the Generic OUT Report endpoint interrupt and select the endpoint */ Endpoint_ClearEndpointInterrupt(GENERIC_OUT_EPNUM); /* Create a temporary buffer to hold the read in report from the host */ uint8_t GenericData[GENERIC_REPORT_SIZE]; /* Read Generic Report Data */ Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData)); /* Process Generic Report Data */ ProcessGenericHIDReport(GenericData); /* Finalize the stream transfer to send the last packet */ Endpoint_ClearOUT(); } #endif /* Restore previously selected endpoint */ Endpoint_SelectEndpoint(PrevSelectedEndpoint); }