I have been looking at adding a BSOM option for my counters for European and African customers. I plan to build my own carrier board but am starting with the BSOM Evaluation Board. I have not made any changes to the jumpers other than removing the four associated with Ethernet.
However, I cannot get an accurate state of charge or battery state using the System commands I typically use with the Boron. The documentation seems to imply that the BSOM will enable the bq24195 PMIC and MAX17043 fuel gauge by default. I can get battery voltage using the PMIC API call but what am I missing on using the System commands as I would like to maintain code compatibility with the Boron based devices.
I looked at this post for inspiration but my results are not the same when I run @tunahammer’s code.
Here is my simplified sketch
#include "Particle.h"
SYSTEM_MODE(MANUAL); // This will enable user code to start executing automatically.
SYSTEM_THREAD(ENABLED); // Means my code will not be held up by Particle processes.
FuelGauge fuel;
SerialLogHandler LogHandler(LOG_LEVEL_INFO);
void setup() {
fuel.begin();
waitFor(Serial.isConnected, 10000); // Wait for serial connection
delay(1000);
Log.info("Finished setup with state of charge %4.2f battery at %4.2fV", System.batteryCharge(), fuel.getVCell());
}
void loop() {
}
Output
0000002576 [app] INFO: Finished setup with state of charge -1.00 battery at 3.74V
Thank you for taking a look. Your approach makes sense since the BSOM can not always count on having a FuelGauge.
I updated the code to declare conf and then ran this in setup()
void setup() {
fuel.begin();
waitFor(Serial.isConnected, 10000); // Wait for serial connection
delay(1000);
conf.feature(SystemPowerFeature::PMIC_DETECTION);
int res = System.setPowerConfiguration(conf);
Log.info("setPowerConfiguration=%d", res);
Log.info("Finished setup with state of charge %4.2f batter at %4.2fV", System.batteryCharge(), fuel.getVCell());
}
But, this did not fix the issue:
0000002837 [app] INFO: setPowerConfiguration=0
0000002839 [app] INFO: Finished setup with state of charge -1.00 batter at 3.88V
This is a new evaluation board “M.2 SOM EVAL BOARD v1.1” and is is slightly different in appearance from the one in the docs:
The jumpers for the PM_INT, PM_SDA, PM_SCL are all closed.
The SoC and power source may not be available immediately after the PMIC is initialized. At least for testing, I’d try looping for a while and see if the result becomes available eventually. Maybe try every 5 seconds for a minute just to see if it’s a time-related issue or something else. You can do this in loop().
Thank you! Waiting a little bit was the solution. I wrote this sketch to see just how long - it very consistently comes back with the same answer 1.02 seconds.
This code works:
#include "Particle.h"
SYSTEM_MODE(MANUAL); // This will enable user code to start executing automatically.
SYSTEM_THREAD(ENABLED); // Means my code will not be held up by Particle processes.
FuelGauge fuel;
SystemPowerConfiguration conf;
SerialLogHandler LogHandler(LOG_LEVEL_INFO);
unsigned long clockStart = 0;
void setup() {
waitFor(Serial.isConnected, 10000); // Wait for serial connection
delay(1000);
fuel.begin();
clockStart = millis();
conf.feature(SystemPowerFeature::PMIC_DETECTION);
int res = System.setPowerConfiguration(conf);
Log.info("setPowerConfiguration %s", (res == 0) ? "was successful":"has failed");
}
void loop() {
float stateOfCharge = System.batteryCharge();
if (stateOfCharge == -1) delay(100);
else {
Log.info("State of charge %4.2f%% battery at %4.2fV after %4.2fseconds", System.batteryCharge(), fuel.getVCell(), (millis()-clockStart)/1000.0);
while(1);
}
}
Output
0000002536 [app] INFO: setPowerConfiguration was successful
0000003543 [app] INFO: State of charge 90.39% battery at 4.09V after 1.02seconds