Minor updates to the Benito programmer - remove redundant PORT register manipulations.
[pub/USBasp.git] / Demos / Device / LowLevel / KeyboardMouse / KeyboardMouse.c
index ddfe05a..7d486e1 100644 (file)
@@ -174,7 +174,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the LED report has been sent by the host */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
 \r
                                /* Read in the LED report from the host */\r
                                uint8_t LEDStatus = Endpoint_Read_Byte();\r
@@ -195,9 +199,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Clear the endpoint data */\r
                                Endpoint_ClearOUT();\r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
@@ -212,6 +214,10 @@ void Keyboard_HID_Task(void)
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+         return;\r
+\r
        /* Check if board button is not pressed, if so mouse mode enabled */\r
        if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))\r
        {\r
@@ -228,51 +234,47 @@ void Keyboard_HID_Task(void)
                if (JoyStatus_LCL & JOY_PRESS)\r
                  KeyboardReportData.KeyCode[0] = 0x08; // E\r
        }\r
-       \r
-       /* Check if the USB system is connected to a host and report protocol mode is enabled */\r
-       if (USB_IsConnected)\r
+\r
+       /* Select the Keyboard Report Endpoint */\r
+       Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);\r
+\r
+       /* Check if Keyboard Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
        {\r
-               /* Select the Keyboard Report Endpoint */\r
-               Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);\r
-\r
-               /* Check if Keyboard Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\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_ClearIN();\r
-\r
-                       /* Clear the report data afterwards */\r
-                       memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));\r
-               }\r
-\r
-               /* Select the Keyboard LED Report Endpoint */\r
-               Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);\r
-\r
-               /* Check if Keyboard LED Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {               \r
-                       /* Read in the LED report from the host */\r
-                       uint8_t LEDStatus = Endpoint_Read_Byte();\r
-                       uint8_t LEDMask   = LEDS_LED2;\r
-                       \r
-                       if (LEDStatus & 0x01) // NUM Lock\r
-                         LEDMask |= LEDS_LED1;\r
-                       \r
-                       if (LEDStatus & 0x02) // CAPS Lock\r
-                         LEDMask |= LEDS_LED3;\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_ClearIN();\r
 \r
-                       if (LEDStatus & 0x04) // SCROLL Lock\r
-                         LEDMask |= LEDS_LED4;\r
+               /* Clear the report data afterwards */\r
+               memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));\r
+       }\r
+\r
+       /* Select the Keyboard LED Report Endpoint */\r
+       Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);\r
+\r
+       /* Check if Keyboard LED Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
+       {               \r
+               /* Read in the LED report from the host */\r
+               uint8_t LEDStatus = Endpoint_Read_Byte();\r
+               uint8_t LEDMask   = LEDS_LED2;\r
+               \r
+               if (LEDStatus & 0x01) // NUM Lock\r
+                 LEDMask |= LEDS_LED1;\r
+               \r
+               if (LEDStatus & 0x02) // CAPS Lock\r
+                 LEDMask |= LEDS_LED3;\r
 \r
-                       /* Set the status LEDs to the current Keyboard LED status */\r
-                       LEDs_SetAllLEDs(LEDMask);\r
+               if (LEDStatus & 0x04) // SCROLL Lock\r
+                 LEDMask |= LEDS_LED4;\r
 \r
-                       /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
-                       Endpoint_ClearOUT();\r
-               }\r
+               /* Set the status LEDs to the current Keyboard LED status */\r
+               LEDs_SetAllLEDs(LEDMask);\r
+\r
+               /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
+               Endpoint_ClearOUT();\r
        }\r
 }\r
 \r
@@ -283,6 +285,10 @@ void Mouse_HID_Task(void)
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+         return;\r
+\r
        /* Check if board button is pressed, if so mouse mode enabled */\r
        if (Buttons_GetStatus() & BUTTONS_BUTTON1)\r
        {\r
@@ -300,23 +306,19 @@ void Mouse_HID_Task(void)
                  MouseReportData.Button  = (1 << 0);\r
        }\r
 \r
-       /* Check if the USB system is connected to a host and report protocol mode is enabled */\r
-       if (USB_IsConnected)\r
-       {\r
-               /* Select the Mouse Report Endpoint */\r
-               Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);\r
+       /* Select the Mouse Report Endpoint */\r
+       Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);\r
 \r
-               /* Check if Mouse Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Write Mouse Report Data */\r
-                       Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
+       /* Check if Mouse Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
+       {\r
+               /* Write Mouse Report Data */\r
+               Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
 \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
 \r
-                       /* Clear the report data afterwards */\r
-                       memset(&MouseReportData, 0, sizeof(MouseReportData));\r
-               }\r
+               /* Clear the report data afterwards */\r
+               memset(&MouseReportData, 0, sizeof(MouseReportData));\r
        }\r
 }\r