Detecting Pyboard D and USB connection SOLVED

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Detecting Pyboard D and USB connection SOLVED

Post by pythoncoder » Wed Apr 03, 2019 3:07 pm

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?
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Detecting Pyboard D and USB connection

Post by dhylands » Wed Apr 03, 2019 4:34 pm

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

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Detecting Pyboard D and USB connection

Post by Roberthh » Wed Apr 03, 2019 6:50 pm

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.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Detecting Pyboard D and USB connection

Post by Damien » Thu Apr 04, 2019 12:06 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Detecting Pyboard D and USB connection

Post by pythoncoder » Thu Apr 04, 2019 5:57 am

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.
Peter Hinch
Index to my micropython libraries.

Post Reply