cmd_system.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #define LOG_LOCAL_LEVEL ESP_LOG_INFO
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include "esp_log.h"
  12. #include "esp_console.h"
  13. #include "esp_system.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. #include "config.h"
  28. #include "esp_sleep.h"
  29. #include "driver/uart.h" // for the uart driver access
  30. #ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
  31. #define WITH_TASKS_INFO 1
  32. #endif
  33. static const char * TAG = "cmd_system";
  34. static void register_free();
  35. static void register_heap();
  36. static void register_version();
  37. static void register_restart();
  38. static void register_deep_sleep();
  39. static void register_light_sleep();
  40. static void register_factory_boot();
  41. static void register_restart_ota();
  42. #if WITH_TASKS_INFO
  43. static void register_tasks();
  44. #endif
  45. void register_system()
  46. {
  47. register_free();
  48. register_heap();
  49. register_version();
  50. register_restart();
  51. register_deep_sleep();
  52. register_light_sleep();
  53. register_factory_boot();
  54. register_restart_ota();
  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. if(!wait_for_commit()){
  94. ESP_LOGW(TAG,"Unable to commit configuration. ");
  95. }
  96. ESP_LOGW(TAG, "Restarting after tx complete");
  97. uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
  98. esp_restart();
  99. return ESP_OK;
  100. }
  101. #else
  102. if(partition_subtype !=ESP_PARTITION_SUBTYPE_APP_FACTORY){
  103. ESP_LOGW(TAG,"SQUEEZELITE application is already active");
  104. if(!wait_for_commit()){
  105. ESP_LOGW(TAG,"Unable to commit configuration. ");
  106. }
  107. ESP_LOGW(TAG, "Restarting after tx complete");
  108. uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
  109. esp_restart();
  110. return ESP_OK;
  111. }
  112. #endif
  113. esp_err_t err = ESP_OK;
  114. bool bFound=false;
  115. ESP_LOGI(TAG, "Looking for partition type %u",partition_subtype);
  116. const esp_partition_t *partition;
  117. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, partition_subtype, NULL);
  118. if(it == NULL){
  119. ESP_LOGE(TAG,"Unable initialize partition iterator!");
  120. set_status_message(ERROR, "Reboot failed. Cannot iterate through partitions");
  121. }
  122. else
  123. {
  124. ESP_LOGD(TAG, "Found partition. Getting info.");
  125. partition = (esp_partition_t *) esp_partition_get(it);
  126. ESP_LOGD(TAG, "Releasing partition iterator");
  127. esp_partition_iterator_release(it);
  128. if(partition != NULL){
  129. ESP_LOGI(TAG, "Found application partition %s sub type %u", partition->label,partition_subtype);
  130. err=esp_ota_set_boot_partition(partition);
  131. if(err!=ESP_OK){
  132. ESP_LOGE(TAG,"Unable to set partition as active for next boot. %s",esp_err_to_name(err));
  133. bFound=false;
  134. set_status_message(ERROR, "Unable to select partition for reboot.");
  135. }
  136. else{
  137. ESP_LOGW(TAG, "Application partition %s sub type %u is selected for boot", partition->label,partition_subtype);
  138. bFound=true;
  139. set_status_message(WARNING, "Rebooting!");
  140. }
  141. }
  142. else
  143. {
  144. ESP_LOGE(TAG,"partition type %u not found! Unable to reboot to recovery.",partition_subtype);
  145. set_status_message(ERROR, "Partition not found.");
  146. }
  147. ESP_LOGD(TAG, "Yielding to other processes");
  148. taskYIELD();
  149. if(bFound) {
  150. ESP_LOGW(TAG,"Configuration %s changes. ",config_has_changes()?"has":"does not have");
  151. if(!wait_for_commit()){
  152. ESP_LOGW(TAG,"Unable to commit configuration. ");
  153. }
  154. ESP_LOGW(TAG, "Restarting after tx complete");
  155. uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
  156. esp_restart();
  157. }
  158. }
  159. return ESP_OK;
  160. }
  161. static int restart(int argc, char **argv)
  162. {
  163. ESP_LOGW(TAG, "\n\nPerforming a simple restart to the currently active partition.");
  164. if(!wait_for_commit()){
  165. ESP_LOGW(TAG,"Unable to commit configuration. ");
  166. }
  167. ESP_LOGW(TAG, "Restarting after tx complete");
  168. uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
  169. esp_restart();
  170. return 0;
  171. }
  172. void simple_restart()
  173. {
  174. ESP_LOGW(TAG,"\n\n Called to perform a simple system reboot.");
  175. if(!wait_for_commit()){
  176. ESP_LOGW(TAG,"Unable to commit configuration. ");
  177. }
  178. ESP_LOGW(TAG, "Restarting after tx complete");
  179. uart_wait_tx_done(UART_NUM_1, 500 / portTICK_RATE_MS);
  180. esp_restart();
  181. }
  182. esp_err_t guided_restart_ota(){
  183. ESP_LOGW(TAG,"\n\nCalled for a reboot to OTA Application");
  184. guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  185. return ESP_FAIL; // return fail. This should never return... we're rebooting!
  186. }
  187. esp_err_t guided_factory(){
  188. ESP_LOGW(TAG,"\n\nCalled for a reboot to recovery application");
  189. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  190. return ESP_FAIL; // return fail. This should never return... we're rebooting!
  191. }
  192. static int restart_factory(int argc, char **argv)
  193. {
  194. ESP_LOGW(TAG, "Executing guided boot into recovery");
  195. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  196. return 0; // return fail. This should never return... we're rebooting!
  197. }
  198. static int restart_ota(int argc, char **argv)
  199. {
  200. ESP_LOGW(TAG, "Executing guided boot into ota app 0");
  201. guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  202. return 0; // return fail. This should never return... we're rebooting!
  203. }
  204. static void register_restart()
  205. {
  206. const esp_console_cmd_t cmd = {
  207. .command = "restart",
  208. .help = "Software reset of the chip",
  209. .hint = NULL,
  210. .func = &restart,
  211. };
  212. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  213. }
  214. static void register_restart_ota()
  215. {
  216. const esp_console_cmd_t cmd = {
  217. .command = "restart_ota",
  218. .help = "Selects the ota app partition to boot from and performa a software reset of the chip",
  219. .hint = NULL,
  220. .func = &restart_ota,
  221. };
  222. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  223. }
  224. static void register_factory_boot()
  225. {
  226. const esp_console_cmd_t cmd = {
  227. .command = "recovery",
  228. .help = "Resets and boot to recovery (if available)",
  229. .hint = NULL,
  230. .func = &restart_factory,
  231. };
  232. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  233. }
  234. /** 'free' command prints available heap memory */
  235. static int free_mem(int argc, char **argv)
  236. {
  237. printf("%d\n", esp_get_free_heap_size());
  238. return 0;
  239. }
  240. static void register_free()
  241. {
  242. const esp_console_cmd_t cmd = {
  243. .command = "free",
  244. .help = "Get the current size of free heap memory",
  245. .hint = NULL,
  246. .func = &free_mem,
  247. };
  248. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  249. }
  250. /* 'heap' command prints minumum heap size */
  251. static int heap_size(int argc, char **argv)
  252. {
  253. uint32_t heap_size = heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
  254. ESP_LOGI(TAG, "min heap size: %u", heap_size);
  255. return 0;
  256. }
  257. static void register_heap()
  258. {
  259. const esp_console_cmd_t heap_cmd = {
  260. .command = "heap",
  261. .help = "Get minimum size of free heap memory that was available during program execution",
  262. .hint = NULL,
  263. .func = &heap_size,
  264. };
  265. ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
  266. }
  267. /** 'tasks' command prints the list of tasks and related information */
  268. #if WITH_TASKS_INFO
  269. static int tasks_info(int argc, char **argv)
  270. {
  271. const size_t bytes_per_task = 40; /* see vTaskList description */
  272. char *task_list_buffer = malloc(uxTaskGetNumberOfTasks() * bytes_per_task);
  273. if (task_list_buffer == NULL) {
  274. ESP_LOGE(TAG, "failed to allocate buffer for vTaskList output");
  275. return 1;
  276. }
  277. fputs("Task Name\tStatus\tPrio\tHWM\tTask#", stdout);
  278. #ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
  279. fputs("\tAffinity", stdout);
  280. #endif
  281. fputs("\n", stdout);
  282. vTaskList(task_list_buffer);
  283. fputs(task_list_buffer, stdout);
  284. free(task_list_buffer);
  285. return 0;
  286. }
  287. static void register_tasks()
  288. {
  289. const esp_console_cmd_t cmd = {
  290. .command = "tasks",
  291. .help = "Get information about running tasks",
  292. .hint = NULL,
  293. .func = &tasks_info,
  294. };
  295. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  296. }
  297. #endif // WITH_TASKS_INFO
  298. /** 'deep_sleep' command puts the chip into deep sleep mode */
  299. static struct {
  300. struct arg_int *wakeup_time;
  301. struct arg_int *wakeup_gpio_num;
  302. struct arg_int *wakeup_gpio_level;
  303. struct arg_end *end;
  304. } deep_sleep_args;
  305. static int deep_sleep(int argc, char **argv)
  306. {
  307. int nerrors = arg_parse(argc, argv, (void **) &deep_sleep_args);
  308. if (nerrors != 0) {
  309. arg_print_errors(stderr, deep_sleep_args.end, argv[0]);
  310. return 1;
  311. }
  312. if (deep_sleep_args.wakeup_time->count) {
  313. uint64_t timeout = 1000ULL * deep_sleep_args.wakeup_time->ival[0];
  314. ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
  315. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  316. }
  317. if (deep_sleep_args.wakeup_gpio_num->count) {
  318. int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
  319. if (!rtc_gpio_is_valid_gpio(io_num)) {
  320. ESP_LOGE(TAG, "GPIO %d is not an RTC IO", io_num);
  321. return 1;
  322. }
  323. int level = 0;
  324. if (deep_sleep_args.wakeup_gpio_level->count) {
  325. level = deep_sleep_args.wakeup_gpio_level->ival[0];
  326. if (level != 0 && level != 1) {
  327. ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
  328. return 1;
  329. }
  330. }
  331. ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
  332. io_num, level ? "HIGH" : "LOW");
  333. ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
  334. }
  335. rtc_gpio_isolate(GPIO_NUM_12);
  336. esp_deep_sleep_start();
  337. }
  338. static void register_deep_sleep()
  339. {
  340. deep_sleep_args.wakeup_time =
  341. arg_int0("t", "time", "<t>", "Wake up time, ms");
  342. deep_sleep_args.wakeup_gpio_num =
  343. arg_int0(NULL, "io", "<n>",
  344. "If specified, wakeup using GPIO with given number");
  345. deep_sleep_args.wakeup_gpio_level =
  346. arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
  347. deep_sleep_args.end = arg_end(3);
  348. const esp_console_cmd_t cmd = {
  349. .command = "deep_sleep",
  350. .help = "Enter deep sleep mode. "
  351. "Two wakeup modes are supported: timer and GPIO. "
  352. "If no wakeup option is specified, will sleep indefinitely.",
  353. .hint = NULL,
  354. .func = &deep_sleep,
  355. .argtable = &deep_sleep_args
  356. };
  357. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  358. }
  359. /** 'light_sleep' command puts the chip into light sleep mode */
  360. static struct {
  361. struct arg_int *wakeup_time;
  362. struct arg_int *wakeup_gpio_num;
  363. struct arg_int *wakeup_gpio_level;
  364. struct arg_end *end;
  365. } light_sleep_args;
  366. static int light_sleep(int argc, char **argv)
  367. {
  368. int nerrors = arg_parse(argc, argv, (void **) &light_sleep_args);
  369. if (nerrors != 0) {
  370. arg_print_errors(stderr, light_sleep_args.end, argv[0]);
  371. return 1;
  372. }
  373. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
  374. if (light_sleep_args.wakeup_time->count) {
  375. uint64_t timeout = 1000ULL * light_sleep_args.wakeup_time->ival[0];
  376. ESP_LOGI(TAG, "Enabling timer wakeup, timeout=%lluus", timeout);
  377. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  378. }
  379. int io_count = light_sleep_args.wakeup_gpio_num->count;
  380. if (io_count != light_sleep_args.wakeup_gpio_level->count) {
  381. ESP_LOGE(TAG, "Should have same number of 'io' and 'io_level' arguments");
  382. return 1;
  383. }
  384. for (int i = 0; i < io_count; ++i) {
  385. int io_num = light_sleep_args.wakeup_gpio_num->ival[i];
  386. int level = light_sleep_args.wakeup_gpio_level->ival[i];
  387. if (level != 0 && level != 1) {
  388. ESP_LOGE(TAG, "Invalid wakeup level: %d", level);
  389. return 1;
  390. }
  391. ESP_LOGI(TAG, "Enabling wakeup on GPIO%d, wakeup on %s level",
  392. io_num, level ? "HIGH" : "LOW");
  393. ESP_ERROR_CHECK( gpio_wakeup_enable(io_num, level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL) );
  394. }
  395. if (io_count > 0) {
  396. ESP_ERROR_CHECK( esp_sleep_enable_gpio_wakeup() );
  397. }
  398. if (CONFIG_CONSOLE_UART_NUM <= UART_NUM_1) {
  399. ESP_LOGI(TAG, "Enabling UART wakeup (press ENTER to exit light sleep)");
  400. ESP_ERROR_CHECK( uart_set_wakeup_threshold(CONFIG_CONSOLE_UART_NUM, 3) );
  401. ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_CONSOLE_UART_NUM) );
  402. }
  403. fflush(stdout);
  404. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  405. esp_light_sleep_start();
  406. esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  407. const char *cause_str;
  408. switch (cause) {
  409. case ESP_SLEEP_WAKEUP_GPIO:
  410. cause_str = "GPIO";
  411. break;
  412. case ESP_SLEEP_WAKEUP_UART:
  413. cause_str = "UART";
  414. break;
  415. case ESP_SLEEP_WAKEUP_TIMER:
  416. cause_str = "timer";
  417. break;
  418. default:
  419. cause_str = "unknown";
  420. printf("%d\n", cause);
  421. }
  422. ESP_LOGI(TAG, "Woke up from: %s", cause_str);
  423. return 0;
  424. }
  425. static void register_light_sleep()
  426. {
  427. light_sleep_args.wakeup_time =
  428. arg_int0("t", "time", "<t>", "Wake up time, ms");
  429. light_sleep_args.wakeup_gpio_num =
  430. arg_intn(NULL, "io", "<n>", 0, 8,
  431. "If specified, wakeup using GPIO with given number");
  432. light_sleep_args.wakeup_gpio_level =
  433. arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
  434. light_sleep_args.end = arg_end(3);
  435. const esp_console_cmd_t cmd = {
  436. .command = "light_sleep",
  437. .help = "Enter light sleep mode. "
  438. "Two wakeup modes are supported: timer and GPIO. "
  439. "Multiple GPIO pins can be specified using pairs of "
  440. "'io' and 'io_level' arguments. "
  441. "Will also wake up on UART input.",
  442. .hint = NULL,
  443. .func = &light_sleep,
  444. .argtable = &light_sleep_args
  445. };
  446. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  447. }