Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/USBasp.git] / Demos / Device / LowLevel / MassStorage / MassStorage.c
index 2c80fc5..01d27f0 100644 (file)
@@ -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