Page 1 of 1

Detecting Pyboard D and USB connection SOLVED

Posted: Wed Apr 03, 2019 3:07 pm
by pythoncoder
Two questions. What is the best way programmatically to detect a Pyboard D as distinct from a V1.x board.

Secondly I want to detect the presence of a USB connection to either the HS or FS port on the 'D' or to the USB port on a Pyboard 1.x. At present I have

Code: Select all

mode = pyb.usb_mode()
    if mode is None:
        # ignore this bit of code
    elif 'VCP' in mode:  # User has enabled VCP in boot.py: establish if there is a physical connection
        usb_conn = pyb.Pin.board.USB_VBUS.value()  # USB is connected to pyb V1.x
        if not usb_conn:
            usb_conn = hasattr(pyb.Pin.board, 'USB_HS_DP') and pyb.Pin.board.USB_HS_DP.value()
        if not usb_conn:
            usb_conn = hasattr(pyb.Pin.board, 'USB_DP') and pyb.Pin.board.USB_DP.value()
Is this right? Is there a better way?

Re: Detecting Pyboard D and USB connection

Posted: Wed Apr 03, 2019 4:34 pm
by dhylands
Each board has a MICROPY_HW_BOARD_NAME macro defined, and this will show up at the beginning of:

Code: Select all

import os
print(os.uname().machine)
(before the word 'with')

On the PYBV11 you can check the USB_VBUS pin (which you're doing in your code).

I'm not sure about the PYBD since mine hasn't arrived yet

Re: Detecting Pyboard D and USB connection

Posted: Wed Apr 03, 2019 6:50 pm
by Roberthh
The Pin name USB_VBUS exists. If I get it's value when connected to USB, it returns 0, and as well, when not connected to USB
On a Pyboard 1.1 it returns 1 when connected to USB,
On PYBD, there are also names as USB_DP, USB_DM, USB_HS_DM and USB_HS_DP. The latter returns always 1, whether connected to USB or not.

Re: Detecting Pyboard D and USB connection

Posted: Thu Apr 04, 2019 12:06 am
by Damien
On PYBD the USB_VUSB and USB_ID pins are not connected through to the USB port (either FS or HS), so these names should be removed from the build (they are just normal IO pins, Y1 and Y2).

If you want to detect if the VCP is enabled and has an active connection on the PC via a serial terminal (ie not just plugged in), you can use pyb.USB_VCP().isconnected(). You can't tell if it's FS or HS.

If you want to detect if the USB is plugged in to a PC and enumerated, but may or may not have a serial terminal connection, then inspect the registers directly:

Code: Select all

USB_HS = 0x40040000
USB_FS = 0x50000000
if machine.mem32[USB_HS] & (1 << 19): # check BSVLD bit
    # have USB HS connection
if machine.mem32[USB_FS] & (1 << 19): # check BSVLD bit
    # have USB FS connection
You can also try the CIDSTS bit (connector ID status), bit number 16.

At the moment only one of HS or FS can be active at any one time, but in the future I hope to support both together.

Re: Detecting Pyboard D and USB connection

Posted: Thu Apr 04, 2019 5:57 am
by pythoncoder
Thank you. I'm porting my micropower uasyncio version: I detect an active USB connection and assume debugging via USB. So I disable micopower features which would otherwise wreck the USB link.

For anyone coming across this in a forum search

Code: Select all

pyb.USB_VCP().isconnected()
also works on the Pyboard 1.x.