New isPinOk()

This commit is contained in:
Blaz Kristan 2022-09-23 14:35:17 +02:00
parent 222b92807e
commit d86d88c7b7
3 changed files with 55 additions and 52 deletions

View File

@ -200,7 +200,11 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
int8_t pin = btn["pin"][0] | -1;
if (pin > -1 && pinManager.allocatePin(pin, false, PinOwner::Button)) {
btnPin[s] = pin;
#ifdef ESP32
pinMode(btnPin[s], buttonType[s]==BTN_TYPE_PUSH_ACT_HIGH ? INPUT_PULLDOWN : INPUT_PULLUP);
#else
pinMode(btnPin[s], INPUT_PULLUP);
#endif
} else {
btnPin[s] = -1;
}

View File

@ -214,9 +214,7 @@ bool PinManagerClass::isPinAllocated(byte gpio, PinOwner tag)
return bitRead(pinAlloc[by], bi);
}
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3)
// ESP32-S3 GPIO layout
/* see https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/gpio.html
/* see https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/gpio.html
* The ESP32-S3 chip features 45 physical GPIO pins (GPIO0 ~ GPIO21 and GPIO26 ~ GPIO48). Each pin can be used as a general-purpose I/O
* Strapping pins: GPIO0, GPIO3, GPIO45 and GPIO46 are strapping pins. For more infomation, please refer to ESP32-S3 datasheet.
* Serial TX = GPIO43, RX = GPIO44; LED BUILTIN is usually GPIO39
@ -231,43 +229,40 @@ bool PinManagerClass::isPinAllocated(byte gpio, PinOwner tag)
* adc_power_acquire(): Please do not use the interrupt of GPIO36 and GPIO39 when using ADC or Wi-Fi and Bluetooth with sleep mode enabled. As a workaround, call adc_power_acquire() in the APP.
* Since the ADC2 module is also used by the Wi-Fi, reading operation of adc2_get_raw() may fail between esp_wifi_start() and esp_wifi_stop(). Use the return code to see whether the reading is successful.
*/
// Check if supplied GPIO is ok to use
bool PinManagerClass::isPinOk(byte gpio, bool output)
{
#ifdef ESP32
if (digitalPinIsValid(gpio)) {
#if defined(CONFIG_IDF_TARGET_ESP32C3)
if ((gpio > 10) && (gpio < 18)) return false; // 11-17 SPI FLASH
if (gpio < 22) return true;
#else // S2 & S3
#if defined(CONFIG_IDF_TARGET_ESP32S3)
if (gpio < 19) return true; // 00 to 18 are for general use. Be careful about straping pins GPIO0 and GPIO3 - these may be pulled-up or pulled-down on your board.
if (gpio < 21) return false; // 19 + 20 = USB-JTAG. Not recommended for other uses.
if ((gpio > 21) && (gpio < 33)) return false; // 22 to 32: not connected + SPI FLASH
//if (gpio <38) return false; // 33 to 37: not available if using _octal_ SPI Flash or _octal_ PSRAM
if (gpio < 49) return true; // 38 to 48 are for general use. Be careful about straping pins GPIO45 and GPIO46 - these may be pull-up or pulled-down on your board.
// strapping pins: 2, 8, & 9
if ((gpio > 11) && (gpio < 18)) return false; // 11-17 SPI FLASH
if ((gpio > 17) && (gpio < 20)) return false; // 18-19 USB-JTAG
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
// 00 to 18 are for general use. Be careful about straping pins GPIO0 and GPIO3 - these may be pulled-up or pulled-down on your board.
if (gpio > 18 && gpio < 21) return false; // 19 + 20 = USB-JTAG. Not recommended for other uses.
if (gpio > 21 && gpio < 33) return false; // 22 to 32: not connected + SPI FLASH
if (gpio > 32 && gpio < 38) return false; // 33 to 37: not available if using _octal_ SPI Flash or _octal_ PSRAM
// 38 to 48 are for general use. Be careful about straping pins GPIO45 and GPIO46 - these may be pull-up or pulled-down on your board.
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
if (gpio < 22) return true; // 00 to 21 are for general use.
if ((gpio > 21) && (gpio < 33)) return false; // 22 to 32: not connected + SPI FLASH
if (gpio < 46) return true; // 33 to 45 are for general use.
if (gpio == 46 && !output) return true; // 46 input only
// strapping pins: 0, 45 & 46
if (gpio > 21 && gpio < 33) return false; // 22 to 32: not connected + SPI FLASH
// JTAG: GPIO39-42 are usually used for inline debugging
// GPIO46 is input only and pulled down
#else
if (gpio > 5 && gpio < 12) return false; //SPI flash pins
#endif
#endif
return false;
}
#else // ESP32 and ESP8266 GPIO layout
bool PinManagerClass::isPinOk(byte gpio, bool output)
{
if (output) return digitalPinCanOutput(gpio);
else return true;
}
#else
if (gpio < 6) return true;
if (gpio < 12) return false; //SPI flash pins
#ifdef ESP8266
if (gpio < 17) return true;
#else //ESP32
if (gpio < 34) return true;
if (gpio < 40 && !output) return true; //34-39 input only
#endif
#endif
return false;
}
#endif
PinOwner PinManagerClass::getPinOwner(byte gpio) {
if (!isPinOk(gpio, false)) return PinOwner::None;

View File

@ -167,8 +167,12 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
int hw_btn_pin = request->arg(bt).toInt();
if (pinManager.allocatePin(hw_btn_pin,false,PinOwner::Button)) {
btnPin[i] = hw_btn_pin;
pinMode(btnPin[i], INPUT_PULLUP);
buttonType[i] = request->arg(be).toInt();
#ifdef ESP32
pinMode(btnPin[i], buttonType[i]==BTN_TYPE_PUSH_ACT_HIGH ? INPUT_PULLDOWN : INPUT_PULLUP);
#else
pinMode(btnPin[i], INPUT_PULLUP);
#endif
} else {
btnPin[i] = -1;
buttonType[i] = BTN_TYPE_NONE;