RAK3401Board.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include "RAK3401Board.h"
  4. #ifdef NRF52_POWER_MANAGEMENT
  5. // Static configuration for power management
  6. // Values set in variant.h defines
  7. const PowerMgtConfig power_config = {
  8. .lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
  9. .lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
  10. .voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
  11. };
  12. void RAK3401Board::initiateShutdown(uint8_t reason) {
  13. // Disable SKY66122 FEM (CSD+CPS LOW = shutdown, <1 uA)
  14. digitalWrite(SX126X_POWER_EN, LOW);
  15. // Disable 3V3 switched peripherals and 5V boost
  16. digitalWrite(PIN_3V3_EN, LOW);
  17. if (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
  18. reason == SHUTDOWN_REASON_BOOT_PROTECT) {
  19. configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
  20. }
  21. enterSystemOff(reason);
  22. }
  23. #endif
  24. void RAK3401Board::begin() {
  25. NRF52BoardDCDC::begin();
  26. pinMode(PIN_VBAT_READ, INPUT);
  27. #ifdef PIN_USER_BTN
  28. pinMode(PIN_USER_BTN, INPUT_PULLUP);
  29. #endif
  30. #ifdef PIN_USER_BTN_ANA
  31. pinMode(PIN_USER_BTN_ANA, INPUT_PULLUP);
  32. #endif
  33. #if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
  34. Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
  35. #endif
  36. Wire.begin();
  37. // PIN_3V3_EN (WB_IO2, P0.34) controls the 3V3_S switched peripheral rail
  38. // AND the 5V boost regulator (U5) on the RAK13302 that powers the SKY66122 PA.
  39. // Must stay HIGH during radio operation — do not toggle for power saving.
  40. pinMode(PIN_3V3_EN, OUTPUT);
  41. digitalWrite(PIN_3V3_EN, HIGH);
  42. // Enable SKY66122-11 FEM on the RAK13302 module.
  43. // CSD and CPS are tied together on the RAK13302 PCB, routed to IO3 (P0.21).
  44. // HIGH = FEM active (LNA for RX, PA path available for TX).
  45. // TX/RX switching (CTX) is handled by SX1262 DIO2 via SetDIO2AsRfSwitchCtrl.
  46. pinMode(SX126X_POWER_EN, OUTPUT);
  47. #ifdef NRF52_POWER_MANAGEMENT
  48. // Boot voltage protection check (may not return if voltage too low)
  49. // We need to call this after we configure SX126X_POWER_EN as output but before we pull high
  50. checkBootVoltage(&power_config);
  51. #endif
  52. digitalWrite(SX126X_POWER_EN, HIGH);
  53. delay(1); // SKY66122 turn-on settling time (tON = 3us typ)
  54. }