console_example_main.c 2.7 KB

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