Skip to content

pbio/drv/bluetooth_nxt: Fix reconnecting. - #516

Merged
laurensvalk merged 5 commits into
pybricks:masterfrom
fuzic:nxt-bt-reject
Oct 23, 2026
Merged

laurensvalk merged 5 commits into
pybricks:masterfrom
fuzic:nxt-bt-reject

Conversation

@fuzic

@fuzic fuzic commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Fixes Bluetooth on NXT getting stuck after a disconnect:

  • Dropping a host that doesn't speak Pybricks (e.g. the macOS Bluetooth panel) shut down Bluetooth until a restart. Now it goes back to listening.
  • If the host disconnects while the hub is sending, the BC4 stops taking data. stdout then stalled on all transports, USB included. Now the link is dropped after 1 s.
  • The BC4 was reset after every connection, which made macOS fail to reconnect, and each failure caused another reset. It's now only reset when it stops responding.

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.

@laurensvalk
laurensvalk force-pushed the nxt-bt-reject branch 2 times, most recently from f96dcec to e0c4c09 Compare October 23, 2026 09:36
fuzic added 5 commits October 23, 2026 11:53
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
laurensvalk merged commit df04d72 into pybricks:master Oct 23, 2026
16 checks passed
@laurensvalk

Copy link
Copy Markdown
Member

Merged, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants