HeltecV3Board.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. public:
  22. RefCountedDigitalPin periph_power;
  23. HeltecV3Board() : periph_power(PIN_VEXT_EN) { }
  24. void begin() {
  25. ESP32Board::begin();
  26. // Auto-detect correct ADC_CTRL pin polarity (different for boards >3.2)
  27. pinMode(PIN_ADC_CTRL, INPUT);
  28. adc_active_state = !digitalRead(PIN_ADC_CTRL);
  29. pinMode(PIN_ADC_CTRL, OUTPUT);
  30. digitalWrite(PIN_ADC_CTRL, !adc_active_state); // Initially inactive
  31. periph_power.begin();
  32. esp_reset_reason_t reason = esp_reset_reason();
  33. if (reason == ESP_RST_DEEPSLEEP) {
  34. long wakeup_source = esp_sleep_get_ext1_wakeup_status();
  35. if (wakeup_source & (1 << P_LORA_DIO_1)) { // received a LoRa packet (while in deep sleep)
  36. startup_reason = BD_STARTUP_RX_PACKET;
  37. }
  38. rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
  39. rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
  40. }
  41. }
  42. void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) {
  43. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  44. // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
  45. rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
  46. rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
  47. rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
  48. if (pin_wake_btn < 0) {
  49. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
  50. } else {
  51. 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
  52. }
  53. if (secs > 0) {
  54. esp_sleep_enable_timer_wakeup(secs * 1000000);
  55. }
  56. // Finally set ESP32 into sleep
  57. esp_deep_sleep_start(); // CPU halts here and never returns!
  58. }
  59. void powerOff() override {
  60. enterDeepSleep(0);
  61. }
  62. uint16_t getBattMilliVolts() override {
  63. analogReadResolution(10);
  64. digitalWrite(PIN_ADC_CTRL, adc_active_state);
  65. uint32_t raw = 0;
  66. for (int i = 0; i < 8; i++) {
  67. raw += analogRead(PIN_VBAT_READ);
  68. }
  69. raw = raw / 8;
  70. digitalWrite(PIN_ADC_CTRL, !adc_active_state);
  71. return (ADC_MULTIPLIER * (3.3 / 1024.0) * raw) * 1000;
  72. }
  73. const char* getManufacturerName() const override {
  74. return "Heltec V3";
  75. }
  76. };