Document CDC Host class driver functions and enums.
[pub/USBasp.git] / Demos / Device / LowLevel / MassStorage / MassStorage.c
index 2c80fc5..1a2d8d4 100644 (file)
@@ -83,7 +83,7 @@ void SetupHardware(void)
 }\r
 \r
 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */\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
@@ -95,7 +95,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 Mass Storage 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
@@ -104,7 +104,7 @@ void EVENT_USB_Disconnect(void)
 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration\r
  *  of the USB device after enumeration - the device endpoints are configured and the Mass Storage management task started.\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
@@ -129,7 +129,7 @@ void EVENT_USB_ConfigurationChanged(void)
  *  control requests that are not handled internally by the USB library (including the Mass Storage class-specific\r
  *  requests) 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
        /* Process UFI specific control requests */\r
        switch (USB_ControlRequest.bRequest)\r
@@ -142,9 +142,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Indicate that the current transfer should be aborted */\r
                                IsMassStoreReset = true;                        \r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
@@ -158,9 +156,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                \r
                                Endpoint_ClearIN();\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
@@ -172,67 +168,67 @@ void EVENT_USB_UnhandledControlPacket(void)
  */\r
 void MassStorage_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
+       /* Select the Data Out Endpoint */\r
+       Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
+       \r
+       /* Check to see if a command from the host has been issued */\r
+       if (Endpoint_IsReadWriteAllowed())\r
        {\r
-               /* Select the Data Out Endpoint */\r
-               Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
-               \r
-               /* Check to see if a command from the host has been issued */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Indicate busy */\r
-                       LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
+               /* Indicate busy */\r
+               LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
 \r
-                       /* Process sent command block from the host */\r
-                       if (ReadInCommandBlock())\r
-                       {\r
-                               /* Check direction of command, select Data IN endpoint if data is from the device */\r
-                               if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)\r
-                                 Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
+               /* Process sent command block from the host */\r
+               if (ReadInCommandBlock())\r
+               {\r
+                       /* Check direction of command, select Data IN endpoint if data is from the device */\r
+                       if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)\r
+                         Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
 \r
-                               /* Decode the received SCSI command */\r
-                               SCSI_DecodeSCSICommand();\r
+                       /* Decode the received SCSI command */\r
+                       SCSI_DecodeSCSICommand();\r
 \r
-                               /* Load in the CBW tag into the CSW to link them together */\r
-                               CommandStatus.Tag = CommandBlock.Tag;\r
+                       /* Load in the CBW tag into the CSW to link them together */\r
+                       CommandStatus.Tag = CommandBlock.Tag;\r
 \r
-                               /* Load in the data residue counter into the CSW */\r
-                               CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;\r
+                       /* Load in the data residue counter into the CSW */\r
+                       CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;\r
 \r
-                               /* Stall the selected data pipe if command failed (if data is still to be transferred) */\r
-                               if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))\r
-                                 Endpoint_StallTransaction();\r
+                       /* Stall the selected data pipe if command failed (if data is still to be transferred) */\r
+                       if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))\r
+                         Endpoint_StallTransaction();\r
 \r
-                               /* Return command status block to the host */\r
-                               ReturnCommandStatus();\r
-                               \r
-                               /* Check if a Mass Storage Reset occurred */\r
-                               if (IsMassStoreReset)\r
-                               {\r
-                                       /* Reset the data endpoint banks */\r
-                                       Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);\r
-                                       Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);\r
-                                       \r
-                                       Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
-                                       Endpoint_ClearStall();\r
-                                       Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
-                                       Endpoint_ClearStall();\r
-                               }\r
-\r
-                               /* Indicate ready */\r
-                               LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
-                       }\r
-                       else\r
+                       /* Return command status block to the host */\r
+                       ReturnCommandStatus();\r
+                       \r
+                       /* Check if a Mass Storage Reset occurred */\r
+                       if (IsMassStoreReset)\r
                        {\r
-                               /* Indicate error reading in the command block from the host */\r
-                               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+                               /* Reset the data endpoint banks */\r
+                               Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);\r
+                               Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);\r
+                               \r
+                               Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
+                               Endpoint_ClearStall();\r
+                               Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
+                               Endpoint_ClearStall();\r
                        }\r
-               }\r
 \r
-               /* Clear the abort transfer flag */\r
-               IsMassStoreReset = false;\r
+                       /* Indicate ready */\r
+                       LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
+               }\r
+               else\r
+               {\r
+                       /* Indicate error reading in the command block from the host */\r
+                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+               }\r
        }\r
+\r
+       /* Clear the abort transfer flag */\r
+       IsMassStoreReset = false;\r
 }\r
 \r
 /** Function to read in a command block from the host, via the bulk data OUT endpoint. This function reads in the next command block\r