]> granicus.if.org Git - esp-idf/blob - examples/system/console/main/console_example_main.c
esp32: Allow SPIRAM_MALLOC_RESERVE_INTERNAL to span multiple regions of memory
[esp-idf] / examples / system / console / main / console_example_main.c
1 /* Console example
2
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include "esp_system.h"
13 #include "esp_log.h"
14 #include "esp_console.h"
15 #include "esp_vfs_dev.h"
16 #include "driver/uart.h"
17 #include "linenoise/linenoise.h"
18 #include "argtable3/argtable3.h"
19 #include "cmd_decl.h"
20 #include "esp_vfs_fat.h"
21 #include "nvs.h"
22 #include "nvs_flash.h"
23
24 static const char* TAG = "example";
25
26 /* Console command history can be stored to and loaded from a file.
27  * The easiest way to do this is to use FATFS filesystem on top of
28  * wear_levelling library.
29  */
30 #if CONFIG_STORE_HISTORY
31
32 #define MOUNT_PATH "/data"
33 #define HISTORY_PATH MOUNT_PATH "/history.txt"
34
35 static void initialize_filesystem()
36 {
37     static wl_handle_t wl_handle;
38     const esp_vfs_fat_mount_config_t mount_config = {
39             .max_files = 4,
40             .format_if_mount_failed = true
41     };
42     esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle);
43     if (err != ESP_OK) {
44         ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
45         return;
46     }
47 }
48 #endif // CONFIG_STORE_HISTORY
49
50 static void initialize_nvs()
51 {
52     esp_err_t err = nvs_flash_init();
53     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
54         ESP_ERROR_CHECK( nvs_flash_erase() );
55         err = nvs_flash_init();
56     }
57     ESP_ERROR_CHECK(err);
58 }
59
60 static void initialize_console()
61 {
62     /* Disable buffering on stdin and stdout */
63     setvbuf(stdin, NULL, _IONBF, 0);
64     setvbuf(stdout, NULL, _IONBF, 0);
65
66     /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
67     esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
68     /* Move the caret to the beginning of the next line on '\n' */
69     esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
70
71     /* Install UART driver for interrupt-driven reads and writes */
72     ESP_ERROR_CHECK( uart_driver_install(CONFIG_CONSOLE_UART_NUM,
73             256, 0, 0, NULL, 0) );
74
75     /* Tell VFS to use UART driver */
76     esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
77
78     /* Initialize the console */
79     esp_console_config_t console_config = {
80             .max_cmdline_args = 8,
81             .max_cmdline_length = 256,
82 #if CONFIG_LOG_COLORS
83             .hint_color = atoi(LOG_COLOR_CYAN)
84 #endif
85     };
86     ESP_ERROR_CHECK( esp_console_init(&console_config) );
87
88     /* Configure linenoise line completion library */
89     /* Enable multiline editing. If not set, long commands will scroll within
90      * single line.
91      */
92     linenoiseSetMultiLine(1);
93
94     /* Tell linenoise where to get command completions and hints */
95     linenoiseSetCompletionCallback(&esp_console_get_completion);
96     linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
97
98     /* Set command history size */
99     linenoiseHistorySetMaxLen(100);
100
101 #if CONFIG_STORE_HISTORY
102     /* Load command history from filesystem */
103     linenoiseHistoryLoad(HISTORY_PATH);
104 #endif
105 }
106
107 void app_main()
108 {
109     initialize_nvs();
110
111 #if CONFIG_STORE_HISTORY
112     initialize_filesystem();
113 #endif
114
115     initialize_console();
116
117     /* Register commands */
118     esp_console_register_help_command();
119     register_system();
120     register_wifi();
121
122     /* Prompt to be printed before each line.
123      * This can be customized, made dynamic, etc.
124      */
125     const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
126
127     printf("\n"
128            "This is an example of ESP-IDF console component.\n"
129            "Type 'help' to get the list of commands.\n"
130            "Use UP/DOWN arrows to navigate through command history.\n"
131            "Press TAB when typing command name to auto-complete.\n");
132
133     /* Figure out if the terminal supports escape sequences */
134     int probe_status = linenoiseProbe();
135     if (probe_status) { /* zero indicates success */
136         printf("\n"
137                "Your terminal application does not support escape sequences.\n"
138                "Line editing and history features are disabled.\n"
139                "On Windows, try using Putty instead.\n");
140         linenoiseSetDumbMode(1);
141 #if CONFIG_LOG_COLORS
142         /* Since the terminal doesn't support escape sequences,
143          * don't use color codes in the prompt.
144          */
145         prompt = "esp32> ";
146 #endif //CONFIG_LOG_COLORS
147     }
148
149     /* Main loop */
150     while(true) {
151         /* Get a line using linenoise.
152          * The line is returned when ENTER is pressed.
153          */
154         char* line = linenoise(prompt);
155         if (line == NULL) { /* Ignore empty lines */
156             continue;
157         }
158         /* Add the command to the history */
159         linenoiseHistoryAdd(line);
160 #if CONFIG_STORE_HISTORY
161         /* Save command history to filesystem */
162         linenoiseHistorySave(HISTORY_PATH);
163 #endif
164
165         /* Try to run the command */
166         int ret;
167         esp_err_t err = esp_console_run(line, &ret);
168         if (err == ESP_ERR_NOT_FOUND) {
169             printf("Unrecognized command\n");
170         } else if (err == ESP_ERR_INVALID_ARG) {
171             // command was empty
172         } else if (err == ESP_OK && ret != ESP_OK) {
173             printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(err));
174         } else if (err != ESP_OK) {
175             printf("Internal error: %s\n", esp_err_to_name(err));
176         }
177         /* linenoise allocates line buffer on the heap, so need to free it */
178         linenoiseFree(line);
179     }
180 }