#include #include #include #include #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" #include "driver/adc.h" #include "esp_adc_cal.h" #include "esp_smartconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "application.h" #include "utils.h" #include "dhcp_wd.h" #include "settings.h" #include "console/console_server.h" #include "web/websrv.h" #include "wifi_conn.h" #include "console/register_cmds.h" #include "tasks.h" #include "heap_debug.h" static const char *TAG = "main"; //#define NUM_RECORDS 200 //static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM uint32_t heapdebug_start; const char *heapdebug_start_name; void app_main(void) { // ESP_ERROR_CHECK( heap_trace_init_standalone(trace_record, NUM_RECORDS) ); HEAP_MEASURE_START("NVS,settings,isr,i2c"); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_register_shutdown_handler(cspemu_run_shutdown_handlers)); ESP_ERROR_CHECK(esp_event_loop_create_default()); settings_init(); settings_load(); // Start IDF service for pin change interrupts ESP_ERROR_CHECK(gpio_install_isr_service(0)); HEAP_MEASURE_END(); HEAP_MEASURE_START("Netif"); ESP_LOGD(TAG, "initing netif"); ESP_ERROR_CHECK(esp_netif_init()); HEAP_MEASURE_END(); if (g_Settings.wifi_enabled && (g_Settings.sta_enabled || g_Settings.ap_enabled)) { HEAP_MEASURE_START("WIFI SETUP"); initialise_wifi(); HEAP_MEASURE_END(); HEAP_MEASURE_START("WEBSRV SETUP"); websrv_init(); HEAP_MEASURE_END(); g_State.wifi_inited = true; } else { // initialise the bare minimum so wifi config can be changed ESP_LOGD(TAG, "initing wifi"); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_LOGD(TAG, "set storage, set sta"); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH)); g_State.wifi_inited = true; } HEAP_MEASURE_START("CONSOLE INIT"); console_init(NULL); register_console_commands(); HEAP_MEASURE_END(); HEAP_MEASURE_START("CONSOLE start"); console_setup_uart_stdio(); ESP_ERROR_CHECK(console_start_stdio(NULL, NULL)); HEAP_MEASURE_END(); HEAP_MEASURE_START("Telnet start"); telnetsrv_start(CONSOLE_TELNET_PORT); HEAP_MEASURE_END(); ESP_LOGI(TAG, "Startup finished, free heap = %u, cmds %"PRIu32, esp_get_free_heap_size(), console_count_commands()); }