Console component re-initialization problem

Air_Hamster
Posts: 3
Joined: Wed May 26, 2021 12:04 pm

Console component re-initialization problem

Postby Air_Hamster » Wed May 26, 2021 12:23 pm

Hello!

In our devices we need to initialize and deinitialize the console.
But RTOS thread stucks when calling setvbuf() the second time.
I think that it because setvbuf() tries to lock stdin but it is already locked.
How must I deinit console properly?

The code is presented below. Most of it taken from ESP-IDF system/console example.
Thanks.

Code: Select all

static void initialize_console(void)
{
    /* Register commands */
    esp_console_register_help_command();
    register_system();

    /* Drain stdout before reconfiguring it */
    fflush(stdout);
    fsync(fileno(stdout));

    /* Disable buffering on stdin */
    setvbuf(stdin, NULL, _IONBF, 0);  //<- stucks here
    
    /* 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);

    /* 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,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 120, // should be less than UART_FIFO_LEN (128)
        .source_clk = UART_SCLK_REF_TICK,
    };
    /* Install UART driver for interrupt-driven reads and writes */
    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);

    /* Initialize the console */
    esp_console_config_t console_config = {
        .max_cmdline_length = 256,
        .max_cmdline_args = 8,
#if CONFIG_LOG_COLORS
        .hint_color = atoi(LOG_COLOR_CYAN),
        .hint_bold = 1
#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);

    /* Don't return empty lines */
    linenoiseAllowEmpty(false);
}

void CommandLineTask(void* argument)
{
    /* Main loop with linenoise processing, nothing interesting here */
}

void cli_start(void)
{
    initialize_console();
    xTaskCreatePinnedToCore(CommandLineTask, "CLI", 4 * configMINIMAL_STACK_SIZE, NULL, 3,
                            &TaskHandleCommandLine, 0);
}

void cli_stop(void)
{
    ESP_LOGI(TAG, "Terminating console");
    vTaskDelete(TaskHandleCommandLine);
    esp_console_deinit();
}
Ilia Lutchenko
Embedded Engineer

Who is online

Users browsing this forum: Baidu [Spider] and 65 guests