RAK3112Board.h 2.6 KB

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