Skip to content

Commit 533ace0

Browse files
committed
drv/bluetooth_stm32_cc2640: Work around scanning stopping.
See pybricks/support#1096
1 parent cf36ff1 commit 533ace0

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
- Fixed Move Hub accelerometer not working since v3.3.0b5 ([support#1269]).
1313
- Fixed Bluetooth chip locking up on Technic and City hubs when broadcasting ([support#1095]).
1414
- Fixed potential crash when GC occurs while observing BLE data ([support#1278])
15+
- Fixed Technic Hub and City Hub eventually stopping observing BLE data after
16+
a few minutes ([support#1096]) by implementing an auto-reset workaround.
1517

1618
[support#1095]: https://github.com/pybricks/support/issues/1095
19+
[support#1096]: https://github.com/pybricks/support/issues/1096
1720
[support#1269]: https://github.com/pybricks/support/issues/1269
1821
[support#1278]: https://github.com/pybricks/support/issues/1278
1922

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,20 @@ void pbdrv_bluetooth_stop_broadcasting(void) {
954954
static PT_THREAD(observe_task(struct pt *pt, pbio_task_t *task)) {
955955
PT_BEGIN(pt);
956956

957+
// HACK: Always stop observing first so that this task always does a clean
958+
// restart of observing. This clears the size limited buffer of discovered
959+
// devices. Should be replaced by a generalized scan process that only
960+
// restarts scanning when needed. https://github.com/pybricks/support/issues/1096
961+
if (is_observing) {
962+
PT_WAIT_WHILE(pt, write_xfer_size);
963+
GAP_DeviceDiscoveryCancel();
964+
PT_WAIT_UNTIL(pt, hci_command_status);
965+
if (read_buf[8] == bleSUCCESS) {
966+
PT_WAIT_UNTIL(pt, device_discovery_done);
967+
}
968+
is_observing = false;
969+
}
970+
957971
if (!is_observing) {
958972
PT_WAIT_WHILE(pt, write_xfer_size);
959973
GAP_DeviceDiscoveryRequest(GAP_DEVICE_DISCOVERY_MODE_NONDISCOVERABLE, 0, GAP_FILTER_POLICY_SCAN_ANY_CONNECT_ANY);

pybricks/common/pb_type_ble.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,22 @@ STATIC mp_obj_t pb_module_ble_observe(mp_obj_t self_in, mp_obj_t channel_in) {
429429

430430
// Have not received data yet or timed out.
431431
if (ch_data.rssi == INT8_MIN) {
432+
433+
// HACK: Work around observing eventually stopping on the CC2640 due to
434+
// full buffer of discovered devices. Needs to be fixed at the driver
435+
// level with a scan process that restarts automatically.
436+
// See https://github.com/pybricks/support/issues/1096
437+
#if PBDRV_CONFIG_BLUETOOTH_STM32_CC2640
438+
static uint32_t time_restart = 0;
439+
uint32_t time_now = mp_hal_ticks_ms();
440+
if (time_now - time_restart > OBSERVED_DATA_TIMEOUT_MS) {
441+
pbio_task_t task;
442+
pbdrv_bluetooth_start_observing(&task, handle_observe_event);
443+
pb_module_tools_pbio_task_do_blocking(&task, -1);
444+
time_restart = time_now;
445+
}
446+
#endif
447+
432448
return mp_const_none;
433449
}
434450

0 commit comments

Comments
 (0)