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 [] = {
@@ -554,17 +555,23 @@ static void ble_hs_shutdown_stop_cb(int status, void *arg) {
554
555
555
556
static struct ble_hs_stop_listener ble_hs_shutdown_stop_listener ;
556
557
557
- void mp_bluetooth_nimble_port_shutdown (void ) {
558
+ int mp_bluetooth_nimble_port_shutdown (void ) {
558
559
DEBUG_printf ("mp_bluetooth_nimble_port_shutdown (nimble default)\n" );
559
560
// By default, just call ble_hs_stop directly and wait for the stack to stop.
560
561
561
562
mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_STOPPING ;
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
+ // Stack had not responded (via ble_hs_shutdown_stop_cb)
570
+ return MP_ETIMEDOUT ;
571
+ }
572
+ mp_event_wait_ms (1 );
567
573
}
574
+ return 0 ;
568
575
}
569
576
570
577
#endif // !MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY
@@ -629,13 +636,12 @@ int mp_bluetooth_init(void) {
629
636
630
637
// Run the scheduler while we wait for stack startup.
631
638
// 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 ();
639
+ volatile mp_uint_t start = mp_hal_ticks_ms ();
633
640
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 ) {
641
+ if ((mp_hal_ticks_ms () - start ) > NIMBLE_STARTUP_TIMEOUT ) {
636
642
break ;
637
643
}
638
- mp_event_wait_ms (NIMBLE_STARTUP_TIMEOUT - elapsed );
644
+ mp_event_wait_ms (1 );
639
645
}
640
646
641
647
if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE ) {
@@ -659,10 +665,11 @@ int mp_bluetooth_init(void) {
659
665
return 0 ;
660
666
}
661
667
662
- void mp_bluetooth_deinit (void ) {
668
+ int mp_bluetooth_deinit (void ) {
663
669
DEBUG_printf ("mp_bluetooth_deinit %d\n" , mp_bluetooth_nimble_ble_state );
670
+ int ret = 0 ;
664
671
if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF ) {
665
- return ;
672
+ return 0 ;
666
673
}
667
674
668
675
// Must call ble_hs_stop() in a port-specific way to stop the background
@@ -675,7 +682,7 @@ void mp_bluetooth_deinit(void) {
675
682
676
683
DEBUG_printf ("mp_bluetooth_deinit: starting port shutdown\n" );
677
684
678
- mp_bluetooth_nimble_port_shutdown ();
685
+ ret = mp_bluetooth_nimble_port_shutdown ();
679
686
assert (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF );
680
687
} else {
681
688
mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF ;
@@ -692,6 +699,7 @@ void mp_bluetooth_deinit(void) {
692
699
#endif
693
700
694
701
DEBUG_printf ("mp_bluetooth_deinit: shut down\n" );
702
+ return ret ;
695
703
}
696
704
697
705
bool mp_bluetooth_is_active (void ) {
0 commit comments