Skip to content

Commit e3c69a2

Browse files
cmaglieaethaniel
authored andcommitted
[Wire] simplified coding unnecessarily complex (hfvogt)
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
1 parent 00f1d73 commit e3c69a2

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

Diff for: libraries/Wire/Wire.cpp

+8-19
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,15 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
6868
rxBuffer.store_char(sercom->readDataWIRE());
6969

7070
// Connected to slave
71-
//while(toRead--)
72-
for(byteRead = 0; byteRead < quantity; ++byteRead)
71+
for (byteRead = 1; byteRead < quantity; ++byteRead)
7372
{
74-
if( byteRead == quantity - 1) // Stop transmission
75-
{
76-
sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission
77-
//sercom->readDataWIRE(); // Clear data register to send NACK
78-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
79-
}
80-
else // Continue transmission
81-
{
82-
sercom->prepareAckBitWIRE(); // Prepare Acknowledge
83-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave
84-
rxBuffer.store_char( sercom->readDataWIRE() ); // Read data and send the ACK
85-
}
73+
sercom->prepareAckBitWIRE(); // Prepare Acknowledge
74+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave
75+
rxBuffer.store_char(sercom->readDataWIRE()); // Read data and send the ACK
8676
}
77+
sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission
78+
//sercom->readDataWIRE(); // Clear data register to send NACK
79+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
8780
}
8881

8982
return byteRead;
@@ -134,12 +127,8 @@ uint8_t TwoWire::endTransmission(bool stopBit)
134127
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
135128
return 3 ; // Nack or error
136129
}
137-
138-
if(txBuffer.available() == 0)
139-
{
140-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
141-
}
142130
}
131+
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
143132

144133
return 0;
145134
}

0 commit comments

Comments
 (0)