Repository navigation
pbio/drv/bluetooth_nxt: Fix reconnecting. - #516
Merged
Merged
Conversation
laurensvalk
force-pushed
the
nxt-bt-reject
branch
2 times, most recently
from
October 23, 2026 09:36
f96dcec to
e0c4c09
Compare
UDP_ISR still reports endpoints whose interrupt is masked (e.g. the bulk OUT endpoint while a received packet waits for the process). The handler picked the lowest one, so a masked endpoint could repeatedly sidetrack handling of the real interrupt (e.g. a completed bulk IN packet), which stayed asserted and re-entered the handler forever, hanging the hub. Select the endpoint from UDP_ISR & UDP_IMR, as the Linux at91_udc driver does. Reproduced by sending stdin bursts while the hub was printing, with the CSR fix from the next commit applied. A watchdog on the system tick caught the UDP interrupt firing 141086 times per second, in the masked bulk OUT branch of the handler. With this change, 15 of 15 rounds completed over three runs.
The CSR helpers write a flag and then poll until the controller reports the change. Some flags are changed back by the controller itself (e.g. TXPKTRDY is cleared once the packet is sent). If an interrupt delays the poll for longer, the loop never sees the flag set and just waits indefinitely. This happened with an I2C sensor in use, since the bit-banged I2C interrupt runs every 26us. While reading the ultrasonic sensor, the hub froze within a few minutes. Recording the interrupted program counter from the system tick showed it spinning in pbdrv_usb_nxt_csr_set_flag() (which was called to set TXPKTRDY for a bulk IN packet). A CSR write only needs a fixed time to take effect (3 UDPCK and 3 MCK cycles), so wait that long instead of polling. This also replaces the loops waiting for endpoint configuration writes, which could hang the same way. With this change, the same sensor test ran 7 times without a freeze (it previously froze within 1-3 runs, 4 times out of 4).
The serial driver drops RFCOMM hosts that don't send a Pybricks message within 5 seconds (e.g. the macOS Bluetooth panel, which opens the port when connecting). On NXT, this disconnect also stopped the Bluetooth process, which was only meant to happen on shutdown. Nothing could connect after that until the hub was restarted. Drop just the connection and go back to listening, as on BTstack. A shutdown still waits for the connection to close.
If the host closes the connection while the hub is sending (e.g. stdout at the end of a program), the BC4 can stop taking data without dropping the stream. The transmission then never finishes, which stalls stdout on all transports and leaves the BC4 unable to accept a new connection. Give up after 1 second, as the USB driver does after 500 ms, and drop the link. The BC4 does not take commands in this state, so go straight to the reset that restarts it instead of closing the connection first.
The BC4 was reset after every connection. This drops the radio link without telling the host, which then fails to connect again for a while (e.g. macOS keeps trying the old link). Each failed attempt ended in another reset, so reconnecting could fail indefinitely. Once the BC4 reports the connection closed, wait for the next one without a reset. Only reset it if it stops responding.
laurensvalk
force-pushed
the
nxt-bt-reject
branch
from
October 23, 2026 09:55
e0c4c09 to
df04d72
Compare
Member
|
Merged, thank you! |
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.
Fixes Bluetooth on NXT getting stuck after a disconnect:
Known limitation: disconnecting mid-transfer still needs a BC4 reset, after which macOS fails the first reconnect attempt. Unsubscribing before closing avoids this.
Tested on hardware with a macOS host.