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

General • Minimal C code to detect RP2040 or RP2350 plugged to a USB host

$
0
0
This is an update to the thread viewtopic.php?t=369119 but it's locked.

I was able to solve the issue of detecting when a USB host is connected using this code for the RP2040. I believe this is the simplest version possible:

Code:

usb_hw->phy_direct_override = USB_USBPHY_DIRECT_OVERRIDE_DP_PULLUP_EN_OVERRIDE_EN_BITS;usb_hw->phy_direct = USB_USBPHY_DIRECT_DP_PULLUP_EN_BITS;while (true){    if ((usb_hw->phy_direct & USB_USBPHY_DIRECT_RX_DP_BITS) != USB_USBPHY_DIRECT_RX_DP_BITS)    {        printf("Do my stuff when USB connected\n");     }}
But it did not work for RP2350. For RP2350 I had to also force the USB in device mode:

Code:

// Enable the USB controller in device mode.// This was not necessary in the RP2040, but it is in the RP2350Ausb_hw->main_ctrl = USB_MAIN_CTRL_CONTROLLER_EN_BITS;
My custom board cannot distinguish if the power comes from USB VBUS or directly from the +5V in the board, so I think there is no other alternative.

Statistics: Posted by logronoide — Fri Mar 14, 2025 11:39 am



Viewing all articles
Browse latest Browse all 8013

Trending Articles