X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/ece9e3d5991dd481fc36baf799a1a85d14b88251..526e398986583e2fb65c0a36a2fbf2ce153446e5:/Demos/Device/GenericHID/GenericHID.c diff --git a/Demos/Device/GenericHID/GenericHID.c b/Demos/Device/GenericHID/GenericHID.c index cacbdc55c..695342d0e 100644 --- a/Demos/Device/GenericHID/GenericHID.c +++ b/Demos/Device/GenericHID/GenericHID.c @@ -36,21 +36,15 @@ #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 }, + { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, #endif #if !defined(INTERRUPT_DATA_ENDPOINT) - { Task: USB_HID_Report , TaskStatus: TASK_STOP }, + { .Task = USB_HID_Report , .TaskStatus = TASK_STOP }, #endif }; @@ -103,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); } @@ -112,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); } @@ -152,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())); - - 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; @@ -266,26 +274,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,7 +309,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(); } } } @@ -349,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 */ @@ -374,7 +388,7 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK) ProcessGenericHIDReport(GenericData); /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearCurrentBank(); + Endpoint_ClearOUT(); } #endif