tests/periph_i2c: Update to new I2C API#9168
Conversation
dylad
left a comment
There was a problem hiding this comment.
Nice job ! just a few mistakes.
tests/periph_i2c/main.c
Outdated
|
|
||
| if (speed < 0 || speed > 4) { | ||
| puts("Error: Speed value not in range (0 - 4)"); | ||
| res = i2c_init(dev); |
There was a problem hiding this comment.
i2c_init must not be call there. It only belongs to drivers/periph_common/init.c
tests/periph_i2c/main.c
Outdated
| puts("Error: Init: Given device not available"); | ||
| return 1; | ||
| } | ||
| else if (res == 0){ |
|
I will wait for all changes to the api before fixing things |
|
Let me (or someone else ) test it first and give an ACK. |
|
@dylad So it is a bit heavy but should support most all API and have a few nice features for later. Eventually I will do a minimal shell (binary based) but for now this should do. Once everything is up to date and merged it would be nice to make buildtest and see if memory issues occur on any board (right now they all fail because the API isn't fully ported). Here is it in working with the samr21 (ie, the API is working). |
tests/periph_i2c/main.c
Outdated
| return 1; | ||
| } | ||
|
|
||
| int cmd_i2c_acquire(int argc, char **argv) { |
There was a problem hiding this comment.
I don't think we should use this here. acquire and release functions are supposed to be handled by the driver not the user. In the current case, if the user use cmd_i2c_acquire, it will block the I2C.
There was a problem hiding this comment.
Yup I know, I think if it is in the API it should be tested (the fact that it blocks shouldn't be a problem for the test since I should be able to expect timeout and provide resets). If you don't wish to have this tested in the automated test we can remove it but I think since it is exposed to the user it should be tests. Also some clarification on it's expected behavior would be nice (ie is it required to acquire before use? Can we release twice without getting an error?)
There was a problem hiding this comment.
You're right I mess up things sorry. acquire doesn't belong to CPU driver but device driver so we must be able to test the acquire/release function.
You can leave it as it.
| printf("Error: ETIMEDOUT [%d]\n", -res); | ||
| return 1; | ||
| } | ||
| else if (res == I2C_ACK) { |
There was a problem hiding this comment.
From the code below, this will never be used but is handled in the function with specific success output.
Meaning: this could be removed, or not?
There was a problem hiding this comment.
I'd like to leave it in, just in case somebody does something in the future it won't say unknown error since nothing specifies that one must handle the ACK first.
|
|
||
| int print_i2c_error(int res) { | ||
| if (res == -EOPNOTSUPP) { | ||
| printf("Error: EOPNOTSUPP [%d]\n", -res); |
There was a problem hiding this comment.
here and below: does it make sense to print the number, too? IMHO simply using puts("Error: EOPNOTSUPP!"); should be fine and enough.
There was a problem hiding this comment.
The number is more there if the python (or any other) interface wants to use it, since it is a standard errno it may be easy just to feed it into a standard decoding function and get more info (I also don't think it adds too much bloat).
tests/periph_i2c/main.c
Outdated
| int dev, speed, res; | ||
| int check_dev(void) { | ||
| if (i2c_dev < 0) { | ||
| puts("Error: no I2C device was initialized"); |
There was a problem hiding this comment.
there is ENODEV which you could use here and print similarly to the other errors? See print_i2c_error, too
There was a problem hiding this comment.
Yup, nice catch, will do! [TODO]
|
|
||
| dev = atoi(argv[1]); | ||
| speed = atoi(argv[2]); | ||
| if (dev < 0 || dev >= (int)I2C_NUMOF) { |
There was a problem hiding this comment.
minor nit, but this can be simplified to:
if (dev < 0 || dev >= (int)I2C_NUMOF) {
puts("Error: Given device not available");
i2c_dev = -1;
return 1;
}
printf("Success: i2c_%i successfully assigned as master\n", dev);
i2c_dev = dev;
return 0;
omitting the else branch because of the return in the if part and also the final return 1 would've never been reached, too.
There was a problem hiding this comment.
Yup, will do. [TODO]
tests/periph_i2c/main.c
Outdated
| return 1; | ||
| } | ||
|
|
||
| if (argc != 1) { |
There was a problem hiding this comment.
why bother? You don't expect nor use arguments, so (IMHO) no need to check the number.
There was a problem hiding this comment.
OK, I a bit think an exact structure should be enforced but I will change it anyways. [TODO]
tests/periph_i2c/main.c
Outdated
| res = i2c_init_master(dev, map_speed[speed]); | ||
| if (res == -1) { | ||
| puts("Error: Init: Given device not available"); | ||
| if (argc != 1) { |
tests/periph_i2c/main.c
Outdated
|
|
||
|
|
||
| if (res == I2C_ACK) { | ||
| printf("Success: i2c_%i successfully read 1 byte from reg 0x%02x: [", |
There was a problem hiding this comment.
why not having one printf only, instead of 2 + puts?
There was a problem hiding this comment.
Yup, legacy stuff. [TODO]
tests/periph_i2c/main.c
Outdated
| res = i2c_read_byte(i2c_dev, addr, &data, flags); | ||
|
|
||
| if (res == I2C_ACK) { | ||
| printf("Success: i2c_%i successfully read 1 byte: [", i2c_dev); |
There was a problem hiding this comment.
nit picky: omit successfully because its already a Success 😉
applies to other output above and below, too.
There was a problem hiding this comment.
Yup legacy, will do. [TODO] x3
tests/periph_i2c/main.c
Outdated
| } | ||
|
|
||
| if (res == I2C_ACK) { | ||
| printf("Success: i2c_%i successfully read %i bytes from reg 0x%02x: [", |
There was a problem hiding this comment.
IMHO its a bit confusing, that the user puts in the address of a register in decimal and its printed in HEX here, or not? At least from what I found atoi expects numbers of base 10, but you could use strtol with base 16 instead.
There was a problem hiding this comment.
and btw. maybe this printing could be a helper function? Something like:
static inline print_i2c_read(i2c_t dev, uint16_t *reg, uint8_t *buf, size_t len)
{
assert ((dev > 0) && (len > 0));
printf("Success: i2c_%i read %i byte(s) ", dev, len);
if (reg != Null) {
printf("from reg 0x%02x ", reg);
}
printf(": [ ");
for (unsigned i = 0; i < len; i++) {
printf("0x%02x ", data[i]);
}
puts("]");
}
and then be use in i2c_read_reg(s)/byte(s)
There was a problem hiding this comment.
I may just do strtol (legacy used atoi), I know lots of times I like seeing the hex numbers (since most data sheets support that). With the interface it gets abstracted out though. Either way I'll improve somehow. [TODO]
tests/periph_i2c/main.c
Outdated
| int res; | ||
| uint8_t addr; | ||
| int length = argc - 2; | ||
| uint8_t reg; |
There was a problem hiding this comment.
is this enough, because the API uses uint16_t, or not?
There was a problem hiding this comment.
Yup, legacy stuff [TODO]
tests/periph_i2c/main.c
Outdated
|
|
||
| dev = atoi(argv[1]); | ||
| speed = atoi(argv[2]); | ||
| static inline int get_num(char *str) { |
There was a problem hiding this comment.
Please put function opening curly on a new line
tests/periph_i2c/main.c
Outdated
| { | ||
| (void)argv; | ||
| if (argc != 1) { | ||
| puts("Error: Aquire: Invalid number of arguments!"); |
tests/periph_i2c/main.c
Outdated
| int print_i2c_error(int res) | ||
| { | ||
|
|
||
| int check_dev(void) { |
There was a problem hiding this comment.
More curlies that should go on a new line
|
@smlng @MrKevinWeiss can we resolve the controversial change request? |
smlng
left a comment
There was a problem hiding this comment.
@MrKevinWeiss: @keestux is right. Can you fix this, i.e. opening braces of function should be on a separate line (on their own). Otherwise the code looks good and ready to merge - but I would rather leave this to @dylad or @aabadie, who seem to manage the I2C rework
|
If @smlng thinks this PR is good to go after the final comment is adressed then I guess we can go :) |
| static inline void print_i2c_read(i2c_t dev, uint16_t *reg, uint8_t *buf, | ||
| size_t len) | ||
| { | ||
| printf("Success: i2c_%i read %i byte(s) ", dev, len); |
There was a problem hiding this comment.
travis complains here, should printf("Success: i2c_%i read %i byte(s) ", dev, (int)len); for instance.
There was a problem hiding this comment.
casting length from size_t to int is the easiest, I guess.
There was a problem hiding this comment.
@MrKevinWeiss can you fix this small issue before merge, because it will hit us anyway when this branch will be merged back into master.
There was a problem hiding this comment.
oops missed that, sure!
|
@dylad apart from the minor issue travis reports, this is good to go. And @MrKevinWeiss would be happy to get this done (over with 😬) and work on refining the test scripts eventually making I2C testing easier for everyone, that is: no need to merge different PRs together to run tests. |
|
@smlng I'm not an expert in testing/CI and so, but this looks OK to me. If all comments + Travis are addressed, we can click on the big green button :) |
tests/periph_i2c/main.c
Outdated
| puts("Error: Speed value not in range (0 - 4)"); | ||
| (void)argv; | ||
| (void)argc; | ||
| if (check_dev() == 1){ |
There was a problem hiding this comment.
space before curly (there are more like these)
| puts("]"); | ||
| return 0; | ||
|
|
||
| switch (GET_I2C_CLK_SPD(i2c_dev)) { |
There was a problem hiding this comment.
The indenting of this switch/case statement is somewhat unusual. The Coding Convention does not say anything specific about it, but I like to see an extra indentation for the statements besides the "case".
switch (GET_I2C_CLK_SPD(i2c_dev)) {
case case I2C_SPEED_LOW:
printf("Success: i2...");
break;
case I2C_SPEED_NORMAL:
tests/periph_i2c/main.c
Outdated
| } | ||
| printf("0x%02x", buf[i]); | ||
| } | ||
| puts("]"); |
There was a problem hiding this comment.
Why the mix of puts and printf?
Personally I prefer to see just printf's, for consistency.
tests/periph_i2c/main.c
Outdated
| } | ||
| printf(": ["); | ||
| for (int i = 0; i < len; i++) { | ||
| if (i != 0){ |
tests/periph_i2c/main.c
Outdated
| } | ||
|
|
||
| int cmd_init_master(int argc, char **argv) | ||
| static inline int get_num(char *str) |
There was a problem hiding this comment.
I'd like to see "const char *" for the argument
|
If everything is addressed, I think you can squash. |
468d8e3 to
067bff4
Compare
067bff4 to
0beaf30
Compare
|
@dylad we squashed it, you wanna hit merge? |
|
Here we go ! |
tests/periph_i2c: Update to new I2C API
tests/periph_i2c: Update to new I2C API
tests/periph_i2c: Update to new I2C API
Updated test for command shell interface to new i2c api. Added some features and simplified a bit.
Contribution description
-update to new api
-standardize variable names
-add new commands to match api closer
-remove i2c clock speed functionality
Issues/PRs references
Issue #6577
Involves PR #9162, #6576, #9257