Detect USB at power-up (not necessarily connected)? (H7 Plus)

At power-on, I would like to detect whether the OpenMV H7 Plus has been plugged in to a computer. Clearly it can figure this out because the green flashes for the boot loader appear only when connected to USB; they are skipped when powering from battery.

The pyb.USB_VCP().isconnected() method is not what I’m looking for; it reports whether the serial port has been connected… and it never has at the instant of power-on.

Is there a method in some class I missed or a hardware register somewhere I could look at? (I couldn’t see anything useful in pyb.info()… looks like mostly just memory management details.)

Thank you for your thoughts and suggestions!

Context, if helpful:

My application involves saving an image to the H7 Plus’s micro-SD card, then entering standby for 5 minutes (or perhaps longer).

At some point, users will need to collect these images by connecting the H7 Plus to USB and copying them from the micro-SD and then deleting the originals. I’d rather not make them remove the micro-SD card; few computers have readers built-in.

When connected to a computer, it is important not to run my usual code; entering standby would interrupt copying the images. I suspect it could also be problematic to write new images to the filesystem while the micro-SD is being read and written to by the host OS.

While plugged in to USB, I would also like to be able to look for a possible VCP connection to allow optional configuration of device parameters using a simple serial UI.

We need to add this to the API.

Here’s how the bootloader sees it:

Um, you can get get the voltage level of the VBUS pin.

Just make a pin object and use USB_VBUS or PA9 as the name and then check if it’s high.

Mmm… this might not be the best though since there’s no pull down. It might randomly work.

Thank you. The phrase “might randomly work” isn’t too reassuring, but is there a reason the STM’s built-in pull-down couldn’t help?

usb_vbus = Pin("USB_VBUS", Pin.IN, Pin.PULL_DOWN).value()

…having imported Pin from pyb, of course. With only a couple of tests, putting this at the beginning of the script seems to reliably indicate whether there was USB power at boot. (To test it’s 0 on battery, I connect USB later and check the value of usb_vbus printed in a loop.)

By the way, at first I got an error saying that “PA9” is not defined. But naming it USB_VBUS is more readable for this use anyway.

I forgot about the pull down. That definitely fixes the issue.

Great; thank you for confirming. I couldn’t perceive anything different about that pin/port from the the STM32 documentation, but I’m new to OpenMV.