Fix USB VCP lockup on disconnect (issue #11348)#11351
Open
daijoubu wants to merge 6 commits intoiNavFlight:maintenance-9.xfrom
Open
Fix USB VCP lockup on disconnect (issue #11348)#11351daijoubu wants to merge 6 commits intoiNavFlight:maintenance-9.xfrom
daijoubu wants to merge 6 commits intoiNavFlight:maintenance-9.xfrom
Conversation
Two issues fixed: 1. serial.c: serialIsConnected() was not returning the result from the vtable isConnected call, always returning true regardless of actual connection state. 2. vcpf4/usbd_cdc_vcp.c: VCP_DataTx() had infinite loops waiting for TX state and buffer space. If USB was disconnected, these loops would never exit, locking up the flight controller. Added 50ms timeout to both blocking loops. On timeout, returns USBD_FAIL and CDC_Send_DATA returns 0 to indicate failure. Tested on SPEEDYBEEF405WING - FC continues operating normally after USB disconnect instead of locking up. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Same fix as STM32F4: added 50ms timeout to blocking loops in CDC_Send_DATA() that wait for TX state and buffer space. On timeout, returns partial byte count (or 0) instead of blocking forever when USB is disconnected. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The previous implementation incorrectly subtracted RX buffer usage from TX buffer size. Fixed to properly calculate free space in the outbound (APP_Rx) circular buffer using the correct pointer math. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Register callback for CDC control line state changes to track when the host has actually opened the COM port (DTR signal). This provides more reliable connection detection than just checking USB enumeration state. usbVcpIsConnected() now returns true only when: - USB is connected (hardware) - USB is configured (enumerated) - Host has opened COM port (DTR high) This helps prevent writes to USB when no host application is listening, which can cause buffer buildup and delays. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
On boards without VBUS sensing, USB hardware disconnect may not be detected. Treat USB suspend event as a disconnect to prevent blocking on writes when cable is unplugged. When suspend occurs, set bDeviceState = UNCONNECTED so that usbIsConnected() returns false and writes are skipped. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Default cdcPortOpened to true so MSP works immediately on connection. DTR state will be updated when host explicitly sets/clears it. Previous default of false broke MSP on tools that don't assert DTR. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #11348 - Flight controller freeze on USB disconnect while MSP is active.
6 crashes reported by user on MATEK F405 Wing V2 when disconnecting Configurator with LOG_DEBUG enabled.
Root Cause
USB VCP disconnection was not detected properly, causing:
Changes
Testing
Tested on SPEEDYBEEF405WING (STM32F4) - rapid USB connect/disconnect no longer causes freeze.
Files Changed
src/main/drivers/serial.c- FIX 1: serialIsConnected() return statementsrc/main/drivers/serial_usb_vcp.c- DTR trackingsrc/main/vcp_hal/usbd_cdc_interface.c- STM32F7/H7 fixessrc/main/vcpf4/usbd_cdc_vcp.c- STM32F4 fixessrc/main/vcpf4/usbd_usr.c- suspend detection