console: make uart param configurable

Closes https://github.com/espressif/esp-idf/issues/4845
This commit is contained in:
morris 2020-03-05 20:03:06 +08:00
parent 0a7ccb8095
commit 46a3f3e516
2 changed files with 68 additions and 15 deletions

View file

@ -18,6 +18,7 @@ extern "C" {
#endif
#include <stddef.h>
#include "sdkconfig.h"
#include "esp_err.h"
// Forward declaration. Definition in linenoise/linenoise.h.
@ -55,19 +56,59 @@ typedef struct {
uint32_t task_stack_size; //!< repl task stack size
uint32_t task_priority; //!< repl task priority
const char *prompt; //!< prompt (NULL represents default: "esp> ")
union {
struct {
int channel; //!< UART channel
uint32_t baud_rate; //!< Comunication baud rate
int tx_gpio; //!< GPIO number for TX path, -1 means using the default
int rx_gpio; //!< GPIO number for RX path, -1 means using the default
} uart; //!< UART specific configuration
} device; //!< device configuration
} esp_console_repl_config_t;
#ifdef CONFIG_ESP_CONSOLE_UART_NUM
#define CONSOLE_DEFAULT_UART_CHANNEL CONFIG_ESP_CONSOLE_UART_NUM
#else
#define CONSOLE_DEFAULT_UART_CHANNEL 0
#endif
#ifdef CONFIG_ESP_CONSOLE_UART_BAUDRATE
#define CONSOLE_DEFAULT_UART_BAUDRATE CONFIG_ESP_CONSOLE_UART_BAUDRATE
#else
#define CONSOLE_DEFAULT_UART_BAUDRATE 115200
#endif
#ifdef CONFIG_ESP_CONSOLE_UART_TX_GPIO
#define CONSOLE_DEFAULT_UART_TX_GPIO CONFIG_ESP_CONSOLE_UART_TX_GPIO
#else
#define CONSOLE_DEFAULT_UART_TX_GPIO 1
#endif
#ifdef CONFIG_ESP_CONSOLE_UART_RX_GPIO
#define CONSOLE_DEFAULT_UART_RX_GPIO CONFIG_ESP_CONSOLE_UART_RX_GPIO
#else
#define CONSOLE_DEFAULT_UART_RX_GPIO 3
#endif
/**
* @brief Default console repl configuration value
*
*/
#define ESP_CONSOLE_REPL_CONFIG_DEFAULT() \
{ \
.max_history_len = 32, \
.history_save_path = NULL, \
.task_stack_size = 4096, \
.task_priority = 2, \
.prompt = NULL, \
#define ESP_CONSOLE_REPL_CONFIG_DEFAULT() \
{ \
.max_history_len = 32, \
.history_save_path = NULL, \
.task_stack_size = 4096, \
.task_priority = 2, \
.prompt = NULL, \
.device = { \
.uart = { \
.channel = CONSOLE_DEFAULT_UART_CHANNEL, \
.baud_rate = CONSOLE_DEFAULT_UART_BAUDRATE, \
.tx_gpio = CONSOLE_DEFAULT_UART_TX_GPIO, \
.rx_gpio = CONSOLE_DEFAULT_UART_RX_GPIO, \
} \
} \
}
/**
@ -232,6 +273,8 @@ esp_err_t esp_console_register_help_command(void);
/**
* @brief Initialize console REPL environment
*
* @param config REPL configuration
*
* @note This is a all-in-one function to establish the environment needed for REPL, includes:
* - Install the UART driver on the console UART (8n1, 115200, REF_TICK clock source)
* - Configures the stdin/stdout to go through the UART driver

View file

@ -48,6 +48,12 @@ static char s_prompt[CONSOLE_PROMPT_LEN_MAX];
*/
static const char *s_history_save_path = NULL;
/**
* @brief default uart channel number
*
*/
static int s_uart_channel = -1;
/**
* @brief REPL task handle
*
@ -122,21 +128,23 @@ esp_err_t esp_console_repl_init(const esp_console_repl_config_t *config)
* correct while APB frequency is changing in light sleep mode.
*/
const uart_config_t uart_config = {
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.baud_rate = config->device.uart.baud_rate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.source_clk = UART_SCLK_REF_TICK,
};
/* Install UART driver for interrupt-driven reads and writes */
ret = uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0);
ret = uart_driver_install(config->device.uart.channel, 256, 0, 0, NULL, 0);
if (ret != ESP_OK) {
goto _exit;
}
uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config);
s_uart_channel = config->device.uart.channel;
uart_param_config(s_uart_channel, &uart_config);
uart_set_pin(s_uart_channel, config->device.uart.tx_gpio, config->device.uart.rx_gpio, -1, -1);
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
esp_vfs_dev_uart_use_driver(s_uart_channel);
/* Initialize the console */
esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
@ -220,8 +228,9 @@ esp_err_t esp_console_repl_init(const esp_console_repl_config_t *config)
_console_del:
esp_console_deinit();
esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM);
uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM);
esp_vfs_dev_uart_use_nonblocking(s_uart_channel);
uart_driver_delete(s_uart_channel);
s_uart_channel = -1;
s_repl_state = CONSOLE_REPL_STATE_DEINIT;
_exit:
return ret;
@ -240,8 +249,9 @@ esp_err_t esp_console_repl_deinit(void)
s_repl_state = CONSOLE_REPL_STATE_DEINIT;
esp_console_deinit();
esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM);
uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM);
esp_vfs_dev_uart_use_nonblocking(s_uart_channel);
uart_driver_delete(s_uart_channel);
s_uart_channel = -1;
s_repl_task_hdl = NULL;
s_history_save_path = NULL;
_exit: