Page 1 of 2

BLE and USB not working together

Posted: Fri Nov 05, 2021 2:26 am
by chris_zou
Environment: ESP32S3
I want to use both BLE and USB stack in the project.
However it is discovered BLE and USB stack can not work together.
I combine the example "blehr" and "tusb_sample_descriptor".
Without initialization code from "blehr", "tusb_sample_descriptor" is working and can be detected by Windows PC.
With initialization code from "blehr", "tusb_sample_descriptor" does not work and Windows PC can not detect the device at all.
More specifically, as long as the below code is executed, USB stack will not work

Code: Select all

ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
Is this a known hardware or SDK limitation?

Thank you.

Re: BLE and USB not working together

Posted: Sat Nov 06, 2021 3:42 am
by ESP_Sprite
There's no explicit reason why those two can't work together... not really more we can say unless we see your code.

Re: BLE and USB not working together

Posted: Mon Nov 08, 2021 1:02 am
by chris_zou
below is the main function (it is from the example blehr, the only changes are USB initialization code)

Code: Select all

void app_main(void)
{
    int rc;

    /* Initialize NVS — it is used to store PHY calibration data */
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());

    nimble_port_init();
    /* Initialize the NimBLE host configuration */
    ble_hs_cfg.sync_cb = blehr_on_sync;
    ble_hs_cfg.reset_cb = blehr_on_reset;

    /* name, period/time,  auto reload, timer ID, callback */
    blehr_tx_timer = xTimerCreate("blehr_tx_timer", pdMS_TO_TICKS(1000), pdTRUE, (void *)0, blehr_tx_hrate);

    rc = gatt_svr_init();
    assert(rc == 0);

    /* Set the default device name */
    rc = ble_svc_gap_device_name_set(device_name);
    assert(rc == 0);

    /* Start the task */
    nimble_port_freertos_init(blehr_host_task);

    /** initilaize USB */
    tinyusb_config_t tusb_cfg = {
        .descriptor = NULL,
        .string_descriptor = NULL,
        .external_phy = false // In the most cases you need to use a `false` value
    };

    ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
    ESP_LOGI(tag, "USB initialization DONE");

}
sdkconfig.defaults

Code: Select all

# Override some defaults so BT stack is enabled
# in this example

#
# BT config
#
CONFIG_BT_ENABLED=y
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
CONFIG_BTDM_CTRL_MODE_BTDM=n
CONFIG_BT_BLUEDROID_ENABLED=n
CONFIG_BT_NIMBLE_ENABLED=y

#
# USB config
#
CONFIG_TINYUSB=y
CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID=n
CONFIG_TINYUSB_DESC_CUSTOM_VID=0x303A
CONFIG_TINYUSB_DESC_USE_DEFAULT_PID=n
CONFIG_TINYUSB_DESC_CUSTOM_PID=0x3000
The full project is attached (blehr.zip).

Can you help check if there is any problem with the configurations?
Appreciate your support.

Re: BLE and USB not working together

Posted: Mon Nov 15, 2021 2:34 pm
by dskplay
I'm experiencing the same problem with the ESP32S3. I'm using Bluedroid rather than Nimble for my BLE code, but otherwise my code setup and usb initialisation are almost identical. Similar to chris_zou, my BLE and USB code work as expected independently of one another, but when both are initialised, the BLE code always wins (and functions as expected), regardless of the order in which the USB and BLE are initialised. That said, if I initialise USB first, then BLE, I do briefly get successful USB enumeration, but the BLE initialisation quickly puts a stop to that.

I have narrowed down the point of failure to the inclusion of:

Code: Select all

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
esp_bt_controller_init(&bt_cfg);
Pretty much the first line in my BLE server initialisation - there's a lot going on in esp_bt_controller_init() so i didn't investigate any further yet.

Perhaps there is a conflicting configuration in BT_CONTROLLER_INIT_CONFIG_DEFAULT() for the ESP32S3? I have tried changing the default hci_uart_no from 1 to 2, to no avail, but to be honest I'm shooting in the dark here... My initial thought was that perhaps BLE and USB are using some shared physical resource, and only one can be active at a time, but that is entirely speculative on my part, and ESP_Sprite seems to be suggesting that this shouldn't be the case (is there any documentation to support this?). I've also tried changing the default BLE core from 1 to 2, but still no joy.

BT_CONTROLLER_INIT_CONFIG_DEFAULT() is defined as follows in 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_CTRL_BLE_MAX_CONN_EFF,                     \
    .bt_max_acl_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN_EFF,           \
    .bt_sco_datapath = CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF,          \
    .auto_latency = BTDM_CTRL_AUTO_LATENCY_EFF,                            \
    .bt_legacy_auth_vs_evt = BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF,         \
    .bt_max_sync_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF,         \
    .ble_sca = CONFIG_BTDM_BLE_SLEEP_CLOCK_ACCURACY_INDEX_EFF,             \
    .pcm_role = CONFIG_BTDM_CTRL_PCM_ROLE_EFF,                             \
    .pcm_polar = CONFIG_BTDM_CTRL_PCM_POLAR_EFF,                           \
    .hli = BTDM_CTRL_HLI,                                                  \
    .magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL,                           \
};
Do any of the parameters in here jump out as being possible suspects?

Re: BLE and USB not working together

Posted: Tue Nov 23, 2021 6:57 am
by metoo0713
同样的问题
E:\esp-idf\components\esp_phy\src\phy_init.c
esp_err_t ret = register_chipv7_phy(init_data, cal_data, calibration_mode);

Re: BLE and USB not working together

Posted: Wed Nov 24, 2021 5:11 pm
by chegewara

Re: BLE and USB not working together

Posted: Sun Jan 16, 2022 4:33 am
by nickbits
I was having this issue with an S3 and Bluetooth in controller only mode as well.

Short answer:
Set the secondary console output to "No secondary console" in ESP System Settings. It is set to JTAG by default, at least starting from the USB host example.

Longer answer:
I also traced it down to the same call to register_chipv7_phy.
The issue is that even if CONFIG_ESP_PHY_ENABLE_USB is defined, it is not actually enabled if the secondary console is set to JTAG. Initialization is conditional on the result of

Code: Select all

usb_serial_jtag_ll_txfifo_writable() == 1
and it returns 0, presumably because I do not have anything connected to the JTAG pins.

Re: BLE and USB not working together

Posted: Mon Jan 17, 2022 1:22 am
by ESP_Sprite
What version of ESP-IDF is that in?

Re: BLE and USB not working together

Posted: Tue Jan 18, 2022 8:34 pm
by nickbits
What version of ESP-IDF is that in?
I'm not sure if that Q was meant for me or OP but I am using the latest (as of a few days ago anyway) from the master branch.

Re: BLE and USB not working together

Posted: Wed Jan 19, 2022 4:20 am
by ESP_Sprite
Yes, I intended to ask you. I see the line now. I think the USB PLL can be simply turned on unconditionally... Just for reference, can you change that 'if' line to 'if (1) {' and see if you still have issues?