]> granicus.if.org Git - esp-idf/blob
d3dc6c12f9dc6846b86c6978369d2cb89caf7bfa
[esp-idf] /
1 /* Console example — various system commands
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 <ctype.h>
13
14 #include "esp_log.h"
15 #include "esp_console.h"
16 #include "esp_system.h"
17 #include "esp_sleep.h"
18 #include "driver/rtc_io.h"
19 #include "argtable3/argtable3.h"
20
21 #include "ble_mesh_console_decl.h"
22
23 #if CONFIG_IDF_CMAKE
24 #define CONFIG_ESPTOOLPY_PORT "Which is choosen by Users for CMake"
25 #endif
26
27 static void register_free();
28 static void register_restart();
29 static void register_make();
30
31 void register_system()
32 {
33     register_free();
34     register_restart();
35     register_make();
36 }
37
38 /** 'restart' command restarts the program */
39
40 static int restart(int argc, char **argv)
41 {
42     printf("%s, %s", __func__, "Restarting");
43     esp_restart();
44 }
45
46 static void register_restart()
47 {
48     const esp_console_cmd_t cmd = {
49         .command = "restart",
50         .help = "Restart the program",
51         .hint = NULL,
52         .func = &restart,
53     };
54     ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
55 }
56
57 /** 'free' command prints available heap memory */
58
59 static int free_mem(int argc, char **argv)
60 {
61     printf("%d\n", esp_get_free_heap_size());
62     return 0;
63 }
64
65 static void register_free()
66 {
67     const esp_console_cmd_t cmd = {
68         .command = "free",
69         .help = "Get the total size of heap memory available",
70         .hint = NULL,
71         .func = &free_mem,
72     };
73     ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
74 }
75
76 static int make(int argc, char **argv)
77 {
78     int count = REG_READ(RTC_CNTL_STORE0_REG);
79     if (++count >= 3) {
80         printf("This is not the console you are looking for.\n");
81         return 0;
82     }
83     REG_WRITE(RTC_CNTL_STORE0_REG, count);
84
85     const char *make_output =
86         R"(LD build/console.elf
87 esptool.py v2.1-beta1
88 )";
89
90     const char* flash_output[] = {
91 R"(Flashing binaries to serial port )" CONFIG_ESPTOOLPY_PORT R"( (app at offset 0x10000)...
92 esptool.py v2.1-beta1
93 Connecting....
94 )",
95 R"(Chip is ESP32D0WDQ6 (revision 0)
96 Uploading stub...
97 Running stub...
98 Stub running...
99 Changing baud rate to 921600
100 Changed.
101 Configuring flash size...
102 Auto-detected Flash size: 4MB
103 Flash params set to 0x0220
104 Compressed 15712 bytes to 9345...
105 )",
106 R"(Wrote 15712 bytes (9345 compressed) at 0x00001000 in 0.1 seconds (effective 1126.9 kbit/s)...
107 Hash of data verified.
108 Compressed 333776 bytes to 197830...
109 )",
110 R"(Wrote 333776 bytes (197830 compressed) at 0x00010000 in 3.3 seconds (effective 810.3 kbit/s)...
111 Hash of data verified.
112 Compressed 3072 bytes to 82...
113 )",
114 R"(Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 1588.4 kbit/s)...
115 Hash of data verified.
116 Leaving...
117 Hard resetting...
118 )"
119     };
120
121     const char* monitor_output =
122 R"(MONITOR
123 )" LOG_COLOR_W R"(--- idf_monitor on )" CONFIG_ESPTOOLPY_PORT R"( 115200 ---
124 --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --
125 )" LOG_RESET_COLOR;
126
127     bool need_make = false;
128     bool need_flash = false;
129     bool need_monitor = false;
130     for (int i = 1; i < argc; ++i) {
131         if (strcmp(argv[i], "all") == 0) {
132             need_make = true;
133         } else if (strcmp(argv[i], "flash") == 0) {
134             need_make = true;
135             need_flash = true;
136         } else if (strcmp(argv[i], "monitor") == 0) {
137             need_monitor = true;
138         } else if (argv[i][0] == '-') {
139             /* probably -j option */
140         } else if (isdigit((int) argv[i][0])) {
141             /* might be an argument to -j */
142         } else {
143             printf("make: *** No rule to make target `%s'.  Stop.\n", argv[i]);
144             /* Technically this is an error, but let's not spoil the output */
145             return 0;
146         }
147     }
148     if (argc == 1) {
149         need_make = true;
150     }
151     if (need_make) {
152         printf("%s", make_output);
153     }
154     if (need_flash) {
155         size_t n_items = sizeof(flash_output) / sizeof(flash_output[0]);
156         for (int i = 0; i < n_items; ++i) {
157             printf("%s", flash_output[i]);
158             vTaskDelay(200/portTICK_PERIOD_MS);
159         }
160     }
161     if (need_monitor) {
162         printf("%s", monitor_output);
163         esp_restart();
164     }
165     return 0;
166 }
167
168 static void register_make()
169 {
170     const esp_console_cmd_t cmd = {
171         .command = "make",
172         .help = NULL, /* Do not include in 'help' output */
173         .hint = "all | flash | monitor",
174         .func = &make,
175     };
176     ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
177 }
178
179