X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/ece9e3d5991dd481fc36baf799a1a85d14b88251..4a8ac5e474741f14492597880e751b3f806cb965:/Demos/Device/GenericHID/GenericHID.c diff --git a/Demos/Device/GenericHID/GenericHID.c b/Demos/Device/GenericHID/GenericHID.c index cacbdc55c..f43499a0f 100644 --- a/Demos/Device/GenericHID/GenericHID.c +++ b/Demos/Device/GenericHID/GenericHID.c @@ -36,22 +36,11 @@ #include "GenericHID.h" -/* Project Tags, for reading out using the ButtLoad project */ -BUTTLOADTAG(ProjName, "LUFA GenHID App"); -BUTTLOADTAG(BuildTime, __TIME__); -BUTTLOADTAG(BuildDate, __DATE__); -BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING); - /* Scheduler Task List */ TASK_LIST { - #if !defined(INTERRUPT_CONTROL_ENDPOINT) - { Task: USB_USBTask , TaskStatus: TASK_STOP }, - #endif - - #if !defined(INTERRUPT_DATA_ENDPOINT) - { Task: USB_HID_Report , TaskStatus: TASK_STOP }, - #endif + { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, + { .Task = USB_HID_Report , .TaskStatus = TASK_STOP }, }; /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */ @@ -83,26 +72,14 @@ int main(void) Scheduler_Start(); } -/** Event handler for the USB_Reset event. This fires when the USB interface is reset by the USB host, before the - * enumeration process begins, and enables the control endpoint interrupt so that control requests can be handled - * asynchronously when they arrive rather than when the control endpoint is polled manually. - */ -EVENT_HANDLER(USB_Reset) -{ - #if defined(INTERRUPT_CONTROL_ENDPOINT) - /* Select the control endpoint */ - Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP); - - /* Enable the endpoint SETUP interrupt ISR for the control endpoint */ - USB_INT_Enable(ENDPOINT_INT_SETUP); - #endif -} - /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and * starts the library USB task to begin the enumeration and USB management process. */ -EVENT_HANDLER(USB_Connect) +void EVENT_USB_Connect(void) { + /* Start USB management task */ + Scheduler_SetTaskMode(USB_USBTask, TASK_RUN); + /* Indicate USB enumerating */ UpdateStatus(Status_USBEnumerating); } @@ -110,8 +87,12 @@ EVENT_HANDLER(USB_Connect) /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via * the status LEDs and stops the USB management task. */ -EVENT_HANDLER(USB_Disconnect) +void EVENT_USB_Disconnect(void) { + /* Stop running HID reporting and USB management tasks */ + Scheduler_SetTaskMode(USB_HID_Report, TASK_STOP); + Scheduler_SetTaskMode(USB_USBTask, TASK_STOP); + /* Indicate USB not ready */ UpdateStatus(Status_USBNotReady); } @@ -119,28 +100,18 @@ EVENT_HANDLER(USB_Disconnect) /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration * of the USB device after enumeration, and configures the generic HID device endpoints. */ -EVENT_HANDLER(USB_ConfigurationChanged) +void EVENT_USB_ConfigurationChanged(void) { /* Setup Generic IN Report Endpoint */ Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE); - #if defined(INTERRUPT_DATA_ENDPOINT) - /* Enable the endpoint IN interrupt ISR for the report endpoint */ - USB_INT_Enable(ENDPOINT_INT_IN); - #endif - /* Setup Generic OUT Report Endpoint */ Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE); - #if defined(INTERRUPT_DATA_ENDPOINT) - /* Enable the endpoint OUT interrupt ISR for the report endpoint */ - USB_INT_Enable(ENDPOINT_INT_OUT); - #endif - /* Indicate USB connected and ready */ UpdateStatus(Status_USBReady); } @@ -149,50 +120,50 @@ EVENT_HANDLER(USB_ConfigurationChanged) * control requests that are not handled internally by the USB library (including the HID commands, which are * all issued via the control endpoint), so that they can be handled appropriately for the application. */ -EVENT_HANDLER(USB_UnhandledControlPacket) +void EVENT_USB_UnhandledControlPacket(void) { /* Handle HID Class specific requests */ - switch (bRequest) + switch (USB_ControlRequest.bRequest) { case REQ_GetReport: - if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { - Endpoint_ClearSetupReceived(); - uint8_t GenericData[GENERIC_REPORT_SIZE]; - + + Endpoint_ClearSETUP(); + CreateGenericHIDReport(GenericData); /* Write the report data to the control endpoint */ Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData)); /* Finalize the stream transfer to send the last packet or clear the host abort */ - Endpoint_ClearSetupOUT(); + Endpoint_ClearOUT(); } break; case REQ_SetReport: - if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) + if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { - Endpoint_ClearSetupReceived(); + uint8_t GenericData[GENERIC_REPORT_SIZE]; + + Endpoint_ClearSETUP(); /* Wait until the generic report has been sent by the host */ - while (!(Endpoint_IsSetupOUTReceived())); - - uint8_t GenericData[GENERIC_REPORT_SIZE]; + while (!(Endpoint_IsOUTReceived())); - Endpoint_Read_Control_Stream(&GenericData, sizeof(GenericData)); + Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData)); ProcessGenericHIDReport(GenericData); /* Clear the endpoint data */ - Endpoint_ClearSetupOUT(); + Endpoint_ClearOUT(); /* Wait until the host is ready to receive the request confirmation */ - while (!(Endpoint_IsSetupINReady())); + while (!(Endpoint_IsINReady())); /* Handshake the request by sending an empty IN packet */ - Endpoint_ClearSetupIN(); + Endpoint_ClearIN(); } break; @@ -258,7 +229,6 @@ void CreateGenericHIDReport(uint8_t* DataArray) DataArray[i] = LastReceived[i]; } -#if !defined(INTERRUPT_DATA_ENDPOINT) TASK(USB_HID_Report) { /* Check if the USB system is connected to a host */ @@ -266,26 +236,32 @@ TASK(USB_HID_Report) { Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM); - if (Endpoint_ReadWriteAllowed()) + /* Check to see if a packet has been sent from the host */ + if (Endpoint_IsOUTReceived()) { - /* Create a tempoary 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); + /* 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)); + + /* Process Generic Report Data */ + ProcessGenericHIDReport(GenericData); + } /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearCurrentBank(); + Endpoint_ClearOUT(); } Endpoint_SelectEndpoint(GENERIC_IN_EPNUM); - if (Endpoint_ReadWriteAllowed()) + /* Check to see if the host is ready to accept another packet */ + if (Endpoint_IsINReady()) { - /* Create a tempoary buffer to hold the report to send to the host */ + /* Create a temporary buffer to hold the report to send to the host */ uint8_t GenericData[GENERIC_REPORT_SIZE]; /* Create Generic Report Data */ @@ -295,89 +271,7 @@ TASK(USB_HID_Report) Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData)); /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearCurrentBank(); + Endpoint_ClearIN(); } } } -#endif - -/** 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_ClearCurrentBank(); - } - - /* 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_ClearCurrentBank(); - } - #endif - - /* Restore previously selected endpoint */ - Endpoint_SelectEndpoint(PrevSelectedEndpoint); -}