Switch from GPIO array to Pin array

This commit is contained in:
Theo Arends 2020-04-28 14:42:47 +02:00
parent 4536746d03
commit 60e7a73b60
8 changed files with 54 additions and 15 deletions

View File

@ -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)
{

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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<GPIO_SENSOR_END;i++) { // Theo/Gemu: This needs to change when pin[] becomes real pin array
// if (pin[i]==gpiopin) {
if (Pin(i)==gpiopin) {
@ -1591,6 +1592,14 @@ chknext:
goto exit;
}
}
#else
if ((gpiopin < ARRAY_SIZE(pin)) && (pin[gpiopin] > 0)) {
fvar = pin[gpiopin];
// skip ] bracket
len++;
goto exit;
}
#endif
fvar=999;
goto exit;
}

View File

@ -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);

View File

@ -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
}

View File

@ -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<GPIO_SENSOR_END;i++) { // Theo/Gemu: This needs to change when pin[] has becomes real pin array
// if (pin[i]==gpiopin) {
if (Pin(i)==gpiopin) {
return true;
}
}
#else
if ((gpiopin < ARRAY_SIZE(pin)) && (pin[gpiopin] > 0)) {
return true;
}
#endif
return false;
}