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.

Such function is very useful to avoid the H7+ to initiate a deep sleep before user had time to connect to the IDE. However, it seems like the current firmware does not include the USB_VBUS name anymore.

import pyb
from pyb import Pin

usb_check = Pin("USB_VBUS", Pin.IN, Pin.PULL_DOWN)

while(True):
    if(usb_check): pyb.LED(3).on()
    else: pyb.LED(1).on()
    
    pyb.delay(1000)

This code gives me an error message “ValueError: Pin(USB_VBUS) doesn’t exist” on line 4.
Any help on this ?

Please make a ticket on github to re-add this. It’s just a PIN in the CSV file here: micropython/pins.csv at dc4bf943970558609fa38ecdc1646bd3fa57ee66 · openmv/micropython · GitHub

What’s happening is that the - symbol in front of the pin makes it private.

Done. Thank you.

Is there an alternative way to implement this function with the current firmware ?

The thresholding method. It can select just the correct pixels.

You can pass multiple thresholds to be used.

I am not so sure how this is related to my question. Mistake maybe ?

Sorry, was responding to the wrong email thread.

I already did a PR to fix this. Once it’s merged a new development firmware will be available that you can download.