|
60 | 60 | static uint8_t nimble_address_mode = BLE_OWN_ADDR_RANDOM; |
61 | 61 |
|
62 | 62 | #define NIMBLE_STARTUP_TIMEOUT 2000 |
| 63 | +#define NIMBLE_SHUTDOWN_TIMEOUT 500 |
63 | 64 |
|
64 | 65 | // Any BLE_HS_xxx code not in this table will default to MP_EIO. |
65 | 66 | static int8_t ble_hs_err_to_errno_table[] = { |
@@ -562,8 +563,19 @@ void mp_bluetooth_nimble_port_shutdown(void) { |
562 | 563 |
|
563 | 564 | ble_hs_stop(&ble_hs_shutdown_stop_listener, ble_hs_shutdown_stop_cb, NULL); |
564 | 565 |
|
| 566 | + mp_uint_t start = mp_hal_ticks_ms(); |
565 | 567 | while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { |
566 | | - mp_event_wait_indefinite(); |
| 568 | + if ((mp_hal_ticks_ms() - start) > NIMBLE_SHUTDOWN_TIMEOUT) { |
| 569 | + break; |
| 570 | + } |
| 571 | + mp_event_wait_ms(1); |
| 572 | + } |
| 573 | + |
| 574 | + // Failure to correctly shutdown can be detected in application by querying |
| 575 | + // BLE().active(), else as part of shutdown sequece will usually be resolved |
| 576 | + // after reset anyway. |
| 577 | + if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { |
| 578 | + DEBUG_printf("mp_bluetooth_nimble_port_shutdown failed: timeout\n"); |
567 | 579 | } |
568 | 580 | } |
569 | 581 |
|
@@ -629,13 +641,12 @@ int mp_bluetooth_init(void) { |
629 | 641 |
|
630 | 642 | // Run the scheduler while we wait for stack startup. |
631 | 643 | // On non-ringbuffer builds (NimBLE on STM32/Unix) this will also poll the UART and run the event queue. |
632 | | - mp_uint_t timeout_start_ticks_ms = mp_hal_ticks_ms(); |
| 644 | + volatile mp_uint_t start = mp_hal_ticks_ms(); |
633 | 645 | while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { |
634 | | - uint32_t elapsed = mp_hal_ticks_ms() - timeout_start_ticks_ms; |
635 | | - if (elapsed > NIMBLE_STARTUP_TIMEOUT) { |
| 646 | + if ((mp_hal_ticks_ms() - start) > NIMBLE_STARTUP_TIMEOUT) { |
636 | 647 | break; |
637 | 648 | } |
638 | | - mp_event_wait_ms(NIMBLE_STARTUP_TIMEOUT - elapsed); |
| 649 | + mp_event_wait_ms(1); |
639 | 650 | } |
640 | 651 |
|
641 | 652 | if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) { |
|
0 commit comments