Skip to content

Conversation

mali
Copy link
Contributor

@mali mali commented May 30, 2018

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

@dylad dylad added Area: drivers Area: Device drivers TF: I2C Marks issues and PRs related to the work of the I²C rework task force labels May 30, 2018
@ZetaR60
Copy link
Contributor

ZetaR60 commented Jun 6, 2018

@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!

/* receive data byte */
my_data[i] = TWDR;
DEBUG("Byte %i received\n", i+1);
my_data[0] = TWDR;
Copy link
Contributor

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.

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];
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Copy link
Member

@dylad dylad left a 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.

@@ -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) {
Copy link
Member

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

int i2c_write_byte(i2c_t dev, uint8_t address, uint8_t data)
{
return i2c_write_bytes(dev, address, &data, 1);
return I2C_ACK;
Copy link
Member

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


assert(length > 0);

assert (len > 0);
Copy link
Member

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;
}

}

/* send out data bytes */
bytes = _write(data, length);
if(!_write(data, len))
return I2C_DATA_NACK;
Copy link
Member

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;


return bytes;
return I2C_ACK;
Copy link
Member

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;

}

void i2c_poweron(i2c_t dev)
static void i2c_poweron(i2c_t dev)
{
assert(dev < I2C_NUMOF);
Copy link
Member

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

}

return length;
Copy link
Member

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

@dylad
Copy link
Member

dylad commented Jun 13, 2018

@mali Any progress on this ?

@mali
Copy link
Contributor Author

mali commented Jun 15, 2018

@dylad I'm wating for an i2c shield I've ordered. (the same that aabadie had lent me during the Hackaton).
It should arrive soon, i hope, so i'll be able to test for real.

@MrKevinWeiss
Copy link
Contributor

@mali Any update? I can test here.

@mali
Copy link
Contributor Author

mali commented Jul 2, 2018

@MrKevinWeiss , please do !

@aabadie aabadie force-pushed the atmega_new_i2c_if branch from 5543aaf to fe84e0e Compare July 3, 2018 09:05
@aabadie
Copy link
Contributor

aabadie commented Jul 3, 2018

@mali, I allowed myself to update this PR with a few things:

  • use assert as requested by @dylad
  • fix a few issues related to coding conventions
  • improve debug messages
  • adapt I2C bus speed switch in device initialization
  • adapt atmega based board configuration

@aabadie aabadie force-pushed the atmega_new_i2c_if branch from fe84e0e to 09da63d Compare July 3, 2018 09:18
@mali
Copy link
Contributor Author

mali commented Jul 3, 2018

@aabadie , no problem, you're welcome !
I know time is ticking and I do not have as much as I would have liked

@aabadie aabadie force-pushed the atmega_new_i2c_if branch from 09da63d to 07e545b Compare July 3, 2018 11:04
@Aang-3
Copy link

Aang-3 commented Jul 3, 2018

We have just tested it on an Arduino Mega 2560 board using the driver_hts221' test. It seems to work fine.

@aabadie
Copy link
Contributor

aabadie commented Jul 3, 2018

Thanks for testing @Aang-3. Just in case, I squashed the branch. @dylad, are you happy with the changes ?

@aabadie aabadie force-pushed the atmega_new_i2c_if branch from 07e545b to 7ec8623 Compare July 3, 2018 11:18
Copy link
Member

@dylad dylad left a 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.

@aabadie
Copy link
Contributor

aabadie commented Jul 3, 2018

Please wait before merging. With @Aang-3, we realized that this PR is broken in the current state.

@aabadie aabadie force-pushed the atmega_new_i2c_if branch from 7ec8623 to ca0950f Compare July 3, 2018 11:51
@aabadie
Copy link
Contributor

aabadie commented Jul 3, 2018

@dylad, I fixed the driver (plus a couple of other things), so it can be merged now.

@aabadie aabadie force-pushed the atmega_new_i2c_if branch from ca0950f to d797048 Compare July 3, 2018 11:58
@MrKevinWeiss
Copy link
Contributor

I am having some issues testing... I need some time to figure it out.

Copy link
Contributor

@MrKevinWeiss MrKevinWeiss left a 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

/* send start condition and slave address */
if (_start(address, I2C_FLAG_READ) != 0) {
return 0;
if (~(flags & I2C_NOSTART)) {
Copy link
Contributor

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 ~


/* send register address and wait for complete transfer to be finished*/
if (_write(&reg, 1) != 1) {
if (~(flags & I2C_NOSTOP)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, ! not ~

}

/* start transmission and send slave address */
if (_start(address, I2C_FLAG_WRITE) != 0) {
return 0;
if (~(flags & I2C_NOSTART)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Almost there, ! not ~


return bytes;
}
if (~(flags & I2C_NOSTOP)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

finally, ! not ~

@aabadie
Copy link
Contributor

aabadie commented Jul 4, 2018

It seems like the flags are not functioning, this made write reg fail the test

Good catch! And to move fast here, I pushed a fixup commit that addresses your comments. Please test again @MrKevinWeiss !

@MrKevinWeiss
Copy link
Contributor

ACK, passing all non-critical tests!

mali and others added 2 commits July 4, 2018 10:38
Rework atmega i2c to match new i2c interface.
Only i2c_read_bytes and i2c_write_bytes are implemented.
@aabadie aabadie force-pushed the atmega_new_i2c_if branch from 3f96610 to bc83ad1 Compare July 4, 2018 08:39
@aabadie
Copy link
Contributor

aabadie commented Jul 4, 2018

Squashed. We can merge if Travis doesn't report know issues.

@aabadie aabadie merged commit c4bf16e into RIOT-OS:new_i2c_if Jul 4, 2018
@mali
Copy link
Contributor Author

mali commented Jul 4, 2018

👍

basilfx pushed a commit to basilfx/RIOT that referenced this pull request Jul 10, 2018
cpu/atmega_common: adapt to new i2c api
dylad pushed a commit to dylad/RIOT that referenced this pull request Jul 10, 2018
cpu/atmega_common: adapt to new i2c api
dylad pushed a commit that referenced this pull request Jul 11, 2018
cpu/atmega_common: adapt to new i2c api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: drivers Area: Device drivers TF: I2C Marks issues and PRs related to the work of the I²C rework task force
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants