cmd_system.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* Console example — various system commands
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include "esp_log.h"
  11. #include "esp_console.h"
  12. #include "esp_system.h"
  13. #include "esp_sleep.h"
  14. #include "esp_spi_flash.h"
  15. #include "driver/rtc_io.h"
  16. #include "driver/uart.h"
  17. #include "argtable3/argtable3.h"
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/task.h"
  20. #include "soc/rtc_cntl_reg.h"
  21. #include "esp32/rom/uart.h"
  22. #include "cmd_system.h"
  23. #include "sdkconfig.h"
  24. #ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
  25. #define WITH_TASKS_INFO 1
  26. #endif
  27. #define LWS_MAGIC_REBOOT_TYPE_ADS 0x50001ffc
  28. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
  29. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
  30. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
  31. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA 0xfac0eeee
  32. static const char * TAG = "platform_esp32";
  33. static void register_free();
  34. static void register_heap();
  35. static void register_version();
  36. static void register_restart();
  37. static void register_deep_sleep();
  38. static void register_light_sleep();
  39. static void register_factory_boot();
  40. #if WITH_TASKS_INFO
  41. static void register_tasks();
  42. #endif
  43. void register_system()
  44. {
  45. register_free();
  46. register_heap();
  47. register_version();
  48. register_restart();
  49. register_deep_sleep();
  50. register_light_sleep();
  51. register_factory_boot();
  52. #if WITH_TASKS_INFO
  53. register_tasks();
  54. #endif
  55. }
  56. /* 'version' command */
  57. static int get_version(int argc, char **argv)
  58. {
  59. esp_chip_info_t info;
  60. esp_chip_info(&info);
  61. printf("IDF Version:%s\r\n", esp_get_idf_version());
  62. printf("Chip info:\r\n");
  63. printf("\tmodel:%s\r\n", info.model == CHIP_ESP32 ? "ESP32" : "Unknow");
  64. printf("\tcores:%d\r\n", info.cores);
  65. printf("\tfeature:%s%s%s%s%d%s\r\n",
  66. info.features & CHIP_FEATURE_WIFI_BGN ? "/802.11bgn" : "",
  67. info.features & CHIP_FEATURE_BLE ? "/BLE" : "",
  68. info.features & CHIP_FEATURE_BT ? "/BT" : "",
  69. info.features & CHIP_FEATURE_EMB_FLASH ? "/Embedded-Flash:" : "/External-Flash:",
  70. spi_flash_get_chip_size() / (1024 * 1024), " MB");
  71. printf("\trevision number:%d\r\n", info.revision);
  72. return 0;
  73. }
  74. static void register_version()
  75. {
  76. const esp_console_cmd_t cmd = {
  77. .command = "version",
  78. .help = "Get version of chip and SDK",
  79. .hint = NULL,
  80. .func = &get_version,
  81. };
  82. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  83. }
  84. /** 'restart' command restarts the program */
  85. static int restart(int argc, char **argv)
  86. {
  87. ESP_LOGI(TAG, "Restarting");
  88. esp_restart();
  89. }
  90. void guided_factory()
  91. {
  92. ESP_LOGI(TAG, "Rebooting to factory.");
  93. uint32_t *p_force_factory_magic = (uint32_t *)LWS_MAGIC_REBOOT_TYPE_ADS;
  94. *p_force_factory_magic = LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY;
  95. esp_restart();
  96. }
  97. static int restart_factory(int argc, char **argv)
  98. {
  99. guided_factory();
  100. return 1;
  101. }
  102. static void register_restart()
  103. {
  104. const esp_console_cmd_t cmd = {
  105. .command = "restart",
  106. .help = "Software reset of the chip",
  107. .hint = NULL,
  108. .func = &restart,
  109. };
  110. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  111. }
  112. static void register_factory_boot()
  113. {
  114. const esp_console_cmd_t cmd = {
  115. .command = "factory",
  116. .help = "Resets and boot to factory (if available)",
  117. .hint = NULL,
  118. .func = &restart_factory,
  119. };
  120. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  121. }
  122. /** 'free' command prints available heap memory */
  123. static int free_mem(int argc, char **argv)
  124. {
  125. printf("%d\n", esp_get_free_heap_size());
  126. return 0;
  127. }
  128. static void register_free()
  129. {
  130. const esp_console_cmd_t cmd = {
  131. .command = "free",
  132. .help = "Get the current size of free heap memory",
  133. .hint = NULL,
  134. .func = &free_mem,
  135. };
  136. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  137. }
  138. /* 'heap' command prints minumum heap size */
  139. static int heap_size(int argc, char **argv)
  140. {
  141. uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
  142. ESP_LOGI(TAG, "min heap size: %u", heap_size);
  143. return 0;
  144. }
  145. static void register_heap()
  146. {
  147. const esp_console_cmd_t heap_cmd = {
  148. .command = "heap",
  149. .help = "Get minimum size of free heap memory that was available during program execution",
  150. .hint = NULL,
  151. .func = &heap_size,
  152. };
  153. ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
  154. }
  155. /** 'tasks' command prints the list of tasks and related information */
  156. #if WITH_TASKS_INFO
  157. static int tasks_info(int argc, char **argv)
  158. {
  159. const size_t bytes_per_task = 40; /* see vTaskList description */
  160. char *task_list_buffer = malloc(uxTaskGetNumberOfTasks() * bytes_per_task);
  161. if (task_list_buffer == NULL) {
  162. ESP_LOGE(TAG, "failed to allocate buffer for vTaskList output");
  163. return 1;
  164. }
  165. fputs("Task Name\tStatus\tPrio\tHWM\tTask#", stdout);
  166. #ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
  167. fputs("\tAffinity", stdout);
  168. #endif
  169. fputs("\n", stdout);
  170. vTaskList(task_list_buffer);
  171. fputs(task_list_buffer, stdout);
  172. free(task_list_buffer);
  173. return 0;
  174. }
  175. static void register_tasks()
  176. {
  177. const esp_console_cmd_t cmd = {
  178. .command = "tasks",
  179. .help = "Get information about running tasks",
  180. .hint = NULL,
  181. .func = &tasks_info,
  182. };
  183. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  184. }
  185. #endif // WITH_TASKS_INFO
  186. /** 'deep_sleep' command puts the chip into deep sleep mode */
  187. static struct {
  188. struct arg_int *wakeup_time;
  189. struct arg_int *wakeup_gpio_num;
  190. struct arg_int *wakeup_gpio_level;
  191. struct arg_end *end;
  192. } deep_sleep_args;
  193. static int deep_sleep(int argc, char **argv)
  194. {
  195. int nerrors = arg_parse(argc, argv, (void **) &deep_sleep_args);
  196. if (nerrors != 0) {
  197. arg_print_errors(stderr, deep_sleep_args.end, argv[0]);
  198. return 1;
  199. }
  200. if (deep_sleep_args.wakeup_time->count) {
  201. uint64_t timeout = 1000ULL * deep_sleep_args.wakeup_time->ival[0];
  202. ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
  203. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  204. }
  205. if (deep_sleep_args.wakeup_gpio_num->count) {
  206. int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
  207. if (!rtc_gpio_is_valid_gpio(io_num)) {
  208. ESP_LOGE(TAG, "GPIO %d is not an RTC IO", io_num);
  209. return 1;
  210. }
  211. int level = 0;
  212. if (deep_sleep_args.wakeup_gpio_level->count) {
  213. level = deep_sleep_args.wakeup_gpio_level->ival[0];
  214. if (level != 0 && level != 1) {
  215. ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
  216. return 1;
  217. }
  218. }
  219. ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
  220. io_num, level ? "HIGH" : "LOW");
  221. ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
  222. }
  223. rtc_gpio_isolate(GPIO_NUM_12);
  224. esp_deep_sleep_start();
  225. }
  226. static void register_deep_sleep()
  227. {
  228. deep_sleep_args.wakeup_time =
  229. arg_int0("t", "time", "<t>", "Wake up time, ms");
  230. deep_sleep_args.wakeup_gpio_num =
  231. arg_int0(NULL, "io", "<n>",
  232. "If specified, wakeup using GPIO with given number");
  233. deep_sleep_args.wakeup_gpio_level =
  234. arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
  235. deep_sleep_args.end = arg_end(3);
  236. const esp_console_cmd_t cmd = {
  237. .command = "deep_sleep",
  238. .help = "Enter deep sleep mode. "
  239. "Two wakeup modes are supported: timer and GPIO. "
  240. "If no wakeup option is specified, will sleep indefinitely.",
  241. .hint = NULL,
  242. .func = &deep_sleep,
  243. .argtable = &deep_sleep_args
  244. };
  245. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  246. }
  247. /** 'light_sleep' command puts the chip into light sleep mode */
  248. static struct {
  249. struct arg_int *wakeup_time;
  250. struct arg_int *wakeup_gpio_num;
  251. struct arg_int *wakeup_gpio_level;
  252. struct arg_end *end;
  253. } light_sleep_args;
  254. static int light_sleep(int argc, char **argv)
  255. {
  256. int nerrors = arg_parse(argc, argv, (void **) &light_sleep_args);
  257. if (nerrors != 0) {
  258. arg_print_errors(stderr, light_sleep_args.end, argv[0]);
  259. return 1;
  260. }
  261. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
  262. if (light_sleep_args.wakeup_time->count) {
  263. uint64_t timeout = 1000ULL * light_sleep_args.wakeup_time->ival[0];
  264. ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
  265. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  266. }
  267. int io_count = light_sleep_args.wakeup_gpio_num->count;
  268. if (io_count != light_sleep_args.wakeup_gpio_level->count) {
  269. ESP_LOGE(TAG, "Should have same number of 'io' and 'io_level' arguments");
  270. return 1;
  271. }
  272. for (int i = 0; i < io_count; ++i) {
  273. int io_num = light_sleep_args.wakeup_gpio_num->ival[i];
  274. int level = light_sleep_args.wakeup_gpio_level->ival[i];
  275. if (level != 0 && level != 1) {
  276. ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
  277. return 1;
  278. }
  279. ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
  280. io_num, level ? "HIGH" : "LOW");
  281. ESP_ERROR_CHECK( gpio_wakeup_enable(io_num, level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL) );
  282. }
  283. if (io_count > 0) {
  284. ESP_ERROR_CHECK( esp_sleep_enable_gpio_wakeup() );
  285. }
  286. if (CONFIG_CONSOLE_UART_NUM <= UART_NUM_1) {
  287. ESP_LOGI(TAG, "Enabling UART wakeup (press ENTER to exit light sleep)");
  288. ESP_ERROR_CHECK( uart_set_wakeup_threshold(CONFIG_CONSOLE_UART_NUM, 3) );
  289. ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_CONSOLE_UART_NUM) );
  290. }
  291. fflush(stdout);
  292. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  293. esp_light_sleep_start();
  294. esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  295. const char *cause_str;
  296. switch (cause) {
  297. case ESP_SLEEP_WAKEUP_GPIO:
  298. cause_str = "GPIO";
  299. break;
  300. case ESP_SLEEP_WAKEUP_UART:
  301. cause_str = "UART";
  302. break;
  303. case ESP_SLEEP_WAKEUP_TIMER:
  304. cause_str = "timer";
  305. break;
  306. default:
  307. cause_str = "unknown";
  308. printf("%d\n", cause);
  309. }
  310. ESP_LOGI(TAG, "Woke up from: %s", cause_str);
  311. return 0;
  312. }
  313. static void register_light_sleep()
  314. {
  315. light_sleep_args.wakeup_time =
  316. arg_int0("t", "time", "<t>", "Wake up time, ms");
  317. light_sleep_args.wakeup_gpio_num =
  318. arg_intn(NULL, "io", "<n>", 0, 8,
  319. "If specified, wakeup using GPIO with given number");
  320. light_sleep_args.wakeup_gpio_level =
  321. arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
  322. light_sleep_args.end = arg_end(3);
  323. const esp_console_cmd_t cmd = {
  324. .command = "light_sleep",
  325. .help = "Enter light sleep mode. "
  326. "Two wakeup modes are supported: timer and GPIO. "
  327. "Multiple GPIO pins can be specified using pairs of "
  328. "'io' and 'io_level' arguments. "
  329. "Will also wake up on UART input.",
  330. .hint = NULL,
  331. .func = &light_sleep,
  332. .argtable = &light_sleep_args
  333. };
  334. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  335. }