X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/cc879df4f014e403e57caca50a5791c996c0d79c..fa8d25ef29eb9e96f9089d17eeb64b3a5edb8e66:/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c?ds=sidebyside diff --git a/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c b/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c index 99f116eb3..855ce8df3 100644 --- a/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c +++ b/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c @@ -48,7 +48,7 @@ int main(void) { SetupHardware(); - puts_P(PSTR(ESC_RESET ESC_FG_CYAN "Mass Storage Host Demo running.\r\n" ESC_FG_WHITE)); + puts_P(PSTR(ESC_FG_CYAN "Mass Storage Host Demo running.\r\n" ESC_FG_WHITE)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); @@ -79,7 +79,7 @@ void SetupHardware(void) /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and * starts the library USB task to begin the enumeration and USB management process. */ -void EVENT_USB_DeviceAttached(void) +void EVENT_USB_Host_DeviceAttached(void) { puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE)); LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); @@ -88,7 +88,7 @@ void EVENT_USB_DeviceAttached(void) /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and * stops the library USB task management process. */ -void EVENT_USB_DeviceUnattached(void) +void EVENT_USB_Host_DeviceUnattached(void) { puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); @@ -97,18 +97,18 @@ void EVENT_USB_DeviceUnattached(void) /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully * enumerated by the host and is now ready to be used by the application. */ -void EVENT_USB_DeviceEnumerationComplete(void) +void EVENT_USB_Host_DeviceEnumerationComplete(void) { LEDs_SetAllLEDs(LEDMASK_USB_READY); } /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ -void EVENT_USB_HostError(const uint8_t ErrorCode) +void EVENT_USB_Host_HostError(const uint8_t ErrorCode) { USB_ShutDown(); - puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n")); - printf_P(PSTR(" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode); + printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n" + " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); for(;;); @@ -117,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 * enumerating an attached USB device. */ -void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode) +void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode) { - puts_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n")); - printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode); - printf_P(PSTR(" -- Sub Error Code %d\r\n"), SubErrorCode); - printf_P(PSTR(" -- In State %d\r\n" ESC_FG_WHITE), USB_HostState); + printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n" + " -- Error Code %d\r\n" + " -- Sub Error Code %d\r\n" + " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); } @@ -160,8 +160,8 @@ void MassStorage_Task(void) /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */ if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful) { - puts_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n")); - printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); + printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n" + " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode); /* Indicate error via status LEDs */ LEDs_SetAllLEDs(LEDMASK_USB_ERROR); @@ -171,21 +171,18 @@ void MassStorage_Task(void) break; } + puts_P(PSTR("Mass Storage Disk Enumerated.\r\n")); + USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: - puts_P(PSTR("Mass Storage Disk Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; - break; - case HOST_STATE_Ready: /* Indicate device busy via the status LEDs */ LEDs_SetAllLEDs(LEDMASK_USB_BUSY); /* Send the request, display error and wait for device detach if request fails */ if ((ErrorCode = MassStore_GetMaxLUN(&MassStore_MaxLUNIndex)) != HOST_SENDCONTROL_Successful) { - ShowDiskReadError(PSTR("Get Max LUN"), false, ErrorCode); + ShowDiskReadError(PSTR("Get Max LUN"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -197,7 +194,7 @@ void MassStorage_Task(void) /* Reset the Mass Storage device interface, ready for use */ if ((ErrorCode = MassStore_MassStorageReset()) != HOST_SENDCONTROL_Successful) { - ShowDiskReadError(PSTR("Mass Storage Reset"), false, ErrorCode); + ShowDiskReadError(PSTR("Mass Storage Reset"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -206,16 +203,17 @@ void MassStorage_Task(void) /* Get sense data from the device - many devices will not accept any other commands until the sense data * is read - both on start-up and after a failed command */ SCSI_Request_Sense_Response_t SenseData; - if (((ErrorCode = MassStore_RequestSense(0, &SenseData)) != 0) || (SCSICommandStatus.Status != Command_Pass)) + if ((ErrorCode = MassStore_RequestSense(0, &SenseData)) != 0) { - ShowDiskReadError(PSTR("Request Sense"), (SCSICommandStatus.Status != Command_Pass), ErrorCode); + ShowDiskReadError(PSTR("Request Sense"), ErrorCode); + USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; } /* Set the prevent removal flag for the device, allowing it to be accessed */ - if (((ErrorCode = MassStore_PreventAllowMediumRemoval(0, true)) != 0) || (SCSICommandStatus.Status != Command_Pass)) + if ((ErrorCode = MassStore_PreventAllowMediumRemoval(0, true)) != 0) { - ShowDiskReadError(PSTR("Prevent/Allow Medium Removal"), (SCSICommandStatus.Status != Command_Pass), ErrorCode); + ShowDiskReadError(PSTR("Prevent/Allow Medium Removal"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -223,9 +221,9 @@ void MassStorage_Task(void) /* Get inquiry data from the device */ SCSI_Inquiry_Response_t InquiryData; - if (((ErrorCode = MassStore_Inquiry(0, &InquiryData)) != 0) || (SCSICommandStatus.Status != Command_Pass)) + if ((ErrorCode = MassStore_Inquiry(0, &InquiryData)) != 0) { - ShowDiskReadError(PSTR("Inquiry"), (SCSICommandStatus.Status != Command_Pass), ErrorCode); + ShowDiskReadError(PSTR("Inquiry"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -237,23 +235,30 @@ void MassStorage_Task(void) /* Wait until disk ready */ puts_P(PSTR("Waiting until ready..")); - do + for (;;) { Serial_TxByte('.'); - - if ((ErrorCode = MassStore_TestUnitReady(0)) != 0) + + /* Abort if device removed */ + if (USB_HostState == HOST_STATE_Unattached) + break; + + /* Check to see if the attached device is ready for new commands */ + ErrorCode = MassStore_TestUnitReady(0); + + /* If attached device is ready, abort the loop */ + if (!(ErrorCode)) + break; + + /* If an error othe than a logical command failure (indicating device busy) returned, abort */ + if (ErrorCode != MASS_STORE_SCSI_COMMAND_FAILED) { - ShowDiskReadError(PSTR("Test Unit Ready"), false, ErrorCode); + ShowDiskReadError(PSTR("Test Unit Ready"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; } } - while ((SCSICommandStatus.Status != Command_Pass) && USB_IsConnected); - - /* Abort if device removed */ - if (!(USB_IsConnected)) - break; puts_P(PSTR("\r\nRetrieving Capacity... ")); @@ -261,9 +266,9 @@ void MassStorage_Task(void) SCSI_Capacity_t DiskCapacity; /* Retrieve disk capacity */ - if (((ErrorCode = MassStore_ReadCapacity(0, &DiskCapacity)) != 0) || (SCSICommandStatus.Status != Command_Pass)) + if ((ErrorCode = MassStore_ReadCapacity(0, &DiskCapacity)) != 0) { - ShowDiskReadError(PSTR("Read Capacity"), (SCSICommandStatus.Status != Command_Pass), ErrorCode); + ShowDiskReadError(PSTR("Read Capacity"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -276,10 +281,9 @@ void MassStorage_Task(void) uint8_t BlockBuffer[DiskCapacity.BlockSize]; /* Read in the first 512 byte block from the device */ - if (((ErrorCode = MassStore_ReadDeviceBlock(0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer)) != 0) || - (SCSICommandStatus.Status != Command_Pass)) + if ((ErrorCode = MassStore_ReadDeviceBlock(0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer)) != 0) { - ShowDiskReadError(PSTR("Read Device Block"), (SCSICommandStatus.Status != Command_Pass), ErrorCode); + ShowDiskReadError(PSTR("Read Device Block"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -320,18 +324,21 @@ void MassStorage_Task(void) while (!(Buttons_GetStatus() & BUTTONS_BUTTON1)) { /* Abort if device removed */ - if (!(USB_IsConnected)) + if (USB_HostState == HOST_STATE_Unattached) break; } + + /* Abort if device removed */ + if (USB_HostState == HOST_STATE_Unattached) + break; /* Print out the entire disk contents in ASCII format */ - for (uint32_t CurrBlock = 0; CurrBlock < DiskCapacity.Blocks; CurrBlock++) + for (uint32_t CurrBlockAddress = 0; CurrBlockAddress < DiskCapacity.Blocks; CurrBlockAddress++) { /* Read in the next block of data from the device */ - if (((ErrorCode = MassStore_ReadDeviceBlock(0, CurrBlock, 1, DiskCapacity.BlockSize, BlockBuffer)) != 0) || - (SCSICommandStatus.Status != Command_Pass)) + if ((ErrorCode = MassStore_ReadDeviceBlock(0, CurrBlockAddress, 1, DiskCapacity.BlockSize, BlockBuffer)) != 0) { - ShowDiskReadError(PSTR("Read Device Block"), (SCSICommandStatus.Status != Command_Pass), ErrorCode); + ShowDiskReadError(PSTR("Read Device Block"), ErrorCode); USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; @@ -346,7 +353,7 @@ void MassStorage_Task(void) } /* Abort if device removed */ - if (!(USB_IsConnected)) + if (USB_HostState == HOST_STATE_Unattached) break; } @@ -363,17 +370,15 @@ void MassStorage_Task(void) * printing error codes to the serial port and waiting until the device is removed before * continuing. * - * \param[in] CommandString ASCII string located in PROGMEM space indicating what operation failed - * \param[in] FailedAtSCSILayer Indicates if the command failed at the (logical) SCSI layer or at the physical USB layer - * \param[in] ErrorCode Error code of the function which failed to complete successfully + * \param[in] CommandString ASCII string located in PROGMEM space indicating what operation failed + * \param[in] ErrorCode Error code of the function which failed to complete successfully */ -void ShowDiskReadError(char* CommandString, bool FailedAtSCSILayer, uint8_t ErrorCode) +void ShowDiskReadError(char* CommandString, uint8_t ErrorCode) { - if (FailedAtSCSILayer) + if (ErrorCode == MASS_STORE_SCSI_COMMAND_FAILED) { /* Display the error code */ printf_P(PSTR(ESC_FG_RED "SCSI command error (%S).\r\n"), CommandString); - printf_P(PSTR(" -- Status Code: %d" ESC_FG_WHITE), ErrorCode); } else {