console_example_main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Basic console example (esp_console_repl API)
  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 "esp_system.h"
  10. #include "esp_log.h"
  11. #include "esp_console.h"
  12. #include "esp_vfs_dev.h"
  13. #include "esp_vfs_fat.h"
  14. #include "nvs.h"
  15. #include "nvs_flash.h"
  16. #include "cmd_system.h"
  17. #include "cmd_wifi.h"
  18. #include "cmd_nvs.h"
  19. /*
  20. * We warn if a secondary serial console is enabled. A secondary serial console is always output-only and
  21. * hence not very useful for interactive console applications. If you encounter this warning, consider disabling
  22. * the secondary serial console in menuconfig unless you know what you are doing.
  23. */
  24. #if SOC_USB_SERIAL_JTAG_SUPPORTED
  25. #if !CONFIG_ESP_CONSOLE_SECONDARY_NONE
  26. #warning "A secondary serial console is not useful when using the console component. Please disable it in menuconfig."
  27. #endif
  28. #endif
  29. static const char* TAG = "example";
  30. #define PROMPT_STR CONFIG_IDF_TARGET
  31. /* Console command history can be stored to and loaded from a file.
  32. * The easiest way to do this is to use FATFS filesystem on top of
  33. * wear_levelling library.
  34. */
  35. #if CONFIG_CONSOLE_STORE_HISTORY
  36. #define MOUNT_PATH "/data"
  37. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  38. static void initialize_filesystem(void)
  39. {
  40. static wl_handle_t wl_handle;
  41. const esp_vfs_fat_mount_config_t mount_config = {
  42. .max_files = 4,
  43. .format_if_mount_failed = true
  44. };
  45. esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle);
  46. if (err != ESP_OK) {
  47. ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  48. return;
  49. }
  50. }
  51. #endif // CONFIG_STORE_HISTORY
  52. static void initialize_nvs(void)
  53. {
  54. esp_err_t err = nvs_flash_init();
  55. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  56. ESP_ERROR_CHECK( nvs_flash_erase() );
  57. err = nvs_flash_init();
  58. }
  59. ESP_ERROR_CHECK(err);
  60. }
  61. void app_main(void)
  62. {
  63. esp_console_repl_t *repl = NULL;
  64. esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
  65. /* Prompt to be printed before each line.
  66. * This can be customized, made dynamic, etc.
  67. */
  68. repl_config.prompt = PROMPT_STR ">";
  69. repl_config.max_cmdline_length = CONFIG_CONSOLE_MAX_COMMAND_LINE_LENGTH;
  70. initialize_nvs();
  71. #if CONFIG_CONSOLE_STORE_HISTORY
  72. initialize_filesystem();
  73. repl_config.history_save_path = HISTORY_PATH;
  74. ESP_LOGI(TAG, "Command history enabled");
  75. #else
  76. ESP_LOGI(TAG, "Command history disabled");
  77. #endif
  78. /* Register commands */
  79. esp_console_register_help_command();
  80. register_system_common();
  81. register_system_sleep();
  82. #if SOC_WIFI_SUPPORTED
  83. register_wifi();
  84. #endif
  85. register_nvs();
  86. #if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) || defined(CONFIG_ESP_CONSOLE_UART_CUSTOM)
  87. esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  88. ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
  89. #elif defined(CONFIG_ESP_CONSOLE_USB_CDC)
  90. esp_console_dev_usb_cdc_config_t hw_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
  91. ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&hw_config, &repl_config, &repl));
  92. #elif defined(CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
  93. esp_console_dev_usb_serial_jtag_config_t hw_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
  94. ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&hw_config, &repl_config, &repl));
  95. #else
  96. #error Unsupported console type
  97. #endif
  98. ESP_ERROR_CHECK(esp_console_start_repl(repl));
  99. }