X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/df5500e81cc40633eb5edee59410030f0aa77c2d..bb3879331211a19c3adc3927cac870cc7e36b775:/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c diff --git a/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c b/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c index 761b379dc..cdce70071 100644 --- a/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c +++ b/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c @@ -45,13 +45,16 @@ USB_ClassInfo_MS_Host_t FlashDisk_MS_Interface = .Config = { .DataINPipeNumber = 1, + .DataINPipeDoubleBank = false, + .DataOUTPipeNumber = 2, + .DataOUTPipeDoubleBank = false, }, }; /** Main program entry point. This routine configures the hardware required by the application, then - * starts the scheduler to run the application tasks. + * enters a loop to run the application tasks in sequence. */ int main(void) { @@ -71,9 +74,8 @@ int main(void) uint16_t ConfigDescriptorSize; uint8_t ConfigDescriptorData[512]; - if ((USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful) || - (ConfigDescriptorSize > sizeof(ConfigDescriptorData)) || - (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData))) + if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData, + sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful) { printf("Error Retrieving Configuration Descriptor.\r\n"); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); @@ -82,9 +84,9 @@ int main(void) } if (MS_Host_ConfigurePipes(&FlashDisk_MS_Interface, - ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError) + ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError) { - printf("Attached Device Not a Valid Mouse.\r\n"); + printf("Attached Device Not a Valid Mass Storage Device.\r\n"); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -98,7 +100,7 @@ int main(void) break; } - printf("Mouse Enumerated.\r\n"); + printf("Mass Storage Device Enumerated.\r\n"); USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: @@ -113,6 +115,8 @@ int main(void) break; } + printf("Total LUNs: %d - Using first LUN in device.\r\n", (MaxLUNIndex + 1)); + if (MS_Host_ResetMSInterface(&FlashDisk_MS_Interface)) { printf("Error resetting Mass Storage interface.\r\n"); @@ -121,10 +125,27 @@ int main(void) break; } + SCSI_Request_Sense_Response_t SenseData; + if (MS_Host_RequestSense(&FlashDisk_MS_Interface, 0, &SenseData) != 0) + { + printf("Error retrieving device sense.\r\n"); + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + USB_HostState = HOST_STATE_WaitForDeviceRemoval; + break; + } + + if (MS_Host_PreventAllowMediumRemoval(&FlashDisk_MS_Interface, 0, true)) + { + printf("Error setting Prevent Device Removal bit.\r\n"); + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + USB_HostState = HOST_STATE_WaitForDeviceRemoval; + break; + } + SCSI_Inquiry_Response_t InquiryData; - if (MS_Host_GetInquiryData(&FlashDisk_MS_Interface, &InquiryData)) + if (MS_Host_GetInquiryData(&FlashDisk_MS_Interface, 0, &InquiryData)) { - printf("Error retreiving device Inquiry data.\r\n"); + printf("Error retrieving device Inquiry data.\r\n"); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -132,15 +153,17 @@ int main(void) printf("Vendor \"%.8s\", Product \"%.16s\"\r\n", InquiryData.VendorID, InquiryData.ProductID); - bool DeviceReady; - printf("Waiting until ready...\r\n")); + printf("Waiting until ready...\r\n"); - do + for (;;) { - if (USB_HostState != HOST_STATE_Configured) + uint8_t ErrorCode = MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0); + + if (!(ErrorCode)) break; - if (MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0, &DeviceReady)) + /* Check if an error other than a logical command error (device busy) received */ + if (ErrorCode != MS_ERROR_LOGICAL_CMD_FAILED) { printf("Error waiting for device to be ready.\r\n"); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); @@ -148,12 +171,11 @@ int main(void) break; } } - while (DeviceReady == false); - puts_P(PSTR("Retrieving Capacity... ")); + printf("Retrieving Capacity... "); SCSI_Capacity_t DiskCapacity; - if (MS_Host_ReadDeviceCapacity(0, &DiskCapacity)) + if (MS_Host_ReadDeviceCapacity(&FlashDisk_MS_Interface, 0, &DiskCapacity)) { printf("Error retrieving device capacity.\r\n"); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); @@ -163,6 +185,41 @@ int main(void) printf("%lu blocks of %lu bytes.\r\n", DiskCapacity.Blocks, DiskCapacity.BlockSize); + uint8_t BlockBuffer[DiskCapacity.BlockSize]; + + if (MS_Host_ReadDeviceBlocks(&FlashDisk_MS_Interface, 0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer)) + { + printf("Error reading device block.\r\n"); + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + USB_HostState = HOST_STATE_WaitForDeviceRemoval; + break; + } + + printf("\r\nContents of first block:\r\n"); + + for (uint16_t Chunk = 0; Chunk < (DiskCapacity.BlockSize >> 4); Chunk++) + { + uint8_t* ChunkPtr = &BlockBuffer[Chunk << 4]; + + /* Print out the 16 bytes of the chunk in HEX format */ + for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++) + { + char CurrByte = *(ChunkPtr + ByteOffset); + printf_P(PSTR("%.2X "), CurrByte); + } + + printf(" "); + + /* Print out the 16 bytes of the chunk in ASCII format */ + for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++) + { + char CurrByte = *(ChunkPtr + ByteOffset); + putchar(isprint(CurrByte) ? CurrByte : '.'); + } + + printf("\r\n"); + } + LEDs_SetAllLEDs(LEDMASK_USB_READY); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break;