-
Notifications
You must be signed in to change notification settings - Fork 2.1k
cpu/atmega_common: adapt to new i2c api #9252
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
Conversation
@mali I have gone over this PR and #9165 several times, but I have been unable to track down the problem I have been having in #9165. The slave does not ack on the bus with the new code, but I have confirmed that the old code works fine. The two code sets don't have a lot of differences in behavior prior to the failure, so it is probably just something I have overlooked. Could you take a look and see if you spot anything that could be causing it? If anyone has any other I2C devices that have been ported and can be tested with this PR it would be much appreciated! |
cpu/atmega_common/periph/i2c.c
Outdated
/* receive data byte */ | ||
my_data[i] = TWDR; | ||
DEBUG("Byte %i received\n", i+1); | ||
my_data[0] = TWDR; |
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 looks like an introduced bug.
cpu/atmega_common/periph/i2c.c
Outdated
static int _write(const uint8_t *data, int length) | ||
{ | ||
for (int i = 0; i < length; i++) { | ||
/* Load DATA into TWDR Register. | ||
* Clear TWINT bit in TWCR to start transmission of data */ | ||
TWDR = data[i]; | ||
TWDR = data[0]; |
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.
Same here
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 I2C API has evolved a bit lately. We decide to use errno return code so some basic changes are needed.
cpu/atmega_common/periph/i2c.c
Outdated
@@ -57,18 +64,24 @@ int i2c_init_master(i2c_t dev, i2c_speed_t speed) | |||
|
|||
/* check if the line is valid */ | |||
if (dev >= I2C_NUMOF) { |
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.
prefer the use of assert(dev < I2C_NUMOF)
instead of if
cpu/atmega_common/periph/i2c.c
Outdated
int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data) | ||
{ | ||
return i2c_write_bytes(dev, address, &data, 1); | ||
return I2C_ACK; |
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.
I2C_ACK doesn't exist anymore as we switch to errno return code for the whole API. You can replace I2C_ACK by 0
cpu/atmega_common/periph/i2c.c
Outdated
|
||
assert(length > 0); | ||
|
||
assert (len > 0); |
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.
must be switch to :
if(data == NULL || len == 0) {
return -EINVAL;
}
cpu/atmega_common/periph/i2c.c
Outdated
} | ||
|
||
/* send out data bytes */ | ||
bytes = _write(data, length); | ||
if(!_write(data, len)) | ||
return I2C_DATA_NACK; |
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.
must be switch to return -EIO;
cpu/atmega_common/periph/i2c.c
Outdated
|
||
return bytes; | ||
return I2C_ACK; |
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.
must be switch to return 0;
cpu/atmega_common/periph/i2c.c
Outdated
} | ||
|
||
void i2c_poweron(i2c_t dev) | ||
static void i2c_poweron(i2c_t dev) | ||
{ | ||
assert(dev < I2C_NUMOF); |
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 unify the use of assert for each dev test against I2C_NUMOF
cpu/atmega_common/periph/i2c.c
Outdated
} | ||
|
||
return length; |
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.
I2C API functions does not return the length anymore. This should be replaced by 0
@mali Any progress on this ? |
@dylad I'm wating for an i2c shield I've ordered. (the same that aabadie had lent me during the Hackaton). |
@mali Any update? I can test here. |
@MrKevinWeiss , please do ! |
5543aaf
to
fe84e0e
Compare
fe84e0e
to
09da63d
Compare
@aabadie , no problem, you're welcome ! |
09da63d
to
07e545b
Compare
We have just tested it on an Arduino Mega 2560 board using the driver_hts221' test. It seems to work fine. |
07e545b
to
7ec8623
Compare
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.
@aabadie I'm fime with the current state, I have some idea in the future but this is out-of-scope.
ACK
Just wait a bit so others can test if they want just to be sure.
Please wait before merging. With @Aang-3, we realized that this PR is broken in the current state. |
7ec8623
to
ca0950f
Compare
@dylad, I fixed the driver (plus a couple of other things), so it can be merged now. |
ca0950f
to
d797048
Compare
I am having some issues testing... I need some time to figure it 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.
It seems like the flags are not functioning, this made write reg fail the test. I believe if you have ~(0x00 & 0x04) == (0xFF) == true but if you have ~(0x04 & 0x04) == 0xFB == true. You want !(0x00 & 0x04) == true and !(0x04 & 0x04) == false
cpu/atmega_common/periph/i2c.c
Outdated
/* send start condition and slave address */ | ||
if (_start(address, I2C_FLAG_READ) != 0) { | ||
return 0; | ||
if (~(flags & I2C_NOSTART)) { |
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 think you mean ! not ~
cpu/atmega_common/periph/i2c.c
Outdated
|
||
/* send register address and wait for complete transfer to be finished*/ | ||
if (_write(®, 1) != 1) { | ||
if (~(flags & I2C_NOSTOP)) { |
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.
Same here, ! not ~
cpu/atmega_common/periph/i2c.c
Outdated
} | ||
|
||
/* start transmission and send slave address */ | ||
if (_start(address, I2C_FLAG_WRITE) != 0) { | ||
return 0; | ||
if (~(flags & I2C_NOSTART)) { |
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.
Almost there, ! not ~
cpu/atmega_common/periph/i2c.c
Outdated
|
||
return bytes; | ||
} | ||
if (~(flags & I2C_NOSTOP)) { |
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.
finally, ! not ~
Good catch! And to move fast here, I pushed a fixup commit that addresses your comments. Please test again @MrKevinWeiss ! |
ACK, passing all non-critical tests! |
Rework atmega i2c to match new i2c interface. Only i2c_read_bytes and i2c_write_bytes are implemented.
3f96610
to
bc83ad1
Compare
Squashed. We can merge if Travis doesn't report know issues. |
👍 |
cpu/atmega_common: adapt to new i2c api
cpu/atmega_common: adapt to new i2c api
cpu/atmega_common: adapt to new i2c api
Contribution description
Rework atmega i2c to match new i2c interface.
Only i2c_read_bytes and i2c_write_bytes are implemented.
WIP : Comments welcome !
Issues/PRs references
#6577