@@ -84,6 +84,24 @@ sfeTkError_t sfeTkArdI2C::ping()
8484 return _i2cPort->endTransmission () == 0 ? kSTkErrOk : kSTkErrFail ;
8585}
8686
87+ // ---------------------------------------------------------------------------------
88+ // writeByte()
89+ //
90+ // Writes a single byte to the device.
91+ //
92+ // Returns true on success, false on failure
93+ //
94+ sfeTkError_t sfeTkArdI2C::writeByte (uint8_t dataToWrite)
95+ {
96+ if (!_i2cPort)
97+ return kSTkErrBusNotInit ;
98+
99+ // do the Arduino I2C work
100+ _i2cPort->beginTransmission (address ());
101+ _i2cPort->write (dataToWrite);
102+ return _i2cPort->endTransmission () == 0 ? kSTkErrOk : kSTkErrFail ;
103+ }
104+
87105// ---------------------------------------------------------------------------------
88106// writeRegisterByte()
89107//
@@ -102,6 +120,7 @@ sfeTkError_t sfeTkArdI2C::writeRegisterByte(uint8_t devReg, uint8_t dataToWrite)
102120 _i2cPort->write (dataToWrite);
103121 return _i2cPort->endTransmission () == 0 ? kSTkErrOk : kSTkErrFail ;
104122}
123+
105124// ---------------------------------------------------------------------------------
106125// writeRegisterWord()
107126//
@@ -155,7 +174,7 @@ sfeTkError_t sfeTkArdI2C::readRegisterByte(uint8_t devReg, uint8_t &dataToRead)
155174
156175 _i2cPort->beginTransmission (address ());
157176 _i2cPort->write (devReg);
158- _i2cPort->endTransmission ();
177+ _i2cPort->endTransmission (( int ) getStop () );
159178 _i2cPort->requestFrom (address (), (uint8_t )1 );
160179
161180 while (_i2cPort->available ()) // slave may send less than requested
@@ -207,21 +226,23 @@ int32_t sfeTkArdI2C::readRegisterRegion(uint8_t devReg, uint8_t *data, size_t nu
207226
208227 while (numBytes > 0 )
209228 {
210- _i2cPort->beginTransmission (address ());
211-
212229 if (bFirstInter)
213230 {
231+ _i2cPort->beginTransmission (address ());
232+
214233 _i2cPort->write (devReg);
234+
235+ if (_i2cPort->endTransmission (getStop ()) != 0 )
236+ return kSTkErrFail ; // error with the end transmission
237+
215238 bFirstInter = false ;
216239 }
217240
218- if (_i2cPort->endTransmission () != 0 )
219- return kSTkErrFail ; // error with the end transmission
220-
221241 // We're chunking in data - keeping the max chunk to kMaxI2CBufferLength
222242 nChunk = numBytes > _bufferChunkSize ? _bufferChunkSize : numBytes;
223243
224- nReturned = _i2cPort->requestFrom ((int )address (), (int )nChunk, (int )true );
244+ // Request the bytes. If this is the last chunk, always send a stop
245+ nReturned = _i2cPort->requestFrom ((int )address (), (int )nChunk, (int )(nChunk == numBytes ? true : getStop ()));
225246
226247 // No data returned, no dice
227248 if (nReturned == 0 )
0 commit comments