Skip to content

Commit e576493

Browse files
author
Andrew Cunningham
committed
some small formatting changes and location of stop condition
* TwoWire::endTransmission layout changed to be more inline with TwoWire::requestFrom because they are essentially functioning in a similar way. * Sending of stop condition moved to within block of successful start. Apon successful start but unsuccessful read/write, stop condition is also sent - regardless of whether stop has been requested at that time.
1 parent 6438545 commit e576493

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

Diff for: libraries/Wire/Wire.cpp

+16-29
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,20 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
9797

9898
sercom->prepareNackBitWIRE(); // prepare NACK for slave
9999

100+
if (stopBit || didTimeout() || !busOwner) sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
101+
100102
if (!busOwner || sercom->didTimeout())
101103
{
102104
byteRead--; // because last read byte was garbage/invalid
103105
}
104106

105107
}
106108

107-
// Send Stop if we still have control of the bus, or hit a timeout
108-
if ((stopBit && busOwner) || sercom->didTimeout())
109-
{
110-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
111-
}
112-
113109
// catch and handle timeout condition
114110
if (sercom->didTimeout())
115111
{
116112
// reset the bus
117113
setClock(activeBaudrate);
118-
transmissionBegun = false;
119114
return 0;
120115
}
121116

@@ -147,42 +142,34 @@ uint8_t TwoWire::endTransmission(bool stopBit)
147142
uint8_t errCode = 0;
148143
bool busOwner;
149144

150-
transmissionBegun = false ;
151-
152145
// Start I2C transmission
153-
if ( !sercom->startTransmissionWIRE( txAddress, WIRE_WRITE_FLAG ) )
146+
if ( sercom->startTransmissionWIRE( txAddress, WIRE_WRITE_FLAG ) )
154147
{
155-
errCode = 2; // Address error
156-
}
157-
158-
// Send all buffer
159-
if (!errCode) {
148+
// successful start so transmit data
160149
while ( txBuffer.available() && (busOwner = sercom->isBusOwnerWIRE()) )
161150
{
162-
// Trying to send data
163-
if ( !sercom->sendDataMasterWIRE( txBuffer.read_char() ) )
164-
{
165-
errCode = 3; // Nack or error
166-
txBuffer.clear();
167-
break;
168-
}
151+
// Trying to send data
152+
if ( !sercom->sendDataMasterWIRE( txBuffer.read_char() ) )
153+
{
154+
errCode = 3; // Nack or error
155+
txBuffer.clear();
156+
break;
157+
}
169158
}
170-
}
171159

172-
// Send Stop if we still have control of the bus, or hit an error
173-
if ((stopBit && busOwner) || errCode)
174-
{
175-
sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
176-
}
160+
if (stopBit || errCode || !busOwner) sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop
161+
162+
} else errCode = 2; // Address error
177163

178164
// catch timeout condition
179165
if (sercom->didTimeout()) {
180166
// reset the bus
181167
setClock(activeBaudrate);
182-
transmissionBegun = false;
183168
errCode = 4;
184169
}
185170

171+
transmissionBegun = false ;
172+
186173
return errCode;
187174
}
188175

0 commit comments

Comments
 (0)