esp_bt_controller_init fail - why ?

ESP32Toad
Posts: 5
Joined: Sun Sep 20, 2020 4:30 am

esp_bt_controller_init fail - why ?

Postby ESP32Toad » Sun Sep 20, 2020 4:48 am

I am trying to make the simplest possible passive BLE scanner on an ESP32 (specifically a Adafruit Feather Huzzah 32)
Basing my code on Neil Kolbans c example here: https://github.com/nkolban/esp32-snippe ... ner/ble1.c
I know he wrote C++ wrappers that are also included in the 1.0.4 ESP code for Arduino but I have different issues w that code and my Govee thermometer BLE devices, so I want to create the simplest possible BLE scanner code.

The code below fails in the esp_bt_controller_init call. Why ?

Code: Select all

void setup()
{
  Serial.begin(115200);
  esp_err_t status;

  // Initialize NVS flash storage with layout given in the partition table
  esp_err_t 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 );

  // Initialize BT controller to allocate task and other resource.
  esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  if (esp_bt_controller_init(&bt_cfg) != ESP_OK)
  {
    ESP_LOGE(tag, "Bluetooth controller initialize failed");
    return;
  }
  ...

More details from the compiler:

Code: Select all

Sketch uses 862543 bytes (43%) of program storage space. Maximum is 1966080 bytes.
Global variables use 35432 bytes (10%) of dynamic memory, leaving 292248 bytes for local variables. Maximum is 327680 bytes.
esptool.py v2.6
Serial port /dev/cu.SLAB_USBtoUART
Connecting........____
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 3c:71:bf:6e:95:70
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...
Arduino IDE settings:
Board: Adafruit ESP32 Feather https://www.adafruit.com/product/3405
Partition Scheme: Minimal SPIFFS (large APPS with OTA)
Espressiv ESP32 v1.0.4 installed

So it doesn't look like this is due to any memory issues ...

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: esp_bt_controller_init fail - why ?

Postby ESP_Sprite » Tue Sep 22, 2020 9:20 am

It could possibly help to inspect the return value of esp_bt_controller_init instead of just checking if it's OK... otherwise, your code looks OK to me.

ESP32Toad
Posts: 5
Joined: Sun Sep 20, 2020 4:30 am

Re: esp_bt_controller_init fail - why ?

Postby ESP32Toad » Tue Sep 22, 2020 6:37 pm

Thanks ESP_Sprite !
I did look at the esp_bt_controller_init return code but then referring to the ESP32 documentation p 129 ("Read the Docs Template Documentation, Release v4.1-dev-2071-gf91080637") it just says
Return ESP_OK - success, other - failed

so not that useful :)

Strangely I have found that if I include *ANY* of Neil Kolbans BLE library files from the 1.0.4 ESP release, even just something as innocent as BLEUUID.h, the compiler kicks in and loads all of the library and then the esp_bt_controller_init succeeds. I have not found out why this is the case (I did follow the .h includes down the rabbit hole some way but couldnt figure it out)

Here is the code I tried:

Code: Select all

#include "BLEUUID.h"
#include <bt.h>

void setup() {
  Serial.begin(115200);
  esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  if (esp_bt_controller_init(&bt_cfg) != ESP_OK)
    Serial.printf( "Bluetooth controller initialize failed\n");
  else  
    Serial.printf( "BT init succeess\n" );
}

void loop() {}
With the BLEUUID.h include, init succeeds.
If you comment line 1 out, init fails.
Perhaps I am missing some other BLE stack init call or static etc. ?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: esp_bt_controller_init fail - why ?

Postby ESP_Sprite » Wed Sep 23, 2020 11:32 am

Sure, but a code that is returned there usually indicates a 'standard' type of error - out of memory, parameter issues, ..., and knowing what it complains about may help in figuring out what is wrong.

Not sure about why including the header helps - I'm not familiar enough with the (Arduino implementation of the) BT stack to comment on that.

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: esp_bt_controller_init fail - why ?

Postby lbernstone » Wed Sep 23, 2020 1:01 pm

Does btStart() work for you? Perhaps your expectation that anything other than a ESP_OK is a failure is incorrect.

ESP32Toad
Posts: 5
Joined: Sun Sep 20, 2020 4:30 am

Re: esp_bt_controller_init fail - why ?

Postby ESP32Toad » Wed Sep 23, 2020 10:03 pm

Thanks guys for the ideas, checking out both:

1. return value from esp_bt_controller_init(&bt_cfg); call is:
ESP_ERR_INVALID_STATE (0x103): Invalid state
So the esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); macro does not actually
produce a config the library itself likes ???


2. looked at btStart() in arduino-esp32/cores/esp32/esp32-hal-bt.c
here: https://github.com/espressif/arduino-es ... 2-hal-bt.c

Code: Select all

bool btStart(){
    esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED){
        return true;
    }
    if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE){
        esp_bt_controller_init(&cfg);
        while(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE){}
    }
So that does pretty much exactly what I am doing (I added the esp_bt_controller_get_status() call to my code just to see whether it makes a difference - it does not and the controller is not yet enabled when I try to init it btw.)
I also tried adding the esp_bt_controller_get_status guard call from the btStart code, it returns idle, and the init call still fails - so that does not make any difference.

Does anyone have sample code using just ESP32 raw C calls for BLE (vs using Neils 1.0.1 BLE C++ library on Arduino for example). I would like to see if any other calls need to happen before esp_bt_controller_init.
Or does that call require additional config parameters to be set before init is called (the btStart implementation shows this is not necessary though)

Puzzled...

ESP32Toad
Posts: 5
Joined: Sun Sep 20, 2020 4:30 am

Re: esp_bt_controller_init fail - why ?

Postby ESP32Toad » Wed Sep 23, 2020 11:07 pm

For reference, I also found the macro definition is esp_bt.h
https://github.com/espressif/arduino-es ... t/esp_bt.h

Code: Select all

#define BT_CONTROLLER_INIT_CONFIG_DEFAULT() {                              \
    .controller_task_stack_size = ESP_TASK_BT_CONTROLLER_STACK,            \
    .controller_task_prio = ESP_TASK_BT_CONTROLLER_PRIO,                   \
    .hci_uart_no = BT_HCI_UART_NO_DEFAULT,                                 \
    .hci_uart_baudrate = BT_HCI_UART_BAUDRATE_DEFAULT,                     \
    .scan_duplicate_mode = SCAN_DUPLICATE_MODE,                            \
    .scan_duplicate_type = SCAN_DUPLICATE_TYPE_VALUE,                     \
    .normal_adv_size = NORMAL_SCAN_DUPLICATE_CACHE_SIZE,                   \
    .mesh_adv_size = MESH_DUPLICATE_SCAN_CACHE_SIZE,                       \
    .send_adv_reserved_size = SCAN_SEND_ADV_RESERVED_SIZE,                 \
    .controller_debug_flag = CONTROLLER_ADV_LOST_DEBUG_BIT,                \
    .mode = BTDM_CONTROLLER_MODE_EFF,                                      \
    .ble_max_conn = CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF,               \
    .bt_max_acl_conn = CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_EFF,     \
    .bt_sco_datapath = CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF,          \
    .bt_max_sync_conn = CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_EFF,   \
    .magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL,                           \
};

Who is online

Users browsing this forum: No registered users and 37 guests