@@ -480,7 +480,8 @@ int mountOrFormat(const enum StorageDevices deviceName,
480480 const enum FileSystems fileSystem,
481481 const enum ActionTypes mountOrFormat)
482482{
483- // Determine if we're running on Machine Control or not on the first call to mount() or mkfs() -->
483+ // Determine if we're running on Machine Control or not on the first call to mount(), mkfs(), or
484+ // register_hotplug_callback() -->
484485 if (false == hasMountedBefore)
485486 {
486487 hasMountedBefore = true ;
@@ -599,18 +600,31 @@ int register_hotplug_callback(const enum StorageDevices deviceName, void (* cons
599600 case DEV_USB:
600601 { // Curly braces necessary to keep new variables inside the case statement
601602
603+ // Determine if we're running on Machine Control or not on the first call to mount(), mkfs(), or
604+ // register_hotplug_callback() -->
605+ if (false == hasMountedBefore)
606+ {
607+ hasMountedBefore = true ;
608+ if (BOARD_MACHINE_CONTROL == detectBoardType ())
609+ {
610+ runningOnMachineControl = true ;
611+ }
612+ }
613+ // <--
614+ #if defined(ARDUINO_PORTENTA_H7_M7)
615+ if (true == runningOnMachineControl)
616+ {
617+ // We need to apply power manually to the female USB A connector on the Machine Control
618+ mbed::DigitalOut enablePower (PB_14, 0 );
619+ }
620+ #endif
602621 // A USB mass storage device is already mounted at that mount point, or
603622 // registered for the hotplug event
604623 if (nullptr != usb.device )
605624 {
606625 errno = EBUSY;
607626 return -1 ;
608627 }
609- #if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
610- // There is no support for callbacks in the USBHostMSD class on this platform
611- errno = ENOTSUP;
612- return -1 ;
613- #else
614628 // We must create a USBHostMSD object to attach the callback to, but we
615629 // don't create a file system object because we don't fully mount() anything
616630 USBHostMSD *usbHostDevice = nullptr ;
@@ -620,10 +634,15 @@ int register_hotplug_callback(const enum StorageDevices deviceName, void (* cons
620634 errno = ENOTBLK;
621635 return -1 ;
622636 }
637+ #if ((defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)))
638+ // The Arduino_USBHostMbed5 library doesn't initialize the USB stack until
639+ // the first connect() call because of an older bugfix (commit 72d0aa6), so
640+ // we perform one connect() here to initialize the stack
641+ usbHostDevice->connect ();
642+ #endif
623643 if (false == (usbHostDevice->attach_detected_callback (callbackFunction)))
624644 {
625- delete usbHostDevice; // Ok to delete, because this isn't on the H7
626- usbHostDevice = nullptr ;
645+ deleteDevice (DEV_USB, &usb);
627646 errno = EINVAL;
628647 return -1 ;
629648 }
@@ -632,7 +651,7 @@ int register_hotplug_callback(const enum StorageDevices deviceName, void (* cons
632651 // Prevent multiple registrations
633652 hotplugCallbackAlreadyRegistered = true ;
634653 return 0 ;
635- # endif
654+
636655 } // Curly braces necessary to keep new variables inside the case statement
637656 default :
638657 errno = ENOTBLK;
0 commit comments