Corrections to the unfinished AVRISP Programmer project to allow AVRStudio to connect...
[pub/USBasp.git] / Demos / Host / LowLevel / MassStorageHost / MassStorageHost.c
index 200c81a..2b27b12 100644 (file)
@@ -48,8 +48,7 @@ int main(void)
 {\r
        SetupHardware();\r
 \r
-       puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY\r
-              "Mass Storage Host Demo running.\r\n" ESC_INVERSE_OFF));\r
+       puts_P(PSTR(ESC_FG_CYAN "Mass Storage Host Demo running.\r\n" ESC_FG_WHITE));\r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
@@ -80,36 +79,36 @@ void SetupHardware(void)
 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and\r
  *  starts the library USB task to begin the enumeration and USB management process.\r
  */\r
-void EVENT_USB_DeviceAttached(void)\r
+void EVENT_USB_Host_DeviceAttached(void)\r
 {\r
-       puts_P(PSTR("Device Attached.\r\n"));\r
+       puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
 }\r
 \r
 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and\r
  *  stops the library USB task management process.\r
  */\r
-void EVENT_USB_DeviceUnattached(void)\r
+void EVENT_USB_Host_DeviceUnattached(void)\r
 {\r
-       puts_P(PSTR("\r\nDevice Unattached.\r\n"));\r
+       puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 }\r
 \r
 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully\r
  *  enumerated by the host and is now ready to be used by the application.\r
  */\r
-void EVENT_USB_DeviceEnumerationComplete(void)\r
+void EVENT_USB_Host_DeviceEnumerationComplete(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
 }\r
 \r
 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */\r
-void EVENT_USB_HostError(const uint8_t ErrorCode)\r
+void EVENT_USB_Host_HostError(const uint8_t ErrorCode)\r
 {\r
        USB_ShutDown();\r
 \r
-       puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n"));\r
-       printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);\r
+       printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"\r
+                                " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
        for(;;);\r
@@ -118,12 +117,12 @@ void EVENT_USB_HostError(const uint8_t ErrorCode)
 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while\r
  *  enumerating an attached USB device.\r
  */\r
-void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)\r
+void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)\r
 {\r
-       puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n"));\r
-       printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);\r
-       printf_P(PSTR(" -- Sub Error Code %d\r\n"), SubErrorCode);\r
-       printf_P(PSTR(" -- In State %d\r\n"), USB_HostState);\r
+       printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"\r
+                                " -- Error Code %d\r\n"\r
+                                " -- Sub Error Code %d\r\n"\r
+                                " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);\r
        \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 }\r
@@ -144,42 +143,39 @@ void MassStorage_Task(void)
                        if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)\r
                        {\r
                                if (ErrorCode == ControlError)\r
-                                 puts_P(PSTR("Control Error (Get Configuration).\r\n"));\r
+                                 puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));\r
                                else\r
-                                 puts_P(PSTR("Invalid Device.\r\n"));\r
+                                 puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));\r
 \r
-                               printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
+                               printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
                                \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Wait until USB device disconnected */\r
-                               while (USB_IsConnected);\r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
 \r
                        /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */\r
                        if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)\r
                        {\r
-                               puts_P(PSTR("Control Error (Set Configuration).\r\n"));\r
-                               printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
+                               printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"\r
+                                                        " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Wait until USB device disconnected */\r
-                               while (USB_IsConnected);\r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
                                \r
+                       puts_P(PSTR("Mass Storage Disk Enumerated.\r\n"));\r
+\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       puts_P(PSTR("Mass Storage Disk Enumerated.\r\n"));\r
-                               \r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-               case HOST_STATE_Ready:\r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                        \r
@@ -187,6 +183,8 @@ void MassStorage_Task(void)
                        if ((ErrorCode = MassStore_GetMaxLUN(&MassStore_MaxLUNIndex)) != HOST_SENDCONTROL_Successful)\r
                        {       \r
                                ShowDiskReadError(PSTR("Get Max LUN"), false, ErrorCode);\r
+\r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
                        \r
@@ -197,6 +195,8 @@ void MassStorage_Task(void)
                        if ((ErrorCode = MassStore_MassStorageReset()) != HOST_SENDCONTROL_Successful)\r
                        {\r
                                ShowDiskReadError(PSTR("Mass Storage Reset"), false, ErrorCode);\r
+                               \r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
                        \r
@@ -213,6 +213,8 @@ void MassStorage_Task(void)
                        if (((ErrorCode = MassStore_PreventAllowMediumRemoval(0, true)) != 0) || (SCSICommandStatus.Status != Command_Pass))\r
                        {\r
                                ShowDiskReadError(PSTR("Prevent/Allow Medium Removal"), (SCSICommandStatus.Status != Command_Pass), ErrorCode);\r
+                               \r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
 \r
@@ -221,6 +223,8 @@ void MassStorage_Task(void)
                        if (((ErrorCode = MassStore_Inquiry(0, &InquiryData)) != 0) || (SCSICommandStatus.Status != Command_Pass))\r
                        {\r
                                ShowDiskReadError(PSTR("Inquiry"), (SCSICommandStatus.Status != Command_Pass), ErrorCode);\r
+                               \r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
 \r
@@ -234,17 +238,19 @@ void MassStorage_Task(void)
                        {\r
                                Serial_TxByte('.');\r
                                \r
-                               if ((ErrorCode = MassStore_TestUnitReady(0)) != 0)\r
+                               /* Abort if device removed */\r
+                               if (USB_HostState == HOST_STATE_Unattached)\r
+                                 break;\r
+\r
+                               if ((ErrorCode = MassStore_TestUnitReady(0)) != PIPE_RWSTREAM_NoError)\r
                                {\r
                                        ShowDiskReadError(PSTR("Test Unit Ready"), false, ErrorCode);\r
-                                       break;                          \r
+\r
+                                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+                                       break;\r
                                }\r
                        }\r
-                       while ((SCSICommandStatus.Status != Command_Pass) && USB_IsConnected);\r
-                       \r
-                       /* Abort if device removed */\r
-                       if (!(USB_IsConnected))\r
-                         break;\r
+                       while (SCSICommandStatus.Status != Command_Pass);\r
 \r
                        puts_P(PSTR("\r\nRetrieving Capacity... "));\r
 \r
@@ -255,6 +261,8 @@ void MassStorage_Task(void)
                        if (((ErrorCode = MassStore_ReadCapacity(0, &DiskCapacity)) != 0) || (SCSICommandStatus.Status != Command_Pass))\r
                        {\r
                                ShowDiskReadError(PSTR("Read Capacity"), (SCSICommandStatus.Status != Command_Pass), ErrorCode);\r
+                               \r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
                        \r
@@ -269,6 +277,8 @@ void MassStorage_Task(void)
                            (SCSICommandStatus.Status != Command_Pass))\r
                        {\r
                                ShowDiskReadError(PSTR("Read Device Block"), (SCSICommandStatus.Status != Command_Pass), ErrorCode);\r
+                               \r
+                               USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
                        \r
@@ -307,7 +317,7 @@ void MassStorage_Task(void)
                        while (!(Buttons_GetStatus() & BUTTONS_BUTTON1))\r
                        {\r
                                /* Abort if device removed */\r
-                               if (!(USB_IsConnected))\r
+                               if (USB_HostState == HOST_STATE_Unattached)\r
                                  break;\r
                        }\r
                        \r
@@ -319,6 +329,8 @@ void MassStorage_Task(void)
                                    (SCSICommandStatus.Status != Command_Pass))\r
                                {\r
                                        ShowDiskReadError(PSTR("Read Device Block"), (SCSICommandStatus.Status != Command_Pass), ErrorCode);\r
+                                       \r
+                                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                        break;\r
                                }\r
 \r
@@ -331,7 +343,7 @@ void MassStorage_Task(void)
                                }\r
 \r
                                /* Abort if device removed */\r
-                               if (!(USB_IsConnected))\r
+                               if (USB_HostState == HOST_STATE_Unattached)\r
                                  break;\r
                        }\r
                        \r
@@ -339,8 +351,7 @@ void MassStorage_Task(void)
                        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
                        \r
                        /* Wait until USB device disconnected */\r
-                       while (USB_IsConnected);\r
-                       \r
+                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                        break;\r
        }\r
 }\r
@@ -349,30 +360,27 @@ void MassStorage_Task(void)
  *  printing error codes to the serial port and waiting until the device is removed before\r
  *  continuing.\r
  *\r
- *  \param CommandString  ASCII string located in PROGMEM space indicating what operation failed\r
- *  \param FailedAtSCSILayer  Indicates if the command failed at the (logical) SCSI layer or at the physical USB layer\r
- *  \param ErrorCode      Error code of the function which failed to complete successfully\r
+ *  \param[in] CommandString      ASCII string located in PROGMEM space indicating what operation failed\r
+ *  \param[in] FailedAtSCSILayer  Indicates if the command failed at the (logical) SCSI layer or at the physical USB layer\r
+ *  \param[in] ErrorCode          Error code of the function which failed to complete successfully\r
  */\r
 void ShowDiskReadError(char* CommandString, bool FailedAtSCSILayer, uint8_t ErrorCode)\r
 {\r
        if (FailedAtSCSILayer)\r
        {\r
                /* Display the error code */\r
-               printf_P(PSTR(ESC_BG_RED "SCSI command error (%S).\r\n"), CommandString);\r
-               printf_P(PSTR("  -- Status Code: %d"), ErrorCode);\r
+               printf_P(PSTR(ESC_FG_RED "SCSI command error (%S).\r\n"), CommandString);\r
+               printf_P(PSTR("  -- Status Code: %d" ESC_FG_WHITE), ErrorCode);\r
        }\r
        else\r
        {\r
                /* Display the error code */\r
-               printf_P(PSTR(ESC_BG_RED "Command error (%S).\r\n"), CommandString);\r
-               printf_P(PSTR("  -- Error Code: %d"), ErrorCode);       \r
+               printf_P(PSTR(ESC_FG_RED "Command error (%S).\r\n"), CommandString);\r
+               printf_P(PSTR("  -- Error Code: %d" ESC_FG_WHITE), ErrorCode);  \r
        }\r
 \r
        Pipe_Freeze();\r
 \r
        /* Indicate device error via the status LEDs */\r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
-\r
-       /* Wait until USB device disconnected */\r
-       while (USB_IsConnected);\r
 }\r