Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions components/cli/include/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
#define __linux__

#include "driver/uart.h"
#include "driver/usb_serial_jtag.h"
#include "esp_err.h"
#include "esp_system.h"
#include "esp_vfs_dev.h"
#include "esp_vfs_usb_serial_jtag.h"

#include <sdkconfig.h>

#include <cli/cli.h>

#include "line_input.hpp"

#ifdef CONFIG_ESP_CONSOLE_USB_CDC
#error The cli component is incompatible with USB CDC console.
#endif // CONFIG_ESP_CONSOLE_USB_CDC

namespace espp {
/**
* @brief Class for implementing a basic Cli using the external cli library.
Expand Down Expand Up @@ -40,19 +49,52 @@ class Cli : private cli::CliSession {
if (configured) {
return;
}

// drain stdout before reconfiguring it
fflush(stdout);
fsync(fileno(stdout));

// Initialize VFS & UART so we can use std::cout/cin
// _IOFBF = full buffering
// _IOLBF = line buffering
// _IONBF = no buffering
// disable buffering on stdin
setvbuf(stdin, nullptr, _IONBF, 0);

// The code blow was generously provided from:
// https://github.com/espressif/esp-idf/issues/8789#issuecomment-1103523381
#if !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
/* 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 = {
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
.source_clk = UART_SCLK_REF_TICK,
#else
.source_clk = UART_SCLK_XTAL,
#endif
};
/* Install UART driver for interrupt-driven reads and writes */
ESP_ERROR_CHECK(
uart_driver_install((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0));
ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
256, 0, 0, NULL, 0) );
ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) );
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
#else // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
usb_serial_jtag_driver_config_t cfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
usb_serial_jtag_driver_install(&cfg);
esp_vfs_usb_serial_jtag_use_driver();
esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
#endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
configured = true;
}

Expand Down