From 60e7a73b6066733c948993076df47f48cdecfaa8 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 28 Apr 2020 14:42:47 +0200 Subject: [PATCH] Switch from GPIO array to Pin array --- tasmota/support.ino | 16 +++++++++------- tasmota/support_tasmota.ino | 24 +++++++++++++++++------- tasmota/tasmota.ino | 6 +++++- tasmota/tasmota_globals.h | 2 ++ tasmota/xdrv_10_scripter.ino | 9 +++++++++ tasmota/xdrv_31_tasmota_slave.ino | 2 ++ tasmota/xnrg_01_hlw8012.ino | 4 ++++ tasmota/xsns_53_sml.ino | 6 ++++++ 8 files changed, 54 insertions(+), 15 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index 0986a007e..1bcdb9015 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1079,11 +1079,14 @@ uint32_t Pin(uint32_t gpio, uint32_t index) ICACHE_RAM_ATTR; uint32_t Pin(uint32_t gpio, uint32_t index = 0); uint32_t Pin(uint32_t gpio, uint32_t index) { -//#ifdef ESP8266 +#ifdef LEGACY_GPIO_ARRAY return pin[gpio + index]; // Pin number configured for gpio or 99 if not used -/* #else - uint16_t real_gpio = (gpio << 5) + index; +//#ifdef ESP8266 + uint16_t real_gpio = gpio + index; +//#else +// uint16_t real_gpio = (gpio << 5) + index; +//endif for (uint32_t i = 0; i < ARRAY_SIZE(pin); i++) { if (pin[i] == real_gpio) { return i; // Pin number configured for gpio @@ -1091,7 +1094,6 @@ uint32_t Pin(uint32_t gpio, uint32_t index) { } return 99; // No pin used for gpio #endif -*/ } boolean PinUsed(uint32_t gpio, uint32_t index = 0); @@ -1100,20 +1102,20 @@ boolean PinUsed(uint32_t gpio, uint32_t index) { } void SetPin(uint32_t lpin, uint32_t gpio) { -//#ifdef ESP8266 +#ifdef LEGACY_GPIO_ARRAY pin[gpio] = lpin; -/* #else pin[lpin] = gpio; #endif -*/ } +#ifdef LEGACY_GPIO_ARRAY void InitAllPins(void) { for (uint32_t i = 0; i < ARRAY_SIZE(pin); i++) { SetPin(99, i); } } +#endif void DigitalWrite(uint32_t gpio_pin, uint32_t index, uint32_t state) { diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 3db5ea2f7..72137edab 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1383,13 +1383,15 @@ void GpioInit(void) my_adc0 = template_adc0; // Force Template override } +#ifdef LEGACY_GPIO_ARRAY InitAllPins(); +#endif for (uint32_t i = 0; i < ARRAY_SIZE(my_module.io); i++) { uint32_t mpin = ValidPin(i, my_module.io[i]); DEBUG_CORE_LOG(PSTR("INI: gpio pin %d, mpin %d"), i, mpin); - if (mpin) { + if (mpin) { // Above GPIO_NONE XdrvMailbox.index = mpin; XdrvMailbox.payload = i; @@ -1398,16 +1400,16 @@ void GpioInit(void) mpin -= (GPIO_SWT1_NP - GPIO_SWT1); } else if ((mpin >= GPIO_KEY1_NP) && (mpin < (GPIO_KEY1_NP + MAX_KEYS))) { - ButtonPullupFlag(mpin - GPIO_KEY1_NP); // 0 .. 3 + ButtonPullupFlag(mpin - GPIO_KEY1_NP); // 0 .. 3 mpin -= (GPIO_KEY1_NP - GPIO_KEY1); } else if ((mpin >= GPIO_KEY1_INV) && (mpin < (GPIO_KEY1_INV + MAX_KEYS))) { - ButtonInvertFlag(mpin - GPIO_KEY1_INV); // 0 .. 3 + ButtonInvertFlag(mpin - GPIO_KEY1_INV); // 0 .. 3 mpin -= (GPIO_KEY1_INV - GPIO_KEY1); } else if ((mpin >= GPIO_KEY1_INV_NP) && (mpin < (GPIO_KEY1_INV_NP + MAX_KEYS))) { - ButtonPullupFlag(mpin - GPIO_KEY1_INV_NP); // 0 .. 3 - ButtonInvertFlag(mpin - GPIO_KEY1_INV_NP); // 0 .. 3 + ButtonPullupFlag(mpin - GPIO_KEY1_INV_NP); // 0 .. 3 + ButtonInvertFlag(mpin - GPIO_KEY1_INV_NP); // 0 .. 3 mpin -= (GPIO_KEY1_INV_NP - GPIO_KEY1); } else if ((mpin >= GPIO_REL1_INV) && (mpin < (GPIO_REL1_INV + MAX_RELAYS))) { @@ -1433,9 +1435,13 @@ void GpioInit(void) mpin = XdrvMailbox.index; }; } - if (mpin) { SetPin(i, mpin); } + if (mpin) { SetPin(i, mpin); } // Anything above GPIO_NONE and below GPIO_SENSOR_END } +#ifndef LEGACY_GPIO_ARRAY + AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)pin, ARRAY_SIZE(pin)); +#endif + #ifdef ESP8266 if ((2 == Pin(GPIO_TXD)) || (H801 == my_module_type)) { Serial.set_tx(2); } #endif // ESP8266 @@ -1446,9 +1452,11 @@ void GpioInit(void) #ifdef USE_SPI spi_flg = (((PinUsed(GPIO_SPI_CS) && (Pin(GPIO_SPI_CS) > 14)) || (Pin(GPIO_SPI_CS) < 12)) || ((PinUsed(GPIO_SPI_DC) && (Pin(GPIO_SPI_DC) > 14)) || (Pin(GPIO_SPI_DC) < 12))); if (spi_flg) { +#ifdef LEGACY_GPIO_ARRAY for (uint32_t i = 0; i < GPIO_MAX; i++) { if ((Pin(i) >= 12) && (Pin(i) <=14)) { SetPin(99, i); } } +#endif my_module.io[12] = GPIO_SPI_MISO; SetPin(12, GPIO_SPI_MISO); my_module.io[13] = GPIO_SPI_MOSI; @@ -1456,7 +1464,7 @@ void GpioInit(void) my_module.io[14] = GPIO_SPI_CLK; SetPin(14, GPIO_SPI_CLK); } - soft_spi_flg = (PinUsed(GPIO_SSPI_CS) && PinUsed(GPIO_SSPI_SCLK) && (PinUsed(GPIO_SSPI_MOSI) || PinUsed(GPIO_SSPI_MOSI))); + soft_spi_flg = (PinUsed(GPIO_SSPI_CS) && PinUsed(GPIO_SSPI_SCLK) && (PinUsed(GPIO_SSPI_MOSI) || PinUsed(GPIO_SSPI_MISO))); #endif // USE_SPI // Set any non-used GPIO to INPUT - Related to resetPins() in support_legacy_cores.ino @@ -1534,7 +1542,9 @@ void GpioInit(void) #ifdef USE_ARILUX_RF if ((3 == i) && (leds_present < 2) && !PinUsed(GPIO_ARIRFSEL)) { SetPin(Pin(GPIO_LED4), GPIO_ARIRFSEL); // Legacy support where LED4 was Arilux RF enable +#ifdef LEGACY_GPIO_ARRAY SetPin(99, GPIO_LED4); +#endif } else { #endif pinMode(Pin(GPIO_LED1, i), OUTPUT); diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 69be1bbe5..4f84832ac 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -119,7 +119,7 @@ uint16_t blink_counter = 0; // Number of blink cycles uint16_t seriallog_timer = 0; // Timer to disable Seriallog uint16_t syslog_timer = 0; // Timer to re-enable syslog_level //#ifdef ESP32 -//uint16_t pin[MAX_GPIO_PIN]; // Possible pin configurations +//uint16_t pin[MAX_GPIO_PIN] = { 0 }; // Possible pin configurations //#endif int16_t save_data_counter; // Counter and flag for config save to Flash RulesBitfield rules_flag; // Rule state flags (16 bits) @@ -130,7 +130,11 @@ uint8_t latching_relay_pulse = 0; // Latching relay pulse timer uint8_t ssleep; // Current copy of Settings.sleep uint8_t blinkspeed = 1; // LED blink rate //#ifdef ESP8266 +#ifdef LEGACY_GPIO_ARRAY uint8_t pin[GPIO_MAX]; // Possible pin configurations +#else +uint8_t pin[MAX_GPIO_PIN] = { 0 }; // Possible pin configurations +#endif //#endif uint8_t active_device = 1; // Active device in ExecuteCommandPower uint8_t leds_present = 0; // Max number of LED supported diff --git a/tasmota/tasmota_globals.h b/tasmota/tasmota_globals.h index 9b72ba954..8881f8334 100644 --- a/tasmota/tasmota_globals.h +++ b/tasmota/tasmota_globals.h @@ -53,6 +53,8 @@ extern "C" void resetPins(); * Mandatory defines satisfying disabled defines \*********************************************************************************************/ +//#define LEGACY_GPIO_ARRAY + #ifndef MODULE #define MODULE SONOFF_BASIC // [Module] Select default model #endif diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index 614989ab4..0c2ad48cf 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -1582,6 +1582,7 @@ chknext: if (!strncmp(vname,"pd[",3)) { GetNumericResult(vname+3,OPER_EQU,&fvar,0); uint8_t gpiopin=fvar; +#ifdef LEGACY_GPIO_ARRAY for (uint8_t i=0;i 0)) { + fvar = pin[gpiopin]; + // skip ] bracket + len++; + goto exit; + } +#endif fvar=999; goto exit; } diff --git a/tasmota/xdrv_31_tasmota_slave.ino b/tasmota/xdrv_31_tasmota_slave.ino index 37b98c897..370a3f6d6 100644 --- a/tasmota/xdrv_31_tasmota_slave.ino +++ b/tasmota/xdrv_31_tasmota_slave.ino @@ -447,7 +447,9 @@ void TasmotaSlave_Init(void) TasmotaSlave_Serial->setTimeout(50); if (PinUsed(GPIO_TASMOTASLAVE_RST_INV)) { SetPin(Pin(GPIO_TASMOTASLAVE_RST_INV), GPIO_TASMOTASLAVE_RST); +#ifdef LEGACY_GPIO_ARRAY SetPin(99, GPIO_TASMOTASLAVE_RST_INV); +#endif TSlave.inverted = HIGH; } pinMode(Pin(GPIO_TASMOTASLAVE_RST), OUTPUT); diff --git a/tasmota/xnrg_01_hlw8012.ino b/tasmota/xnrg_01_hlw8012.ino index 12a10abb6..7a3becf0a 100644 --- a/tasmota/xnrg_01_hlw8012.ino +++ b/tasmota/xnrg_01_hlw8012.ino @@ -250,7 +250,9 @@ void HlwDrvInit(void) Hlw.model_type = 0; // HLW8012 if (PinUsed(GPIO_HJL_CF)) { SetPin(Pin(GPIO_HJL_CF), GPIO_HLW_CF); +#ifdef LEGACY_GPIO_ARRAY SetPin(99, GPIO_HJL_CF); +#endif Hlw.model_type = 1; // HJL-01/BL0937 } @@ -259,7 +261,9 @@ void HlwDrvInit(void) Hlw.ui_flag = true; // Voltage on high if (PinUsed(GPIO_NRG_SEL_INV)) { SetPin(Pin(GPIO_NRG_SEL_INV), GPIO_NRG_SEL); +#ifdef LEGACY_GPIO_ARRAY SetPin(99, GPIO_NRG_SEL_INV); +#endif Hlw.ui_flag = false; // Voltage on low } diff --git a/tasmota/xsns_53_sml.ino b/tasmota/xsns_53_sml.ino index c61bfa20d..632f64f50 100755 --- a/tasmota/xsns_53_sml.ino +++ b/tasmota/xsns_53_sml.ino @@ -1831,12 +1831,18 @@ uint8_t *script_meter; #endif bool Gpio_used(uint8_t gpiopin) { +#ifdef LEGACY_GPIO_ARRAY for (uint16_t i=0;i 0)) { + return true; + } +#endif return false; }