Add check to Pipe_IsEndpointBound() in the Mass Storage Host mode Class driver to...
[pub/USBasp.git] / Demos / Host / ClassDriver / MassStorageHost / MassStorageHost.c
index 0f9134f..0155335 100644 (file)
@@ -98,7 +98,7 @@ int main(void)
                                        break;\r
                                }\r
                                \r
-                               printf("Mouse Enumerated.\r\n");\r
+                               printf("Mass Storage Device Enumerated.\r\n");\r
                                USB_HostState = HOST_STATE_Configured;\r
                                break;\r
                        case HOST_STATE_Configured:\r
@@ -121,8 +121,25 @@ int main(void)
                                        break;\r
                                }\r
                                \r
+                               SCSI_Request_Sense_Response_t SenseData;\r
+                               if (MS_Host_RequestSense(&FlashDisk_MS_Interface, 0, &SenseData) != 0)\r
+                               {\r
+                                       printf("Error retrieving device sense.\r\n");\r
+                                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+                                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+                                       break;\r
+                               }\r
+                       \r
+                               if (MS_Host_PreventAllowMediumRemoval(&FlashDisk_MS_Interface, 0, true))\r
+                               {\r
+                                       printf("Error setting Prevent Device Removal bit.\r\n");\r
+                                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+                                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+                                       break;\r
+                               }\r
+\r
                                SCSI_Inquiry_Response_t InquiryData;\r
-                               if (MS_Host_GetInquiryData(&FlashDisk_MS_Interface, &InquiryData))\r
+                               if (MS_Host_GetInquiryData(&FlashDisk_MS_Interface, 0, &InquiryData))\r
                                {\r
                                        printf("Error retreiving device Inquiry data.\r\n");\r
                                        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
@@ -133,11 +150,16 @@ int main(void)
                                printf("Vendor \"%.8s\", Product \"%.16s\"\r\n", InquiryData.VendorID, InquiryData.ProductID);\r
                                \r
                                printf("Waiting until ready...\r\n");\r
-                               bool DeviceReady;\r
 \r
-                               do\r
+                               for (;;)\r
                                {\r
-                                       if (MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0, &DeviceReady))\r
+                                       uint8_t ErrorCode = MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0);\r
+                                       \r
+                                       if (!(ErrorCode))\r
+                                         break;\r
+\r
+                                       /* Check if an error other than a logical command error (device busy) received */\r
+                                       if (ErrorCode != MS_ERROR_LOGICAL_CMD_FAILED)\r
                                        {\r
                                                printf("Error waiting for device to be ready.\r\n");\r
                                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
@@ -145,9 +167,8 @@ int main(void)
                                                break;\r
                                        }\r
                                }\r
-                               while (!(DeviceReady));\r
 \r
-                               puts_P(PSTR("Retrieving Capacity... "));\r
+                               printf("Retrieving Capacity... ");\r
 \r
                                SCSI_Capacity_t DiskCapacity;\r
                                if (MS_Host_ReadDeviceCapacity(&FlashDisk_MS_Interface, 0, &DiskCapacity))\r
@@ -160,6 +181,41 @@ int main(void)
                                \r
                                printf("%lu blocks of %lu bytes.\r\n", DiskCapacity.Blocks, DiskCapacity.BlockSize);\r
 \r
+                               uint8_t BlockBuffer[DiskCapacity.BlockSize];\r
+\r
+                               if (MS_Host_ReadDeviceBlocks(&FlashDisk_MS_Interface, 0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer))\r
+                               {\r
+                                       printf("Error reading device block.\r\n");\r
+                                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+                                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+                                       break;\r
+                               }\r
+                       \r
+                               printf("\r\nContents of first block:\r\n");\r
+\r
+                               for (uint16_t Chunk = 0; Chunk < (DiskCapacity.BlockSize >> 4); Chunk++)\r
+                               {\r
+                                       uint8_t* ChunkPtr = &BlockBuffer[Chunk << 4];\r
+                                       \r
+                                       /* Print out the 16 bytes of the chunk in HEX format */\r
+                                       for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)\r
+                                       {\r
+                                               char CurrByte = *(ChunkPtr + ByteOffset);\r
+                                               printf_P(PSTR("%.2X "), CurrByte);\r
+                                       }\r
+                                       \r
+                                       printf("    ");\r
+\r
+                                       /* Print out the 16 bytes of the chunk in ASCII format */\r
+                                       for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)\r
+                                       {\r
+                                               char CurrByte = *(ChunkPtr + ByteOffset);\r
+                                               putchar(isprint(CurrByte) ? CurrByte : '.');\r
+                                       }\r
+                                       \r
+                                       printf("\r\n");\r
+                               }\r
+\r
                                LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
                                USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r