console_example_main.c 2.8 KB

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