X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/dc83f73ca34610aaf48962abeb06fb21d9bde23b..526e398986583e2fb65c0a36a2fbf2ce153446e5:/Demos/Device/GenericHID/GenericHID.c?ds=sidebyside diff --git a/Demos/Device/GenericHID/GenericHID.c b/Demos/Device/GenericHID/GenericHID.c index fcd0e4d87..695342d0e 100644 --- a/Demos/Device/GenericHID/GenericHID.c +++ b/Demos/Device/GenericHID/GenericHID.c @@ -36,11 +36,17 @@ #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 +}; /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */ static uint8_t LastReceived[GENERIC_REPORT_SIZE]; @@ -61,11 +67,14 @@ int main(void) /* Indicate USB not ready */ UpdateStatus(Status_USBNotReady); + /* Initialize Scheduler so that it can be used */ + Scheduler_Init(); + /* Initialize USB Subsystem */ USB_Init(); - - /* >> APPLICATION INIT CODE HERE << */ - for (;;); + + /* Scheduling - routine never returns, so put this last in the main function */ + Scheduler_Start(); } /** Event handler for the USB_Reset event. This fires when the USB interface is reset by the USB host, before the @@ -74,11 +83,13 @@ int main(void) */ 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 @@ -86,6 +97,11 @@ EVENT_HANDLER(USB_Reset) */ EVENT_HANDLER(USB_Connect) { + #if !defined(INTERRUPT_CONTROL_ENDPOINT) + /* Start USB management task */ + Scheduler_SetTaskMode(USB_USBTask, TASK_RUN); + #endif + /* Indicate USB enumerating */ UpdateStatus(Status_USBEnumerating); } @@ -95,6 +111,15 @@ EVENT_HANDLER(USB_Connect) */ EVENT_HANDLER(USB_Disconnect) { + /* Stop running HID reporting and USB management tasks */ + #if !defined(INTERRUPT_DATA_ENDPOINT) + Scheduler_SetTaskMode(USB_HID_Report, TASK_STOP); + #endif + + #if !defined(INTERRUPT_CONTROL_ENDPOINT) + Scheduler_SetTaskMode(USB_USBTask, TASK_STOP); + #endif + /* Indicate USB not ready */ UpdateStatus(Status_USBNotReady); } @@ -109,16 +134,20 @@ EVENT_HANDLER(USB_ConfigurationChanged) 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); @@ -131,47 +160,47 @@ EVENT_HANDLER(USB_ConfigurationChanged) EVENT_HANDLER(USB_UnhandledControlPacket) { /* 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())); + while (!(Endpoint_IsOUTReceived())); - uint8_t GenericData[GENERIC_REPORT_SIZE]; - - 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; @@ -237,6 +266,55 @@ 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 */ + if (USB_IsConnected) + { + 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)); + + /* Process Generic Report Data */ + ProcessGenericHIDReport(GenericData); + } + + /* 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()) + { + /* 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(); + } + } +} +#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 @@ -247,6 +325,7 @@ 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)) { @@ -259,7 +338,9 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) /* 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)) { @@ -272,7 +353,7 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) /* Clear the Generic IN Report endpoint interrupt and select the endpoint */ Endpoint_ClearEndpointInterrupt(GENERIC_IN_EPNUM); - /* 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 */ @@ -282,7 +363,7 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData)); /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearCurrentBank(); + Endpoint_ClearIN(); } /* Check if Generic OUT endpoint has interrupted */ @@ -297,7 +378,7 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) /* Clear the Generic OUT Report endpoint interrupt and select the endpoint */ Endpoint_ClearEndpointInterrupt(GENERIC_OUT_EPNUM); - /* Create a tempoary buffer to hold the read in report from the host */ + /* Create a temporary buffer to hold the read in report from the host */ uint8_t GenericData[GENERIC_REPORT_SIZE]; /* Read Generic Report Data */ @@ -307,8 +388,9 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) ProcessGenericHIDReport(GenericData); /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearCurrentBank(); + Endpoint_ClearOUT(); } + #endif /* Restore previously selected endpoint */ Endpoint_SelectEndpoint(PrevSelectedEndpoint);