Fixed inability to enumerate any devices while in host mode (broken in a previous...
[pub/USBasp.git] / Demos / Device / Keyboard / Keyboard.c
index d118ede..33545bf 100644 (file)
  \r
 #include "Keyboard.h"\r
 \r
-/* Project Tags, for reading out using the ButtLoad project */\r
-BUTTLOADTAG(ProjName,    "LUFA Keyboard App");\r
-BUTTLOADTAG(BuildTime,   __TIME__);\r
-BUTTLOADTAG(BuildDate,   __DATE__);\r
-BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);\r
-\r
 /* Scheduler Task List */\r
 TASK_LIST\r
 {\r
        #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
-       { Task: USB_USBTask          , TaskStatus: TASK_STOP },\r
+       { .Task = USB_USBTask          , .TaskStatus = TASK_STOP },\r
        #endif\r
        \r
        #if !defined(INTERRUPT_DATA_ENDPOINT)\r
-       { Task: USB_Keyboard_Report  , TaskStatus: TASK_STOP },\r
+       { .Task = USB_Keyboard_Report  , .TaskStatus = TASK_STOP },\r
        #endif\r
 };\r
 \r
@@ -199,46 +193,33 @@ EVENT_HANDLER(USB_ConfigurationChanged)
 EVENT_HANDLER(USB_UnhandledControlPacket)\r
 {\r
        /* Handle HID Class specific requests */\r
-       switch (bRequest)\r
+       switch (USB_ControlRequest.bRequest)\r
        {\r
                case REQ_GetReport:\r
-                       if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
                                USB_KeyboardReport_Data_t KeyboardReportData;\r
 \r
+                               Endpoint_ClearSETUP();\r
+       \r
                                /* Create the next keyboard report for transmission to the host */\r
                                CreateKeyboardReport(&KeyboardReportData);\r
 \r
-                               /* Ignore report type and ID number value */\r
-                               Endpoint_Discard_Word();\r
-                               \r
-                               /* Ignore unused Interface number value */\r
-                               Endpoint_Discard_Word();\r
-\r
-                               /* Read in the number of bytes in the report to send to the host */\r
-                               uint16_t wLength = Endpoint_Read_Word_LE();\r
-                               \r
-                               /* If trying to send more bytes than exist to the host, clamp the value at the report size */\r
-                               if (wLength > sizeof(KeyboardReportData))\r
-                                 wLength = sizeof(KeyboardReportData);\r
-\r
-                               Endpoint_ClearSetupReceived();\r
-       \r
                                /* Write the report data to the control endpoint */\r
-                               Endpoint_Write_Control_Stream_LE(&KeyboardReportData, wLength);\r
+                               Endpoint_Write_Control_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
                                \r
                                /* Finalize the stream transfer to send the last packet or clear the host abort */\r
-                               Endpoint_ClearSetupOUT();\r
+                               Endpoint_ClearOUT();\r
                        }\r
                \r
                        break;\r
                case REQ_SetReport:\r
-                       if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the LED report has been sent by the host */\r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()));\r
 \r
                                /* Read in the LED report from the host */\r
                                uint8_t LEDStatus = Endpoint_Read_Byte();\r
@@ -247,79 +228,73 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                ProcessLEDReport(LEDStatus);\r
                        \r
                                /* Clear the endpoint data */\r
-                               Endpoint_ClearSetupOUT();\r
+                               Endpoint_ClearOUT();\r
 \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupINReady()));\r
-                               Endpoint_ClearSetupIN();\r
+                               while (!(Endpoint_IsINReady()));\r
+                               Endpoint_ClearIN();\r
                        }\r
                        \r
                        break;\r
                case REQ_GetProtocol:\r
-                       if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                /* Write the current protocol flag to the host */\r
                                Endpoint_Write_Byte(UsingReportProtocol);\r
                                \r
                                /* Send the flag to the host */\r
-                               Endpoint_ClearSetupIN();\r
+                               Endpoint_ClearIN();\r
 \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
-                               Endpoint_ClearSetupOUT();\r
+                               while (!(Endpoint_IsOUTReceived()));\r
+                               Endpoint_ClearOUT();\r
                        }\r
                        \r
                        break;\r
                case REQ_SetProtocol:\r
-                       if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               /* Read in the wValue parameter containing the new protocol mode */\r
-                               uint16_t wValue = Endpoint_Read_Word_LE();\r
-                                                               \r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
 \r
                                /* Set or clear the flag depending on what the host indicates that the current Protocol should be */\r
-                               UsingReportProtocol = (wValue != 0x0000);\r
+                               UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);\r
 \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupINReady()));\r
-                               Endpoint_ClearSetupIN();\r
+                               while (!(Endpoint_IsINReady()));\r
+                               Endpoint_ClearIN();\r
                        }\r
                        \r
                        break;\r
                case REQ_SetIdle:\r
-                       if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               /* Read in the wValue parameter containing the idle period */\r
-                               uint16_t wValue = Endpoint_Read_Word_LE();\r
-                               \r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                /* Get idle period in MSB */\r
-                               IdleCount = (wValue >> 8);\r
+                               IdleCount = (USB_ControlRequest.wValue >> 8);\r
                                \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupINReady()));\r
-                               Endpoint_ClearSetupIN();\r
+                               while (!(Endpoint_IsINReady()));\r
+                               Endpoint_ClearIN();\r
                        }\r
                        \r
                        break;\r
                case REQ_GetIdle:\r
-                       if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {               \r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                /* Write the current idle duration to the host */\r
                                Endpoint_Write_Byte(IdleCount);\r
                                \r
                                /* Send the flag to the host */\r
-                               Endpoint_ClearSetupIN();\r
+                               Endpoint_ClearIN();\r
 \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
-                               Endpoint_ClearSetupOUT();\r
+                               while (!(Endpoint_IsOUTReceived()));\r
+                               Endpoint_ClearOUT();\r
                        }\r
 \r
                        break;\r
@@ -415,13 +390,13 @@ static inline void SendNextReport(void)
        Endpoint_SelectEndpoint(KEYBOARD_EPNUM);\r
 \r
        /* Check if Keyboard Endpoint Ready for Read/Write, and if we should send a report */\r
-       if (Endpoint_ReadWriteAllowed() && SendReport)\r
+       if (Endpoint_IsReadWriteAllowed() && SendReport)\r
        {\r
                /* Write Keyboard Report Data */\r
                Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
 \r
                /* Finalize the stream transfer to send the last packet */\r
-               Endpoint_ClearCurrentBank();\r
+               Endpoint_ClearIN();\r
        }\r
 }\r
 \r
@@ -431,18 +406,22 @@ static inline void ReceiveNextReport(void)
        /* Select the Keyboard LED Report Endpoint */\r
        Endpoint_SelectEndpoint(KEYBOARD_LEDS_EPNUM);\r
 \r
-       /* Check if Keyboard LED Endpoint Ready for Read/Write */\r
-       if (!(Endpoint_ReadWriteAllowed()))\r
-         return;\r
-\r
-       /* Read in the LED report from the host */\r
-       uint8_t LEDReport = Endpoint_Read_Byte();\r
+       /* Check if Keyboard LED Endpoint contains a packet */\r
+       if (Endpoint_IsOUTReceived())\r
+       {\r
+               /* Check to see if the packet contains data */\r
+               if (Endpoint_IsReadWriteAllowed())\r
+               {\r
+                       /* Read in the LED report from the host */\r
+                       uint8_t LEDReport = Endpoint_Read_Byte();\r
 \r
-       /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
-       Endpoint_ClearCurrentBank();\r
+                       /* Process the read LED report from the host */\r
+                       ProcessLEDReport(LEDReport);\r
+               }\r
 \r
-       /* Process the read LED report from the host */\r
-       ProcessLEDReport(LEDReport);\r
+               /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
+               Endpoint_ClearOUT();\r
+       }\r
 }\r
 \r
 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to\r