Minor updates to the Benito programmer - remove redundant PORT register manipulations.
[pub/USBasp.git] / Demos / Device / LowLevel / GenericHID / GenericHID.c
index 40de8b9..34c991f 100644 (file)
@@ -148,7 +148,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the generic report has been sent by the host */\r
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the generic 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
                                Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));\r
 \r
 \r
                                Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));\r
 \r
@@ -158,7 +162,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearOUT();\r
 \r
                                /* Wait until the host is ready to receive the request confirmation */\r
                                Endpoint_ClearOUT();\r
 \r
                                /* Wait until the host is ready to receive the request confirmation */\r
-                               while (!(Endpoint_IsINReady()));\r
+                               while (!(Endpoint_IsINReady()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                                \r
                                /* Handshake the request by sending an empty IN packet */\r
                                Endpoint_ClearIN();\r
                                \r
                                /* Handshake the request by sending an empty IN packet */\r
                                Endpoint_ClearIN();\r
@@ -170,7 +178,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 \r
 /** Function to process the lest received report from the host.\r
  *\r
 \r
 /** Function to process the lest received report from the host.\r
  *\r
- *  \param DataArray  Pointer to a buffer where the last report data is stored\r
+ *  \param[in] DataArray  Pointer to a buffer where the last report data is stored\r
  */\r
 void ProcessGenericHIDReport(uint8_t* DataArray)\r
 {\r
  */\r
 void ProcessGenericHIDReport(uint8_t* DataArray)\r
 {\r
@@ -186,7 +194,7 @@ void ProcessGenericHIDReport(uint8_t* DataArray)
 \r
 /** Function to create the next report to send back to the host at the next reporting interval.\r
  *\r
 \r
 /** Function to create the next report to send back to the host at the next reporting interval.\r
  *\r
- *  \param DataArray  Pointer to a buffer where the next report data should be stored\r
+ *  \param[out] DataArray  Pointer to a buffer where the next report data should be stored\r
  */\r
 void CreateGenericHIDReport(uint8_t* DataArray)\r
 {\r
  */\r
 void CreateGenericHIDReport(uint8_t* DataArray)\r
 {\r
@@ -202,47 +210,47 @@ void CreateGenericHIDReport(uint8_t* DataArray)
 \r
 void HID_Task(void)\r
 {\r
 \r
 void HID_Task(void)\r
 {\r
-       /* Check if the USB system is connected to a host */\r
-       if (USB_IsConnected)\r
+       /* Device must be connected and configured for the task to run */\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+         return;\r
+\r
+       Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);\r
+       \r
+       /* Check to see if a packet has been sent from the host */\r
+       if (Endpoint_IsOUTReceived())\r
        {\r
        {\r
-               Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);\r
-               \r
-               /* Check to see if a packet has been sent from the host */\r
-               if (Endpoint_IsOUTReceived())\r
+               /* Check to see if the packet contains data */\r
+               if (Endpoint_IsReadWriteAllowed())\r
                {\r
                {\r
-                       /* Check to see if the packet contains data */\r
-                       if (Endpoint_IsReadWriteAllowed())\r
-                       {\r
-                               /* Create a temporary buffer to hold the read in report from the host */\r
-                               uint8_t GenericData[GENERIC_REPORT_SIZE];\r
-                               \r
-                               /* Read Generic Report Data */\r
-                               Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));\r
-                               \r
-                               /* Process Generic Report Data */\r
-                               ProcessGenericHIDReport(GenericData);\r
-                       }\r
+                       /* Create a temporary buffer to hold the read in report from the host */\r
+                       uint8_t GenericData[GENERIC_REPORT_SIZE];\r
+                       \r
+                       /* Read Generic Report Data */\r
+                       Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));\r
+                       \r
+                       /* Process Generic Report Data */\r
+                       ProcessGenericHIDReport(GenericData);\r
+               }\r
 \r
 \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearOUT();\r
-               }       \r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearOUT();\r
+       }       \r
 \r
 \r
-               Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);\r
+       Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);\r
+       \r
+       /* Check to see if the host is ready to accept another packet */\r
+       if (Endpoint_IsINReady())\r
+       {\r
+               /* Create a temporary buffer to hold the report to send to the host */\r
+               uint8_t GenericData[GENERIC_REPORT_SIZE];\r
                \r
                \r
-               /* Check to see if the host is ready to accept another packet */\r
-               if (Endpoint_IsINReady())\r
-               {\r
-                       /* Create a temporary buffer to hold the report to send to the host */\r
-                       uint8_t GenericData[GENERIC_REPORT_SIZE];\r
-                       \r
-                       /* Create Generic Report Data */\r
-                       CreateGenericHIDReport(GenericData);\r
+               /* Create Generic Report Data */\r
+               CreateGenericHIDReport(GenericData);\r
 \r
 \r
-                       /* Write Generic Report Data */\r
-                       Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));\r
+               /* Write Generic Report Data */\r
+               Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));\r
 \r
 \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
-               }\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
        }\r
 }\r
        }\r
 }\r