Added incomplete MIDIToneGenerator project.
[pub/USBasp.git] / Demos / Device / LowLevel / GenericHID / GenericHID.c
index 40de8b9..4d3f4c1 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -41,7 +41,7 @@ static uint8_t LastReceived[GENERIC_REPORT_SIZE];
 \r
 \r
 /** Main program entry point. This routine configures the hardware required by the application, then\r
 \r
 \r
 /** Main program entry point. This routine configures the hardware required by the application, then\r
- *  starts the scheduler to run the USB management task.\r
+ *  enters a loop to run the application tasks in sequence.\r
  */\r
 int main(void)\r
 {\r
  */\r
 int main(void)\r
 {\r
@@ -74,7 +74,7 @@ void SetupHardware(void)
 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and\r
  *  starts the library USB task to begin the enumeration and USB management process.\r
  */\r
 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and\r
  *  starts the library USB task to begin the enumeration and USB management process.\r
  */\r
-void EVENT_USB_Connect(void)\r
+void EVENT_USB_Device_Connect(void)\r
 {\r
        /* Indicate USB enumerating */\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
 {\r
        /* Indicate USB enumerating */\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
@@ -83,7 +83,7 @@ void EVENT_USB_Connect(void)
 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via\r
  *  the status LEDs and stops the USB management task.\r
  */\r
 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via\r
  *  the status LEDs and stops the USB management task.\r
  */\r
-void EVENT_USB_Disconnect(void)\r
+void EVENT_USB_Device_Disconnect(void)\r
 {\r
        /* Indicate USB not ready */\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 {\r
        /* Indicate USB not ready */\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
@@ -92,7 +92,7 @@ void EVENT_USB_Disconnect(void)
 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration\r
  *  of the USB device after enumeration, and configures the generic HID device endpoints.\r
  */\r
 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration\r
  *  of the USB device after enumeration, and configures the generic HID device endpoints.\r
  */\r
-void EVENT_USB_ConfigurationChanged(void)\r
+void EVENT_USB_Device_ConfigurationChanged(void)\r
 {\r
        /* Indicate USB connected and ready */\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
 {\r
        /* Indicate USB connected and ready */\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
@@ -114,11 +114,11 @@ void EVENT_USB_ConfigurationChanged(void)
        }\r
 }\r
 \r
        }\r
 }\r
 \r
-/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific\r
+/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific\r
  *  control requests that are not handled internally by the USB library (including the HID commands, which are\r
  *  all issued via the control endpoint), so that they can be handled appropriately for the application.\r
  */\r
  *  control requests that are not handled internally by the USB library (including the HID commands, which are\r
  *  all issued via the control endpoint), so that they can be handled appropriately for the application.\r
  */\r
-void EVENT_USB_UnhandledControlPacket(void)\r
+void EVENT_USB_Device_UnhandledControlRequest(void)\r
 {\r
        /* Handle HID Class specific requests */\r
        switch (USB_ControlRequest.bRequest)\r
 {\r
        /* Handle HID Class specific requests */\r
        switch (USB_ControlRequest.bRequest)\r
@@ -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