(RP2040) tud_connected() and tud_mounted() stay true after disconnected
Operating System
Linux
Board
Raspberry Pi Pico
Firmware
#include "Adafruit_TinyUSB.h"
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial1.begin(115200); //for debug
}
void loop()
{
digitalWrite(LED_BUILTIN, tud_connected());
}
What happened ?
tud_connected() and tud_mounted() correctly return FALSE before USB is connected, and TRUE when connected. However, they continue to return TRUE indefinitely after USB is no longer connected.
How to reproduce ?
- power up Pi Pico from an external source (I feed VBUS thru a diode)
- (LED stays off, tud_connected() returning FALSE)
- Connect Pico to USB host
- (LED turns on, tud_connected() returning TRUE)
- Disconnect USB
- (LED stays on, tud_connected() keeps returning true)
- Scratch head
- if USB is reconnected, LED will flicker off a couple of times, then stay on
- Scratch head some more
Debug Log as txt file (LOG/CFG_TUSB_DEBUG=2)
Screenshots
No response
I have checked existing issues, dicussion and documentation
- [X] I confirm I have checked existing issues, dicussion and documentation.
I'm seeing the same behaviour here too. I'm trying to figure out why sending MIDI notes to the host randomly stops working until I unplug and re-plug the USB, but as in the issue report, once connected tud_connected() and tud_mounted() continue to report that they are connected even when the USB cable is unplugged.
On the PI Pico, depending on your use case, this issue may be worked around by sensing VBUS instead; GPIO 24 is hardwired for that on the Pico. (WL_GPIO 2 on the PicoW) This will only work if VBUS is fed exclusively by USB though.
On the PI Pico, depending on your use case, this issue may be worked around by sensing VBUS instead; GPIO 24 is hardwired for that on the Pico. (WL_GPIO 2 on the PicoW) This will only work if VBUS is fed exclusively by USB though.
Good to know; but in my case the +v USB pin is cut and the Pico (actually Seeed XIAO RP2040) is externally powered so this wouldn't be a suitable workaround.
Without VBUS there is no way to do disconnection detection.
https://github.com/hathach/tinyusb/blob/a435befcdeb6bbd40cf3ba342756f8d73f031957/src/portable/raspberrypi/rp2040/dcd_rp2040.c#L44
You should use tud_ready()