R1NeoBoard.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include "R1NeoBoard.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 R1NeoBoard::initiateShutdown(uint8_t reason) {
  13. // Disable LoRa module power before shutdown
  14. digitalWrite(SX126X_POWER_EN, LOW);
  15. // Signal IO controller that MCU is off, then release DCDC latch
  16. digitalWrite(PIN_SOFT_SHUTDOWN, LOW);
  17. digitalWrite(PIN_DCDC_EN_MCU_HOLD, LOW);
  18. if (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
  19. reason == SHUTDOWN_REASON_BOOT_PROTECT) {
  20. configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
  21. }
  22. enterSystemOff(reason);
  23. }
  24. #endif // NRF52_POWER_MANAGEMENT
  25. void R1NeoBoard::begin() {
  26. // R1 Neo peculiarity: tell DCDC converter to stay powered.
  27. // Must be done as soon as practical during boot.
  28. pinMode(PIN_DCDC_EN_MCU_HOLD, OUTPUT);
  29. digitalWrite(PIN_DCDC_EN_MCU_HOLD, HIGH);
  30. // R1 Neo peculiarity: Tell I/O Controller device is on
  31. // Enables passthrough of buttons and LEDs
  32. pinMode(PIN_SOFT_SHUTDOWN, OUTPUT);
  33. digitalWrite(PIN_SOFT_SHUTDOWN, HIGH);
  34. NRF52BoardDCDC::begin();
  35. // button is active high and passed through from I/O controller
  36. pinMode(PIN_USER_BTN, INPUT);
  37. pinMode(PIN_BUZZER, OUTPUT);
  38. digitalWrite(PIN_BUZZER, LOW);
  39. // battery pins
  40. pinMode(PIN_BAT_CHG, INPUT);
  41. pinMode(PIN_VBAT_READ, INPUT);
  42. Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL);
  43. Wire.begin();
  44. pinMode(SX126X_POWER_EN, OUTPUT);
  45. #ifdef NRF52_POWER_MANAGEMENT
  46. // Boot voltage protection check (may not return if voltage too low)
  47. // We need to call this after we configure SX126X_POWER_EN as output but before we pull high
  48. checkBootVoltage(&power_config);
  49. #endif
  50. digitalWrite(SX126X_POWER_EN, HIGH);
  51. delay(10); // give sx1262 some time to power up
  52. }