@@ -18,8 +18,8 @@ enum TestTypes : uint8_t
1818 TEST_PORTENTA_H7_USB,
1919 TEST_PORTENTA_MACHINE_CONTROL_SDCARD,
2020 TEST_PORTENTA_MACHINE_CONTROL_USB,
21- TEST_OPTA_SDCARD,
22- TEST_OPTA_USB
21+ TEST_OPTA_SDCARD, // Not currently implemented
22+ TEST_OPTA_USB // Logging to thumb drive
2323};
2424
2525// !!! TEST CONFIGURATION !!! -->
@@ -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;
@@ -71,22 +77,24 @@ void setup() {
7177 Serial.println (" Testing started, please wait..." );
7278 Serial.println ();
7379
74- if (( TEST_PORTENTA_MACHINE_CONTROL_SDCARD == selectedTest) || (TEST_OPTA_SDCARD == selectedTest) )
80+ if (TEST_PORTENTA_MACHINE_CONTROL_SDCARD == selectedTest)
7581 {
76- // Machine Control and Opta no SD Card supported test -->
82+ // Machine Control no SD Card supported test -->
7783 retVal = mount (DEV_SDCARD, FS_FAT, MNT_DEFAULT);
7884 if ((-1 != retVal) || (ENOTBLK != errno))
7985 {
80- Serial.println (" [FAIL] Machine Control and Opta no SD Card supported test failed" );
86+ Serial.println (" [FAIL] Machine Control no SD Card supported test failed" );
87+ Serial.println ();
88+ Serial.println (" FAILURE: Finished with errors (see list above for details)" );
8189 }
8290 else
8391 {
8492 Serial.println (" Testing complete." );
8593 Serial.println ();
8694 Serial.println (" SUCCESS: Finished without errors" );
8795 (void ) umount (DEV_SDCARD);
88- for ( ; ; ) ; // Stop testing here
8996 }
97+ for ( ; ; ) ; // Stop testing here
9098 // <-- Machine Control and Opta no SD Card supported test
9199 }
92100
@@ -104,60 +112,55 @@ void setup() {
104112 }
105113 // <-- Register hotplug callback for SD Card test
106114
107- if (TEST_PORTENTA_C33_USB == selectedTest)
115+ // Register unplug callback for SD Card test -->
116+ if (DEV_SDCARD == deviceName)
108117 {
109- // Register nullptr callback test -->
110- retVal = register_hotplug_callback (DEV_USB, nullptr );
111- if ((-1 != retVal) || (EFAULT != errno))
118+ // Using usbCallback2() is fine because it doesn't get registered anyway
119+ retVal = register_unplug_callback (DEV_SDCARD, usbCallback2 );
120+ if ((-1 != retVal) || (ENOTSUP != errno))
112121 {
113122 allTestsOk = false ;
114- Serial.print (" [FAIL] Register nullptr callback test failed" );
123+ Serial.print (" [FAIL] Register unplug callback for SD Card test failed" );
115124 Serial.println ();
116125 }
117- // <-- Register nullptr callback test
118126 }
127+ // <-- Register unplug callback for SD Card test
119128
120- if ((TEST_PORTENTA_H7_USB == selectedTest) || (TEST_PORTENTA_MACHINE_CONTROL_USB == selectedTest) || (TEST_OPTA_USB == selectedTest) )
129+ if (DEV_USB == deviceName )
121130 {
122- // Register unsupported callback test -->
123- retVal = register_hotplug_callback (DEV_USB, usbCallback );
124- if ((-1 != retVal) || (ENOTSUP != errno))
131+ // Register nullptr callback test (hotplug) -->
132+ retVal = register_hotplug_callback (DEV_USB, nullptr );
133+ if ((-1 != retVal) || (EFAULT != errno))
125134 {
126135 allTestsOk = false ;
127- Serial.println (" [FAIL] Register unsupported callback test" );
136+ Serial.print (" [FAIL] Register nullptr callback test failed (hotplug) " );
128137 Serial.println ();
129138 }
130- // <-- Register unsupported callback test
139+ // <-- Register nullptr callback test (hotplug)
140+
141+ // Register nullptr callback test (unplug) -->
142+ retVal = register_unplug_callback (DEV_USB, nullptr );
143+ if ((-1 != retVal) || (EFAULT != errno))
144+ {
145+ allTestsOk = false ;
146+ Serial.print (" [FAIL] Register nullptr callback test failed (unplug)" );
147+ Serial.println ();
148+ }
149+ // <-- Register nullptr callback test (unplug)
131150 }
132151
133- // This isn't a test, just wait for a USB thumb drive -->
152+ // Wait for a USB thumb drive -->
134153 if (DEV_USB == deviceName)
135154 {
136155 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
156+ (void ) register_hotplug_callback (DEV_USB, usbCallback);
157+ while (false == usbAttached) {
158+ delay (500 );
156159 }
157160 Serial.println (" Thank you!" );
158161 Serial.println ();
159162 }
160- // <-- This isn't a test, just wait for a USB thumb drive
163+ // <-- Wait for a USB thumb drive
161164
162165#if defined(PERFORM_FORMATTING_TESTS)
163166 Serial.println (" The formatting tests you selected can take a while to complete" );
@@ -335,26 +338,35 @@ void setup() {
335338 (void ) umount (deviceName);
336339 // <-- mount() when already mounted test
337340
338- if (TEST_PORTENTA_C33_USB == selectedTest )
341+ if (DEV_USB == deviceName )
339342 {
340- // Register multiple callbacks test -->
343+ // Register multiple callbacks test (hotplug) -->
341344 retVal = register_hotplug_callback (DEV_USB, usbCallback);
342345 if ((-1 != retVal) || (EBUSY != errno))
343346 {
344347 allTestsOk = false ;
345- Serial.println (" [FAIL] Register multiple callbacks test failed" );
348+ Serial.println (" [FAIL] Register multiple callbacks test failed (hotplug) " );
346349 }
347- // <-- Register multiple callbacks test
350+ // <-- Register multiple callbacks test (hotplug)
348351 }
349352
350- // Deregister callback not supported test -->
353+ // Deregister callback not supported test (hotplug) -->
351354 retVal = deregister_hotplug_callback (DEV_USB);
352355 if ((-1 != retVal) || (ENOSYS != errno))
353356 {
354357 allTestsOk = false ;
355- Serial.println (" [FAIL] Deregister callback not supported test failed" );
358+ Serial.println (" [FAIL] Deregister callback not supported test failed (hotplug) " );
356359 }
357- // <-- Deregister callback not supported test
360+ // <-- Deregister callback not supported test (hotplug)
361+
362+ // Deregister callback not supported test (unplug) -->
363+ retVal = deregister_unplug_callback (DEV_USB);
364+ if ((-1 != retVal) || (ENOSYS != errno))
365+ {
366+ allTestsOk = false ;
367+ Serial.println (" [FAIL] Deregister callback not supported test failed (unplug)" );
368+ }
369+ // <-- Deregister callback not supported test (unplug)
358370
359371 // Remove before persistent storage test -->
360372 (void ) mount (deviceName, FS_FAT, MNT_DEFAULT);
@@ -451,6 +463,35 @@ void setup() {
451463 (void ) umount (deviceName);
452464 // <-- Persistent storage test
453465
466+ // These tests can't be performed on the Opta because we log to USB
467+ if (TEST_OPTA_USB != selectedTest)
468+ {
469+ // Wait for USB thumb drive removal -->
470+ if (DEV_USB == deviceName)
471+ {
472+ Serial.println ();
473+ Serial.println (" Please remove the thumb drive" );
474+ (void ) register_unplug_callback (DEV_USB, usbCallback2);
475+ while (false == usbDetached) {
476+ delay (500 );
477+ }
478+ Serial.println (" Thank you!" );
479+ }
480+ // <-- Wait for USB thumb drive removal
481+
482+ if (DEV_USB == deviceName)
483+ {
484+ // Register multiple callbacks test (unplug) -->
485+ retVal = register_unplug_callback (DEV_USB, usbCallback2);
486+ if ((-1 != retVal) || (EBUSY != errno))
487+ {
488+ allTestsOk = false ;
489+ Serial.println (" [FAIL] Register multiple callbacks test failed (unplug)" );
490+ }
491+ // <-- Register multiple callbacks test (unplug)
492+ }
493+ }
494+
454495 // Final report -->
455496 Serial.println ();
456497 Serial.println (" Testing complete." );
@@ -468,7 +509,7 @@ void setup() {
468509 // Opta final report -->
469510 if (TEST_OPTA_USB == selectedTest)
470511 {
471- (void ) mount (deviceName , FS_FAT, MNT_DEFAULT);
512+ (void ) mount (DEV_USB , FS_FAT, MNT_DEFAULT);
472513 FILE *logFile = fopen (" /usb/testlog.txt" , " w" );
473514 if (true == allTestsOk)
474515 {
@@ -480,7 +521,7 @@ void setup() {
480521 fprintf (logFile, " FAILURE: Finished with errors" );
481522 fclose (logFile);
482523 }
483- (void ) umount (deviceName );
524+ (void ) umount (DEV_USB );
484525 }
485526 // <--
486527}
0 commit comments