RAK3112Board.h 2.6 KB

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