@@ -24,7 +24,7 @@ enum TestTypes : uint8_t
2424
2525// !!! TEST CONFIGURATION !!! -->
2626
27- constexpr enum TestTypes selectedTest = TEST_PORTENTA_C33_SDCARD ;
27+ constexpr enum TestTypes selectedTest = TEST_PORTENTA_H7_USB ;
2828
2929// Notice that formtting tests can take a while to complete
3030
@@ -35,12 +35,18 @@ constexpr enum TestTypes selectedTest = TEST_PORTENTA_C33_SDCARD;
3535// <-- !!! TEST CONFIGURATION !!!
3636
3737volatile bool usbAttached = false ;
38+ volatile bool usbDetached = false ;
3839
3940void usbCallback ()
4041{
4142 usbAttached = true ;
4243}
4344
45+ void usbCallback2 ()
46+ {
47+ usbDetached = true ;
48+ }
49+
4450void setup () {
4551 bool allTestsOk = true ;
4652 enum StorageDevices deviceName;
@@ -104,60 +110,55 @@ void setup() {
104110 }
105111 // <-- Register hotplug callback for SD Card test
106112
107- if (TEST_PORTENTA_C33_USB == selectedTest)
113+ // Register unplug callback for SD Card test -->
114+ if (DEV_SDCARD == deviceName)
108115 {
109- // Register nullptr callback test -->
110- retVal = register_hotplug_callback (DEV_USB, nullptr );
111- if ((-1 != retVal) || (EFAULT != errno))
116+ // Using usbCallback2() is fine because it doesn't get registered anyway
117+ retVal = register_unplug_callback (DEV_SDCARD, usbCallback2 );
118+ if ((-1 != retVal) || (ENOTSUP != errno))
112119 {
113120 allTestsOk = false ;
114- Serial.print (" [FAIL] Register nullptr callback test failed" );
121+ Serial.print (" [FAIL] Register unplug callback for SD Card test failed" );
115122 Serial.println ();
116123 }
117- // <-- Register nullptr callback test
118124 }
125+ // <-- Register unplug callback for SD Card test
119126
120- if ((TEST_PORTENTA_H7_USB == selectedTest) || (TEST_PORTENTA_MACHINE_CONTROL_USB == selectedTest) || (TEST_OPTA_USB == selectedTest) )
127+ if (DEV_USB == deviceName )
121128 {
122- // Register unsupported callback test -->
123- retVal = register_hotplug_callback (DEV_USB, usbCallback );
124- if ((-1 != retVal) || (ENOTSUP != errno))
129+ // Register nullptr callback test (hotplug) -->
130+ retVal = register_hotplug_callback (DEV_USB, nullptr );
131+ if ((-1 != retVal) || (EFAULT != errno))
125132 {
126133 allTestsOk = false ;
127- Serial.println (" [FAIL] Register unsupported callback test" );
134+ Serial.print (" [FAIL] Register nullptr callback test failed (hotplug) " );
128135 Serial.println ();
129136 }
130- // <-- Register unsupported callback test
137+ // <-- Register nullptr callback test (hotplug)
138+
139+ // Register nullptr callback test (unplug) -->
140+ retVal = register_unplug_callback (DEV_USB, nullptr );
141+ if ((-1 != retVal) || (EFAULT != errno))
142+ {
143+ allTestsOk = false ;
144+ Serial.print (" [FAIL] Register nullptr callback test failed (unplug)" );
145+ Serial.println ();
146+ }
147+ // <-- Register nullptr callback test (unplug)
131148 }
132149
133- // This isn't a test, just wait for a USB thumb drive -->
150+ // Wait for a USB thumb drive -->
134151 if (DEV_USB == deviceName)
135152 {
136153 Serial.println (" Please insert a thumb drive" );
137- if (TEST_PORTENTA_C33_USB == selectedTest)
138- {
139- // This board supports hotplug callbacks
140- (void ) register_hotplug_callback (DEV_USB, usbCallback);
141- while (false == usbAttached) {
142- delay (500 );
143- }
144- }
145- else if ((TEST_PORTENTA_H7_USB == selectedTest) || (TEST_PORTENTA_MACHINE_CONTROL_USB == selectedTest) || (TEST_OPTA_USB == selectedTest))
146- {
147- // These boards don't support hotplug callbacks, so loop on mount() tries
148- while (0 != mount (DEV_USB, FS_FAT, MNT_DEFAULT)) {
149- delay (500 );
150- }
151- (void ) umount (DEV_USB);
152- }
153- else
154- {
155- for ( ; ;) ; // Shouldn't get here unless there's a bug in the test code
154+ (void ) register_hotplug_callback (DEV_USB, usbCallback);
155+ while (false == usbAttached) {
156+ delay (500 );
156157 }
157158 Serial.println (" Thank you!" );
158159 Serial.println ();
159160 }
160- // <-- This isn't a test, just wait for a USB thumb drive
161+ // <-- Wait for a USB thumb drive
161162
162163#if defined(PERFORM_FORMATTING_TESTS)
163164 Serial.println (" The formatting tests you selected can take a while to complete" );
@@ -335,26 +336,35 @@ void setup() {
335336 (void ) umount (deviceName);
336337 // <-- mount() when already mounted test
337338
338- if (TEST_PORTENTA_C33_USB == selectedTest )
339+ if (DEV_USB == deviceName )
339340 {
340- // Register multiple callbacks test -->
341+ // Register multiple callbacks test (hotplug) -->
341342 retVal = register_hotplug_callback (DEV_USB, usbCallback);
342343 if ((-1 != retVal) || (EBUSY != errno))
343344 {
344345 allTestsOk = false ;
345- Serial.println (" [FAIL] Register multiple callbacks test failed" );
346+ Serial.println (" [FAIL] Register multiple callbacks test failed (hotplug) " );
346347 }
347- // <-- Register multiple callbacks test
348+ // <-- Register multiple callbacks test (hotplug)
348349 }
349350
350- // Deregister callback not supported test -->
351+ // Deregister callback not supported test (hotplug) -->
351352 retVal = deregister_hotplug_callback (DEV_USB);
352353 if ((-1 != retVal) || (ENOSYS != errno))
353354 {
354355 allTestsOk = false ;
355356 Serial.println (" [FAIL] Deregister callback not supported test failed" );
356357 }
357- // <-- Deregister callback not supported test
358+ // <-- Deregister callback not supported test (hotplug)
359+
360+ // Deregister callback not supported test (unplug) -->
361+ retVal = deregister_unplug_callback (DEV_USB);
362+ if ((-1 != retVal) || (ENOSYS != errno))
363+ {
364+ allTestsOk = false ;
365+ Serial.println (" [FAIL] Deregister callback not supported test failed" );
366+ }
367+ // <-- Deregister callback not supported test (unplug)
358368
359369 // Remove before persistent storage test -->
360370 (void ) mount (deviceName, FS_FAT, MNT_DEFAULT);
@@ -451,6 +461,31 @@ void setup() {
451461 (void ) umount (deviceName);
452462 // <-- Persistent storage test
453463
464+ // Wait for USB thumb drive removal -->
465+ if (DEV_USB == deviceName)
466+ {
467+ Serial.println ();
468+ Serial.println (" Please remove the thumb drive" );
469+ (void ) register_unplug_callback (DEV_USB, usbCallback2);
470+ while (false == usbDetached) {
471+ delay (500 );
472+ }
473+ Serial.println (" Thank you!" );
474+ }
475+ // <-- Wait for USB thumb drive removal
476+
477+ if (DEV_USB == deviceName)
478+ {
479+ // Register multiple callbacks test (unplug) -->
480+ retVal = register_unplug_callback (DEV_USB, usbCallback);
481+ if ((-1 != retVal) || (EBUSY != errno))
482+ {
483+ allTestsOk = false ;
484+ Serial.println (" [FAIL] Register multiple callbacks test failed (unplug)" );
485+ }
486+ // <-- Register multiple callbacks test (unplug)
487+ }
488+
454489 // Final report -->
455490 Serial.println ();
456491 Serial.println (" Testing complete." );
0 commit comments