XiaoRP2040Board.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include <Arduino.h>
  3. #include <MeshCore.h>
  4. /*
  5. * This board has no built-in way to read battery voltage.
  6. * Nevertheless it's very easy to make it work, you only require two 1% resistors.
  7. * If your using the WIO SX1262 Addon for xaio, make sure you dont connect D0!
  8. *
  9. * BAT+ -----+
  10. * |
  11. * VSYS --+ -/\/\/\/\- --+
  12. * 200k |
  13. * +-- D0
  14. * |
  15. * GND --+ -/\/\/\/\- --+
  16. * | 100k
  17. * BAT- -----+
  18. */
  19. #define PIN_VBAT_READ 26 // D0
  20. #define BATTERY_SAMPLES 8
  21. #define ADC_MULTIPLIER (3.0f * 3.3f * 1000)
  22. class XiaoRP2040Board : public mesh::MainBoard {
  23. protected:
  24. uint8_t startup_reason;
  25. public:
  26. void begin();
  27. uint8_t getStartupReason() const override { return startup_reason; }
  28. #ifdef P_LORA_TX_LED
  29. void onBeforeTransmit() override { digitalWrite(P_LORA_TX_LED, HIGH); }
  30. void onAfterTransmit() override { digitalWrite(P_LORA_TX_LED, LOW); }
  31. #endif
  32. uint16_t getBattMilliVolts() override {
  33. #if defined(PIN_VBAT_READ) && defined(ADC_MULTIPLIER)
  34. analogReadResolution(12);
  35. uint32_t raw = 0;
  36. for (int i = 0; i < BATTERY_SAMPLES; i++) {
  37. raw += analogRead(PIN_VBAT_READ);
  38. }
  39. raw = raw / BATTERY_SAMPLES;
  40. return (ADC_MULTIPLIER * raw) / 4096;
  41. #else
  42. return 0;
  43. #endif
  44. }
  45. const char *getManufacturerName() const override { return "Xiao RP2040"; }
  46. void reboot() override { rp2040.reboot(); }
  47. bool startOTAUpdate(const char *id, char reply[]) override;
  48. };