Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/PowerFSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,17 @@ static void lsIdle()
default:
// We woke for some other reason (button press, device IRQ interrupt)

#ifdef BUTTON_PIN
bool pressed = !digitalRead(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN);
#if HAS_BUTTON
uint32_t _btnPin = 0xFF;
#if defined(USERPREFS_BUTTON_PIN)
_btnPin = USERPREFS_BUTTON_PIN;
#elif defined(BUTTON_PIN)
_btnPin = BUTTON_PIN;
#endif
if (config.device.button_gpio != 0) {
_btnPin = config.device.button_gpio;
}
bool pressed = (_btnPin != 0xFF) ? !digitalRead(_btnPin) : false;
#else
bool pressed = false;
#endif
Expand Down
44 changes: 31 additions & 13 deletions src/input/InputBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static bool touchBacklightActive = false;
#endif
#endif

#if defined(BUTTON_PIN) || defined(ARCH_PORTDUINO)
#if HAS_BUTTON || defined(ARCH_PORTDUINO)
ButtonThread *UserButtonThread = nullptr;
#endif

Expand Down Expand Up @@ -165,28 +165,39 @@ void InputBroker::pollSoonWorker(void *p)
void InputBroker::Init()
{

#ifdef BUTTON_PIN
#if HAS_BUTTON
#ifdef ARCH_ESP32
uint32_t _btnPin = 0xFF;
#if defined(USERPREFS_BUTTON_PIN)
_btnPin = USERPREFS_BUTTON_PIN;
#elif defined(BUTTON_PIN)
_btnPin = BUTTON_PIN;
#endif
if (config.device.button_gpio != 0) {
_btnPin = config.device.button_gpio;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

if (_btnPin != 0xFF) {
#if ESP_ARDUINO_VERSION_MAJOR >= 3
#ifdef BUTTON_NEED_PULLUP
pinMode(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, INPUT_PULLUP);
pinMode(_btnPin, INPUT_PULLUP);
#else
pinMode(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, INPUT); // default to BUTTON_PIN
pinMode(_btnPin, INPUT);
#endif
#else
pinMode(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN, INPUT); // default to BUTTON_PIN
pinMode(_btnPin, INPUT);
#ifdef BUTTON_NEED_PULLUP
gpio_pullup_en((gpio_num_t)(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN));
delay(10);
gpio_pullup_en((gpio_num_t)_btnPin);
delay(10);
#endif
#endif
}
#ifdef BUTTON_NEED_PULLUP2
gpio_pullup_en((gpio_num_t)BUTTON_NEED_PULLUP2);
delay(10);
#endif
#endif
#endif
#endif

// buttons are now inputBroker, so have to come after setupModules
#if HAS_BUTTON
Expand Down Expand Up @@ -314,12 +325,17 @@ void InputBroker::Init()
BackButtonThread->initButton(backConfig);
#endif

#if defined(BUTTON_PIN)
#if HAS_BUTTON
uint32_t _pinNum = 0xFF;
#if defined(USERPREFS_BUTTON_PIN)
int _pinNum = config.device.button_gpio ? config.device.button_gpio : USERPREFS_BUTTON_PIN;
#else
int _pinNum = config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN;
_pinNum = USERPREFS_BUTTON_PIN;
#elif defined(BUTTON_PIN)
_pinNum = BUTTON_PIN;
#endif
if (config.device.button_gpio != 0) {
_pinNum = config.device.button_gpio;
}

#ifndef BUTTON_ACTIVE_LOW
#define BUTTON_ACTIVE_LOW true
#endif
Expand All @@ -329,7 +345,8 @@ void InputBroker::Init()

// Buttons. Moved here cause we need NodeDB to be initialized
// If your variant.h has a BUTTON_PIN defined, go ahead and define BUTTON_ACTIVE_LOW and BUTTON_ACTIVE_PULLUP
UserButtonThread = new ButtonThread("UserButton");
if (_pinNum != 0xFF) {
UserButtonThread = new ButtonThread("UserButton");
#if !MESHTASTIC_EXCLUDE_SCREEN
if (screen) {
ButtonConfig userConfig;
Expand Down Expand Up @@ -378,6 +395,7 @@ void InputBroker::Init()
userConfigNoScreen.triplePress = INPUT_BROKER_GPS_TOGGLE;
UserButtonThread->initButton(userConfigNoScreen);
}
}
#endif
#endif

Expand Down
37 changes: 24 additions & 13 deletions src/platform/esp32/main-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,35 +351,46 @@ void cpuDeepSleep(uint32_t msecToWake)

// FIXME, disable internal rtc pullups/pulldowns on the non isolated pins. for inputs that we aren't using
// to detect wake and in normal operation the external part drives them hard.
#ifdef BUTTON_PIN
// Only GPIOs which are have RTC functionality can be used in this bit map: 0,2,4,12-15,25-27,32-39.
#if HAS_BUTTON
uint32_t _btnPin = 0xFF;
#if defined(USERPREFS_BUTTON_PIN)
_btnPin = USERPREFS_BUTTON_PIN;
#elif defined(BUTTON_PIN)
_btnPin = BUTTON_PIN;
#endif
if (config.device.button_gpio != 0) {
_btnPin = config.device.button_gpio;
}

uint64_t gpioMask = 0;
#if SOC_RTCIO_HOLD_SUPPORTED && SOC_PM_SUPPORT_EXT_WAKEUP
uint64_t gpioMask = (1ULL << (config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN));
if (_btnPin != 0xFF) {
gpioMask |= (1ULL << _btnPin);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
#endif
#ifdef ALT_BUTTON_WAKE
gpioMask |= (1ULL << BUTTON_PIN_ALT);
#endif
#ifdef BUTTON_NEED_PULLUP
gpio_pullup_en((gpio_num_t)BUTTON_PIN);
if (_btnPin != 0xFF) {
gpio_pullup_en((gpio_num_t)_btnPin);
}
#endif

// Not needed because both of the current boards have external pullups
// FIXME change polarity in hw so we can wake on ANY_HIGH instead - that would allow us to use all three buttons (instead
// of just the first) gpio_pullup_en((gpio_num_t)BUTTON_PIN);

if (gpioMask != 0) {
#ifdef ESP32S3_WAKE_TYPE
esp_sleep_enable_ext1_wakeup(gpioMask, ESP32S3_WAKE_TYPE);
esp_sleep_enable_ext1_wakeup(gpioMask, ESP32S3_WAKE_TYPE);
#else
#if SOC_PM_SUPPORT_EXT_WAKEUP
#ifdef CONFIG_IDF_TARGET_ESP32
// ESP_EXT1_WAKEUP_ALL_LOW has been deprecated since esp-idf v5.4 for any other target.
esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW);
// ESP_EXT1_WAKEUP_ALL_LOW has been deprecated since esp-idf v5.4 for any other target.
esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW);
#else
esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ANY_LOW);
esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ANY_LOW);
#endif
#endif

#endif // #end ESP32S3_WAKE_TYPE
}
#endif
variant_shutdown();

Expand Down
60 changes: 47 additions & 13 deletions src/sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,26 @@ void doDeepSleep(uint32_t msecToWake, bool skipPreflight = false, bool skipSaveN
if (shouldLoraWake(msecToWake)) {
enableLoraInterrupt();
}
#ifdef BUTTON_PIN
// Avoid leakage through button pin
if (GPIO_IS_VALID_OUTPUT_GPIO(BUTTON_PIN)) {
#if HAS_BUTTON
uint32_t _btnPin = 0xFF;
#if defined(USERPREFS_BUTTON_PIN)
_btnPin = USERPREFS_BUTTON_PIN;
#elif defined(BUTTON_PIN)
_btnPin = BUTTON_PIN;
#endif
if (config.device.button_gpio != 0) {
_btnPin = config.device.button_gpio;
}
if (_btnPin != 0xFF) {
// Avoid leakage through button pin
if (GPIO_IS_VALID_OUTPUT_GPIO(_btnPin)) {
#ifdef BUTTON_NEED_PULLUP
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(_btnPin, INPUT_PULLUP);
#else
pinMode(BUTTON_PIN, INPUT);
pinMode(_btnPin, INPUT);
#endif
gpio_hold_en((gpio_num_t)BUTTON_PIN);
gpio_hold_en((gpio_num_t)_btnPin);
}
}
#endif
#ifdef SENSECAP_INDICATOR
Expand Down Expand Up @@ -425,8 +436,19 @@ esp_sleep_wakeup_cause_t doLightSleep(uint64_t sleepMsec) // FIXME, use a more r
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
#endif

#if defined(BUTTON_PIN) && defined(BUTTON_NEED_PULLUP)
gpio_pullup_en((gpio_num_t)BUTTON_PIN);
#if HAS_BUTTON && defined(BUTTON_NEED_PULLUP)
uint32_t _btnPin_pullup = 0xFF;
#if defined(USERPREFS_BUTTON_PIN)
_btnPin_pullup = USERPREFS_BUTTON_PIN;
#elif defined(BUTTON_PIN)
_btnPin_pullup = BUTTON_PIN;
#endif
if (config.device.button_gpio != 0) {
_btnPin_pullup = config.device.button_gpio;
}
if (_btnPin_pullup != 0xFF) {
gpio_pullup_en((gpio_num_t)_btnPin_pullup);
}
#endif

#ifdef SERIAL0_RX_GPIO
Expand Down Expand Up @@ -459,9 +481,19 @@ esp_sleep_wakeup_cause_t doLightSleep(uint64_t sleepMsec) // FIXME, use a more r
// Side-key interrupt line from PCA9535 expander (active low).
gpio_wakeup_enable((gpio_num_t)BOARD_PCA9535_INT, GPIO_INTR_LOW_LEVEL);
#endif
#ifdef BUTTON_PIN
gpio_num_t pin = (gpio_num_t)(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN);
gpio_wakeup_enable(pin, GPIO_INTR_LOW_LEVEL);
#if HAS_BUTTON
uint32_t _btnPin_wake = 0xFF;
#if defined(USERPREFS_BUTTON_PIN)
_btnPin_wake = USERPREFS_BUTTON_PIN;
#elif defined(BUTTON_PIN)
_btnPin_wake = BUTTON_PIN;
#endif
if (config.device.button_gpio != 0) {
_btnPin_wake = config.device.button_gpio;
}
if (_btnPin_wake != 0xFF) {
gpio_wakeup_enable((gpio_num_t)_btnPin_wake, GPIO_INTR_LOW_LEVEL);
}
#endif
#if defined(INPUTDRIVER_TWO_WAY_ROCKER_BTN) || defined(INPUTDRIVER_ENCODER_BTN)
#if defined(INPUTDRIVER_TWO_WAY_ROCKER_BTN)
Expand Down Expand Up @@ -509,9 +541,11 @@ esp_sleep_wakeup_cause_t doLightSleep(uint64_t sleepMsec) // FIXME, use a more r
#ifdef BOARD_PCA9535_INT
gpio_wakeup_disable((gpio_num_t)BOARD_PCA9535_INT);
#endif
#ifdef BUTTON_PIN
#if HAS_BUTTON
// Disable wake-on-button interrupt. Re-attach normal button-interrupts
gpio_wakeup_disable(pin);
if (_btnPin_wake != 0xFF) {
gpio_wakeup_disable((gpio_num_t)_btnPin_wake);
}
#endif
#ifdef INPUTDRIVER_WAKE_BTN_PIN
gpio_wakeup_disable((gpio_num_t)INPUTDRIVER_WAKE_BTN_PIN);
Expand Down