Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8026

General • Re: Minimal C code to detect RP2040 plugged to a USB host?

$
0
0
You can use the USB PHY override bits, which lets you access the two USB data pins somewhat like GPIOs.

I use the following in a similar bootloader to yours, except that my USB setup is reversed and I'm looking for a USB device rather than a USB host to be plugged in:

Code:

    // Minimal enable of the USB PHY, just so we can tell if something    // is plugged in to the daughterboard socket.    // We can tell this as a USB device will pull up the DP pin,    // but we need to enable the pull-downs on DP/DM to avoid stray    // pickup.    usb_hw->phy_direct_override =        USB_USBPHY_DIRECT_OVERRIDE_DM_PULLDN_EN_OVERRIDE_EN_BITS        | USB_USBPHY_DIRECT_OVERRIDE_DP_PULLDN_EN_OVERRIDE_EN_BITS;    usb_hw->phy_direct = USB_USBPHY_DIRECT_DM_PULLDN_EN_BITS        | USB_USBPHY_DIRECT_DP_PULLDN_EN_BITS;             for (unsigned u = 0; u < 100; u++)    {        if ((usb_hw->phy_direct            & (USB_USBPHY_DIRECT_RX_DP_BITS | USB_USBPHY_DIRECT_RX_DM_BITS)            )            ==  USB_USBPHY_DIRECT_RX_DP_BITS)        {            printf("Daughterboard detected\n");            OUR_UID = (OUR_UID & 0x00ffffff) | (UID_UBHU_ENT << 24);            break;        }        busy_wait_ms(1);    // Allow up to 100ms before giving up.    }
In your case you are looking for a host attached, so I think you need to enable the pull-up on D+ (only) rather than pull-downs, wait for it to actually go high, then look for the host driving it low again.

I'm not sure how robust that's going to be: the host will probably give up doing anything after a short while, given that you've given it the device detect signature then not responded at all to the data it sends you. But if it's powered via this port then you can just unplug and replug to try again; if it's externally powered then I'd be worried about falsely detecting the host and getting stuck in the bootrom with no way out.

Statistics: Posted by arg001 — Mon Apr 15, 2024 6:50 pm



Viewing all articles
Browse latest Browse all 8026

Trending Articles