cmd_system.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #include "esp_partition.h"
  25. #include "esp_ota_ops.h"
  26. #include "platform_esp32.h"
  27. #ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
  28. #define WITH_TASKS_INFO 1
  29. #endif
  30. #define LWS_MAGIC_REBOOT_TYPE_ADS 0x50001ffc
  31. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
  32. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
  33. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
  34. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA 0xfac0eeee
  35. static const char * TAG = "platform_esp32";
  36. static void register_free();
  37. static void register_heap();
  38. static void register_version();
  39. static void register_restart();
  40. static void register_deep_sleep();
  41. static void register_light_sleep();
  42. static void register_factory_boot();
  43. #if WITH_TASKS_INFO
  44. static void register_tasks();
  45. #endif
  46. void register_system()
  47. {
  48. register_free();
  49. register_heap();
  50. register_version();
  51. register_restart();
  52. register_deep_sleep();
  53. register_light_sleep();
  54. register_factory_boot();
  55. #if WITH_TASKS_INFO
  56. register_tasks();
  57. #endif
  58. }
  59. /* 'version' command */
  60. static int get_version(int argc, char **argv)
  61. {
  62. esp_chip_info_t info;
  63. esp_chip_info(&info);
  64. printf("IDF Version:%s\r\n", esp_get_idf_version());
  65. printf("Chip info:\r\n");
  66. printf("\tmodel:%s\r\n", info.model == CHIP_ESP32 ? "ESP32" : "Unknow");
  67. printf("\tcores:%d\r\n", info.cores);
  68. printf("\tfeature:%s%s%s%s%d%s\r\n",
  69. info.features & CHIP_FEATURE_WIFI_BGN ? "/802.11bgn" : "",
  70. info.features & CHIP_FEATURE_BLE ? "/BLE" : "",
  71. info.features & CHIP_FEATURE_BT ? "/BT" : "",
  72. info.features & CHIP_FEATURE_EMB_FLASH ? "/Embedded-Flash:" : "/External-Flash:",
  73. spi_flash_get_chip_size() / (1024 * 1024), " MB");
  74. printf("\trevision number:%d\r\n", info.revision);
  75. return 0;
  76. }
  77. static void register_version()
  78. {
  79. const esp_console_cmd_t cmd = {
  80. .command = "version",
  81. .help = "Get version of chip and SDK",
  82. .hint = NULL,
  83. .func = &get_version,
  84. };
  85. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  86. }
  87. /** 'restart' command restarts the program */
  88. esp_err_t guided_boot(esp_partition_subtype_t partition_subtype)
  89. {
  90. #if RECOVERY_APPLICATION
  91. if(partition_subtype ==ESP_PARTITION_SUBTYPE_APP_FACTORY){
  92. ESP_LOGW(TAG,"RECOVERY application is already active");
  93. return ESP_OK;
  94. }
  95. #endif
  96. bool bFound=false;
  97. ESP_LOGI(TAG, "Looking for partition type %u",partition_subtype);
  98. const esp_partition_t *partition;
  99. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, partition_subtype, NULL);
  100. if(it == NULL){
  101. ESP_LOGE(TAG,"Unable initialize partition iterator!");
  102. set_status_message(ERROR, "Reboot failed. Cannot iterate through partitions");
  103. }
  104. else
  105. {
  106. partition = (esp_partition_t *) esp_partition_get(it);
  107. if(partition != NULL){
  108. ESP_LOGI(TAG, "Found partition type %u",partition_subtype);
  109. esp_ota_set_boot_partition(partition);
  110. bFound=true;
  111. set_status_message(WARNING, "Rebooting!");
  112. }
  113. else
  114. {
  115. ESP_LOGE(TAG,"partition type %u not found! Unable to reboot to recovery.",partition_subtype);
  116. set_status_message(ERROR, "Partition not found.");
  117. }
  118. esp_partition_iterator_release(it);
  119. if(bFound) {
  120. ESP_LOGI(TAG, "Restarting!.");
  121. esp_restart();
  122. }
  123. }
  124. return ESP_OK;
  125. }
  126. static int restart(int argc, char **argv)
  127. {
  128. ESP_LOGI(TAG, "Restarting");
  129. guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  130. // If we're still alive, then there may not be an ota partition to boot from
  131. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  132. return 0; // return fail. This should never return... we're rebooting!
  133. }
  134. esp_err_t guided_restart_ota(){
  135. guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  136. // If we're still alive, then there may not be an ota partition to boot from
  137. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  138. return ESP_FAIL; // return fail. This should never return... we're rebooting!
  139. }
  140. esp_err_t guided_factory(){
  141. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  142. return ESP_FAIL; // return fail. This should never return... we're rebooting!
  143. }
  144. static int restart_factory(int argc, char **argv)
  145. {
  146. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  147. return 0; // return fail. This should never return... we're rebooting!
  148. }
  149. static void register_restart()
  150. {
  151. const esp_console_cmd_t cmd = {
  152. .command = "restart",
  153. .help = "Software reset of the chip",
  154. .hint = NULL,
  155. .func = &restart,
  156. };
  157. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  158. }
  159. static void register_factory_boot()
  160. {
  161. const esp_console_cmd_t cmd = {
  162. .command = "recovery",
  163. .help = "Resets and boot to recovery (if available)",
  164. .hint = NULL,
  165. .func = &restart_factory,
  166. };
  167. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  168. }
  169. /** 'free' command prints available heap memory */
  170. static int free_mem(int argc, char **argv)
  171. {
  172. printf("%d\n", esp_get_free_heap_size());
  173. return 0;
  174. }
  175. static void register_free()
  176. {
  177. const esp_console_cmd_t cmd = {
  178. .command = "free",
  179. .help = "Get the current size of free heap memory",
  180. .hint = NULL,
  181. .func = &free_mem,
  182. };
  183. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  184. }
  185. /* 'heap' command prints minumum heap size */
  186. static int heap_size(int argc, char **argv)
  187. {
  188. uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
  189. ESP_LOGI(TAG, "min heap size: %u", heap_size);
  190. return 0;
  191. }
  192. static void register_heap()
  193. {
  194. const esp_console_cmd_t heap_cmd = {
  195. .command = "heap",
  196. .help = "Get minimum size of free heap memory that was available during program execution",
  197. .hint = NULL,
  198. .func = &heap_size,
  199. };
  200. ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
  201. }
  202. /** 'tasks' command prints the list of tasks and related information */
  203. #if WITH_TASKS_INFO
  204. static int tasks_info(int argc, char **argv)
  205. {
  206. const size_t bytes_per_task = 40; /* see vTaskList description */
  207. char *task_list_buffer = malloc(uxTaskGetNumberOfTasks() * bytes_per_task);
  208. if (task_list_buffer == NULL) {
  209. ESP_LOGE(TAG, "failed to allocate buffer for vTaskList output");
  210. return 1;
  211. }
  212. fputs("Task Name\tStatus\tPrio\tHWM\tTask#", stdout);
  213. #ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
  214. fputs("\tAffinity", stdout);
  215. #endif
  216. fputs("\n", stdout);
  217. vTaskList(task_list_buffer);
  218. fputs(task_list_buffer, stdout);
  219. free(task_list_buffer);
  220. return 0;
  221. }
  222. static void register_tasks()
  223. {
  224. const esp_console_cmd_t cmd = {
  225. .command = "tasks",
  226. .help = "Get information about running tasks",
  227. .hint = NULL,
  228. .func = &tasks_info,
  229. };
  230. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  231. }
  232. #endif // WITH_TASKS_INFO
  233. /** 'deep_sleep' command puts the chip into deep sleep mode */
  234. static struct {
  235. struct arg_int *wakeup_time;
  236. struct arg_int *wakeup_gpio_num;
  237. struct arg_int *wakeup_gpio_level;
  238. struct arg_end *end;
  239. } deep_sleep_args;
  240. static int deep_sleep(int argc, char **argv)
  241. {
  242. int nerrors = arg_parse(argc, argv, (void **) &deep_sleep_args);
  243. if (nerrors != 0) {
  244. arg_print_errors(stderr, deep_sleep_args.end, argv[0]);
  245. return 1;
  246. }
  247. if (deep_sleep_args.wakeup_time->count) {
  248. uint64_t timeout = 1000ULL * deep_sleep_args.wakeup_time->ival[0];
  249. ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
  250. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  251. }
  252. if (deep_sleep_args.wakeup_gpio_num->count) {
  253. int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
  254. if (!rtc_gpio_is_valid_gpio(io_num)) {
  255. ESP_LOGE(TAG, "GPIO %d is not an RTC IO", io_num);
  256. return 1;
  257. }
  258. int level = 0;
  259. if (deep_sleep_args.wakeup_gpio_level->count) {
  260. level = deep_sleep_args.wakeup_gpio_level->ival[0];
  261. if (level != 0 && level != 1) {
  262. ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
  263. return 1;
  264. }
  265. }
  266. ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
  267. io_num, level ? "HIGH" : "LOW");
  268. ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
  269. }
  270. rtc_gpio_isolate(GPIO_NUM_12);
  271. esp_deep_sleep_start();
  272. }
  273. static void register_deep_sleep()
  274. {
  275. deep_sleep_args.wakeup_time =
  276. arg_int0("t", "time", "<t>", "Wake up time, ms");
  277. deep_sleep_args.wakeup_gpio_num =
  278. arg_int0(NULL, "io", "<n>",
  279. "If specified, wakeup using GPIO with given number");
  280. deep_sleep_args.wakeup_gpio_level =
  281. arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
  282. deep_sleep_args.end = arg_end(3);
  283. const esp_console_cmd_t cmd = {
  284. .command = "deep_sleep",
  285. .help = "Enter deep sleep mode. "
  286. "Two wakeup modes are supported: timer and GPIO. "
  287. "If no wakeup option is specified, will sleep indefinitely.",
  288. .hint = NULL,
  289. .func = &deep_sleep,
  290. .argtable = &deep_sleep_args
  291. };
  292. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  293. }
  294. /** 'light_sleep' command puts the chip into light sleep mode */
  295. static struct {
  296. struct arg_int *wakeup_time;
  297. struct arg_int *wakeup_gpio_num;
  298. struct arg_int *wakeup_gpio_level;
  299. struct arg_end *end;
  300. } light_sleep_args;
  301. static int light_sleep(int argc, char **argv)
  302. {
  303. int nerrors = arg_parse(argc, argv, (void **) &light_sleep_args);
  304. if (nerrors != 0) {
  305. arg_print_errors(stderr, light_sleep_args.end, argv[0]);
  306. return 1;
  307. }
  308. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
  309. if (light_sleep_args.wakeup_time->count) {
  310. uint64_t timeout = 1000ULL * light_sleep_args.wakeup_time->ival[0];
  311. ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
  312. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  313. }
  314. int io_count = light_sleep_args.wakeup_gpio_num->count;
  315. if (io_count != light_sleep_args.wakeup_gpio_level->count) {
  316. ESP_LOGE(TAG, "Should have same number of 'io' and 'io_level' arguments");
  317. return 1;
  318. }
  319. for (int i = 0; i < io_count; ++i) {
  320. int io_num = light_sleep_args.wakeup_gpio_num->ival[i];
  321. int level = light_sleep_args.wakeup_gpio_level->ival[i];
  322. if (level != 0 && level != 1) {
  323. ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
  324. return 1;
  325. }
  326. ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
  327. io_num, level ? "HIGH" : "LOW");
  328. ESP_ERROR_CHECK( gpio_wakeup_enable(io_num, level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL) );
  329. }
  330. if (io_count > 0) {
  331. ESP_ERROR_CHECK( esp_sleep_enable_gpio_wakeup() );
  332. }
  333. if (CONFIG_CONSOLE_UART_NUM <= UART_NUM_1) {
  334. ESP_LOGI(TAG, "Enabling UART wakeup (press ENTER to exit light sleep)");
  335. ESP_ERROR_CHECK( uart_set_wakeup_threshold(CONFIG_CONSOLE_UART_NUM, 3) );
  336. ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_CONSOLE_UART_NUM) );
  337. }
  338. fflush(stdout);
  339. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  340. esp_light_sleep_start();
  341. esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  342. const char *cause_str;
  343. switch (cause) {
  344. case ESP_SLEEP_WAKEUP_GPIO:
  345. cause_str = "GPIO";
  346. break;
  347. case ESP_SLEEP_WAKEUP_UART:
  348. cause_str = "UART";
  349. break;
  350. case ESP_SLEEP_WAKEUP_TIMER:
  351. cause_str = "timer";
  352. break;
  353. default:
  354. cause_str = "unknown";
  355. printf("%d\n", cause);
  356. }
  357. ESP_LOGI(TAG, "Woke up from: %s", cause_str);
  358. return 0;
  359. }
  360. static void register_light_sleep()
  361. {
  362. light_sleep_args.wakeup_time =
  363. arg_int0("t", "time", "<t>", "Wake up time, ms");
  364. light_sleep_args.wakeup_gpio_num =
  365. arg_intn(NULL, "io", "<n>", 0, 8,
  366. "If specified, wakeup using GPIO with given number");
  367. light_sleep_args.wakeup_gpio_level =
  368. arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
  369. light_sleep_args.end = arg_end(3);
  370. const esp_console_cmd_t cmd = {
  371. .command = "light_sleep",
  372. .help = "Enter light sleep mode. "
  373. "Two wakeup modes are supported: timer and GPIO. "
  374. "Multiple GPIO pins can be specified using pairs of "
  375. "'io' and 'io_level' arguments. "
  376. "Will also wake up on UART input.",
  377. .hint = NULL,
  378. .func = &light_sleep,
  379. .argtable = &light_sleep_args
  380. };
  381. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  382. }