Power manager
SystemPowerFeature
SerialLogHandler logHandler;
void setup() {
// Apply a custom power configuration
SystemPowerConfiguration conf;
conf.feature(SystemPowerFeature::DISABLE_CHARGING);
int res = System.setPowerConfiguration(conf);
Log.info("setPowerConfiguration=%d", res);
// returns SYSTEM_ERROR_NONE (0) in case of success
// Settings are persisted, you normally wouldn't do this on every startup.
}
System power features are enabled or disabled using the SystemPowerConfiguration::feature() method. The settings are saved in non-volatile storage so you do not need to set them on every startup.
For full sample code sample code for logging power settings to USB serial debug, see the power supply guide.
System.getPowerConfiguration() and System.setPowerConfiguration() can be used in PRE_STARTUP() and STARTUP().
SystemPowerFeature::PMIC_DETECTION
For devices with an external PMIC and Fuel Gauge like the B-Series SoM, enables detection of the bq24195 PMIC connected by I2C to the primary I2C interface (Wire). Since this requires the use of I2C, you should not use pins D0 and D1 for GPIO when using PMIC_DETECTION.
This feature controls whether the system probes for and initializes the PMIC at boot; it corresponds to whether PMIC::begin() succeeds and PMIC::getVersion() returns a valid part revision.
SystemPowerFeature::USE_VIN_SETTINGS_WITH_USB_HOST
Normally, if a USB host is detected, the power limit settings will be determined by DPDM, the negotiation between the USB host and the PMIC to determine, for example, the maximum current available. If this feature is enabled, the VIN settings are used even when a USB host is detected. This is normally done if you are using USB for debugging but still have a power supply connected to VIN.
SystemPowerFeature::DISABLE_CHARGING
Since 3.0.0:
Disables LiPo battery charging. This may be useful if:
- You are manually controlling charging, for example based on an external temperature sensor
- You are using a non-rechargeable battery
- You are powering the device from a power supply connected to the Li+ pin instead of VIN
This is equivalent to calling PMIC::disableCharging() directly, except that it is persistent and is reapplied every time the Power Manager initializes, whereas a direct PMIC::disableCharging() call only lasts until the next time the Power Manager runs (for example, at the next reset).
SystemPowerFeature::DISABLE
Disables the system power management features. If you set this mode you must manually set the values in the PMIC directly.
Warning:
With this feature set, Device OS no longer applies any SystemPowerConfiguration settings or reconfigures the PMIC. All other Power Manager settings in this configuration (powerSourceMaxCurrent, batteryChargeCurrent, etc.) are ignored, and you are responsible for initializing and maintaining every PMIC setting yourself using the PMIC class, including resetting the I2C watchdog timer if it is enabled. This is rarely necessary; most use cases are better served by leaving the Power Manager enabled and only setting the specific features (like DISABLE_CHARGING) that you need.
// EXAMPLE
SerialLogHandler logHandler;
void setup() {
// Apply a custom power configuration
SystemPowerConfiguration conf;
conf.powerSourceMaxCurrent(900)
.powerSourceMinVoltage(4300)
.batteryChargeCurrent(850)
.batteryChargeVoltage(4210);
int res = System.setPowerConfiguration(conf);
Log.info("setPowerConfiguration=%d", res);
// returns SYSTEM_ERROR_NONE (0) in case of success
// Settings are persisted, you normally wouldn't do this on every startup.
}
void loop() {
{
PMIC power(true);
Log.info("Current PMIC settings:");
Log.info("VIN Vmin: %u", power.getInputVoltageLimit());
Log.info("VIN Imax: %u", power.getInputCurrentLimit());
Log.info("Ichg: %u", power.getChargeCurrentValue());
Log.info("Iterm: %u", power.getChargeVoltageValue());
int powerSource = System.powerSource();
int batteryState = System.batteryState();
float batterySoc = System.batteryCharge();
constexpr char const* batteryStates[] = {
"unknown", "not charging", "charging",
"charged", "discharging", "fault", "disconnected"
};
constexpr char const* powerSources[] = {
"unknown", "vin", "usb host", "usb adapter",
"usb otg", "battery"
};
Log.info("Power source: %s", powerSources[std::max(0, powerSource)]);
Log.info("Battery state: %s", batteryStates[std::max(0, batteryState)]);
Log.info("Battery charge: %f", batterySoc);
}
delay(2000);
}
To reset all settings to the default values:
// Reset power manager settings to default values
void setup() {
// To restore the default configuration
SystemPowerConfiguration conf;
System.setPowerConfiguration(conf);
}