HeltecV3Board.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #pragma once
  2. #include <Arduino.h>
  3. #include <helpers/RefCountedDigitalPin.h>
  4. #include <helpers/ESP32Board.h>
  5. // built-ins
  6. #ifndef PIN_VBAT_READ // set in platformio.ini for boards like Heltec Wireless Paper (20)
  7. #define PIN_VBAT_READ 1
  8. #endif
  9. #ifndef PIN_ADC_CTRL // set in platformio.ini for Heltec Wireless Tracker (2)
  10. #define PIN_ADC_CTRL 37
  11. #endif
  12. #ifndef ADC_MULTIPLIER //default ADC multiplier
  13. #define ADC_MULTIPLIER 5.42
  14. #endif
  15. #define PIN_ADC_CTRL_ACTIVE LOW
  16. #define PIN_ADC_CTRL_INACTIVE HIGH
  17. #include <driver/rtc_io.h>
  18. class HeltecV3Board : public ESP32Board {
  19. private:
  20. bool adc_active_state;
  21. bool battery_reporting_enabled;
  22. public:
  23. RefCountedDigitalPin periph_power;
  24. HeltecV3Board() : adc_active_state(false),
  25. battery_reporting_enabled(true),
  26. periph_power(PIN_VEXT_EN) { }
  27. void begin() {
  28. ESP32Board::begin();
  29. // Auto-detect correct ADC_CTRL pin polarity (different for boards >3.2)
  30. pinMode(PIN_ADC_CTRL, INPUT);
  31. adc_active_state = !digitalRead(PIN_ADC_CTRL);
  32. pinMode(PIN_ADC_CTRL, OUTPUT);
  33. digitalWrite(PIN_ADC_CTRL, !adc_active_state); // Initially inactive
  34. periph_power.begin();
  35. esp_reset_reason_t reason = esp_reset_reason();
  36. if (reason == ESP_RST_DEEPSLEEP) {
  37. long wakeup_source = esp_sleep_get_ext1_wakeup_status();
  38. if (wakeup_source & (1 << P_LORA_DIO_1)) { // received a LoRa packet (while in deep sleep)
  39. startup_reason = BD_STARTUP_RX_PACKET;
  40. }
  41. rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
  42. rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
  43. }
  44. }
  45. void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) {
  46. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  47. // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
  48. rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
  49. rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
  50. rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
  51. if (pin_wake_btn < 0) {
  52. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
  53. } else {
  54. 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
  55. }
  56. if (secs > 0) {
  57. esp_sleep_enable_timer_wakeup(secs * 1000000);
  58. }
  59. // Finally set ESP32 into sleep
  60. esp_deep_sleep_start(); // CPU halts here and never returns!
  61. }
  62. void powerOff() override {
  63. enterDeepSleep(0);
  64. }
  65. uint16_t getBattMilliVolts() override {
  66. if (!battery_reporting_enabled) {
  67. return 0;
  68. }
  69. analogReadResolution(10);
  70. digitalWrite(PIN_ADC_CTRL, adc_active_state);
  71. uint32_t raw = 0;
  72. for (int i = 0; i < 8; i++) {
  73. raw += analogRead(PIN_VBAT_READ);
  74. }
  75. raw = raw / 8;
  76. digitalWrite(PIN_ADC_CTRL, !adc_active_state);
  77. uint16_t mv = (ADC_MULTIPLIER * (3.3 / 1024.0) * raw) * 1000;
  78. // Heltec V3 USB-powered boards without a battery can bias or float the
  79. // VBAT sense rail, which shows up as an impossible LiPo voltage.
  80. if (mv > 4400) {
  81. return 0;
  82. }
  83. return mv;
  84. }
  85. bool supportsBatteryReporting() const override {
  86. return true;
  87. }
  88. bool setBatteryReporting(bool enabled) override {
  89. battery_reporting_enabled = enabled;
  90. return true;
  91. }
  92. bool isBatteryReportingEnabled() const override {
  93. return battery_reporting_enabled;
  94. }
  95. const char* getManufacturerName() const override {
  96. return "Heltec V3";
  97. }
  98. };