DIYESP32S3N16R8E22Back2backBoard.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. #include <Arduino.h>
  3. #include <helpers/ESP32Board.h>
  4. #include <driver/rtc_io.h>
  5. class DIYESP32S3N16R8E22Back2backBoard : public ESP32Board {
  6. public:
  7. void begin() {
  8. ESP32Board::begin();
  9. esp_reset_reason_t reason = esp_reset_reason();
  10. if (reason == ESP_RST_DEEPSLEEP) {
  11. long wakeup_source = esp_sleep_get_ext1_wakeup_status();
  12. if (wakeup_source & (1 << P_LORA_DIO_1)) { // received a LoRa packet (while in deep sleep)
  13. startup_reason = BD_STARTUP_RX_PACKET;
  14. }
  15. rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
  16. rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
  17. }
  18. }
  19. void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) {
  20. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  21. // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
  22. rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
  23. rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
  24. rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
  25. if (pin_wake_btn < 0) {
  26. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
  27. } else {
  28. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn
  29. }
  30. if (secs > 0) {
  31. esp_sleep_enable_timer_wakeup(secs * 1000000);
  32. }
  33. // Finally set ESP32 into sleep
  34. esp_deep_sleep_start(); // CPU halts here and never returns!
  35. }
  36. uint16_t getBattMilliVolts() override {
  37. return 0;
  38. }
  39. const char* getManufacturerName() const override {
  40. return "DIY ESP32S3 N16R8 E22 Back2back";
  41. }
  42. };