HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

felipe
Posts: 11
Joined: Mon Jan 18, 2021 5:13 pm

HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby felipe » Tue Jan 19, 2021 12:21 pm

Hi!

I want to control the HCI controller running on ESP32-PICO-KIT over UART, with BlueZ as the host.

I have started from the https://github.com/espressif/esp-idf/bl ... art_demo.c example and I want to change the pin functions to be able to run the HCI UART over USB-UART bridge (UART0 is connected to the bridge). To do this, I reconfigure the UART0 pins to some other available GPIOs and reconfigure UART1 pins to UART0 pins. Similar to this topic, viewtopic.php?t=11596 but the board & GPIO numbers are different.

However, this results in the ESP32 hanging execution (logs stop to show up in idf.py monitor) when I run

Code: Select all

uart_set_pin(CONFIG_BT_HCI_UART_NO, UART0_TX_F, UART0_RX_F, UART0_RTS_F, UART0_CTS_F);
Also the ESP32 HCI controller is not visible on host when I run
btattach -B /dev/ttyUSB0 -S 921600
bluetoothctl list
(bluetoothd running)

I was checking the pinout https://docs.espressif.com/projects/esp ... scriptions and the schematic https://dl.espressif.com/dl/schematics/ ... ematic.pdf to make sure there are no conflicts with flash.

What may be going wrong? Is it possible to have the HCI working on the same UART as the one used for flashing? I don't want to have to connect 2 UARTs to the ESP32 on the PCB.

The code:

Code: Select all

#include <stdio.h>
#include <string.h>
#include "nvs_flash.h"
#include "esp_bt.h"
#include "soc/uhci_periph.h"
#include "driver/uart.h"
#include "driver/periph_ctrl.h"
#include "esp_log.h"

static const char *tag = "CONTROLLER_UART_HCI";

static void uart_gpio_reset(void)
{
#if CONFIG_BT_HCI_UART_NO == 1
    periph_module_enable(PERIPH_UART1_MODULE);
#elif CONFIG_BT_HCI_UART_NO == 2
    periph_module_enable(PERIPH_UART2_MODULE);
#endif
    periph_module_enable(PERIPH_UHCI0_MODULE);

    ESP_LOGI(tag, "Enabled UART %d (periph %d) and HCI %d",
            CONFIG_BT_HCI_UART_NO, PERIPH_UART1_MODULE, PERIPH_UHCI0_MODULE);
#ifdef CONFIG_BT_HCI_UART_NO
    //ESP_LOGI(tag, "HCI UART%d Pin select: TX 5, RX, 18, CTS 23, RTS 19", CONFIG_BT_HCI_UART_NO);
    // default config - UART1 HCI
    // uart_set_pin(CONFIG_BT_HCI_UART_NO, 5, 18, 19, 23);

    ESP_LOGI(tag, "HCI UART%d Pin exchange with UART0", CONFIG_BT_HCI_UART_NO);
    ESP_LOGI(tag, "remap UART0");
    vTaskDelay(30 / portTICK_PERIOD_MS);

    static const unsigned int UART0_TX_F = 1;
    static const unsigned int UART0_RX_F = 3;
    static const unsigned int UART0_RTS_F = 22;
    static const unsigned int UART0_CTS_F = 19;

    // these conflict with flash >
    //static const unsigned int UART1_TX = 10;
    //static const unsigned int UART1_RX = 9;
    //static const unsigned int UART1_RTS = 11;
    //static const unsigned int UART1_CTS = 6;

    // Remap UART0 to different pins
    //uart_set_pin(UART_NUM_0, UART1_TX, UART1_RX, UART1_RTS, UART1_CTS);
    uart_set_pin(UART_NUM_0, 5, 18, 21, 23);

    ESP_LOGI(tag, "remap UART %d", CONFIG_BT_HCI_UART_NO);
    vTaskDelay(30 / portTICK_PERIOD_MS);

    // ^ last log visible on "idf.py monitor"

    // Remap HCI uart to UART0 pins
    uart_set_pin(CONFIG_BT_HCI_UART_NO, UART0_TX_F, UART0_RX_F, UART0_RTS_F, UART0_CTS_F);

    uart_set_baudrate(CONFIG_BT_HCI_UART_NO, 921600);
    ESP_LOGI(tag, "pin echange done.");
    vTaskDelay(30 / portTICK_PERIOD_MS);
#endif
}

void app_main(void)
{
    esp_err_t ret;

    ESP_LOGW(tag, "main start");

    /* Initialize NVS — it is used to store PHY calibration data */
    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_LOGI(tag, "NVS initialized");

    /* As the UART1/2 pin conflict with flash pin, so do matrix of the signal and pin */
    uart_gpio_reset();

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret != ESP_OK) {
        ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret));
        return;
    }
    ESP_LOGI(tag, "bt_controller_init done");

    ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
    if (ret != ESP_OK) {
        ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret));
        return;
    }
    ESP_LOGI(tag, "bt_controller_enable done");
}


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

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby ESP_Sprite » Wed Jan 20, 2021 5:34 am

Just to double-check: what are the actual pins you decided on using?

felipe
Posts: 11
Joined: Mon Jan 18, 2021 5:13 pm

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby felipe » Wed Jan 20, 2021 9:28 am

Hi ESP_Sprite! I want to use the pins normally connected to UART0 and to USB-UART bridge CP2102. Is this possible? This would mean I want to have:
- HCI Bluetooth -> change UART1 to pins 1, 3, 22, 19
- UART0 -> change to whatever other pins

felipe
Posts: 11
Joined: Mon Jan 18, 2021 5:13 pm

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby felipe » Thu Jan 21, 2021 2:19 pm

Basically I want to achieve a minimal working configuration with HCI on UART0 pins.

felipe
Posts: 11
Joined: Mon Jan 18, 2021 5:13 pm

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby felipe » Wed Feb 03, 2021 12:44 pm

Any update?

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

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby ESP_Sprite » Thu Feb 04, 2021 7:15 am

Pin 1? Is that pin number or GPIO? (Sorry, I asked wrong, should have asked which GPIO numbers you're using)

felipe
Posts: 11
Joined: Mon Jan 18, 2021 5:13 pm

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby felipe » Fri Feb 05, 2021 4:46 pm

All numbers I have posted are GPIO numbers, according to https://docs.espressif.com/projects/esp ... scriptions

- UART1 (HCI Bluetooth) -> I want to change to GPIO 1, 3, 22, 19
- UART0 -> I want to change to whatever other GPIO, e.g. 5, 18, 21, 23

felipe
Posts: 11
Joined: Mon Jan 18, 2021 5:13 pm

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby felipe » Fri Feb 05, 2021 4:50 pm

And the most important question is: Is it enough to only have 1 UART connected to ESP32 on the PCB? I want to use this UART for
1. flashing new firmware (FW upgrade)
2. Bluetooth HCI over UART

I'm thinking that maybe with some reset pin it's possible to ask the ESP32 to enter bootloader and then send the new firmware (from esptool).

felipe
Posts: 11
Joined: Mon Jan 18, 2021 5:13 pm

Re: HCI UART over UART0 (One UART for flashing and HCI Bluetooth)

Postby felipe » Wed Dec 07, 2022 3:44 pm

Hi! Any update? Is it possible to use ESP32 HCI with just 1 UART?

Who is online

Users browsing this forum: Baidu [Spider] and 133 guests