Skip to content

drivers: Add fuel gauge max17043 #93021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mabembedded
Copy link
Contributor

Add support for the fuel gauge max17043. This device is similar to the max17048, and so it was used as the basis. There are slight differences in the registers, which are accounted for in the driver. Functionality was tested on a Nordic Thingy91x.

Add support for fuel gauge max17043. This device is similar to the max17048
and so it was used as the basis. There are slight differences in the
registers which are accounted for in the driver. Functionality was
tested using a Thingy 91x.

Signed-off-by: Mohammed Billoo <mab@mab-labs.com>
Copy link

Copy link
Contributor

@msalau msalau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please add a node to tests/drivers/build_all/fuel_gauge/i2c.dtsi to build the driver during CI.

Comment on lines +113 to +118
int rc = max17043_read_register(dev, REGISTER_VERSION, &version);

if (!device_is_ready(cfg->i2c.bus)) {
LOG_ERR("Bus device is not ready");
return -ENODEV;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operations seem to be out of order. The bus should be checked before using it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also change this in drivers/fuel_gauge/max17048.
I never really looked closely into the max17048 driver which @mabembedded took as a template for the max17043, but to see all this minor issues which are the same in the max17048 I am thinking about "refactoring" the max17048 driver and extend it with some missing functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - should I create another PR for fixes to the max17048 after addressing issues in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mabembedded I think you can do it both ways. If you decide to do it in the same PR, split them up in 2 commits one for the max17043 and one for the changes in max17048.

Comment on lines +140 to +155
case FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE:
rc = max17043_percent(dev, &data->charge);

if (rc < 0) {
return rc;
}

val->relative_state_of_charge = data->charge;
break;
case FUEL_GAUGE_VOLTAGE:
rc = max17043_voltage(dev, &data->voltage);
if (rc < 0) {
return rc;
}
val->voltage = data->voltage;
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data->charge and data->voltage are set but never actually used.
The function uses them as local variables.
Did I miss something?

* obtain µV
*/

*response = (uint32_t)raw_voltage * 78.125;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to not use floating point unless CONFIG_FPU=y?

E.g. the following change should give the same result but without floating point calculations.

-	*response = (uint32_t)raw_voltage * 78.125;
+	*response = ((uint32_t)raw_voltage * 78125) / 1000;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this in drivers/fuel_gauge/max17048 too

return rc;
}

static const struct fuel_gauge_driver_api max17043_driver_api = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-static const struct fuel_gauge_driver_api max17043_driver_api = {
+static DEVICE_API(fuel_gauge, max17043_driver_api) = {

Comment on lines +17 to +19
#include <zephyr/pm/device.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/__assert.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that zephyr/pm/device.h and zephyr/sys/__assert.h are not used by the driver.

Copy link
Contributor

@DBS06 DBS06 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thanks for your contribution!
I only did a quick overlook, most likely I will do a deeper review next week.
So far I noticed that the test and the max17043-emulation is missing, which would be nice to add, see tests/drivers/fuel_gauge and drivers/fuel_gauge/max17048/emul_max17048.c.

#define RESET_COMMAND 0x5400
#define QUICKSTART_MODE 0x4000

struct max17043_config {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The max17043_config struct can be moved inside the max17043.c, because I don't see a need to have it in the header.

/**
* Storage for the fuel gauge basic information
*/
struct max17043_data {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @msalau already mentioned here the max17043_data struct can be removed, because it is only used in max17043_get_single_prop() function as a local variable, but besides that it will not be used anywhere.


#define REGISTER_VCELL 0x02
#define REGISTER_SOC 0x04
#define REGISTER_MODE 0x06
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense from your perspective to implement/add this and the following registers?

@ttmut
Copy link
Contributor

ttmut commented Jul 21, 2025

Add support for the fuel gauge max17043. This device is similar to the max17048, and so it was used as the basis. There are slight differences in the registers, which are accounted for in the driver. Functionality was tested on a Nordic Thingy91x.

Can the existing driver be updated to support both models?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants