Skip to content

Commit bdabd84

Browse files
Added new functions
Added new functionality for reading and writing from 16-bit addressed registers. Note: The function names are likely to change as per Kirk's request/approval
1 parent 39a44e1 commit bdabd84

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

src/sfeTk/sfeTkIBus.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class sfeTkIBus
7878
*/
7979
virtual sfeTkError_t writeRegisterWord(uint8_t devReg, uint16_t data) = 0;
8080

81+
8182
/*--------------------------------------------------------------------------
8283
@brief Writes a number of bytes starting at the given register's address.
8384
@@ -90,6 +91,19 @@ class sfeTkIBus
9091
*/
9192
virtual sfeTkError_t writeRegisterRegion(uint8_t devReg, const uint8_t *data, size_t length) = 0;
9293

94+
/*--------------------------------------------------------------------------
95+
@brief Writes a number of bytes starting at the given register's 16-bit address.
96+
97+
@param devAddr The device's 16-bit address/pin
98+
param devReg The device's register's address.
99+
@param data Data to write.
100+
101+
@retval sfeTkError_t kSTkErrOk on successful execution
102+
103+
*/
104+
virtual sfeTkError_t write16BitRegisterRegion(uint16_t devReg, const uint8_t *data, size_t length) = 0;
105+
106+
93107
/*--------------------------------------------------------------------------
94108
@brief Read a single byte from the given register
95109
@@ -124,6 +138,20 @@ class sfeTkIBus
124138
125139
*/
126140
virtual sfeTkError_t readRegisterRegion(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;
141+
142+
/*--------------------------------------------------------------------------
143+
@brief Reads a block of data from the given 16-bit register address.
144+
145+
@param reg The device's 16 bit register's address.
146+
@param data Data to write.
147+
@param numBytes - length of data
148+
@param[out] readBytes - number of bytes read
149+
150+
@retval int returns kSTkErrOk on success, or kSTkErrFail code
151+
152+
*/
153+
virtual sfeTkError_t read16BitRegisterRegion(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;
154+
127155
};
128156

129157
//};

src/sfeTkArdI2C.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,27 @@ sfeTkError_t sfeTkArdI2C::writeRegisterRegion(uint8_t devReg, const uint8_t *dat
155155
return _i2cPort->endTransmission() ? kSTkErrFail : kSTkErrOk;
156156
}
157157

158+
//---------------------------------------------------------------------------------
159+
// write16BitRegisterRegion()
160+
//
161+
// Writes an array of bytes to a given 16-bit register on the target address
162+
//
163+
// Returns the number of bytes written, < 0 is an error
164+
//
165+
sfeTkError_t sfeTkArdI2C::write16BitRegisterRegion(uint16_t devReg, const uint8_t *data, size_t length)
166+
{
167+
if (!_i2cPort)
168+
return kSTkErrBusNotInit;
169+
170+
_i2cPort->beginTransmission(address());
171+
_i2cPort->write((devReg >> 8) & 0xff);
172+
_i2cPort->write(devReg & 0xff);
173+
_i2cPort->write(data, (int)length);
174+
175+
return _i2cPort->endTransmission() ? kSTkErrFail : kSTkErrOk;
176+
}
177+
178+
158179
//---------------------------------------------------------------------------------
159180
// readRegisterByte()
160181
//
@@ -269,3 +290,71 @@ sfeTkError_t sfeTkArdI2C::readRegisterRegion(uint8_t devReg, uint8_t *data, size
269290

270291
return (readBytes == nOrig) ? kSTkErrOk : kSTkErrBusUnderRead; // Success
271292
}
293+
294+
295+
296+
297+
//---------------------------------------------------------------------------------
298+
// read16BitRegisterRegion()
299+
//
300+
// Reads an array of bytes to a given 16-bit register on the target address
301+
//
302+
// Returns the number of bytes read, < 0 is an error
303+
//
304+
sfeTkError_t sfeTkArdI2C::read16BitRegisterRegion(uint16_t devReg, uint8_t *data, size_t numBytes, size_t &readBytes)
305+
{
306+
307+
// got port
308+
if (!_i2cPort)
309+
return kSTkErrBusNotInit;
310+
311+
// Buffer valid?
312+
if (!data)
313+
return kSTkErrBusNullBuffer;
314+
315+
readBytes = 0;
316+
317+
uint16_t nOrig = numBytes; // original number of bytes.
318+
uint8_t nChunk;
319+
uint16_t nReturned;
320+
uint16_t i; // counter in loop
321+
bool bFirstInter = true; // Flag for first iteration - used to send devRegister
322+
323+
while (numBytes > 0)
324+
{
325+
if (bFirstInter)
326+
{
327+
_i2cPort->beginTransmission(address());
328+
329+
_i2cPort->write((devReg >> 8) & 0xff);
330+
_i2cPort->write(devReg & 0xff);
331+
332+
if (_i2cPort->endTransmission(stop()) != 0)
333+
return kSTkErrFail; // error with the end transmission
334+
335+
bFirstInter = false;
336+
}
337+
338+
// We're chunking in data - keeping the max chunk to kMaxI2CBufferLength
339+
nChunk = numBytes > _bufferChunkSize ? _bufferChunkSize : numBytes;
340+
341+
// Request the bytes. If this is the last chunk, always send a stop
342+
nReturned = _i2cPort->requestFrom((int)address(), (int)nChunk, (int)(nChunk == numBytes ? true : stop()));
343+
344+
// No data returned, no dice
345+
if (nReturned == 0)
346+
return kSTkErrBusUnderRead; // error
347+
348+
// Copy the retrieved data chunk to the current index in the data segment
349+
for (i = 0; i < nReturned; i++)
350+
*data++ = _i2cPort->read();
351+
352+
// Decrement the amount of data received from the overall data request amount
353+
numBytes = numBytes - nReturned;
354+
355+
} // end while
356+
357+
readBytes = nOrig - numBytes; // Bytes read.
358+
359+
return (readBytes == nOrig) ? kSTkErrOk : kSTkErrBusUnderRead; // Success
360+
}

src/sfeTkArdI2C.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ class sfeTkArdI2C : public sfeTkII2C
137137
*/
138138
virtual sfeTkError_t writeRegisterRegion(uint8_t devReg, const uint8_t *data, size_t length);
139139

140+
/*--------------------------------------------------------------------------
141+
@brief Writes a number of bytes starting at the given register's 16-bit address.
142+
143+
@param devAddr The device's 16-bit address/pin
144+
param devReg The device's register's address.
145+
@param data Data to write.
146+
147+
@retval sfeTkError_t kSTkErrOk on successful execution
148+
149+
*/
150+
sfeTkError_t write16BitRegisterRegion(uint16_t devReg, const uint8_t *data, size_t length);
151+
152+
140153
/*--------------------------------------------------------------------------
141154
@brief Reads a byte of data from the given register.
142155
@@ -177,6 +190,19 @@ class sfeTkArdI2C : public sfeTkII2C
177190
*/
178191
sfeTkError_t readRegisterRegion(uint8_t devReg, uint8_t *data, size_t numBytes, size_t &readBytes);
179192

193+
/*--------------------------------------------------------------------------
194+
@brief Reads a block of data from the given 16-bit register address.
195+
196+
@param reg The device's 16 bit register's address.
197+
@param data Data to write.
198+
@param numBytes - length of data
199+
@param[out] readBytes - number of bytes read
200+
201+
@retval int returns kSTkErrOk on success, or kSTkErrFail code
202+
203+
*/
204+
sfeTkError_t read16BitRegisterRegion(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes);
205+
180206
// Buffer size chunk getter/setter
181207
/*--------------------------------------------------------------------------
182208
@brief set the buffer chunk size

0 commit comments

Comments
 (0)