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);
(bluetoothd running)btattach -B /dev/ttyUSB0 -S 921600
bluetoothctl list
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");
}