I am trying to convince the M404 to play nicely with a BQ24195 PMIC and MAX17048 fuel gauge and am not having any luck. I am initializing with the correct settings I believe to get it to recognize that it is wired and connected to the I2C on D0 and D1 as expected. I am not getting errors, but am getting empty and/or incorrect (I believe) values.
The firmware is below:
// Include Particle Device OS APIs
#include "Particle.h"
system_tick_t lastSample = 0;
std::chrono::milliseconds sampleInterval = 5s;
// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);
// Run the application and system concurrently in separate threads
SYSTEM_THREAD(ENABLED);
// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO, {
{"app.rtc", LOG_LEVEL_TRACE},
});
enum DeviceState {
STARTUP,
IDLE,
TEST,
};
DeviceState nextState = STARTUP;
// setup() runs once, when the device is first turned on
void setup() {
Cellular.prefer();
// Configure the PMIC
// See: https://docs.particle.io/reference/device-os/firmware/#power-manager
SystemPowerConfiguration conf;
conf.batteryChargeVoltage(4210)
.batteryChargeCurrent(850)
.powerSourceMaxCurrent(2000)
.feature(SystemPowerFeature::PMIC_DETECTION)
.feature(SystemPowerFeature::USE_VIN_SETTINGS_WITH_USB_HOST);
int res = System.setPowerConfiguration(conf);
Log.info("setPowerConfiguration=%d", res);
// Put initialization like pinMode and begin functions here
Serial.begin(9600);
}
// loop() runs over and over again, as quickly as it can execute.
void loop() {
if (millis() - lastSample >= sampleInterval.count()) {
lastSample = millis();
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);
FuelGauge fuel;
Log.info( "voltage=%.2f", fuel.getVCell() );
Log.info( "SoC=%.2f", fuel.getSoC() );
}
}
Example output is below:
0000230000 [app] INFO: Current PMIC settings:
0000230012 [app] INFO: VIN Vmin: 3880
0000230022 [app] INFO: VIN Imax: 1500
0000230032 [app] INFO: Ichg: 832
0000230041 [app] INFO: Iterm: 4208
0000230052 [app] INFO: Power source: unknown
0000230063 [app] INFO: Battery state: not charging
0000230075 [app] INFO: Battery charge: 0.046875
0000230087 [app] INFO: voltage=3.32
0000230098 [app] INFO: SoC=0.04
When I measure the LiPo voltage with a multimeter, I get 3.67V. I can add the schematic if helpful - otherwise, it is being powered by USB, VIN, and BAT. Any help or pointers on where to look would be appreciated!
An aside: it looks like the "tags" need to be updated to include more recent hardware!