espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.33k stars 7.36k forks source link

Bluedroid disable: Prohibited load error #5223

Closed gabyft45ol closed 3 years ago

gabyft45ol commented 3 years ago

Hardware:

Board: ESP32 PICO-D4 Core Installation version: 2.0.0 alpha1 (installed from Board Manager) IDE name: Arduino IDE Upload Speed: 921600 Computer OS: Windows 10

Description:

Hello everyone,

I'm developping an app that allows to receive music from a Bluetooth device or a webradio. As Bluetooth and Wifi use the same antenna, I want to deinit one before enabling the second one.

I have some issue with the disable function for bluedroid: a prohibited load error message appears all the time. I have simplified my app to the following code, and the procedure to replicate the error is the following:

I will be really grateful if someone can help me.

Sketch:

#include "nvs.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_bt_defs.h"
#include "esp_gap_bt_api.h"
#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"

esp_err_t err;

// Button that allows to init/deinit bluetooth
#define BTN_SOURCE    38
void btn_source_interrupt();

int m_source;
bool new_source = false;

void setup() {

  // init the serial communication
  Serial.begin(115200);

  // Buttons set as inputs:
  pinMode(BTN_SOURCE,    INPUT);
  attachInterrupt(digitalPinToInterrupt(BTN_SOURCE),    btn_source_interrupt,    RISING);

  // Init bluetooth
  m_source = 1;
  bluetooth_init();
}

void loop() {

  if(new_source){
    Serial.println("source has changed");
    switch(m_source){
      case 0:
        bluetooth_deinit();
        break;
      case 1:
        bluetooth_init();
        break;
    }
    new_source = false;
  }
}

void IRAM_ATTR btn_source_interrupt() {
  new_source = true;
  if(m_source == 1){
    m_source = 0;
  }
  else{
    m_source++;
  }
}

void bluetooth_init(){
  Serial.println("---------- BLuetooth init ----------");

  // init the flash Memory
  err = nvs_flash_init();
  if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      err = nvs_flash_init();
  }
  ESP_ERROR_CHECK(err);

  err = btStart();
  if(err == ESP_OK){ Serial.println("--- Bluetooth driver initialized");}
  else{Serial.println("--- Failed to initialize Bluetooth driver");}

  esp_bluedroid_status_t bt_stack_status = esp_bluedroid_get_status();

  if(bt_stack_status == ESP_BLUEDROID_STATUS_UNINITIALIZED){
    err = esp_bluedroid_init();
    if(err == ESP_OK){ Serial.println("--- Bluedroid initialized");}
    else{Serial.println("--- Failed to initialize Bluedroid");}
  }

  esp_bluedroid_status_t bt_status = esp_bluedroid_get_status();

  // Enable Bluedroid
  if(bt_status != ESP_BLUEDROID_STATUS_ENABLED){
    err = esp_bluedroid_enable();
    if(err == ESP_OK){ Serial.println("--- Bluedroid enabled");}
    else{Serial.println("--- Failed to enable Bluedroid");}
  }

  // Set the name of the bluetooth device
  err = esp_bt_dev_set_device_name("Radio");
  if(err == ESP_OK){ Serial.println("--- Device name set");}
  else{Serial.println("--- Failed to set the device name");}

  // initialize AVRCP controller FIRST!!
  err = esp_avrc_ct_init();
  if(err == ESP_OK){ Serial.println("--- Controller initialized");}
  else{Serial.println("--- Failed to initialize controller");}

  err = esp_avrc_ct_register_callback(avrc_callback);
  if(err == ESP_OK){ Serial.println("--- avrc callback registered");}
  else{Serial.println("--- Failed to register avrc callback");}

  // this sets up the audio receive
  err = esp_a2d_sink_init();
  if(err == ESP_OK){ Serial.println("--- a2d initialized");}
  else{Serial.println("--- Failed to initialize the a2d");}

  err = esp_a2d_register_callback(a2d_callback);
  if(err == ESP_OK){ Serial.println("--- a2d callback registered");}
  else{ Serial.println("--- Failed to registered the a2d callback");}

  err = esp_a2d_sink_register_data_callback(i2s_callback);
  if(err == ESP_OK){ Serial.println("--- a2d i2s registered");}
  else{ Serial.println("--- Failed to registered the i2s_stop callback");}

  // set discoverable and connectable mode, wait to be connected
  err = esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  if(err == ESP_OK){ Serial.println("--- Device is discoverable and connectable");}
  else{ Serial.println("--- Failed to set the scan mode");}

  Serial.println("--------- Bluetooth ready --------");
}

void a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t*param){}

void avrc_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param) {}

void i2s_callback(const uint8_t *data, uint32_t len){}

void bluetooth_deinit(){
    Serial.println("--------- Bluetooth deinit ---------");
/*
  err = esp_a2d_sink_disconnect(bt_address);
  if(err == ESP_OK){ Serial.println("--- sink disconnected");}
  else{Serial.println("--- Failed to disconnect sink");}
*/

  err = esp_avrc_ct_deinit();
  if(err == ESP_OK){ Serial.println("--- avrc deinitalized");}
  else{Serial.println("--- Failed to deinitalize avrc");}

  err = esp_a2d_sink_deinit();
  if(err == ESP_OK){ Serial.println("--- a2d deinitalized");}
  else{Serial.println("--- Failed to deinitalize a2d");}

  err = esp_bluedroid_disable();
  if(err == ESP_OK){ Serial.println("--- bluedroid disabled");}
  else{Serial.println("--- Failed to disable bluedroid");}

  err = esp_bluedroid_deinit();
  if(err == ESP_OK){ Serial.println("--- bluedroid deinitalized");}
  else{Serial.println("--- Failed to deinitalize bluedroid");}

  err = btStop();
  if(err == ESP_OK){ Serial.println("--- bluetooth driver deinitalized");}
  else{Serial.println("--- Failed to deinitalize bluetooth driver");}

}

Debug Messages:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
08:53:20.506 -> 
08:53:20.506 -> Core  0 register dump:
08:53:20.506 -> PC      : 0x40118130  PS      : 0x00060530  A0      : 0x8010d1ae  A1      : 0x3ffd4f80  
08:53:20.552 -> A2      : 0x3ffdfd68  A3      : 0x00000001  A4      : 0x80094a1c  A5      : 0x3ffd2720  
08:53:20.552 -> A6      : 0x00000000  A7      : 0x3ffd2e60  A8      : 0x00000000  A9      : 0x80000000  
08:53:20.552 -> A10     : 0x00000001  A11     : 0x3ffd2b34  A12     : 0x3ffdfd20  A13     : 0x3ffd2700  
08:53:20.552 -> A14     : 0x00000001  A15     : 0x3ffd2e60  SAR     : 0x00000009  EXCCAUSE: 0x0000001c  
08:53:20.552 -> EXCVADDR: 0x0000000c  LBEG    : 0x4008f7ec  LEND    : 0x4008f7f7  LCOUNT  : 0xffffffff  
08:53:20.552 -> 
08:53:20.552 -> 
08:53:20.552 -> Backtrace:0x4011812d:0x3ffd4f800x4010d1ab:0x3ffd4fa0 0x4010e4df:0x3ffd4fc0 

Decoded version:

[Test.zip](https://github.com/espressif/arduino-esp32/files/6565855/Test.zip)

PC: 0x40118130: btc_a2dp_cb_handler at ../esp-idf/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_av.c line 1509
EXCVADDR: 0x0000000c

Decoding stack results
0x4011812d: btc_a2dp_cb_handler at ../esp-idf/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_av.c line 1509
0x4010d1ab: btc_thread_handler at ../esp-idf/components/bt/common/btc/core/btc_task.c line 184
0x4010e4df: osi_thread_run at ../esp-idf/components/bt/common/osi/thread.c line 67
stale[bot] commented 3 years ago

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 3 years ago

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.