From d3f11eb52809353d7f103cea8b4332f335c26697 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Tue, 24 Nov 2009 14:10:28 +0000
Subject: [PATCH] Make the StandaloneProgrammer project seamlessly read out
 drive contents from either an attached FAT formatted drive when in host mode,
 or the onboard FAT formatted dataflash when in device mode.

---
 .../ClassDriver/MassStorageHost/MassStorageHost.c   |  2 +-
 .../Incomplete/StandaloneProgrammer/DiskDevice.c    |  2 --
 Projects/Incomplete/StandaloneProgrammer/DiskHost.c | 13 ++++---------
 .../StandaloneProgrammer/Lib/PetiteFATFs/diskio.c   |  2 +-
 .../StandaloneProgrammer/StandaloneProgrammer.c     | 10 ++++++++++
 .../StandaloneProgrammer/StandaloneProgrammer.h     |  2 ++
 6 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c b/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c
index 94d363f9e..cdce70071 100644
--- a/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c
+++ b/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c
@@ -84,7 +84,7 @@ 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 Mass Storage Device.\r\n");
 					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
diff --git a/Projects/Incomplete/StandaloneProgrammer/DiskDevice.c b/Projects/Incomplete/StandaloneProgrammer/DiskDevice.c
index 104893a93..2bb27bd55 100644
--- a/Projects/Incomplete/StandaloneProgrammer/DiskDevice.c
+++ b/Projects/Incomplete/StandaloneProgrammer/DiskDevice.c
@@ -77,8 +77,6 @@ void EVENT_USB_Device_ConfigurationChanged(void)
 
 	if (!(MS_Device_ConfigureEndpoints(&DiskDevice_MS_Interface)))
 	  LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
-	
-	pf_mount(&DiskFATState);
 }
 
 /** Event handler for the library USB Unhandled Control Request event. */
diff --git a/Projects/Incomplete/StandaloneProgrammer/DiskHost.c b/Projects/Incomplete/StandaloneProgrammer/DiskHost.c
index c55414179..8e5182099 100644
--- a/Projects/Incomplete/StandaloneProgrammer/DiskHost.c
+++ b/Projects/Incomplete/StandaloneProgrammer/DiskHost.c
@@ -59,16 +59,14 @@ void DiskHost_USBTask(void)
 		if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
 											   sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
 		{
-			printf("ERROR - GetConfig\r\n");
 			LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 			USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 			return;
 		}
 
 		if (MS_Host_ConfigurePipes(&DiskHost_MS_Interface,
-									ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError)
+		                           ConfigDescriptorSize, ConfigDescriptorData) != MS_ENUMERROR_NoError)
 		{
-			printf("ERROR - Pipes\r\n");
 			LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 			USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 			return;
@@ -76,16 +74,16 @@ void DiskHost_USBTask(void)
 		
 		if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
 		{
-			printf("ERROR - SetConfig\r\n");
 			LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 			USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 			return;
 		}
-
+		
+		USB_HostState = HOST_STATE_Configured;
+		
 		uint8_t MaxLUNIndex;
 		if (MS_Host_GetMaxLUN(&DiskHost_MS_Interface, &MaxLUNIndex))
 		{
-			printf("ERROR - MaxLUN\r\n");
 			LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 			USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 			return;
@@ -93,7 +91,6 @@ void DiskHost_USBTask(void)
 		
 		if (MS_Host_ResetMSInterface(&DiskHost_MS_Interface))
 		{
-			printf("ERROR - ResetMS\r\n");
 			LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 			USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 			return;
@@ -102,7 +99,6 @@ void DiskHost_USBTask(void)
 		SCSI_Request_Sense_Response_t SenseData;
 		if (MS_Host_RequestSense(&DiskHost_MS_Interface, 0, &SenseData) != 0)
 		{
-			printf("ERROR - Sense\r\n");
 			LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 			USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 			return;
@@ -111,7 +107,6 @@ void DiskHost_USBTask(void)
 		pf_mount(&DiskFATState);
 		
 		LEDs_SetAllLEDs(LEDMASK_USB_READY);
-		USB_HostState = HOST_STATE_Configured;
 	}
 
 	MS_Host_USBTask(&DiskHost_MS_Interface);		
diff --git a/Projects/Incomplete/StandaloneProgrammer/Lib/PetiteFATFs/diskio.c b/Projects/Incomplete/StandaloneProgrammer/Lib/PetiteFATFs/diskio.c
index b40adf1d8..d46d9b969 100644
--- a/Projects/Incomplete/StandaloneProgrammer/Lib/PetiteFATFs/diskio.c
+++ b/Projects/Incomplete/StandaloneProgrammer/Lib/PetiteFATFs/diskio.c
@@ -42,7 +42,7 @@ DRESULT disk_readp (
 		else if (MS_Host_ReadDeviceBlocks(&DiskHost_MS_Interface, 0, sector, 1, 512, BlockTemp))
 		  ErrorCode = RES_ERROR;
 
-		printf("BLOCK READ #%lu Ret %d\r\n", sector, MS_Host_ReadDeviceBlocks(&DiskHost_MS_Interface, 0, sector, 1, 512, BlockTemp));
+		MS_Host_ReadDeviceBlocks(&DiskHost_MS_Interface, 0, sector, 1, 512, BlockTemp);
 		#endif
 	}
 	else
diff --git a/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.c b/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.c
index e8a863f54..5d8652366 100644
--- a/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.c
+++ b/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.c
@@ -57,6 +57,16 @@ static int Disk_getchar(FILE* Stream)
 	return (ByteWasRead ? ReadByte : _FDEV_EOF);
 }
 
+#if defined(USB_CAN_BE_BOTH)
+/** Event to handle mode changes in the library, to clear the FAT library's drive state structure when transitioning
+ *  between modes. This ensures that the library always works with current disk data.
+ */
+void EVENT_USB_UIDChange(void)
+{
+	pf_mount(&DiskFATState);
+}
+#endif
+
 /** Task to determine if the user is wishes to start the programming sequence, and if so executes the 
  *  required functions to program the attached target (if any) with the files loaded to the dataflash.
  */
diff --git a/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.h b/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.h
index 698070af1..744ad01bf 100644
--- a/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.h
+++ b/Projects/Incomplete/StandaloneProgrammer/StandaloneProgrammer.h
@@ -79,6 +79,8 @@
 			static int Disk_getchar(FILE* Stream);
 		#endif
 		
+		void EVENT_USB_UIDChange(void);
+		
 		void SetupHardware(void);
 		void Programmer_Task(void);
 		
-- 
GitLab