-
Notifications
You must be signed in to change notification settings - Fork 8.2k
battery: Initial dedicated API #46817
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
battery: Initial dedicated API #46817
Conversation
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can return the time to empty instead of using an out param, since this time should not be able to be negative.
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can return the time to full instead of using an out param, since this time should not be able to be negative.
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function needs a timeout param, otherwise the thread could block forever if there is an issue with the battery.
typedef int (*battery_wait_for_stable)(const struct device *dev, k_timeout timeout);
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this function simply calculates time to empty from the battery capacity, then this function should not be an API call, it should be an inline helper, and simply return the time in seconds, without the out param, since the capacity and the current rate can be read with the get_property API
static inline int (*battery_calculate_time_to_empty)(int capacity, int rate)
{
...
...
return time_remaining;
}
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please append _t to typedefs battery_cut_off _t
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sleep commands like this should be handled by the driver through the pm_device actions, like on/off/resume/suspend, preferably using the pm_device_runtime subsystem. Is there a specific reason to have it here as an API call which the pm action does not cover? It can also be part of the dts as a boolean property to enable/disable the sleep functionality pr driver instance, when pm_device action turns it off.
drivers/battery/Kconfig
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This source does not exist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? It's in this PR.
|
hi @aaronemassey will you be addressing comments and feedback raised in the original PR here? :) |
|
Just fyi, folks. I'm including tests and other work with this pull-request. I have this set as a draft for now for that reason. There's more to come! |
Absolutely. I just had to recreate the pull-request as I wanted folks to know I'm still actively working on this. |
f2287bd to
87a722d
Compare
68dc955 to
508ba31
Compare
|
Hello @aaronemassey, This is a pretty exciting addition to Zephyr. Some basic additions to the proposed framework to support charging ICs could be: Additionally charging ICs require changing settings at runtime, so a corresponding battery_set_property_t type would need to be added. Finally, it may be a good idea to support private properties like the sensor subsystem does with the ’SENSOR_CHAN_PRIV_START’ member, but this is a ’nice-to-have’ feature. Let me know if you have any thoughts or questions on this! |
508ba31 to
a2d2eeb
Compare
|
I think we ought to have a separate charger IC device driver now. It's better to keep the device drivers specific and modular within the context of a possible future power supply system. |
rgundi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to split the PR into multiple commits for easier understanding. For e.g.
a. API definition
b. Sample driver (sbs_gauge)
c. Tests/examples of usage
dts/bindings/battery/battery.yaml
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example is shown only for "sbs,sbs-gauge-new-api". No example for zephyr,battery. Please provide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed zephyr,battery to zephyr,fuel-gauge to be more specific in language. Currently it's just a stub that's included by the experimental sbs-gauge compatible. I think that's okay for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standard commands 0x0 to 0x4 are R/W. I don’t see a sbs_cmd_reg_write function in sbs_gauge.c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is focused on adding a fuel-gauge API while maintain feature parity with the sensor version of the sbs_gauge. But you're correct this was a mistake in the original sbs gauge driver. I'll add a todo here to add an sbs_gauge write.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also be part of an API extension in a future PR to allow some generic writing as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Standard Commands 0x25 to 0x2A are reserved. As per the Revision 1.1 of the spec, “Reserved Command Error - 0x0002” needs to be returned if there is an attempt to read/write to these codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I'll fix this.
I think it's best to remove these commands for now and keep this driver to sbs_gauge spec specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standard Command 0x24 is not defined at all in the Revision 1.1 of the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this too. Many of these errors were propagated from the origin sbs_gauge driver. I'll make a 2nd PR fixing that one. Thanks for pointing these out!
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to name it "battery_connect_state" and have only 2 values "connected" and "disconnected" ("not_disconnected" is double negative and should be avoided in general as human brain tends to get confused around double negatives). Since this is mostly a GPIO, not sure what "battery_disconnect_error" can be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The standard commands 0x00, 0x2F, 0x3C, 0x3D, 0x3E and 0x3F are manufacture-defined functions. Different batteries may use different commands for the same functionality. A way to assign commands to these functionalities needs to be provided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Adding in future PR, for now maintaining future parity with sensor sbs_gauge.
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest battery_property_type to have all the commands mentioned in the smart battery spec. That way a separate API would not be needed for time_at_rate, time_to_empty and time_to_full.
include/zephyr/drivers/battery.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include APIs to support below features. These implementations are manufacture specific.
a. Enable battery shipment mode
b. Enable EMShut mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some commands which are manufacture-defined and are not defined in the SBS (like Peak power, peak current etc). Different batteries may have different commands for the same functionality. A way to assign commands to these functionalities needs to be provided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. But consider this future PR work.
a2d2eeb to
05bac9e
Compare
05bac9e to
dfcba93
Compare
|
An unwanted opinion from an inexperienced nobody. In my opinion this API should take more inspiration from the Linux's power_supply subsystem. Also power_supply is not only used for implementing batteries, but also charges, adapters, etc. The Linux's API is mature, a lot of use cases and chargers/battery/coulomb counter ICs are implemented, even SBS. It is similar to Zephyr OS sensors API. It may need some rework, like event notifications, but I think it is still good enough. |
This really does seem like a great match, and honestly isn't that far away from what is being proposed here from all appearances. It's kind of a neat idea though, to make this more than a battery centric API but a power supply API with all the potential attributes that comes with. Thanks for pointing this out! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last thing I missed... the dts binding path is probably not quite right.
Also add this API to the doc/develop/api/overview.rst as experimental as of the next release.
Add initial battery fuel-gauge driver API with the most basic of native_posix driver tests. Signed-off-by: Aaron Massey <[email protected]>
Add a sample sbs gauge driver with feature parity and basic tests comparison to its sensor counter-part. Includes a simple stub test that is extended upon. Signed-off-by: Aaron Massey <[email protected]>
Add a test that validates fetching every property from the sbs gauge driver results in no driver error codes being returned. Signed-off-by: Aaron Massey <[email protected]>
421294b to
b2ac358
Compare
|
Don't forget "Also add this API to the doc/develop/api/overview.rst as experimental as of the next release." |
Where's a good location to add this? In |
2693f77 to
a748966
Compare
Added to the table in the file |
Add a short stub doc as a placeholder for future documentation on the fuel gauge API. Signed-off-by: Aaron Massey <[email protected]>
a748966 to
cc1ff1a
Compare
Oh. Somehow I opened this file up in read-only mode so I thought I wasn't to make changes against it. Thanks! |
|
Disclaimer: I am very inexperienced, so you should probably not waste your time with me! :) Last time, I didn't saw the RFC, but now I understand it. My biggest issue with that RFC (and that PR) is the rarity of separate charger IC and fuel gauge IC in the embedded world. Most boards that feature some kind of battery related IC have either fuel gauge IC, charger IC or an IC that combines them both. Example is MAX77818. Yes, I know, SBS is an example of separate charger and fuel gauge (SBS battery), but firstly rarely embedded device uses SBS, and secondly if you want to change charging parameters (at least in the case you can), (most of the time) you need to communicate the change to the SBS battery, and then the SBS battery will communicate the changes to the charger. As a last example, the very integrated AXP202 from X-Powers. |
Made #52443. Let's discuss this in that issue. I'd like to keep PR comments specific to the code in the PR. Especially because as a feature is experimental it may change drastically if we discover an architectural problem. |
Recreation of #44846 due to deletion of Fork. See #44846 (comment)
*Edit:
RFC: #44847