@@ -216,15 +216,13 @@ static void mp_bt_zephyr_remove_connection(uint8_t conn_handle) {
216216}
217217
218218static void mp_bt_zephyr_connected (struct bt_conn * conn , uint8_t err ) {
219- // CRITICAL: This printf MUST appear if callback is being called by Zephyr
220- mp_printf (& mp_plat_print , ">>> mp_bt_zephyr_connected CALLED: conn=%p err=%u state=%d\n" ,
221- conn , err , mp_bluetooth_zephyr_ble_state );
219+ DEBUG_printf ("mp_bt_zephyr_connected: conn=%p err=%u state=%d\n" , conn , err , mp_bluetooth_zephyr_ble_state );
222220
223221 // Safety check: only process if BLE is fully active and initialized
224222 if (mp_bluetooth_zephyr_ble_state != MP_BLUETOOTH_ZEPHYR_BLE_STATE_ACTIVE
225223 || MP_STATE_PORT (bluetooth_zephyr_root_pointers ) == NULL ) {
226- mp_printf ( & mp_plat_print , ">>> Connection callback ignored - BLE not active (state=%d)\n" ,
227- mp_bluetooth_zephyr_ble_state );
224+ DEBUG_printf ( " Connection callback ignored - BLE not active (state=%d)\n" ,
225+ mp_bluetooth_zephyr_ble_state );
228226 return ;
229227 }
230228
@@ -275,16 +273,14 @@ static void mp_bt_zephyr_connected(struct bt_conn *conn, uint8_t err) {
275273}
276274
277275static void mp_bt_zephyr_disconnected (struct bt_conn * conn , uint8_t reason ) {
278- // CRITICAL: This printf MUST appear if callback is being called by Zephyr
279- mp_printf (& mp_plat_print , ">>> mp_bt_zephyr_disconnected CALLED: conn=%p reason=%u state=%d\n" ,
280- conn , reason , mp_bluetooth_zephyr_ble_state );
276+ DEBUG_printf ("mp_bt_zephyr_disconnected: conn=%p reason=%u state=%d\n" , conn , reason , mp_bluetooth_zephyr_ble_state );
281277
282278 // Safety check: only process if BLE is fully active and initialized
283279 // Ignore callbacks during deinit (SUSPENDED state) to prevent double-unref race
284280 if (mp_bluetooth_zephyr_ble_state != MP_BLUETOOTH_ZEPHYR_BLE_STATE_ACTIVE
285281 || MP_STATE_PORT (bluetooth_zephyr_root_pointers ) == NULL ) {
286- mp_printf ( & mp_plat_print , ">>> Disconnect callback ignored - BLE not active (state=%d)\n" ,
287- mp_bluetooth_zephyr_ble_state );
282+ DEBUG_printf ( " Disconnect callback ignored - BLE not active (state=%d)\n" ,
283+ mp_bluetooth_zephyr_ble_state );
288284 return ;
289285 }
290286
@@ -333,12 +329,9 @@ static void mp_bluetooth_zephyr_bt_ready_cb(int err) {
333329}
334330
335331#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
336- // Debug counter for advertising report investigation
337- static int scan_cb_recv_count = 0 ;
338332
339333void gap_scan_cb_recv (const struct bt_le_scan_recv_info * info , struct net_buf_simple * buf ) {
340- scan_cb_recv_count ++ ;
341- DEBUG_printf ("gap_scan_cb_recv: adv_type=%d [count=%d]\n" , info -> adv_type , scan_cb_recv_count );
334+ DEBUG_printf ("gap_scan_cb_recv: adv_type=%d\n" , info -> adv_type );
342335
343336 if (!mp_bluetooth_is_active ()) {
344337 DEBUG_printf (" --> BLE not active, skipping\n" );
@@ -414,8 +407,8 @@ int mp_bluetooth_init(void) {
414407 #endif
415408
416409 bt_conn_cb_register (& mp_bt_zephyr_conn_callbacks );
417- mp_printf ( & mp_plat_print , ">>> Registered connection callbacks: connected=%p disconnected=%p\n" ,
418- mp_bt_zephyr_conn_callbacks .connected , mp_bt_zephyr_conn_callbacks .disconnected );
410+ DEBUG_printf ( " Registered connection callbacks: connected=%p disconnected=%p\n" ,
411+ mp_bt_zephyr_conn_callbacks .connected , mp_bt_zephyr_conn_callbacks .disconnected );
419412
420413 // Initialize HCI controller (CYW43 BT via WEAK override from pico-sdk)
421414 // This must be called before bt_enable()
@@ -532,32 +525,28 @@ int mp_bluetooth_deinit(void) {
532525
533526 // Stop advertising before unregistering callbacks
534527 // This cleans up any advertising connections (BT_CONN_ADV_CONNECTABLE state)
535- mp_printf ( & mp_plat_print , ">>> Deinit: stopping advertising\n" );
528+ DEBUG_printf ( "Stopping advertising\n" );
536529 mp_bluetooth_gap_advertise_stop ();
537- mp_printf (& mp_plat_print , ">>> Deinit: advertising stopped\n" );
538530
539531 #if CONFIG_BT_GATT_DYNAMIC_DB
540- mp_printf ( & mp_plat_print , ">>> Deinit: unregistering GATT services\n" );
532+ DEBUG_printf ( "Unregistering GATT services\n" );
541533 for (size_t i = 0 ; i < MP_STATE_PORT (bluetooth_zephyr_root_pointers )-> n_services ; ++ i ) {
542534 bt_gatt_service_unregister (MP_STATE_PORT (bluetooth_zephyr_root_pointers )-> services [i ]);
543535 MP_STATE_PORT (bluetooth_zephyr_root_pointers )-> services [i ] = NULL ;
544536 }
545- mp_printf (& mp_plat_print , ">>> Deinit: GATT services unregistered\n" );
546537 #endif
547538
548539 #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
549- mp_printf ( & mp_plat_print , ">>> Deinit: stopping scan\n" );
540+ DEBUG_printf ( "Stopping scan\n" );
550541 mp_bluetooth_gap_scan_stop ();
551542 bt_le_scan_cb_unregister (& mp_bluetooth_zephyr_gap_scan_cb_struct );
552- mp_printf (& mp_plat_print , ">>> Deinit: scan stopped\n" );
553543 #endif
554544
555545 // CRITICAL: Unregister connection callbacks before bt_disable()
556546 // bt_disable() does NOT clear callbacks - they persist and cause double-registration
557547 // on subsequent bt_enable() cycles, leading to crashes
558- mp_printf ( & mp_plat_print , ">>> Deinit: unregistering connection callbacks\n" );
548+ DEBUG_printf ( "Unregistering connection callbacks\n" );
559549 bt_conn_cb_unregister (& mp_bt_zephyr_conn_callbacks );
560- mp_printf (& mp_plat_print , ">>> Deinit: connection callbacks unregistered\n" );
561550
562551 // Use Zephyr's official bt_disable() API to shut down the BLE stack
563552 // This automatically:
@@ -566,11 +555,10 @@ int mp_bluetooth_deinit(void) {
566555 // - Sends HCI RESET to controller
567556 // - Closes HCI resources
568557 // - Clears identity and keys
569- mp_printf ( & mp_plat_print , ">>> Deinit: calling bt_disable()\n" );
558+ DEBUG_printf ( "Calling bt_disable()\n" );
570559 int err = bt_disable ();
571- mp_printf (& mp_plat_print , ">>> Deinit: bt_disable() returned %d\n" , err );
572560 if (err != 0 ) {
573- mp_printf ( & mp_plat_print , ">>> Deinit: bt_disable() FAILED with error %d\n" , err );
561+ DEBUG_printf ( " bt_disable() failed with error %d\n" , err );
574562 // Don't fail deinit - just log and continue with cleanup
575563 }
576564
@@ -686,7 +674,11 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons
686674 mp_bt_zephyr_next_conn = m_new0 (mp_bt_zephyr_conn_t , 1 );
687675 mp_obj_list_append (MP_STATE_PORT (bluetooth_zephyr_root_pointers )-> objs_list , MP_OBJ_FROM_PTR (mp_bt_zephyr_next_conn ));
688676
689- return bt_err_to_errno (bt_le_adv_start (& param , bt_ad_data , bt_ad_len , bt_sd_data , bt_sd_len ));
677+ DEBUG_printf ("Starting advertising: connectable=%d options=0x%x\n" , connectable , param .options );
678+ int ret = bt_le_adv_start (& param , bt_ad_data , bt_ad_len , bt_sd_data , bt_sd_len );
679+ DEBUG_printf ("bt_le_adv_start returned: %d\n" , ret );
680+
681+ return bt_err_to_errno (ret );
690682}
691683
692684void mp_bluetooth_gap_advertise_stop (void ) {
0 commit comments