Skip to content

[PATCH] Wire library: coding unnecessarily complex #20

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

Closed
hfvogt opened this issue Jul 17, 2015 · 2 comments
Closed

[PATCH] Wire library: coding unnecessarily complex #20

hfvogt opened this issue Jul 17, 2015 · 2 comments

Comments

@hfvogt
Copy link

hfvogt commented Jul 17, 2015

In the wire library there are several functions where an - unnecessarily - complex coding has been used:
endTransmission: the availability of data is already checked in while(...), therefore need not be checked again in the loop.
requestFrom: the for-loop has a predefined and fixed number of loops. Therefore a check whether the last element has been reached is unnecessary and does not add any benefit.

Attached are two patches to simplify these functions:

--- ArduinoCore-samd.orig/libraries/Wire/Wire.cpp   2015-07-16 10:36:01.416937191 +0200
+++ ArduinoCore-samd/libraries/Wire/Wire.cpp    2015-07-16 19:17:07.675799549 +0200
@@ -132,12 +132,8 @@ uint8_t TwoWire::endTransmission(bool st
       sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
       return 3 ;  // Nack or error
     }
-
-    if(txBuffer.available() == 0)
-    {
-      sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
-    }
   }
+  sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);

   return 0;
 }
--- ArduinoCore-samd.orig/libraries/Wire/Wire.cpp   2015-07-16 19:17:07.675799549 +0200
+++ ArduinoCore-samd/libraries/Wire/Wire.cpp    2015-07-16 19:24:33.179778166 +0200
@@ -66,22 +66,15 @@ uint8_t TwoWire::requestFrom(uint8_t add
     rxBuffer.store_char(sercom->readDataWIRE());

     // Connected to slave
-    //while(toRead--)
-    for(byteRead = 0; byteRead < quantity; ++byteRead)
+    for(byteRead = 1; byteRead < quantity; ++byteRead)
     {
-      if( byteRead == quantity - 1)  // Stop transmission
-      {
-        sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission
-        //sercom->readDataWIRE(); // Clear data register to send NACK
-        sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
-      }
-      else // Continue transmission
-      {
-        sercom->prepareAckBitWIRE();  // Prepare Acknowledge
-        sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave
-        rxBuffer.store_char( sercom->readDataWIRE() );  // Read data and send the ACK
-      }
+      sercom->prepareAckBitWIRE();  // Prepare Acknowledge
+      sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave
+      rxBuffer.store_char( sercom->readDataWIRE() );  // Read data and send the ACK
     }
+    sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission
+    //sercom->readDataWIRE(); // Clear data register to send NACK
+    sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
   }

   return byteRead;

If this is the wrong place to provide patched please guide me to the right place. I have more patches that I would like to share.

@cmaglie
Copy link
Member

cmaglie commented Jul 18, 2015

Hi @hfvogt
I've edited and cleaned up your previous comments.

This is the right place for submitting patches, if you have others I'll surely take a look at them (but probably this won't happen before Monday).

Our preferred way to receive contributions is through Github's pull requests, because it simplifies the review by reducing the amount of cut&paste. Moreover a Pull Request can be automatically merged using the Github website, so if you have other patches I'd ask you to submit them as Pull Request.

Thank you!

@cmaglie
Copy link
Member

cmaglie commented Aug 13, 2015

LGTM I've opened a pull request on #25 for review.

aethaniel pushed a commit to aethaniel/ArduinoCore-samd that referenced this issue Sep 21, 2015
In the wire library there are several functions where
an unnecessarily complex coding has been used:

  - endTransmission: the availability of data is already
    checked in while(...), therefore need not be checked
    again in the loop.
  - requestFrom: the for-loop has a predefined and fixed
    number of loops. Therefore a check whether the last
    element has been reached is unnecessary and does not
    add any benefit.

Fixes arduino#20
mattairtech referenced this issue in mattairtech/ArduinoCore-samd Nov 24, 2015
In the wire library there are several functions where
an unnecessarily complex coding has been used:

  - endTransmission: the availability of data is already
    checked in while(...), therefore need not be checked
    again in the loop.
  - requestFrom: the for-loop has a predefined and fixed
    number of loops. Therefore a check whether the last
    element has been reached is unnecessary and does not
    add any benefit.

Fixes #20
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

2 participants