XiaoNrf52Board.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifdef XIAO_NRF52
  2. #include <Arduino.h>
  3. #include <Wire.h>
  4. #include "XiaoNrf52Board.h"
  5. #ifdef NRF52_POWER_MANAGEMENT
  6. // Static configuration for power management
  7. // Values set in variant.h defines
  8. const PowerMgtConfig power_config = {
  9. .lpcomp_ain_channel = PWRMGT_LPCOMP_AIN,
  10. .lpcomp_refsel = PWRMGT_LPCOMP_REFSEL,
  11. .voltage_bootlock = PWRMGT_VOLTAGE_BOOTLOCK
  12. };
  13. void XiaoNrf52Board::initiateShutdown(uint8_t reason) {
  14. bool enable_lpcomp = (reason == SHUTDOWN_REASON_LOW_VOLTAGE ||
  15. reason == SHUTDOWN_REASON_BOOT_PROTECT);
  16. pinMode(VBAT_ENABLE, OUTPUT);
  17. digitalWrite(VBAT_ENABLE, enable_lpcomp ? LOW : HIGH);
  18. if (enable_lpcomp) {
  19. configureVoltageWake(power_config.lpcomp_ain_channel, power_config.lpcomp_refsel);
  20. }
  21. enterSystemOff(reason);
  22. }
  23. #endif // NRF52_POWER_MANAGEMENT
  24. void XiaoNrf52Board::begin() {
  25. NRF52BoardDCDC::begin();
  26. // Configure battery voltage ADC
  27. pinMode(PIN_VBAT, INPUT);
  28. pinMode(VBAT_ENABLE, OUTPUT);
  29. digitalWrite(VBAT_ENABLE, LOW); // Enable VBAT divider for reading
  30. analogReadResolution(12);
  31. analogReference(AR_INTERNAL_3_0);
  32. delay(50); // Allow ADC to settle
  33. #ifdef PIN_USER_BTN
  34. pinMode(PIN_USER_BTN, INPUT_PULLUP);
  35. #endif
  36. #if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL)
  37. Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL);
  38. #endif
  39. Wire.begin();
  40. #ifdef P_LORA_TX_LED
  41. pinMode(P_LORA_TX_LED, OUTPUT);
  42. digitalWrite(P_LORA_TX_LED, HIGH);
  43. #endif
  44. #ifdef NRF52_POWER_MANAGEMENT
  45. // Boot voltage protection check (may not return if voltage too low)
  46. checkBootVoltage(&power_config);
  47. #endif
  48. delay(10); // Give sx1262 some time to power up
  49. }
  50. uint16_t XiaoNrf52Board::getBattMilliVolts() {
  51. // https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging
  52. // VBAT_ENABLE must be LOW to read battery voltage
  53. digitalWrite(VBAT_ENABLE, LOW);
  54. int adcvalue = analogRead(PIN_VBAT);
  55. return (adcvalue * ADC_MULTIPLIER * AREF_VOLTAGE) / 4.096;
  56. }
  57. #endif