ESP HCI over UART0

jsuffolk
Posts: 9
Joined: Wed Jul 24, 2019 10:25 pm

ESP HCI over UART0

Postby jsuffolk » Tue Jun 08, 2021 1:01 pm

I'm using an ESP32 with HCI with bluetooth classic RFCOMM via pybluez on a separate microcontroller (Jetson Nano). I only connected UART0 on the ESP32 device to the host microcontroller so I can reflash the device from the microcontroller. I've configured as H4 in menuconfig and disabled dual mode. I change around the pins so it worls. This mostly seems to work with the code at the bottom of this post. However, in using the bluetooth connection I see sporadic "bad checksum in packet" errors, corrupted data (responses that don't match what was sent from the client), and eventually the connection fails entirely. I'm sending small chatty packets: 4 bytes every 100ms.

I don't think it's a problem with the wiring because I'm able to flash the image over the same UART0 connection without problems.

I've tried all different baudrates, changing the TX/RX buffer sizes on the UART, and disabled UART0 debug console in menuconfig.

Is there some setting I've overlooked? Any suggestions for troubleshooting? Is it possible this is because I'm using UART0 pins, even though I'm remapping them?

Code: Select all

/*
   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#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);

#ifdef CONFIG_BT_HCI_UART_NO
    ESP_LOGI(tag, "HCI UART%d Pin select: TX 5, RX 0, RTS 12, CTS 32", CONFIG_BT_HCI_UART_NO);

    uart_set_pin(UART_NUM_0, 5, 0, 12, 32); // Remap UART0 to different pins
    uart_set_pin(CONFIG_BT_HCI_UART_NO, 1, 3, 22, 19); // Remap HCI uart to uart0 pins
    
    const int uart_buffer_size = (1024 * 4);
    //QueueHandle_t uart_queue1;
    QueueHandle_t uart_queue2;
    // Install UART driver using an event queue here
    //ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, 128, 128, 10, &uart_queue1, 0));
    ESP_ERROR_CHECK(uart_driver_install(CONFIG_BT_HCI_UART_NO, uart_buffer_size, uart_buffer_size, 10, &uart_queue2, 0));

    uart_set_baudrate(CONFIG_BT_HCI_UART_NO, 460800);
    // 1 is TXD0
    // 3 is RXD0
    // 22 is RTS0
    // 19 is CTS0
#endif
}

void app_main()
{ 
    esp_err_t ret;

    /* Initialize NVS — it is used to store PHY calibration dataa */
    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 );


    /* 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();
    bt_cfg.auto_latency = true;
    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;
    }

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

    ret = esp_bt_sleep_disable();
    if (ret != ESP_OK) {
        ESP_LOGE(tag, "Bluetooth Sleep Disable Failed: %s", esp_err_to_name(ret));
        return;
    }
}

Who is online

Users browsing this forum: Baidu [Spider], Majestic-12 [Bot] and 221 guests