2017-08-15 04:11:19 -04:00
|
|
|
/* Console example
|
|
|
|
|
|
|
|
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 "esp_system.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_console.h"
|
|
|
|
#include "esp_vfs_dev.h"
|
|
|
|
#include "driver/uart.h"
|
|
|
|
#include "linenoise/linenoise.h"
|
|
|
|
#include "argtable3/argtable3.h"
|
|
|
|
#include "esp_vfs_fat.h"
|
2018-01-08 23:47:32 -05:00
|
|
|
#include "nvs.h"
|
|
|
|
#include "nvs_flash.h"
|
2023-03-24 08:50:48 -04:00
|
|
|
#include "soc/soc_caps.h"
|
2022-05-03 13:46:59 -04:00
|
|
|
#include "cmd_system.h"
|
|
|
|
#include "cmd_wifi.h"
|
|
|
|
#include "cmd_nvs.h"
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2023-06-29 00:09:23 -04:00
|
|
|
/*
|
|
|
|
* We warn if a secondary serial console is enabled. A secondary serial console is always output-only and
|
|
|
|
* hence not very useful for interactive console applications. If you encounter this warning, consider disabling
|
|
|
|
* the secondary serial console in menuconfig unless you know what you are doing.
|
|
|
|
*/
|
|
|
|
#if SOC_USB_SERIAL_JTAG_SUPPORTED
|
|
|
|
#if !CONFIG_ESP_CONSOLE_SECONDARY_NONE
|
|
|
|
#warning "A secondary serial console is not useful when using the console component. Please disable it in menuconfig."
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-05-12 08:55:12 -04:00
|
|
|
#ifdef CONFIG_ESP_CONSOLE_USB_CDC
|
|
|
|
#error This example is incompatible with USB CDC console. Please try "console_usb" example instead.
|
|
|
|
#endif // CONFIG_ESP_CONSOLE_USB_CDC
|
|
|
|
|
2023-04-20 03:50:42 -04:00
|
|
|
#ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|
|
|
|
#error This example is incompatible with USB serial JTAG console.
|
|
|
|
#endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
|
|
|
|
|
2017-08-15 04:11:19 -04:00
|
|
|
static const char* TAG = "example";
|
2020-04-02 18:16:54 -04:00
|
|
|
#define PROMPT_STR CONFIG_IDF_TARGET
|
2017-08-15 04:11:19 -04:00
|
|
|
|
|
|
|
/* Console command history can be stored to and loaded from a file.
|
|
|
|
* The easiest way to do this is to use FATFS filesystem on top of
|
|
|
|
* wear_levelling library.
|
|
|
|
*/
|
|
|
|
#if CONFIG_STORE_HISTORY
|
|
|
|
|
|
|
|
#define MOUNT_PATH "/data"
|
|
|
|
#define HISTORY_PATH MOUNT_PATH "/history.txt"
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void initialize_filesystem(void)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
|
|
|
static wl_handle_t wl_handle;
|
|
|
|
const esp_vfs_fat_mount_config_t mount_config = {
|
|
|
|
.max_files = 4,
|
|
|
|
.format_if_mount_failed = true
|
|
|
|
};
|
2022-03-04 14:15:49 -05:00
|
|
|
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle);
|
2017-08-15 04:11:19 -04:00
|
|
|
if (err != ESP_OK) {
|
2018-02-22 08:48:53 -05:00
|
|
|
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
|
2017-08-15 04:11:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // CONFIG_STORE_HISTORY
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void initialize_nvs(void)
|
2018-01-08 23:47:32 -05:00
|
|
|
{
|
|
|
|
esp_err_t err = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2018-01-08 23:47:32 -05:00
|
|
|
ESP_ERROR_CHECK( nvs_flash_erase() );
|
|
|
|
err = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void initialize_console(void)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
2019-10-18 13:26:57 -04:00
|
|
|
/* Drain stdout before reconfiguring it */
|
|
|
|
fflush(stdout);
|
|
|
|
fsync(fileno(stdout));
|
|
|
|
|
2019-03-13 08:19:55 -04:00
|
|
|
/* Disable buffering on stdin */
|
2017-08-15 04:11:19 -04:00
|
|
|
setvbuf(stdin, NULL, _IONBF, 0);
|
|
|
|
|
|
|
|
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
|
2020-06-09 05:00:47 -04:00
|
|
|
esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
|
2017-08-15 04:11:19 -04:00
|
|
|
/* Move the caret to the beginning of the next line on '\n' */
|
2020-06-09 05:00:47 -04:00
|
|
|
esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2018-08-13 20:44:04 -04:00
|
|
|
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
|
|
|
|
* correct while APB frequency is changing in light sleep mode.
|
|
|
|
*/
|
|
|
|
const uart_config_t uart_config = {
|
2019-04-30 06:51:55 -04:00
|
|
|
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
|
2018-08-13 20:44:04 -04:00
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
2022-04-29 00:10:05 -04:00
|
|
|
#if SOC_UART_SUPPORT_REF_TICK
|
2020-12-14 22:00:02 -05:00
|
|
|
.source_clk = UART_SCLK_REF_TICK,
|
2022-04-29 00:10:05 -04:00
|
|
|
#elif SOC_UART_SUPPORT_XTAL_CLK
|
2020-12-14 22:00:02 -05:00
|
|
|
.source_clk = UART_SCLK_XTAL,
|
|
|
|
#endif
|
2018-08-13 20:44:04 -04:00
|
|
|
};
|
2017-08-15 04:11:19 -04:00
|
|
|
/* Install UART driver for interrupt-driven reads and writes */
|
2019-04-30 06:51:55 -04:00
|
|
|
ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
|
2017-08-15 04:11:19 -04:00
|
|
|
256, 0, 0, NULL, 0) );
|
2019-04-17 08:19:44 -04:00
|
|
|
ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) );
|
2017-08-15 04:11:19 -04:00
|
|
|
|
|
|
|
/* Tell VFS to use UART driver */
|
2019-04-30 06:51:55 -04:00
|
|
|
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
|
2017-08-15 04:11:19 -04:00
|
|
|
|
|
|
|
/* Initialize the console */
|
|
|
|
esp_console_config_t console_config = {
|
|
|
|
.max_cmdline_args = 8,
|
|
|
|
.max_cmdline_length = 256,
|
|
|
|
#if CONFIG_LOG_COLORS
|
|
|
|
.hint_color = atoi(LOG_COLOR_CYAN)
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_console_init(&console_config) );
|
|
|
|
|
|
|
|
/* Configure linenoise line completion library */
|
|
|
|
/* Enable multiline editing. If not set, long commands will scroll within
|
|
|
|
* single line.
|
|
|
|
*/
|
|
|
|
linenoiseSetMultiLine(1);
|
|
|
|
|
|
|
|
/* Tell linenoise where to get command completions and hints */
|
|
|
|
linenoiseSetCompletionCallback(&esp_console_get_completion);
|
|
|
|
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
|
|
|
|
|
|
|
|
/* Set command history size */
|
|
|
|
linenoiseHistorySetMaxLen(100);
|
|
|
|
|
2021-07-14 23:37:11 -04:00
|
|
|
/* Set command maximum length */
|
|
|
|
linenoiseSetMaxLineLen(console_config.max_cmdline_length);
|
|
|
|
|
2020-04-02 18:16:21 -04:00
|
|
|
/* Don't return empty lines */
|
|
|
|
linenoiseAllowEmpty(false);
|
|
|
|
|
2017-08-15 04:11:19 -04:00
|
|
|
#if CONFIG_STORE_HISTORY
|
|
|
|
/* Load command history from filesystem */
|
|
|
|
linenoiseHistoryLoad(HISTORY_PATH);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-08-15 04:11:19 -04:00
|
|
|
{
|
2018-01-08 23:47:32 -05:00
|
|
|
initialize_nvs();
|
|
|
|
|
2017-08-15 04:11:19 -04:00
|
|
|
#if CONFIG_STORE_HISTORY
|
|
|
|
initialize_filesystem();
|
2019-10-18 13:26:57 -04:00
|
|
|
ESP_LOGI(TAG, "Command history enabled");
|
|
|
|
#else
|
|
|
|
ESP_LOGI(TAG, "Command history disabled");
|
2017-08-15 04:11:19 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
initialize_console();
|
|
|
|
|
|
|
|
/* Register commands */
|
|
|
|
esp_console_register_help_command();
|
2023-03-24 08:50:48 -04:00
|
|
|
register_system_common();
|
|
|
|
#ifndef CONFIG_IDF_TARGET_ESP32H2 // needs deep sleep support, IDF-6268
|
|
|
|
register_system_sleep();
|
|
|
|
#endif
|
|
|
|
#if SOC_WIFI_SUPPORTED
|
2017-08-15 04:11:19 -04:00
|
|
|
register_wifi();
|
2023-03-24 08:50:48 -04:00
|
|
|
#endif
|
2019-01-10 20:51:50 -05:00
|
|
|
register_nvs();
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2017-08-16 02:59:28 -04:00
|
|
|
/* Prompt to be printed before each line.
|
|
|
|
* This can be customized, made dynamic, etc.
|
|
|
|
*/
|
2020-04-02 18:16:54 -04:00
|
|
|
const char* prompt = LOG_COLOR_I PROMPT_STR "> " LOG_RESET_COLOR;
|
2017-08-16 02:59:28 -04:00
|
|
|
|
2017-08-15 04:11:19 -04:00
|
|
|
printf("\n"
|
|
|
|
"This is an example of ESP-IDF console component.\n"
|
|
|
|
"Type 'help' to get the list of commands.\n"
|
|
|
|
"Use UP/DOWN arrows to navigate through command history.\n"
|
2020-04-29 17:46:58 -04:00
|
|
|
"Press TAB when typing command name to auto-complete.\n"
|
|
|
|
"Press Enter or Ctrl+C will terminate the console environment.\n");
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2017-08-16 02:59:28 -04:00
|
|
|
/* Figure out if the terminal supports escape sequences */
|
|
|
|
int probe_status = linenoiseProbe();
|
|
|
|
if (probe_status) { /* zero indicates success */
|
|
|
|
printf("\n"
|
|
|
|
"Your terminal application does not support escape sequences.\n"
|
|
|
|
"Line editing and history features are disabled.\n"
|
|
|
|
"On Windows, try using Putty instead.\n");
|
|
|
|
linenoiseSetDumbMode(1);
|
|
|
|
#if CONFIG_LOG_COLORS
|
|
|
|
/* Since the terminal doesn't support escape sequences,
|
|
|
|
* don't use color codes in the prompt.
|
|
|
|
*/
|
2020-04-02 18:16:54 -04:00
|
|
|
prompt = PROMPT_STR "> ";
|
2017-08-16 02:59:28 -04:00
|
|
|
#endif //CONFIG_LOG_COLORS
|
|
|
|
}
|
2017-08-15 04:11:19 -04:00
|
|
|
|
|
|
|
/* Main loop */
|
2017-08-16 02:59:28 -04:00
|
|
|
while(true) {
|
|
|
|
/* Get a line using linenoise.
|
|
|
|
* The line is returned when ENTER is pressed.
|
|
|
|
*/
|
|
|
|
char* line = linenoise(prompt);
|
2020-03-12 18:10:40 -04:00
|
|
|
if (line == NULL) { /* Break on EOF or error */
|
|
|
|
break;
|
2017-08-16 02:59:28 -04:00
|
|
|
}
|
2020-03-16 00:09:46 -04:00
|
|
|
/* Add the command to the history if not empty*/
|
2020-04-02 18:16:21 -04:00
|
|
|
if (strlen(line) > 0) {
|
2020-03-16 00:09:46 -04:00
|
|
|
linenoiseHistoryAdd(line);
|
2017-08-15 04:11:19 -04:00
|
|
|
#if CONFIG_STORE_HISTORY
|
2020-03-16 00:09:46 -04:00
|
|
|
/* Save command history to filesystem */
|
|
|
|
linenoiseHistorySave(HISTORY_PATH);
|
2017-08-15 04:11:19 -04:00
|
|
|
#endif
|
2020-03-16 00:09:46 -04:00
|
|
|
}
|
2017-08-15 04:11:19 -04:00
|
|
|
|
2017-08-16 02:59:28 -04:00
|
|
|
/* Try to run the command */
|
|
|
|
int ret;
|
|
|
|
esp_err_t err = esp_console_run(line, &ret);
|
|
|
|
if (err == ESP_ERR_NOT_FOUND) {
|
|
|
|
printf("Unrecognized command\n");
|
2017-10-12 19:12:08 -04:00
|
|
|
} else if (err == ESP_ERR_INVALID_ARG) {
|
|
|
|
// command was empty
|
2017-08-16 02:59:28 -04:00
|
|
|
} else if (err == ESP_OK && ret != ESP_OK) {
|
2019-10-14 07:49:19 -04:00
|
|
|
printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
|
2017-08-16 02:59:28 -04:00
|
|
|
} else if (err != ESP_OK) {
|
2018-02-22 08:48:53 -05:00
|
|
|
printf("Internal error: %s\n", esp_err_to_name(err));
|
2017-08-15 04:11:19 -04:00
|
|
|
}
|
|
|
|
/* linenoise allocates line buffer on the heap, so need to free it */
|
|
|
|
linenoiseFree(line);
|
|
|
|
}
|
2020-04-02 18:16:21 -04:00
|
|
|
|
2020-03-12 18:10:40 -04:00
|
|
|
ESP_LOGE(TAG, "Error or end-of-input, terminating console");
|
|
|
|
esp_console_deinit();
|
2017-08-15 04:11:19 -04:00
|
|
|
}
|