feat(ble): add dtm configuration command in hci example

This commit is contained in:
zwl 2024-12-19 11:35:30 +08:00
parent 85e299b939
commit 4ad39f320d
8 changed files with 217 additions and 39 deletions

View File

@ -140,7 +140,7 @@ hci_driver_uart_task_create(void)
}
static void
hci_driver_uart_deinit(void)
hci_driver_uart_task_delete(void)
{
if (s_hci_driver_uart_env.tx_task_handler) {
vTaskDelete(s_hci_driver_uart_env.tx_task_handler);
@ -151,6 +151,12 @@ hci_driver_uart_deinit(void)
vTaskDelete(s_hci_driver_uart_env.rx_task_handler);
s_hci_driver_uart_env.rx_task_handler = NULL;
}
}
static void
hci_driver_uart_deinit(void)
{
hci_driver_uart_task_delete();
ESP_ERROR_CHECK(uart_driver_delete(s_hci_driver_uart_env.hci_uart_params->hci_uart_port));
@ -207,12 +213,28 @@ error:
int
hci_driver_uart_reconfig_pin(int tx_pin, int rx_pin, int cts_pin, int rts_pin)
{
int rc;
hci_driver_uart_params_config_t *uart_param = s_hci_driver_uart_env.hci_uart_params;
hci_driver_uart_task_delete();
uart_param->hci_uart_tx_pin = tx_pin;
uart_param->hci_uart_rx_pin = rx_pin;
uart_param->hci_uart_rts_pin = rts_pin;
uart_param->hci_uart_cts_pin = cts_pin;
return hci_driver_uart_config(uart_param);
hci_driver_uart_config(uart_param);
/* Currently, the queue size is set to 1. It will be considered as semaphore. */
ESP_ERROR_CHECK(uart_driver_install(s_hci_driver_uart_env.hci_uart_params->hci_uart_port,
CONFIG_BT_LE_HCI_UART_RX_BUFFER_SIZE,
CONFIG_BT_LE_HCI_UART_TX_BUFFER_SIZE,
1, &s_hci_driver_uart_env.rx_event_queue,
0));
rc = hci_driver_uart_task_create();
if (rc) {
hci_driver_uart_task_delete();
return -2;
}
return 0;
}
hci_driver_ops_t hci_driver_uart_ops = {

View File

@ -60,38 +60,6 @@ typedef struct hci_driver_uart_params_config
*/
int hci_driver_uart_config(hci_driver_uart_params_config_t *uart_config);
#if CONFIG_BT_LE_UART_HCI_DMA_MODE
/**
* @brief Reconfigure the UART pins for the HCI driver.
*
* This function changes the UART pin configuration for the HCI driver.
*
* @param tx_pin The pin number for the UART TX (transmit) line.
* @param rx_pin The pin number for the UART RX (receive) line.
* @param cts_pin The pin number for the UART CTS (clear to send) line.
* @param rts_pin The pin number for the UART RTS (request to send) line.
*
* @return int Returns 0 on success, or a negative error code on failure.
*/
int hci_driver_uart_dma_reconfig_pin(int tx_pin, int rx_pin, int cts_pin, int rts_pin);
#define hci_uart_reconfig_pin hci_driver_uart_dma_reconfig_pin
#else
/**
* @brief Reconfigure the UART pins for the HCI driver.
*
* This function changes the UART pin configuration for the HCI driver.
*
* @param tx_pin The pin number for the UART TX (transmit) line.
* @param rx_pin The pin number for the UART RX (receive) line.
* @param cts_pin The pin number for the UART CTS (clear to send) line.
* @param rts_pin The pin number for the UART RTS (request to send) line.
*
* @return int Returns 0 on success, or a negative error code on failure.
*/
int hci_driver_uart_reconfig_pin(int tx_pin, int rx_pin, int cts_pin, int rts_pin);
#define hci_uart_reconfig_pin hci_driver_uart_reconfig_pin
#endif // CONFIG_BT_LE_UART_HCI_DMA_MODE
#ifdef __cplusplus
}
#endif

View File

@ -43,9 +43,38 @@ extern hci_driver_ops_t hci_driver_vhci_ops;
#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_RAM
#if CONFIG_BT_LE_HCI_INTERFACE_USE_UART
extern hci_driver_ops_t hci_driver_uart_ops;
#if CONFIG_BT_LE_UART_HCI_DMA_MODE
extern hci_driver_ops_t hci_driver_uart_dma_ops;
/**
* @brief Reconfigure the UART pins for the HCI driver.
*
* This function changes the UART pin configuration for the HCI driver.
*
* @param tx_pin The pin number for the UART TX (transmit) line.
* @param rx_pin The pin number for the UART RX (receive) line.
* @param cts_pin The pin number for the UART CTS (clear to send) line.
* @param rts_pin The pin number for the UART RTS (request to send) line.
*
* @return int Returns 0 on success, or a negative error code on failure.
*/
int hci_driver_uart_dma_reconfig_pin(int tx_pin, int rx_pin, int cts_pin, int rts_pin);
#define hci_uart_reconfig_pin hci_driver_uart_dma_reconfig_pin
#else
extern hci_driver_ops_t hci_driver_uart_ops;
/**
* @brief Reconfigure the UART pins for the HCI driver.
*
* This function changes the UART pin configuration for the HCI driver.
*
* @param tx_pin The pin number for the UART TX (transmit) line.
* @param rx_pin The pin number for the UART RX (receive) line.
* @param cts_pin The pin number for the UART CTS (clear to send) line.
* @param rts_pin The pin number for the UART RTS (request to send) line.
*
* @return int Returns 0 on success, or a negative error code on failure.
*/
int hci_driver_uart_reconfig_pin(int tx_pin, int rx_pin, int cts_pin, int rts_pin);
#define hci_uart_reconfig_pin hci_driver_uart_reconfig_pin
#endif // CONFIG_BT_LE_UART_HCI_DMA_MODE
#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_UART

View File

@ -1,4 +1,8 @@
set(srcs "main.c")
if(CONFIG_ENABLE_DTM_CONFIGURATION_COMMAND)
list(APPEND srcs
"dtm_configuration_command.c")
endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS ".")
INCLUDE_DIRS ".")

View File

@ -0,0 +1,9 @@
menu "Example Configuration"
config ENABLE_DTM_CONFIGURATION_COMMAND
bool
prompt "Enable DTM Configuration command"
default n
help
Use this option to enable DTM configuration command.
endmenu

View File

@ -0,0 +1,129 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "esp_log.h"
#include "esp_bt.h"
#include "esp_console.h"
#include "argtable3/argtable3.h"
#include "esp_hci_driver.h"
#define PROMPT_STR CONFIG_IDF_TARGET
static struct {
struct arg_int *cmd_params;
struct arg_end *end;
} dtm_set_tx_power_cmd_args;
static struct {
struct arg_int *tx_pin;
struct arg_int *rx_pin;
struct arg_end *end;
} dtm_reconfig_uart_cmd_args;
static int dtm_set_ble_tx_power_command(int argc, char **argv)
{
esp_err_t ret = ESP_OK;
int nerrors = arg_parse(argc, argv, (void **) &dtm_set_tx_power_cmd_args);
if (nerrors != 0) {
arg_print_errors(stderr, dtm_set_tx_power_cmd_args.end, argv[0]);
return 1;
}
ESP_LOGI(__func__, "Set tx power level '%d'", dtm_set_tx_power_cmd_args.cmd_params->ival[0]);
if (dtm_set_tx_power_cmd_args.cmd_params->ival[0] > 15) {
return 2;
}
ret = esp_ble_tx_power_set_enhanced(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0, dtm_set_tx_power_cmd_args.cmd_params->ival[0]);
if (ret != ESP_OK) {
return 3;
}
return 0;
}
static int dtm_get_ble_tx_power_command(int argc, char **argv)
{
esp_power_level_t power_level = 0xFF;
power_level = esp_ble_tx_power_get_enhanced(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0);
printf("\nCurrent BLE TX power is %d level\n", power_level);
return 0;
}
static int dtm_reconfig_uart_pins_command(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void **) &dtm_reconfig_uart_cmd_args);
if (nerrors != 0) {
arg_print_errors(stderr, dtm_reconfig_uart_cmd_args.end, argv[0]);
return 1;
}
ESP_LOGI(__func__, "reconfig tx:'%d', rx: '%d'",
dtm_reconfig_uart_cmd_args.tx_pin->ival[0], dtm_reconfig_uart_cmd_args.rx_pin->ival[0]);
hci_uart_reconfig_pin(dtm_reconfig_uart_cmd_args.tx_pin->ival[0],
dtm_reconfig_uart_cmd_args.rx_pin->ival[0], -1, -1);
return 0;
}
esp_err_t esp_console_register_set_ble_tx_power_command(void)
{
dtm_set_tx_power_cmd_args.cmd_params = arg_int1("i", "index", "<index>","tx power level index");
dtm_set_tx_power_cmd_args.end = arg_end(1);
esp_console_cmd_t command = {
.command = "set_ble_tx_power",
.help = "Set ble tx power during DTM",
.func = &dtm_set_ble_tx_power_command,
.argtable = &dtm_set_tx_power_cmd_args
};
return esp_console_cmd_register(&command);
}
esp_err_t esp_console_register_get_ble_tx_power_command(void)
{
esp_console_cmd_t command = {
.command = "get_ble_tx_power",
.help = "Get ble tx power during DTM",
.func = &dtm_get_ble_tx_power_command,
};
return esp_console_cmd_register(&command);
}
esp_err_t esp_console_register_reconfig_dtm_pins_command(void)
{
dtm_reconfig_uart_cmd_args.tx_pin = arg_int1("t", "tx", "<tx_pin>","tx pin index");
dtm_reconfig_uart_cmd_args.rx_pin = arg_int1("r", "rx", "<rx_pin>","rx pin index");
dtm_reconfig_uart_cmd_args.end = arg_end(2);
esp_console_cmd_t command = {
.command = "reconfig_dtm_uart_pin",
.help = "Reconfig dtm uart pins during DTM",
.func = &dtm_reconfig_uart_pins_command,
.argtable = &dtm_reconfig_uart_cmd_args
};
return esp_console_cmd_register(&command);
}
esp_err_t dtm_configuration_command_enable(void)
{
esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
/* Prompt to be printed before each line.
* This can be customized, made dynamic, etc.
*/
repl_config.prompt = PROMPT_STR ">";
repl_config.max_cmdline_length = 256;
esp_console_register_set_ble_tx_power_command();
esp_console_register_get_ble_tx_power_command();
esp_console_register_reconfig_dtm_pins_command();
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
ESP_ERROR_CHECK(esp_console_start_repl(repl));
return ESP_OK;
}

View File

@ -0,0 +1,13 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#ifndef __DTM_CONFIGURATION_COMMAND_H__
#define __DTM_CONFIGURATION_COMMAND_H__
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
esp_err_t dtm_configuration_command_enable(void);
#endif

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -12,7 +12,9 @@
#ifndef CONFIG_BT_LE_HCI_INTERFACE_USE_UART
#error "Please Enable Uart for HCI"
#endif
#if (CONFIG_ENABLE_DTM_CONFIGURATION_COMMAND)
#include "dtm_configuration_command.h"
#endif // CONFIG_ENABLE_DTM_CONFIGURATION_COMMAND
#define TAG "BLE_HCI"
void
@ -27,8 +29,10 @@ app_main(void)
}
ESP_ERROR_CHECK(ret);
#if (CONFIG_ENABLE_DTM_CONFIGURATION_COMMAND)
dtm_configuration_command_enable();
#endif // CONFIG_ENABLE_DTM_CONFIGURATION_COMMAND
esp_bt_controller_config_t config_opts = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
/*
* Initialize Bluetooth Controller parameters.
*/