-
Notifications
You must be signed in to change notification settings - Fork 395
To support PowerSaving for all ESP32-based repeaters and NRF52-based repeaters. #1353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
e30b611
ea42140
07b0c72
67a4398
f988eb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,18 +57,39 @@ class ESP32Board : public mesh::MainBoard { | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| void enterLightSleep(uint32_t secs) { | ||||||||||||||
| #if defined(CONFIG_IDF_TARGET_ESP32S3) && defined(P_LORA_DIO_1) // Supported ESP32 variants | ||||||||||||||
| if (rtc_gpio_is_valid_gpio((gpio_num_t)P_LORA_DIO_1)) { // Only enter sleep mode if P_LORA_DIO_1 is RTC pin | ||||||||||||||
| esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); | ||||||||||||||
| esp_sleep_enable_ext1_wakeup((1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // To wake up when receiving a LoRa packet | ||||||||||||||
| #if defined(RADIO_SX1276) && defined(P_LORA_DIO_0) // SX1276 | ||||||||||||||
| gpio_num_t wakeupPin = (gpio_num_t) P_LORA_DIO_0; | ||||||||||||||
| #elif defined(P_LORA_DIO_1) // SX1262 | ||||||||||||||
| gpio_num_t wakeupPin = (gpio_num_t) P_LORA_DIO_1; | ||||||||||||||
| #else | ||||||||||||||
| return; // Not supported | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| if (secs > 0) { | ||||||||||||||
| esp_sleep_enable_timer_wakeup(secs * 1000000); // To wake up every hour to do periodically jobs | ||||||||||||||
| } | ||||||||||||||
| // To configure GPIO wakeup | ||||||||||||||
| esp_sleep_enable_gpio_wakeup(); | ||||||||||||||
| gpio_wakeup_enable((gpio_num_t) wakeupPin, GPIO_INTR_HIGH_LEVEL); // To wake up when receiving a LoRa packet | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the |
||||||||||||||
|
|
||||||||||||||
| esp_light_sleep_start(); // CPU enters light sleep | ||||||||||||||
| // To configure timer wakeup | ||||||||||||||
| if (secs > 0) { | ||||||||||||||
| esp_sleep_enable_timer_wakeup(secs * 1000000ULL); // To wake up periodically to do scheduled jobs | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // To disable CPU interrupt servicing | ||||||||||||||
| noInterrupts(); | ||||||||||||||
|
|
||||||||||||||
| // MCU to enter light sleep | ||||||||||||||
| esp_light_sleep_start(); | ||||||||||||||
| Serial.println(esp_sleep_get_wakeup_cause()); | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably it's better to use the
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| // To avoid ISR flood during wakeup due to HIGH LEVEL interrupt | ||||||||||||||
| #if defined(ESP32) && defined(RADIO_SX1276) && defined(P_LORA_DIO_0) // SX1276 | ||||||||||||||
| gpio_set_intr_type((gpio_num_t)P_LORA_DIO_0, GPIO_INTR_POSEDGE); | ||||||||||||||
| #elif defined(ESP32) && defined(P_LORA_DIO_1) // SX1262 | ||||||||||||||
| gpio_set_intr_type((gpio_num_t)P_LORA_DIO_1, GPIO_INTR_POSEDGE); | ||||||||||||||
| #endif | ||||||||||||||
|
Comment on lines
+85
to
89
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole block can be replaced with a single line
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| // To disable CPU interrupt servicing | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| interrupts(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| void sleep(uint32_t secs) override { | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| #if defined(NRF52_PLATFORM) | ||
| #include "NRF52Board.h" | ||
|
|
||
| #include "nrf_sdm.h" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this, do we? |
||
| #include <bluefruit.h> | ||
|
|
||
| static BLEDfu bledfu; | ||
|
|
@@ -101,4 +101,29 @@ bool NRF52BoardOTA::startOTAUpdate(const char *id, char reply[]) { | |
|
|
||
| return true; | ||
| } | ||
|
|
||
| void NRF52Board::enterLightSleep(uint32_t secs) { | ||
| #if defined(P_LORA_DIO_1) | ||
| // To mark the start of the sleep | ||
| uint32_t startTime = millis(); | ||
|
|
||
| // To wake up when a LoRa packet comes or sleep timeout. Safe to 49-day overflow | ||
| while (digitalRead(P_LORA_DIO_1) == LOW && (millis() - startTime < secs*1000)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the boards with DIO0 interrupt instead of DIO1? The As mentioned above we could use a |
||
| // To enter System-on idle | ||
| __SEV(); // To clear any stale event flag | ||
| __WFE(); // To consume that event | ||
| __WFE(); // To actually puts CPU to sleep | ||
| } | ||
IoTThinks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #endif | ||
| } | ||
|
|
||
| void NRF52Board::sleep(uint32_t secs) { | ||
| // To check if the BLE is powered and looking for/connected to a phone | ||
| uint8_t sd_enabled; | ||
| sd_softdevice_is_enabled(&sd_enabled); // To set sd_enabled to 1 if the BLE stack is active. | ||
|
|
||
| if (!sd_enabled) { // BLE is off ~ No active OTA, safe to go to sleep | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of checking for the softdevice, I would rather implement a generic mechanism to prevent the sleep which can be used in other cases too. See fe17894.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love to have consistent approach for ESP32 and NRF52. I think we need to keep thing simple and effective.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You seem to miss the point. The mentioned commit from my PR implements an easy and straight-forward way in the BaseBoard base class, that can be used by all board types. Your implementation is specific to ESP32 and can only be used from inside the board class.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have same "sleep" methods for ESP32 and NRF52.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In #1238 I floated the idea to use safeToSleep. This could easily be implemented in a generic manner by adding it to diff --git a/src/MeshCore.h b/src/MeshCore.h
index b52ac21..a0ea11b 100644
--- a/src/MeshCore.h
+++ b/src/MeshCore.h
@@ -59,6 +59,11 @@ public:
virtual void enterSleep(uint32_t secs) {
if (!prevent_sleep) sleep(secs);
}
+ virtual bool safeToSleep() { return true; }
virtual void preventSleep() { prevent_sleep = true; }
virtual uint32_t getGpio() { return 0; }
virtual void setGpio(uint32_t values) {}With your changes moving the DIO1 pin configuration from the header files into platform.io the diff --git a/src/helpers/NRF52Board.h b/src/helpers/NRF52Board.h
index f187242..fac7fc3 100644
--- a/src/helpers/NRF52Board.h
+++ b/src/helpers/NRF52Board.h
@@ -16,6 +16,11 @@ public:
virtual void reboot() override { NVIC_SystemReset(); }
virtual void sleep(uint32_t secs) override;
virtual void onRXInterrupt() override;
virtual bool safeToSleep() override { return digitalRead(P_LORA_DIO_1) == LOW; }
}; |
||
| enterLightSleep(secs); // To wake up after "secs" seconds or when receiving a LoRa packet | ||
| } | ||
| } | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ class NRF52Board : public mesh::MainBoard { | |
| virtual uint8_t getStartupReason() const override { return startup_reason; } | ||
| virtual float getMCUTemperature() override; | ||
| virtual void reboot() override { NVIC_SystemReset(); } | ||
| virtual void enterLightSleep(uint32_t secs); | ||
| virtual void sleep(uint32_t secs) override; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need 2 functions, it looks like all can be implemented inside |
||
| }; | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,11 @@ build_flags = | |
| -D HELTEC_LORA_V2 | ||
| -D RADIO_CLASS=CustomSX1276 | ||
| -D WRAPPER_CLASS=CustomSX1276Wrapper | ||
| -D P_LORA_DIO_1=26 | ||
| -D RADIO_SX1276 | ||
| -D P_LORA_DIO_0=26 | ||
| -D P_LORA_DIO_1=35 | ||
| -D P_LORA_NSS=18 | ||
| -D P_LORA_RESET=RADIOLIB_NC | ||
| -D P_LORA_BUSY=RADIOLIB_NC | ||
| -D P_LORA_RESET=14 | ||
| -D P_LORA_SCLK=5 | ||
| -D P_LORA_MISO=19 | ||
| -D P_LORA_MOSI=27 | ||
|
|
@@ -183,7 +184,6 @@ build_flags = | |
| -D WIFI_DEBUG_LOGGING=1 | ||
| -D WIFI_SSID='"myssid"' | ||
| -D WIFI_PWD='"mypwd"' | ||
| -D OFFLINE_QUEUE_SIZE=256 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be removed, right? |
||
| ; -D MESH_PACKET_LOGGING=1 | ||
| ; -D MESH_DEBUG=1 | ||
| build_src_filter = ${Heltec_lora32_v2.build_src_filter} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ build_flags = | |
| -D DISPLAY_CLASS=SSD1306Display | ||
| -D RADIO_CLASS=CustomSX1276 | ||
| -D WRAPPER_CLASS=CustomSX1276Wrapper | ||
| -D RADIO_SX1276 | ||
| -D SX127X_CURRENT_LIMIT=120 | ||
| -D LORA_TX_POWER=20 | ||
| build_src_filter = ${esp32_base.build_src_filter} | ||
|
|
@@ -136,7 +137,6 @@ build_flags = | |
| -D WIFI_SSID='"ssid"' | ||
| -D WIFI_PWD='"password"' | ||
| -D WIFI_DEBUG_LOGGING=1 | ||
| -D OFFLINE_QUEUE_SIZE=256 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be removed, right? |
||
| build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter} | ||
| +<helpers/esp32/*.cpp> | ||
| +<helpers/ui/MomentaryButton.cpp> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.