Skip to content
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

Inconsistent implementation of I2C::write #2725

Closed
matthewelse opened this issue Sep 16, 2016 · 2 comments
Closed

Inconsistent implementation of I2C::write #2725

matthewelse opened this issue Sep 16, 2016 · 2 comments

Comments

@matthewelse
Copy link
Contributor

matthewelse commented Sep 16, 2016

Description

Currently, the I2C class is implemented such that I2C::write returns different values to indicate ACK in different overloads. This seems counter-intuitive and should probably be made consistent. In #1670, the documentation was corrected to reflect the documentation, however nothing was done about the implementation.

cc: @0xc0170

@ciarmcom
Copy link
Member

ARM Internal Ref: IOTMORF-477

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 21, 2016

Thanks @matthewelse for reporting.

I made first corrections in the doc to map what is should be :

diff --git a/drivers/I2C.h b/drivers/I2C.h
index da09d94..84f1d89 100644
--- a/drivers/I2C.h
+++ b/drivers/I2C.h
@@ -117,8 +117,8 @@ public:
      *  @param repeated Repeated start, true - do not send stop at end
      *
      *  @returns
-     *       0 on success (ack),
-     *   non-0 on failure (nack)
+     *      0 or non-zero - written number of bytes,
+     *      negative - I2C_ERROR_XXX status
      */
     int write(int address, const char *data, int length, bool repeated = false);

@@ -127,8 +127,9 @@ public:
      *  @param data data to write out on bus
      *
      *  @returns
-     *    '1' if an ACK was received,
-     *    '0' otherwise
+     *    '0' - NAK was received
+     *    '1' - ACK was received,
+     *    '2' - timeout
      */
     int write(int data);

diff --git a/hal/i2c_api.h b/hal/i2c_api.h
index 677596d..41aef38 100644
--- a/hal/i2c_api.h
+++ b/hal/i2c_api.h
@@ -117,7 +117,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
  *  @param data    The buffer for sending
  *  @param length  Number of bytes to write
  *  @param stop    Stop to be generated after the transfer is done
- *  @return Number of written bytes
+ *  @return
+ *      zero or non-zero - Number of written bytes
+ *      negative - I2C_ERROR_XXX status
  */
 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);

The further would be to align write() methods (one does 0,1,2 statuses, the other one bytes written or failures (negative values). As this issue states, we should focus on targets and fix them.

I'll send PR with this patch, tag there maintainers for review, we can write test to see which ones fail and start patching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants