console_example_main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. static const char* TAG = "example";
  20. #define PROMPT_STR CONFIG_IDF_TARGET
  21. /* Console command history can be stored to and loaded from a file.
  22. * The easiest way to do this is to use FATFS filesystem on top of
  23. * wear_levelling library.
  24. */
  25. #if CONFIG_CONSOLE_STORE_HISTORY
  26. #define MOUNT_PATH "/data"
  27. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  28. static void initialize_filesystem(void)
  29. {
  30. static wl_handle_t wl_handle;
  31. const esp_vfs_fat_mount_config_t mount_config = {
  32. .max_files = 4,
  33. .format_if_mount_failed = true
  34. };
  35. esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle);
  36. if (err != ESP_OK) {
  37. ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  38. return;
  39. }
  40. }
  41. #endif // CONFIG_STORE_HISTORY
  42. static void initialize_nvs(void)
  43. {
  44. esp_err_t err = nvs_flash_init();
  45. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  46. ESP_ERROR_CHECK( nvs_flash_erase() );
  47. err = nvs_flash_init();
  48. }
  49. ESP_ERROR_CHECK(err);
  50. }
  51. void app_main(void)
  52. {
  53. esp_console_repl_t *repl = NULL;
  54. esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
  55. /* Prompt to be printed before each line.
  56. * This can be customized, made dynamic, etc.
  57. */
  58. repl_config.prompt = PROMPT_STR ">";
  59. repl_config.max_cmdline_length = CONFIG_CONSOLE_MAX_COMMAND_LINE_LENGTH;
  60. initialize_nvs();
  61. #if CONFIG_CONSOLE_STORE_HISTORY
  62. initialize_filesystem();
  63. repl_config.history_save_path = HISTORY_PATH;
  64. ESP_LOGI(TAG, "Command history enabled");
  65. #else
  66. ESP_LOGI(TAG, "Command history disabled");
  67. #endif
  68. /* Register commands */
  69. esp_console_register_help_command();
  70. register_system();
  71. register_wifi();
  72. register_nvs();
  73. #if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) || defined(CONFIG_ESP_CONSOLE_UART_CUSTOM)
  74. esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  75. ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
  76. #elif defined(CONFIG_ESP_CONSOLE_USB_CDC)
  77. esp_console_dev_usb_cdc_config_t hw_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
  78. ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&hw_config, &repl_config, &repl));
  79. #elif defined(CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
  80. esp_console_dev_usb_serial_jtag_config_t hw_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
  81. ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&hw_config, &repl_config, &repl));
  82. #else
  83. #error Unsupported console type
  84. #endif
  85. ESP_ERROR_CHECK(esp_console_start_repl(repl));
  86. }