Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
37 changes: 29 additions & 8 deletions src/helpers/ESP32Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the gpio_num_t cast. It's not needed.


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());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it's better to use the MESH_DEBUG_PRINTLN function:

Suggested change
Serial.println(esp_sleep_get_wakeup_cause());
MESH_DEBUG_PRINTLN(esp_sleep_get_wakeup_cause());


// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole block can be replaced with a single line

Suggested change
#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
gpio_set_intr_type(wakeupPin, GPIO_INTR_POSEDGE);


// To disable CPU interrupt servicing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// To disable CPU interrupt servicing
// To enable CPU interrupt servicing

interrupts();
}

void sleep(uint32_t secs) override {
Expand Down
27 changes: 26 additions & 1 deletion src/helpers/NRF52Board.cpp
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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the boards with DIO0 interrupt instead of DIO1? The digitalRead(P_LORA_DIO_1) would be incorrect in this case, right?

As mentioned above we could use a getIrqPin() function to retrieve the correct pin.

// To enter System-on idle
__SEV(); // To clear any stale event flag
__WFE(); // To consume that event
__WFE(); // To actually puts CPU to sleep
}
#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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to have consistent approach for ESP32 and NRF52.
In ESP32, we don't have "enterSleep" and "preventSleep".

I think we need to keep thing simple and effective.
This code works not only for OTA, but also for repeaters with WiFi or BLE active due to any reasons.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have same "sleep" methods for ESP32 and NRF52.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link

Choose a reason for hiding this comment

The 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 MeshCore.h and overriding it in ESP32 as well as NRF52 (or even specific variants if necessary ). Pseudo patch:

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 safeToSleep method should probably only require implementation in the NRF52Board.h and ESP32Board.h. Pseudo patch:

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
2 changes: 2 additions & 0 deletions src/helpers/NRF52Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

Choose a reason for hiding this comment

The 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 sleep()? In case we do want 2 functions, I don't think enterLightSleep() should have public visibility.

};

/*
Expand Down
8 changes: 0 additions & 8 deletions src/helpers/esp32/TBeamBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@
// uint32_t P_LORA_BUSY = 0; //shared, so define at run
// uint32_t P_LORA_DIO_2 = 0; //SX1276 only, so define at run

#define P_LORA_DIO_0 26
#define P_LORA_DIO_1 33
#define P_LORA_NSS 18
#define P_LORA_RESET 23
#define P_LORA_SCLK 5
#define P_LORA_MISO 19
#define P_LORA_MOSI 27

// #define PIN_GPS_RX 34
// #define PIN_GPS_TX 12

Expand Down
12 changes: 0 additions & 12 deletions variants/heltec_mesh_solar/MeshSolarBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@
#include "meshSolarApp.h"
#endif

// LoRa radio module pins for Heltec T114
#define P_LORA_DIO_1 20
#define P_LORA_NSS 24
#define P_LORA_RESET 25
#define P_LORA_BUSY 17
#define P_LORA_SCLK 19
#define P_LORA_MISO 23
#define P_LORA_MOSI 22

#define SX126X_DIO2_AS_RF_SWITCH true
#define SX126X_DIO3_TCXO_VOLTAGE 1.8

class MeshSolarBoard : public NRF52BoardOTA {
public:
MeshSolarBoard() : NRF52BoardOTA("MESH_SOLAR_OTA") {}
Expand Down
9 changes: 9 additions & 0 deletions variants/heltec_mesh_solar/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ build_flags = ${nrf52_base.build_flags}
-I lib/nrf52/s140_nrf52_6.1.1_API/include/nrf52
-I variants/heltec_mesh_solar
-D HELTEC_MESH_SOLAR
-D P_LORA_DIO_1=20
-D P_LORA_NSS=24
-D P_LORA_RESET=25
-D P_LORA_BUSY=17
-D P_LORA_SCLK=19
-D P_LORA_MISO=23
-D P_LORA_MOSI=22
-D SX126X_DIO2_AS_RF_SWITCH=true
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
-D RADIO_CLASS=CustomSX1262
-D WRAPPER_CLASS=CustomSX1262Wrapper
-D LORA_TX_POWER=22
Expand Down
8 changes: 4 additions & 4 deletions variants/heltec_v2/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -183,7 +184,6 @@ build_flags =
-D WIFI_DEBUG_LOGGING=1
-D WIFI_SSID='"myssid"'
-D WIFI_PWD='"mypwd"'
-D OFFLINE_QUEUE_SIZE=256
Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Expand Down
4 changes: 2 additions & 2 deletions variants/heltec_v2/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ HeltecV2Board board;

#if defined(P_LORA_SCLK)
static SPIClass spi;
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi);
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, spi);
#else
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY);
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1);
#endif

WRAPPER_CLASS radio_driver(radio, board);
Expand Down
1 change: 1 addition & 0 deletions variants/lilygo_t3s3_sx1276/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ build_flags =
-D PIN_OLED_RESET=21
-D RADIO_CLASS=CustomSX1276
-D WRAPPER_CLASS=CustomSX1276Wrapper
-D RADIO_SX1276
-D SX127X_CURRENT_LIMIT=120
-D SX176X_RXEN=21
-D SX176X_TXEN=10
Expand Down
7 changes: 7 additions & 0 deletions variants/lilygo_tbeam_SX1262/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ build_flags =
${esp32_base.build_flags}
-I variants/lilygo_tbeam_SX1262
-D TBEAM_SX1262
-D P_LORA_DIO_0=26
-D P_LORA_DIO_1=33
-D P_LORA_NSS=18
-D P_LORA_RESET=23
-D P_LORA_SCLK=5
-D P_LORA_MISO=19
-D P_LORA_MOSI=27
-D SX126X_DIO2_AS_RF_SWITCH=true
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
-D SX126X_CURRENT_LIMIT=140
Expand Down
8 changes: 8 additions & 0 deletions variants/lilygo_tbeam_SX1276/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ build_flags =
${esp32_base.build_flags}
-I variants/lilygo_tbeam_SX1276
-D TBEAM_SX1276
-D P_LORA_DIO_0=26
-D P_LORA_DIO_1=33
-D P_LORA_NSS=18
-D P_LORA_RESET=23
-D P_LORA_SCLK=5
-D P_LORA_MISO=19
-D P_LORA_MOSI=27
-D SX127X_CURRENT_LIMIT=120
-D RADIO_CLASS=CustomSX1276
-D WRAPPER_CLASS=CustomSX1276Wrapper
-D RADIO_SX1276
-D DISPLAY_CLASS=SSD1306Display
-D LORA_TX_POWER=20
-D P_LORA_TX_LED=4
Expand Down
7 changes: 7 additions & 0 deletions variants/lilygo_tbeam_supreme_SX1262/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ build_flags =
${esp32_base.build_flags}
-I variants/lilygo_tbeam_supreme_SX1262
-D TBEAM_SUPREME_SX1262
-D P_LORA_DIO_0=26
-D P_LORA_DIO_1=33
-D P_LORA_NSS=18
-D P_LORA_RESET=23
-D P_LORA_SCLK=5
-D P_LORA_MISO=19
-D P_LORA_MOSI=27
-D SX126X_CURRENT_LIMIT=140
-D SX126X_RX_BOOSTED_GAIN=1
-D RADIO_CLASS=CustomSX1262
Expand Down
2 changes: 1 addition & 1 deletion variants/lilygo_tlora_v2_1/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -136,7 +137,6 @@ build_flags =
-D WIFI_SSID='"ssid"'
-D WIFI_PWD='"password"'
-D WIFI_DEBUG_LOGGING=1
-D OFFLINE_QUEUE_SIZE=256
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
Expand Down
13 changes: 0 additions & 13 deletions variants/promicro/PromicroBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
#include <Arduino.h>
#include <helpers/NRF52Board.h>

#define P_LORA_NSS 13 //P1.13 45
#define P_LORA_DIO_1 11 //P0.10 10
#define P_LORA_RESET 10 //P0.09 9
#define P_LORA_BUSY 16 //P0.29 29
#define P_LORA_MISO 15 //P0.02 2
#define P_LORA_SCLK 12 //P1.11 43
#define P_LORA_MOSI 14 //P1.15 47
#define SX126X_POWER_EN 21 //P0.13 13
#define SX126X_RXEN 2 //P0.17
#define SX126X_TXEN RADIOLIB_NC
#define SX126X_DIO2_AS_RF_SWITCH true
#define SX126X_DIO3_TCXO_VOLTAGE (1.8f)

#define PIN_VBAT_READ 17
#define ADC_MULTIPLIER (1.815f) // dependent on voltage divider resistors. TODO: more accurate battery tracking

Expand Down
12 changes: 12 additions & 0 deletions variants/promicro/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ board = promicro_nrf52840
build_flags = ${nrf52_base.build_flags}
-I variants/promicro
-D PROMICRO
-D P_LORA_NSS=13
-D P_LORA_DIO_1=11
-D P_LORA_RESET=10
-D P_LORA_BUSY=16
-D P_LORA_MISO=15
-D P_LORA_SCLK=12
-D P_LORA_MOSI=14
-D SX126X_POWER_EN=21
-D SX126X_RXEN=2
-D SX126X_TXEN=RADIOLIB_NC
-D SX126X_DIO2_AS_RF_SWITCH=true
-D SX126X_DIO3_TCXO_VOLTAGE=1.8f
-D RADIO_CLASS=CustomSX1262
-D WRAPPER_CLASS=CustomSX1262Wrapper
-D LORA_TX_POWER=22
Expand Down
10 changes: 0 additions & 10 deletions variants/rak4631/RAK4631Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
#include <Arduino.h>
#include <helpers/NRF52Board.h>

// LoRa radio module pins for RAK4631
#define P_LORA_DIO_1 47
#define P_LORA_NSS 42
#define P_LORA_RESET RADIOLIB_NC // 38
#define P_LORA_BUSY 46
#define P_LORA_SCLK 43
#define P_LORA_MISO 45
#define P_LORA_MOSI 44
#define SX126X_POWER_EN 37

//#define PIN_GPS_SDA 13 //GPS SDA pin (output option)
//#define PIN_GPS_SCL 14 //GPS SCL pin (output option)
//#define PIN_GPS_TX 16 //GPS TX pin
Expand Down
8 changes: 8 additions & 0 deletions variants/rak4631/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ build_flags = ${nrf52_base.build_flags}
-I variants/rak4631
-D RAK_4631
-D RAK_BOARD
-D P_LORA_DIO_1=47
-D P_LORA_NSS=42
-D P_LORA_RESET=38
-D P_LORA_BUSY=46
-D P_LORA_SCLK=43
-D P_LORA_MISO=45
-D P_LORA_MOSI=44
-D SX126X_POWER_EN=37
-D PIN_BOARD_SCL=14
-D PIN_BOARD_SDA=13
-D PIN_GPS_TX=PIN_SERIAL1_RX
Expand Down