espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.51k stars 7.26k forks source link

BLE Start Advertising Causes -> Core 1 panic'ed (LoadProhibited) (IDFGH-2409) #4523

Closed jonhunterking66 closed 4 years ago

jonhunterking66 commented 4 years ago

Environment

Problem Description

I have two tasks running initialized in my main. One is a "main task" that contains a switch statement. The various cases will eventually contain binary semaphores to trigger various task functions.

The other task is BLE task that initializes BLE then stops advertising after. In its while loop it waits for a semaphore then starts advertising when it receives one.

Expected Behavior

BLE will initialize properly then proceed to stop advertising. When the main task steps into the case that gives a binary semaphore, the BLE task waiting for the semaphore will trigger and start advertising.

Actual Behavior

BLE does initialize properly and successfully stops scanning. However, when the main task enters the case that gives the binary semaphore to the ble task and start advertising is called, I get a guru mediation error.

Code to reproduce this issue

** I am not putting in all the code to keep it less spamy. Let me know if you want something else!

MAIN CODE

xSemaphoreHandle bleSemaphore;

void app_main()
{
  oto oto1; //struct to hold config and pass to functions
  esp_log_level_set(TAG, ESP_LOG_DEBUG);
  //wifiInit();
  //bleInit();
  //vTaskDelay(100/portTICK_PERIOD_MS);
  bleSemaphore = xSemaphoreCreateBinary();
  xTaskCreate(&main_task, "MAIN", 1024 *3, &oto1, 2, NULL);
  xTaskCreate(&ble_task, "BLE", 2048, &oto1, 1, NULL);
  //xTaskCreate(&wifi_task, "WIFI", 1024 * 3, NULL, 5, NULL);
  //xTaskCreate(&ble_task, "BLE", 2048, &oto1, 3, NULL);
}

void main_task(void * params){
    oto *oto1 = (oto *) params;
    oto1 -> state = 0;
    while(1){
        switch ((oto1->state))
        {
        case Sleep: 
            printf("Case 0\n");
            oto1->state = 1;
            break;
        case BleAdvertisement: 
            printf("Case 1\n");
            xSemaphoreGive(bleSemaphore);
            oto1->state = 2;            
            break;
        case BleConnect: 
            printf("Case 2\n");
            oto1->state = 3;
            break; 
        case Wifi: 
            printf("Case 3\n");
            oto1->state = 4;
            break;   
        case Run: 
            printf("Case 4\n");
            oto1->state = 3;
            break;                        
        default:
            break;
        }
        vTaskDelay(1000/portTICK_PERIOD_MS);
    }
}

BLE CODE


void ble_task(void * params){
    oto *oto1 = (oto *) params;
    uint32_t i = 0;
    uint32_t hex;
    bleInit();
    vTaskDelay(100 / portTICK_PERIOD_MS);
    esp_ble_gap_stop_advertising();
    while (1) 
    {
        xSemaphoreTake(bleSemaphore, portMAX_DELAY);
        printf("In BLE Task\n");
        esp_ble_gap_start_advertising(ESP_BT_MODE_BLE);
        //vTaskDelay(100 / portTICK_PERIOD_MS);
        /*hex = inttohex(i);
        ESP_LOGI(GATTS_TAG, "SENSOR %x", hex);
        ble_indicateTwo(hex);
        vTaskDelay(100 / portTICK_PERIOD_MS);
        i++;
        if (i == 360){
            i = 0;
        }*/

    }

}

void bleInit(){
    esp_err_t ret;

    // Initialize NVS.
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK( ret );

    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
        return;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
        return;
    }
    ret = esp_bluedroid_init();
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
        return;
    }
    ret = esp_bluedroid_enable();
    if (ret) {
        ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
        return;
    }

    ret = esp_ble_gatts_register_callback(gatts_event_handler);
    if (ret){
        ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
        return;
    }
    ret = esp_ble_gap_register_callback(gap_event_handler);
    if (ret){
        ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
        return;
    }
    ret = esp_ble_gatts_app_register(PROFILE_A_APP_ID);
    if (ret){
        ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
        return;
    }
//    ret = esp_ble_gatts_app_register(PROFILE_B_APP_ID);
//   if (ret){
//        ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
//        return;
//    }
    esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
    if (local_mtu_ret){
        ESP_LOGE(GATTS_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
    }
}

Debug Logs

I (71) boot: Chip Revision: 1
I (72) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot: ESP-IDF v4.1-dev-1086-g93a8603c5-dirty 2nd stage bootloader
I (39) boot: compile time 10:16:16
I (40) boot: Enabling RNG early entropy source...
I (46) boot: SPI Speed      : 40MHz
I (50) boot: SPI Mode       : DIO
I (54) boot: SPI Flash Size : 2MB
I (58) boot: Partition Table:
I (61) boot: ## Label            Usage          Type ST Offset   Length
I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (76) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (84) boot:  2 factory          factory app      00 00 00010000 00100000
I (91) boot: End of partition table
I (95) boot_comm: chip revision: 1, min. application chip revision: 0
I (102) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x1a9a8 (108968) map
I (151) esp_image: segment 1: paddr=0x0002a9d0 vaddr=0x3ffbdb60 size=0x02e1c ( 11804) load
I (156) esp_image: segment 2: paddr=0x0002d7f4 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _WindowOverflow4 at /home/jonhunterking/esp/esp-idf/components/freertos/xtensa_vectors.S:1778

I (158) esp_image: segment 3: paddr=0x0002dbfc vaddr=0x40080400 size=0x0241c (  9244) load
I (170) esp_image: segment 4: paddr=0x00030020 vaddr=0x400d0020 size=0x6cdf8 (445944) map
0x400d0020: _stext at ??:?

I (336) esp_image: segment 5: paddr=0x0009ce20 vaddr=0x4008281c size=0x101e8 ( 66024) load
0x4008281c: _xt_coproc_exc at /home/jonhunterking/esp/esp-idf/components/freertos/xtensa_vectors.S:974

I (376) boot: Loaded app from partition at offset 0x10000
I (376) boot: Disabling RNG early entropy source...
I (376) cpu_start: Pro cpu up.
I (380) cpu_start: Application information:
I (385) cpu_start: Project name:     mair-template
I (390) cpu_start: App version:      380ce58-dirty
I (396) cpu_start: Compile time:     Dec 19 2019 10:16:41
I (402) cpu_start: ELF file SHA256:  53cce7ebaaa24d9e...
I (408) cpu_start: ESP-IDF:          v4.1-dev-1086-g93a8603c5-dirty
I (415) cpu_start: Starting app cpu, entry point is 0x40081234
0x40081234: call_start_cpu1 at /home/jonhunterking/esp/esp-idf/components/esp32/cpu_start.c:276

I (0) cpu_start: App cpu up.
I (425) heap_init: Initializing. RAM available for dynamic allocation:
I (432) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (438) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (444) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (450) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
I (456) heap_init: At 3FFC87F0 len 00017810 (94 KiB): DRAM
I (462) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (469) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (475) heap_init: At 40092A04 len 0000D5FC (53 KiB): IRAM
I (481) cpu_start: Pro cpu start user code
I (500) spi_flash: detected chip: generic
I (500) spi_flash: flash io: dio
W (501) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (511) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Case 0
I (618) BTDM_INIT: BT controller compile version [340d4d4]
I (618) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (708) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0
I (1058) BLE: REGISTER_APP_EVT, status 0, app_id 0

I (1078) BLE: CREATE_SERVICE_EVT, status 0,  service_handle 40

I (1088) BLE: SERVICE_START_EVT, status 0, service_handle 40

I (1088) BLE: ADD_CHAR_EVT, status 0,  attr_handle 42, service_handle 40

E (1088) BLE: IN ONE
I (1088) BLE: the gatts demo char length = 3

I (1098) BLE: prf_char[0] =11

I (1098) BLE: prf_char[1] =22

I (1108) BLE: prf_char[2] =33

I (1108) BLE: ADD_CHAR_EVT, status 0,  attr_handle 44, service_handle 40

E (1118) BLE: IN TWO
I (1118) BLE: the gatts demo char length = 3

I (1128) BLE: prf_char[0] =11

I (1128) BLE: prf_char[1] =22

I (1128) BLE: prf_char[2] =33

I (1138) BLE: ADD_CHAR_EVT, status 133,  attr_handle 0, service_handle 40

I (1148) BLE: ADD_DESCR_EVT, status 0, attr_handle 45, service_handle 40

I (1148) BLE: ADD_DESCR_EVT, status 133, attr_handle 0, service_handle 40

I (1168) BLE: Stop adv successfully

Case 0
Case 1
In BLE Task
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4000c344  PS      : 0x00060730  A0      : 0x800dc545  A1      : 0x3ffc9af0  
A2      : 0x3ffc9b08  A3      : 0x00000000  A4      : 0x00000020  A5      : 0x3ffc9b08  
A6      : 0x00000100  A7      : 0x00000002  A8      : 0x40000000  A9      : 0x3ffc9ae0  
A10     : 0x00000000  A11     : 0x00000001  A12     : 0x3ffc9b10  A13     : 0x3ffb65fc  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x00000008  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffc  

ELF file SHA256: 53cce7ebaaa24d9e94682a8ab5e16962240a6a7411d16acd8574edb4929fdb7f

Backtrace: 0x4000c341:0x3ffc9af0 |<-CORRUPTED
gengyuchao commented 4 years ago

You set the wrong param in the function esp_ble_gap_start_advertising(&adv_params); of ble_task.

jonhunterking66 commented 4 years ago

@gengyuchao ESP_BT_MODE_BLE is incorrect?

gengyuchao commented 4 years ago
/**
 * @brief           This function is called to start advertising.
 *
 * @param[in]       adv_params: pointer to User defined adv_params data structure.

 * @return
 *                  - ESP_OK : success
 *                  - other  : failed
 *
 */
esp_err_t esp_ble_gap_start_advertising (esp_ble_adv_params_t *adv_params);

You need to set the adv_params like that

static esp_ble_adv_params_t adv_params = {
    .adv_int_min        = 0x100,
    .adv_int_max        = 0x100,
    .adv_type           = ADV_TYPE_IND,
    .own_addr_type      = BLE_ADDR_TYPE_RANDOM,
    .channel_map        = ADV_CHNL_ALL,
    .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
jonhunterking66 commented 4 years ago

@gengyuchao sorry, I did not include the full code. I do have adv_params initialized as such:

static esp_ble_adv_params_t adv_params = {
    .adv_int_min        = 0x20,
    .adv_int_max        = 0x40,
    .adv_type           = ADV_TYPE_IND,
    .own_addr_type      = BLE_ADDR_TYPE_PUBLIC,
    //.peer_addr            =
    //.peer_addr_type       =
    .channel_map        = ADV_CHNL_ALL,
    .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
gengyuchao commented 4 years ago

So, Please change esp_ble_gap_start_advertising(ESP_BT_MODE_BLE); to esp_ble_gap_start_advertising(&adv_params);.That will resolve you problem.

jonhunterking66 commented 4 years ago

Ahhh, okay, I will give it a try. Thank you!

Alvin1Zhang commented 4 years ago

@jonhunterking66 Thanks for reporting and updates. Feel free to reopen if the issue still happens. Thanks.