Skip to content

drivers/ltc4150: (Re-)implemented driver for the LTC4150 coulomb counter#10755

Merged
miri64 merged 11 commits intoRIOT-OS:masterfrom
maribu:ltc4150-new
Jan 28, 2019
Merged

drivers/ltc4150: (Re-)implemented driver for the LTC4150 coulomb counter#10755
miri64 merged 11 commits intoRIOT-OS:masterfrom
maribu:ltc4150-new

Conversation

@maribu
Copy link
Member

@maribu maribu commented Jan 11, 2019

Contribution description

This PR implements a driver for the LTC4150 coulomb counter. A driver for that device was previously part for RIOT, but was dropped because of quality shortcomings. This driver is written from scratch.

Testing procedure

The LTC4150 is integrated in the MSB-A2 board, thus using that board will be most convenient to use for testing. However, this chip is also cheaply available as breakout board from various vendors, which all seem to be based on the same open hardware PCB.

Testing:

  1. Build, flash & run tests/driver_ltc4150 on the MSB-A2. I intended the program to be self-explaining - so I will not provide more information here to see if I indeed reached that goal
  2. Build, flash & run example/default. Try to read the data using the saul shell command

Issues/PRs references

This PR competes with PR #9653 also opened by me. I personally favor this PR strongly over the old approach.

@maribu maribu added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: drivers Area: Device drivers CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jan 11, 2019
@maribu
Copy link
Member Author

maribu commented Jan 11, 2019

Before you start to review: Let me fix the missing doc Travis complained about and squash

@maribu
Copy link
Member Author

maribu commented Jan 11, 2019

I'm aware that commit "tests/driver_ltc4150: Workarround for msp430" could be squashed into commit "tests: Added test for ltc4150 driver". But I would actually prefer to keep them separated: That commit did not fix a bug in the test, but was a workarround for a bug in the msp430 toolchain (the missing fputs). Once the c lib of the msp430 toolchain gains support for fputs, it can be safely reverted to make the test source code shorter and more readable. For that reason I would like to keep that commit separate.

@maribu
Copy link
Member Author

maribu commented Jan 11, 2019

@MichelRottleuthner: Could you kindly do the testing? I think @smlng does not have a MSB-A2 board at hand (but I'm not sure).

@miri64
Copy link
Member

miri64 commented Jan 12, 2019

I believe @MichelRottleuthner doesn't have one either, but I have 8 ;-). Once the fundamentals and and code design are cleared (e.g. by @MichelRottleuthner or @smlng) I'm happy to test this PR.

return -EINVAL;
}

memset(dev, 0x00, sizeof(ltc4150_dev_t));
Copy link
Member

Choose a reason for hiding this comment

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

this shouldn't be necessary and also avoidable, see discussion in #10751 or #10728

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you elaborate on that?

#10751 is about a potential security issue when sensitive data should be cleared but the compiler optimized out zeroing out the data. In this case it would be no problem at all if the compiler detects that e.g. dev->params is overwrite one line later anyway and skips zeroing out that data. And #10728 is about the use of memcpy, not about memset().

I would say this is a textbook example for when to use memset(): When you want to bring a structure into a defined state before using it. One could also using something like:

dev->params = *params;
dev->start_sec = 0;
dev->charged = 0;
dev->discharged = 0;

But what happens when the structure is extended by one member? Using memset() would still work fine to initialize the whole structure, but the above could would only initialize parts of the struct.

Copy link
Member

Choose a reason for hiding this comment

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

yep, true that ... leave as is.

However, please be consistent and style/usage, i.e. here you write memset(..., 0x00 ...) and below memset(..., 0, ...) ... I think the latter is nicer.

Copy link
Member Author

Choose a reason for hiding this comment

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

However, please be consistent and style/usage, i.e. here you write memset(..., 0x00 ...) and below memset(..., 0, ...) ... I think the latter is nicer.

+1

#define LTC4150_PARAM_SHUTDOWN (GPIO_PIN(0, 5))
#endif
#ifndef LTC4150_PARAM_PULSES
#define LTC4150_PARAM_PULSES (45700)
Copy link
Member

Choose a reason for hiding this comment

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

didn't we agree on adding (at least) a U suffix in the previous PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, sorry

ltc4150_dev_t *dev = _dev;

if (
(dev->params.polarity == GPIO_UNDEF) ||
Copy link
Member

Choose a reason for hiding this comment

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

personally I would not put this on a new line, as the indention stays the same, hence I think its fine to write this as:

if ((dev->params.polarity == GPIO_UNDEF) ||
    (!gpio_read(dev->params.polarity))) {

@smlng smlng self-assigned this Jan 16, 2019
@miri64
Copy link
Member

miri64 commented Jan 16, 2019

@maribu just a remark: if you use the commit summary git commit --fixup=<commit> provides you with, squashing becomes easier due to git rebase -i <base> --autosquash ;-).

Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

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

Tests work as expected, though I was confused due to text speaking about drawn charges, while the "Charging" column was raising constantly.

@maribu
Copy link
Member Author

maribu commented Jan 16, 2019

Tests work as expected, though I was confused due to text speaking about drawn charges, while the "Charging" column was raising constantly.

Ohh! Let me check

@maribu
Copy link
Member Author

maribu commented Jan 16, 2019

@miri64: Fixed :-) I put the values in the wrong columns

@maribu maribu removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 16, 2019
@miri64
Copy link
Member

miri64 commented Jan 16, 2019

OK, this makes more sense, but my confusion stemmed more from the fact, that I connect "more power" with "more charge drawn", but that obviously isn't what the text meant (especially since it stresses "The expected result is that the required current increases with the system load". However, just from the stable I'm still not able to see exactly when the current is supposed to raise. Maybe add another

+---------------+---------------+---------+-------------+-----------+

Whenever the corresponding minute has passed?

@miri64
Copy link
Member

miri64 commented Jan 16, 2019

(I intentionally didn't look at the test's code yet)

@maribu
Copy link
Member Author

maribu commented Jan 16, 2019

@miri64: I added the line between load levels and tried to make the description clearer.

@miri64
Copy link
Member

miri64 commented Jan 16, 2019

Ok, now I can see the raising current clearer. Maybe add a hint, that it is the right column the tester is supposed to look at. Also just from the description it wasn't clear to me, that the test cycles and not just ends after the 3rd level.

@maribu
Copy link
Member Author

maribu commented Jan 16, 2019

@miri64: Addressed :-)

@maribu
Copy link
Member Author

maribu commented Jan 18, 2019

May I squash?

Do we all agree that this PR is better than the competing PR at #9653? If so, I would close #9653

@MichelRottleuthner
Copy link
Contributor

I prefer this PR over the old one. Regarding the squash @miri64 and @smlng should decide because they requested the changes. From my side it's good to go!

@miri64
Copy link
Member

miri64 commented Jan 18, 2019

I hadn't looked at the code as much, just tested and my comments were addressed. So I'm fine with squashing as well.

@miri64
Copy link
Member

miri64 commented Jan 18, 2019

(you should wait for @smlng's go though)

@maribu
Copy link
Member Author

maribu commented Jan 28, 2019

@smlng: May I squash?

@smlng
Copy link
Member

smlng commented Jan 28, 2019

sure, please squash

maribu added 11 commits January 28, 2019 13:33
The shell handlers of the old, depreciated and removed LTC4150 driver are still
in place. This commit removes them
The LTC4150 is a coulomb counter (a.k.a. battery sensor or bidirectional
current sensor) that is used in the MSBA2 board and available for little money
as easy to use break out board.
Implemented an example `ltc4150_recorder_t` implementation as a proof of concept
for the recorder API.
The msp430 toolchain is missing an `fputs()` implementation. This commit makes
them use the `printf("%s", str);` instead of `fputs(str, stdout);`, which is
semantically equivalent (but has more overhead).
@maribu maribu added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 28, 2019
@maribu
Copy link
Member Author

maribu commented Jan 28, 2019

Hmm, the "force-pushed" link is not clever enough to filter out changes due to rebasing against current master. I still have a back up here https://github.com/maribu/RIOT/tree/ltc4150-new-prior-squash for reference, which is neither squashed nor rebased (in other words: Contains the state prior force push).

@miri64
Copy link
Member

miri64 commented Jan 28, 2019

Hmm, the "force-pushed" link is not clever enough to filter out changes due to rebasing against current master.

That's why I usually just squash (i.e. make a rebase against the merge-base with master) if it is not absolutely necessary [to rebase]. It's not about cleverness. The link is just an HTML-ized version of git diff <pre>..<post>.

@maribu
Copy link
Member Author

maribu commented Jan 28, 2019

The link is just an HTML-ized version of git diff <pre>..<post>.

If merging of the old state into upstream can be done automatically, this approach would need to be only slightly adapted to be more clever: Use git diff <pre merged into upstream> .. <post> if the force push did a rebase.

@miri64
Copy link
Member

miri64 commented Jan 28, 2019

Welp, that's for @github to resolve ;-)

@miri64
Copy link
Member

miri64 commented Jan 28, 2019

Re-run the tests. Still working.

@miri64 miri64 merged commit caa1d0b into RIOT-OS:master Jan 28, 2019
@maribu maribu deleted the ltc4150-new branch January 28, 2019 15:01
@maribu
Copy link
Member Author

maribu commented Jan 28, 2019

@miri64, @MichelRottleuthner, @smlng: Thank you very much for your valuable feedback :-)

@danpetry danpetry added this to the Release 2019.04 milestone Mar 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants