Skip to content

Commit f6e5ca9

Browse files
Added SPI implementation for 16-bit register address support
Copied over Kirk's code for the SPI 16-bit address register read and write
1 parent d68bc1e commit f6e5ca9

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/sfeTkArdSPI.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,29 @@ sfeTkError_t sfeTkArdSPI::writeRegisterRegion(uint8_t devReg, const uint8_t *dat
176176
return kSTkErrOk;
177177
}
178178

179+
// 16 bit address version ...
180+
sfeTkError_t sfeTkArdSPI::writeRegister16Region(uint16_t devReg, const uint8_t *data, size_t length)
181+
{
182+
if (!_spiPort)
183+
return kSTkErrBusNotInit;
184+
185+
// Apply settings before work
186+
_spiPort->beginTransaction(_sfeSPISettings);
187+
188+
// Signal communication start
189+
digitalWrite(cs(), LOW);
190+
_spiPort->transfer16(devReg);
191+
192+
for (size_t i = 0; i < length; i++)
193+
_spiPort->transfer(*data++);
194+
195+
// End communication
196+
digitalWrite(cs(), HIGH);
197+
_spiPort->endTransaction();
198+
199+
return kSTkErrOk;
200+
}
201+
179202
sfeTkError_t sfeTkArdSPI::readRegisterByte(uint8_t devReg, uint8_t &data)
180203
{
181204
size_t nRead;
@@ -223,3 +246,36 @@ sfeTkError_t sfeTkArdSPI::readRegisterRegion(uint8_t devReg, uint8_t *data, size
223246

224247
return kSTkErrOk;
225248
}
249+
250+
//---------------------------------------------------------------------------------
251+
// readRegister16Region()
252+
//
253+
// Reads an array of bytes to a given a 16 bit register on the target address
254+
//
255+
// Returns kSTkErrOk on success
256+
//
257+
sfeTkError_t sfeTkArdSPI::readRegister16Region(uint16_t devReg, uint8_t *data, size_t numBytes, size_t &readBytes)
258+
{
259+
if (!_spiPort)
260+
return kSTkErrBusNotInit;
261+
262+
// Apply settings
263+
_spiPort->beginTransaction(_sfeSPISettings);
264+
265+
// Signal communication start
266+
digitalWrite(cs(), LOW);
267+
268+
// A leading "1" must be added to transfer with devRegister to indicate a "read"
269+
_spiPort->transfer16(devReg | kSPIReadBit);
270+
271+
for (size_t i = 0; i < numBytes; i++)
272+
*data++ = _spiPort->transfer(0x00);
273+
274+
// End transaction
275+
digitalWrite(cs(), HIGH);
276+
_spiPort->endTransaction();
277+
278+
readBytes = numBytes;
279+
280+
return kSTkErrOk;
281+
}

src/sfeTkArdSPI.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ class sfeTkArdSPI : public sfeTkISPI
122122
*/
123123
virtual sfeTkError_t writeRegisterRegion(uint8_t devReg, const uint8_t *data, size_t length);
124124

125+
/*--------------------------------------------------------------------------
126+
@brief Writes a number of bytes starting at the given register's address.
127+
@note This method is virtual to allow it to be overridden to support a device that requires a unique impl
128+
129+
@param devReg The device's register's address.
130+
@param data Data to write.
131+
132+
@retval sfeTkError_t - kSTkErrOk on success
133+
*/
134+
virtual sfeTkError_t writeRegister16Region(uint16_t devReg, const uint8_t *data, size_t length);
135+
125136
/*--------------------------------------------------------------------------
126137
@brief Read a single byte from the given register
127138
@@ -155,6 +166,20 @@ class sfeTkArdSPI : public sfeTkISPI
155166
*/
156167
virtual sfeTkError_t readRegisterRegion(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes);
157168

169+
/*--------------------------------------------------------------------------
170+
@brief Reads a block of data from the given register.
171+
@note This method is virtual to allow it to be overridden to support a device that requires a unique impl
172+
173+
@param devReg The device's register's 16 bit address.
174+
@param data Data to write.
175+
@param numBytes - length of data
176+
@param[out] readBytes - Number of bytes read
177+
178+
@retval sfeTkError_t - true on success
179+
*/
180+
virtual sfeTkError_t readRegister16Region(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes);
181+
182+
158183
protected:
159184
// note: The instance data is protected, allowing access if a sub-class is
160185
// created to implement a special read/write routine

0 commit comments

Comments
 (0)