app_main.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "esp_log.h"
  7. #include "esp_console.h"
  8. #include "esp_event.h"
  9. #include "nvs_flash.h"
  10. #include "esp_netif.h"
  11. #include "esp_wifi.h"
  12. extern void register_tsens_cmd(void);
  13. extern void register_wifi_cmd(void);
  14. static int restart(int argc, char **argv)
  15. {
  16. ESP_LOGI("main", "Restarting");
  17. esp_restart();
  18. }
  19. static void register_restart(void)
  20. {
  21. const esp_console_cmd_t cmd = {
  22. .command = "restart",
  23. .help = "Restart the program",
  24. .hint = NULL,
  25. .func = &restart,
  26. };
  27. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  28. }
  29. static void initialize_nvs(void)
  30. {
  31. esp_err_t err = nvs_flash_init();
  32. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  33. ESP_ERROR_CHECK( nvs_flash_erase() );
  34. err = nvs_flash_init();
  35. }
  36. ESP_ERROR_CHECK(err);
  37. }
  38. void app_main(void)
  39. {
  40. esp_console_repl_t *repl = NULL;
  41. esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
  42. repl_config.prompt = "esp>";
  43. repl_config.max_cmdline_length = 256;
  44. initialize_nvs();
  45. ESP_ERROR_CHECK(esp_netif_init());
  46. ESP_ERROR_CHECK(esp_event_loop_create_default());
  47. esp_netif_create_default_wifi_sta();
  48. esp_netif_create_default_wifi_ap();
  49. /* Register commands */
  50. esp_console_register_help_command();
  51. register_restart();
  52. register_tsens_cmd();
  53. register_wifi_cmd();
  54. esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
  55. ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
  56. ESP_ERROR_CHECK(esp_console_start_repl(repl));
  57. }