From a239dc9b964fa8a717cc0142443855b47bd074be Mon Sep 17 00:00:00 2001 From: Bogdan Date: Thu, 18 Aug 2022 12:17:54 +0200 Subject: [PATCH] remove all init from user app --- include/SystemConfiguration.h | 3 + src/HTTPPostSystem.c | 2 - src/SystemConfiguration.c | 247 ++++++++++++++++++++++++++++++++-- 3 files changed, 239 insertions(+), 13 deletions(-) diff --git a/include/SystemConfiguration.h b/include/SystemConfiguration.h index 707dd62..e8eadc0 100644 --- a/include/SystemConfiguration.h +++ b/include/SystemConfiguration.h @@ -245,4 +245,7 @@ esp_err_t ResetInitSysConfig(void); SYS_CONFIG* GetSysConf(void); + esp_err_t WebGuiAppInit(void); + + #endif /* COMPONENTS_WEB_GUI_APPLICATION_INCLUDE_SYSTEMCONFIGURATION_H_ */ diff --git a/src/HTTPPostSystem.c b/src/HTTPPostSystem.c index 3ea338c..5279c48 100644 --- a/src/HTTPPostSystem.c +++ b/src/HTTPPostSystem.c @@ -25,8 +25,6 @@ static const char *TAG = "HTTPServerPost"; - - #define FILE_PATH_MAX (ESP_VFS_PATH_MAX + CONFIG_SPIFFS_OBJ_NAME_LEN) const char pg_11[] = "index.html"; diff --git a/src/SystemConfiguration.c b/src/SystemConfiguration.c index 320d2a1..187f376 100644 --- a/src/SystemConfiguration.c +++ b/src/SystemConfiguration.c @@ -21,22 +21,247 @@ * \copyright Apache License, Version 2.0 */ -#include "SystemConfiguration.h" #include "stdlib.h" #include "string.h" #include "nvs_flash.h" #include "nvs.h" + #include "esp_log.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_system.h" + +#include "driver/gpio.h" +#include "driver/adc.h" +#include "driver/i2c.h" +#include "driver/spi_master.h" + +#include "SystemConfiguration.h" +#include "romfs.h" +#include "NetTransport.h" +#include "Helpers.h" #define STORAGE_NAMESPACE "storage" #define TAG "SystemConfiguration" +#define MANUAL_RESET 1 + static SYS_CONFIG SysConfig; +#define SPI_LOCK_TIMEOUT_MS (1000) + +SemaphoreHandle_t xSemaphoreSPIHandle = NULL; +StaticSemaphore_t xSemaphoreSPIBuf; + +static void InitSysIO(void); +static void InitSysSPI(void); +static void InitSysI2C(void); + +esp_err_t WebGuiAppInit(void) +{ + InitSysIO(); +#if CONFIG_WEBGUIAPP_SPI_ENABLE + InitSysSPI(); +#endif +#if CONFIG_WEBGUIAPP_I2C_ENABLE + InitSysI2C(); +#endif + + esp_err_t err = nvs_flash_init(); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND || MANUAL_RESET == 1) + { + // 1.OTA app partition table has a smaller NVS partition size than the non-OTA + // partition table. This size mismatch may cause NVS initialization to fail. + // 2.NVS partition contains data in new format and cannot be recognized by this version of code. + // If this happens, we erase NVS partition and initialize NVS again. + ESP_ERROR_CHECK(nvs_flash_erase()); + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(ResetInitSysConfig()); + } + ESP_ERROR_CHECK(InitSysConfig()); + + //init rom file system + init_rom_fs("/espfs"); + +#if CONFIG_WEBGUIAPP_GPRS_ENABLE + /*Start PPP modem*/ + if (GetSysConf()->gsmSettings.Flags1.bIsGSMEnabled) + PPPModemStart(); +#endif + +#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE + /*Start Ethernet connection*/ + if (GetSysConf()->ethSettings.Flags1.bIsETHEnabled) + EthStart(); +#endif + +#if CONFIG_WEBGUIAPP_WIFI_ENABLE + /*Start WiFi connection*/ + if (GetSysConf()->wifiSettings.Flags1.bIsWiFiEnabled) + { + if (GetSysConf()->wifiSettings.Flags1.bIsAP) + WiFiAPStart(); + else + WiFiSTAStart(); + } +#endif + + /*Start services depends on client connection*/ + if (CONFIG_WEBGUIAPP_GPRS_ENABLE || + CONFIG_WEBGUIAPP_ETHERNET_ENABLE || + (CONFIG_WEBGUIAPP_WIFI_ENABLE && !GetSysConf()->wifiSettings.Flags1.bIsAP)) + { + //StartTimeGet(); + +#if CONFIG_WEBGUIAPP_MQTT_ENABLE + if (GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled + || GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled) + { + // MQTTRun(); + } +#endif + + } + return ESP_OK; +} + +static void InitSysIO(void) +{ +#if !JTAG_DEBUG + gpio_pad_select_gpio(GPIO_NUM_12); + gpio_pad_select_gpio(GPIO_NUM_13); + gpio_pad_select_gpio(GPIO_NUM_14); + gpio_pad_select_gpio(GPIO_NUM_15); + gpio_set_direction(GPIO_NUM_12, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_13, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_14, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_15, GPIO_MODE_OUTPUT); + gpio_set_level(GPIO_NUM_12, 0); + gpio_set_level(GPIO_NUM_13, 0); + gpio_set_level(GPIO_NUM_14, 0); + gpio_set_level(GPIO_NUM_15, 0); +#endif + + gpio_pad_select_gpio(GPIO_NUM_2); + gpio_pad_select_gpio(GPIO_NUM_0); + gpio_pad_select_gpio(GPIO_NUM_4); + gpio_pad_select_gpio(GPIO_NUM_34); + gpio_pad_select_gpio(GPIO_NUM_16); + gpio_pad_select_gpio(GPIO_NUM_17); + + gpio_pad_select_gpio(GPIO_NUM_25); + gpio_pad_select_gpio(GPIO_NUM_26); + + gpio_pad_select_gpio(GPIO_NUM_27); + gpio_pad_select_gpio(GPIO_NUM_32); + gpio_pad_select_gpio(GPIO_NUM_33); + gpio_pad_select_gpio(GPIO_NUM_39); + + gpio_set_direction(GPIO_NUM_0, GPIO_MODE_INPUT); + gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_16, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT); + + gpio_set_direction(GPIO_NUM_25, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_26, GPIO_MODE_OUTPUT); + + gpio_set_direction(GPIO_NUM_27, GPIO_MODE_INPUT); + gpio_set_direction(GPIO_NUM_32, GPIO_MODE_OUTPUT); + gpio_set_direction(GPIO_NUM_33, GPIO_MODE_INPUT); + + gpio_set_direction(GPIO_NUM_34, GPIO_MODE_INPUT); + gpio_set_direction(GPIO_NUM_39, GPIO_MODE_INPUT); + + gpio_set_level(GPIO_NUM_2, 1); + gpio_set_level(GPIO_NUM_4, 1); + gpio_set_level(GPIO_NUM_16, 1); + gpio_set_level(GPIO_NUM_17, 1); + + gpio_set_level(GPIO_NUM_25, 0); //RELAY + gpio_set_level(GPIO_NUM_26, 0); //TRIAC + + gpio_set_level(GPIO_NUM_32, 1); //0- current , 1- voltage + + ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_IRAM)); + ESP_LOGI(TAG, "GPO extender initialized OK"); + +} + +void spi_device_init_custom(void) +{ + xSemaphoreSPIHandle = xSemaphoreCreateBinaryStatic(&xSemaphoreSPIBuf); + xSemaphoreGive(xSemaphoreSPIHandle); +} + +esp_err_t spi_device_polling_transmit_custom(spi_device_handle_t handle, spi_transaction_t *trans_desc) +{ + esp_err_t res; + if (xSemaphoreTake(xSemaphoreSPIHandle,pdMS_TO_TICKS(SPI_LOCK_TIMEOUT_MS)) == pdTRUE) + { + res = spi_device_polling_transmit(handle, trans_desc); + xSemaphoreGive(xSemaphoreSPIHandle); + } + else + { + res = ESP_ERR_TIMEOUT; + } + return res; +} + +static void InitSysSPI(void) +{ +#ifdef CONFIG_WEBGUIAPP_SPI_ENABLE + spi_device_init_custom(); + spi_bus_config_t buscfg = { + .miso_io_num = CONFIG_SPI_MISO_GPIO, + .mosi_io_num = CONFIG_SPI_MOSI_GPIO, + .sclk_io_num = CONFIG_SPI_SCLK_GPIO, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + }; + ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); + ESP_LOGI(TAG, "SPI BUS initialize OK"); +#else + ESP_LOGI(TAG, "SPI BUS disabeled in config"); +#endif +} + +static void InitSysI2C(void) +{ +#ifdef CONFIG_WEBGUIAPP_I2C_ENABLE + i2c_config_t i2c_config = { + .mode = I2C_MODE_MASTER, + .sda_io_num = CONFIG_I2C_SDA_GPIO, + .scl_io_num = CONFIG_I2C_SCL_GPIO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = CONFIG_I2C_CLOCK + }; + ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &i2c_config)); + ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0)); + ESP_LOGI(TAG, "I2C initialized OK"); +#else + ESP_LOGI(TAG, "I2C bus disabeled in config"); +#endif +} + static void ResetSysConfig(SYS_CONFIG *Conf) { #if CONFIG_WEBGUIAPP_WIFI_ENABLE Conf->wifiSettings.Flags1.bIsWiFiEnabled = CONFIG_WEBGUIAPP_WIFI_ON; + + char id[4]; + char id2[9]; + GetChipId((uint8_t*) id); + BytesToStr((unsigned char*) id, (unsigned char*) id2, 4); + id2[8] = 0x00; + memcpy(Conf->wifiSettings.ApSSID, CONFIG_WEBGUIAPP_WIFI_SSID_AP, sizeof(CONFIG_WEBGUIAPP_WIFI_SSID_AP)); + strcat(Conf->wifiSettings.ApSSID, "_"); + strcat(Conf->wifiSettings.ApSSID, id2); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_WIFI_IP_STA, (esp_ip4_addr_t*) &Conf->wifiSettings.InfIPAddr); esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_WIFI_MASK_STA, (esp_ip4_addr_t*) &Conf->wifiSettings.InfMask); esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_WIFI_GATEWAY_STA, (esp_ip4_addr_t*) &Conf->wifiSettings.InfGateway); @@ -45,10 +270,11 @@ static void ResetSysConfig(SYS_CONFIG *Conf) memcpy(Conf->wifiSettings.ApSecurityKey, CONFIG_WEBGUIAPP_WIFI_KEY_AP, sizeof(CONFIG_WEBGUIAPP_WIFI_KEY_AP)); memcpy(Conf->wifiSettings.InfSSID, CONFIG_WEBGUIAPP_WIFI_SSID_STA, sizeof(CONFIG_WEBGUIAPP_WIFI_SSID_STA)); memcpy(Conf->wifiSettings.InfSecurityKey, CONFIG_WEBGUIAPP_WIFI_KEY_STA, sizeof(CONFIG_WEBGUIAPP_WIFI_KEY_STA)); + Conf->wifiSettings.Flags1.bIsDHCPEnabled = CONFIG_WEBGUIAPP_WIFI_DHCP_ON; - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS1_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->wifiSettings.DNSAddr1); - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS2_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->wifiSettings.DNSAddr2); - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->wifiSettings.DNSAddr3); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS1_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->wifiSettings.DNSAddr1); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS2_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->wifiSettings.DNSAddr2); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->wifiSettings.DNSAddr3); #endif @@ -58,20 +284,19 @@ static void ResetSysConfig(SYS_CONFIG *Conf) esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_ETH_MASK_DEFAULT, (esp_ip4_addr_t*) &Conf->ethSettings.Mask); esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_ETH_GATEWAY_DEFAULT, (esp_ip4_addr_t*) &Conf->ethSettings.Gateway); //Conf->ethSettings.Flags1.bIsDHCPEnabled = CONFIG_WEBGUIAPP_ETHERNET_DHCP_ON ; - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS1_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->ethSettings.DNSAddr1); - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS2_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->ethSettings.DNSAddr2); - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->ethSettings.DNSAddr3); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS1_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->ethSettings.DNSAddr1); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS2_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->ethSettings.DNSAddr2); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->ethSettings.DNSAddr3); #endif #if CONFIG_WEBGUIAPP_GPRS_ENABLE Conf->gsmSettings.Flags1.bIsGSMEnabled = true; - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS1_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->gsmSettings.DNSAddr1); - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS2_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->gsmSettings.DNSAddr2); - esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*)&Conf->gsmSettings.DNSAddr3); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS1_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->gsmSettings.DNSAddr1); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS2_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->gsmSettings.DNSAddr2); + esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &Conf->gsmSettings.DNSAddr3); #endif - #if CONFIG_WEBGUIAPP_MQTT_ENABLE Conf->mqttStation[0].Flags1.bIsGlobalEnabled = CONFIG_MQTT_ON; memcpy(Conf->mqttStation[0].ServerAddr, CONFIG_MQTT_SERVER_URL, sizeof(CONFIG_MQTT_SERVER_URL));