From b073905a2e05ae5a8202f563acfa77ccd7a048ee Mon Sep 17 00:00:00 2001
From: Theo Arends <11044339+arendst@users.noreply.github.com>
Date: Mon, 8 Feb 2021 11:34:29 +0100
Subject: [PATCH] Consolidate ESP specific functions
---
tasmota/support.ino | 64 --------------
tasmota/support_command.ino | 5 +-
.../{support_esp32.ino => support_esp.ino} | 85 +++++++++++++++++--
tasmota/support_tasmota.ino | 5 +-
tasmota/tasmota.ino | 2 -
tasmota/xdrv_01_webserver.ino | 6 +-
6 files changed, 82 insertions(+), 85 deletions(-)
rename tasmota/{support_esp32.ino => support_esp.ino} (86%)
diff --git a/tasmota/support.ino b/tasmota/support.ino
index 8804238f6..93b78acfb 100644
--- a/tasmota/support.ino
+++ b/tasmota/support.ino
@@ -713,44 +713,6 @@ char* GetPowerDevice(char* dest, uint32_t idx, size_t size)
return GetPowerDevice(dest, idx, size, 0);
}
-void GetEspHardwareType(void)
-{
-#ifdef ESP8266
- // esptool.py get_efuses
- uint32_t efuse1 = *(uint32_t*)(0x3FF00050);
- uint32_t efuse2 = *(uint32_t*)(0x3FF00054);
-// uint32_t efuse3 = *(uint32_t*)(0x3FF00058);
-// uint32_t efuse4 = *(uint32_t*)(0x3FF0005C);
-
- TasmotaGlobal.is_8285 = ( (efuse1 & (1 << 4)) || (efuse2 & (1 << 16)) );
- if (TasmotaGlobal.is_8285 && (ESP.getFlashChipRealSize() > 1048576)) {
- TasmotaGlobal.is_8285 = false; // ESP8285 can only have 1M flash
- }
-#else
- TasmotaGlobal.is_8285 = false; // ESP8285 can only have 1M flash
-#endif
-}
-
-String GetDeviceHardware(void)
-{
- char buff[10];
-#ifdef ESP8266
- if (TasmotaGlobal.is_8285) {
- strcpy_P(buff, PSTR("ESP8285"));
- } else {
- strcpy_P(buff, PSTR("ESP8266EX"));
- }
-#endif // ESP8266
-#ifdef ESP32
-#if CONFIG_IDF_TARGET_ESP32S2 // ESP32-S2
- strcpy_P(buff, PSTR("ESP32-S2"));
-#else
- strcpy_P(buff, PSTR("ESP32"));
-#endif // CONFIG_IDF_TARGET_ESP32S2
-#endif // ESP32
- return String(buff);
-}
-
float ConvertTemp(float c)
{
float result = c;
@@ -1585,7 +1547,6 @@ uint32_t ValidPin(uint32_t pin, uint32_t gpio) {
return GPIO_NONE; // Disable flash pins GPIO6, GPIO7, GPIO8 and GPIO11
}
-// if (!TasmotaGlobal.is_8285 && !Settings.flag3.user_esp8285_enable) { // SetOption51 - Enable ESP8285 user GPIO's
if ((WEMOS == Settings.module) && !Settings.flag3.user_esp8285_enable) { // SetOption51 - Enable ESP8285 user GPIO's
if ((9 == pin) || (10 == pin)) {
return GPIO_NONE; // Disable possible flash GPIO9 and GPIO10
@@ -2312,28 +2273,3 @@ String Decompress(const char * compressed, size_t uncompressed_size) {
}
#endif // USE_UNISHOX_COMPRESSION
-
-/*********************************************************************************************\
- * High entropy hardware random generator
- * Thanks to DigitalAlchemist
-\*********************************************************************************************/
-// Based on code from https://raw.githubusercontent.com/espressif/esp-idf/master/components/esp32/hw_random.c
-uint32_t HwRandom(void) {
-#if ESP8266
- // https://web.archive.org/web/20160922031242/http://esp8266-re.foogod.com/wiki/Random_Number_Generator
- #define _RAND_ADDR 0x3FF20E44UL
-#endif // ESP8266
-#ifdef ESP32
- #define _RAND_ADDR 0x3FF75144UL
-#endif // ESP32
- static uint32_t last_ccount = 0;
- uint32_t ccount;
- uint32_t result = 0;
- do {
- ccount = ESP.getCycleCount();
- result ^= *(volatile uint32_t *)_RAND_ADDR;
- } while (ccount - last_ccount < 64);
- last_ccount = ccount;
- return result ^ *(volatile uint32_t *)_RAND_ADDR;
-#undef _RAND_ADDR
-}
diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino
index 683808960..7272cd2ab 100644
--- a/tasmota/support_command.ino
+++ b/tasmota/support_command.ino
@@ -484,8 +484,7 @@ void CmndStatus(void)
}
if ((0 == payload) || (4 == payload)) {
- float freeMem = ESP_getFreeHeap1024();
- Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS4_MEMORY "\":{\"" D_JSON_PROGRAMSIZE "\":%d,\"" D_JSON_FREEMEMORY "\":%d,\"" D_JSON_HEAPSIZE "\":%1_f,\""
+ Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS4_MEMORY "\":{\"" D_JSON_PROGRAMSIZE "\":%d,\"" D_JSON_FREEMEMORY "\":%d,\"" D_JSON_HEAPSIZE "\":%d,\""
#ifdef ESP32
D_JSON_PSRMAXMEMORY "\":%d,\"" D_JSON_PSRFREEMEMORY "\":%d,\""
#endif // ESP32
@@ -494,7 +493,7 @@ void CmndStatus(void)
",\"" D_JSON_FLASHCHIPID "\":\"%06X\""
#endif // ESP8266
",\"FlashFrequency\":%d,\"" D_JSON_FLASHMODE "\":%d"),
- ESP_getSketchSize()/1024, ESP.getFreeSketchSpace()/1024, &freeMem,
+ ESP_getSketchSize()/1024, ESP.getFreeSketchSpace()/1024, ESP_getFreeHeap1024(),
#ifdef ESP32
ESP.getPsramSize()/1024, ESP.getFreePsram()/1024,
#endif // ESP32
diff --git a/tasmota/support_esp32.ino b/tasmota/support_esp.ino
similarity index 86%
rename from tasmota/support_esp32.ino
rename to tasmota/support_esp.ino
index c2206dd89..1f929f400 100644
--- a/tasmota/support_esp32.ino
+++ b/tasmota/support_esp.ino
@@ -1,5 +1,5 @@
/*
- support_esp32.ino - ESP32 specific code for Tasmota
+ support_esp.ino - ESP specific code for Tasmota
Copyright (C) 2021 Theo Arends / Jörg Schüler-Maroldt
@@ -17,6 +17,12 @@
along with this program. If not, see .
*/
+/*********************************************************************************************\
+ * ESP8266 and ESP32 specific code
+ *
+ * At the end the common Tasmota calls are provided
+\*********************************************************************************************/
+
/*********************************************************************************************\
* ESP8266 Support
\*********************************************************************************************/
@@ -47,10 +53,6 @@ uint32_t ESP_getFreeHeap(void) {
return ESP.getFreeHeap();
}
-float ESP_getFreeHeap1024(void) {
- return ((float)ESP_getFreeHeap()) / 1024;
-}
-
void ESP_Restart(void) {
// ESP.restart(); // This results in exception 3 on restarts on core 2.3.0
ESP.reset();
@@ -415,10 +417,6 @@ uint32_t ESP_getFreeHeap(void) {
return ESP.getFreeHeap();
}
-float ESP_getFreeHeap1024(void) {
- return ((float)ESP_getFreeHeap()) / 1024;
-}
-
uint32_t ESP_getMaxAllocHeap(void) {
// largest block of heap that can be allocated at once
uint32_t free_block_size = ESP.getMaxAllocHeap();
@@ -465,3 +463,72 @@ void *special_malloc(uint32_t size) {
}
#endif // ESP32
+
+/*********************************************************************************************\
+ * ESP Support
+\*********************************************************************************************/
+
+String GetDeviceHardware(void) {
+ char buff[10];
+#ifdef ESP8266
+ // esptool.py get_efuses
+ uint32_t efuse1 = *(uint32_t*)(0x3FF00050);
+ uint32_t efuse2 = *(uint32_t*)(0x3FF00054);
+// uint32_t efuse3 = *(uint32_t*)(0x3FF00058);
+// uint32_t efuse4 = *(uint32_t*)(0x3FF0005C);
+
+ bool is_8285 = ( (efuse1 & (1 << 4)) || (efuse2 & (1 << 16)) );
+ if (is_8285 && (ESP.getFlashChipRealSize() > 1048576)) {
+ is_8285 = false; // ESP8285 can only have 1M flash
+ }
+ if (is_8285) {
+ strcpy_P(buff, PSTR("ESP8285"));
+ } else {
+ strcpy_P(buff, PSTR("ESP8266EX"));
+ }
+#endif // ESP8266
+
+#ifdef ESP32
+#if CONFIG_IDF_TARGET_ESP32S2 // ESP32-S2
+ strcpy_P(buff, PSTR("ESP32-S2"));
+#else
+ strcpy_P(buff, PSTR("ESP32"));
+#endif // CONFIG_IDF_TARGET_ESP32S2
+#endif // ESP32
+
+ return String(buff);
+}
+
+uint32_t ESP_getFreeHeap1024(void) {
+ return ESP_getFreeHeap() / 1024;
+}
+/*
+float ESP_getFreeHeap1024(void) {
+ return ((float)ESP_getFreeHeap()) / 1024;
+}
+*/
+
+/*********************************************************************************************\
+ * High entropy hardware random generator
+ * Thanks to DigitalAlchemist
+\*********************************************************************************************/
+// Based on code from https://raw.githubusercontent.com/espressif/esp-idf/master/components/esp32/hw_random.c
+uint32_t HwRandom(void) {
+#if ESP8266
+ // https://web.archive.org/web/20160922031242/http://esp8266-re.foogod.com/wiki/Random_Number_Generator
+ #define _RAND_ADDR 0x3FF20E44UL
+#endif // ESP8266
+#ifdef ESP32
+ #define _RAND_ADDR 0x3FF75144UL
+#endif // ESP32
+ static uint32_t last_ccount = 0;
+ uint32_t ccount;
+ uint32_t result = 0;
+ do {
+ ccount = ESP.getCycleCount();
+ result ^= *(volatile uint32_t *)_RAND_ADDR;
+ } while (ccount - last_ccount < 64);
+ last_ccount = ccount;
+ return result ^ *(volatile uint32_t *)_RAND_ADDR;
+#undef _RAND_ADDR
+}
diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino
index 8db7adad9..23de8a3d9 100644
--- a/tasmota/support_tasmota.ino
+++ b/tasmota/support_tasmota.ino
@@ -658,7 +658,6 @@ void MqttShowPWMState(void)
void MqttShowState(void)
{
char stemp1[TOPSZ];
- float freeMem = ESP_getFreeHeap1024();
ResponseAppendTime();
ResponseAppend_P(PSTR(",\"" D_JSON_UPTIME "\":\"%s\",\"UptimeSec\":%u"), GetUptime().c_str(), UpTime());
@@ -670,8 +669,8 @@ void MqttShowState(void)
#endif // USE_ADC_VCC
#endif // ESP8266
- ResponseAppend_P(PSTR(",\"" D_JSON_HEAPSIZE "\":%1_f,\"SleepMode\":\"%s\",\"Sleep\":%u,\"LoadAvg\":%u,\"MqttCount\":%u"),
- &freeMem, GetTextIndexed(stemp1, sizeof(stemp1), Settings.flag3.sleep_normal, kSleepMode), // SetOption60 - Enable normal sleep instead of dynamic sleep
+ ResponseAppend_P(PSTR(",\"" D_JSON_HEAPSIZE "\":%d,\"SleepMode\":\"%s\",\"Sleep\":%u,\"LoadAvg\":%u,\"MqttCount\":%u"),
+ ESP_getFreeHeap1024(), GetTextIndexed(stemp1, sizeof(stemp1), Settings.flag3.sleep_normal, kSleepMode), // SetOption60 - Enable normal sleep instead of dynamic sleep
TasmotaGlobal.sleep, TasmotaGlobal.loop_load_avg, MqttConnectCount());
for (uint32_t i = 1; i <= TasmotaGlobal.devices_present; i++) {
diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino
index 027ee61d6..79c36def4 100644
--- a/tasmota/tasmota.ino
+++ b/tasmota/tasmota.ino
@@ -142,7 +142,6 @@ struct {
bool pwm_present; // Any PWM channel configured with SetOption15 0
bool i2c_enabled; // I2C configured
bool ntp_force_sync; // Force NTP sync
- bool is_8285; // Hardware device ESP8266EX (0) or ESP8285 (1)
bool skip_light_fade; // Temporarily skip light fading
bool restart_halt; // Do not restart but stay in wait loop
bool module_changed; // Indicate module changed since last restart
@@ -334,7 +333,6 @@ void setup(void) {
snprintf_P(TasmotaGlobal.hostname, sizeof(TasmotaGlobal.hostname)-1, SettingsText(SET_HOSTNAME));
}
- GetEspHardwareType();
GpioInit();
WifiConnect();
diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino
index 15a9ec558..38116bcdb 100644
--- a/tasmota/xdrv_01_webserver.ino
+++ b/tasmota/xdrv_01_webserver.ino
@@ -2066,8 +2066,6 @@ void HandleInformation(void)
char stopic[TOPSZ];
- float freeMem = ESP_getFreeHeap1024();
-
WSContentStart_P(PSTR(D_INFORMATION));
// Save 1k of code space replacing table html with javascript replace codes
// }1 =
@@ -2183,13 +2181,13 @@ void HandleInformation(void)
WSContentSend_P(PSTR("}1" D_FREE_PROGRAM_SPACE "}2%d kB"), ESP.getFreeSketchSpace() / 1024);
#ifdef ESP32
int32_t freeMaxMem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / ESP_getFreeHeap());
- WSContentSend_P(PSTR("}1" D_FREE_MEMORY "}2%1_f kB (" D_FRAGMENTATION " %d%%)"), &freeMem, freeMaxMem);
+ WSContentSend_P(PSTR("}1" D_FREE_MEMORY "}2%d kB (" D_FRAGMENTATION " %d%%)"), ESP_getFreeHeap1024(), freeMaxMem);
if (psramFound()) {
WSContentSend_P(PSTR("}1" D_PSR_MAX_MEMORY "}2%d kB"), ESP.getPsramSize() / 1024);
WSContentSend_P(PSTR("}1" D_PSR_FREE_MEMORY "}2%d kB"), ESP.getFreePsram() / 1024);
}
#else // ESP32
- WSContentSend_P(PSTR("}1" D_FREE_MEMORY "}2%1_f kB"), &freeMem);
+ WSContentSend_P(PSTR("}1" D_FREE_MEMORY "}2%d kB"), ESP_getFreeHeap1024());
#endif // ESP32
WSContentSend_P(PSTR(" |
"));