Skip to content

Commit 5c4d2bc

Browse files
authored
Merge pull request #34 from sparkfun/add_writeWord_writeBlock
Add writeWord writeBlock
2 parents 8422de8 + 0cdd531 commit 5c4d2bc

File tree

5 files changed

+137
-7
lines changed

5 files changed

+137
-7
lines changed

src/sfeTk/sfeTkIBus.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,32 @@ class sfeTkIBus
8787
{
8888
public:
8989
/**--------------------------------------------------------------------------
90-
* @brief Write a single byte to the device*
90+
* @brief Send a single byte to the device*
9191
* @param data Data to write.
9292
*
9393
* @retval sfeTkError_t - kSTkErrOk on successful execution.
9494
*
9595
*/
9696
virtual sfeTkError_t writeByte(uint8_t data) = 0;
9797

98+
/**--------------------------------------------------------------------------
99+
* @brief Send a word to the device.
100+
* @param data Data to write.
101+
*
102+
* @retval sfeTkError_t - kSTkErrOk on successful execution.
103+
*
104+
*/
105+
virtual sfeTkError_t writeWord(uint16_t data) = 0;
106+
107+
/**--------------------------------------------------------------------------
108+
* @brief Send an array of data to the device.
109+
* @param data Data to write.
110+
*
111+
* @retval sfeTkError_t - kSTkErrOk on successful execution.
112+
*
113+
*/
114+
virtual sfeTkError_t writeRegion(const uint8_t *data, size_t length) = 0;
115+
98116
/**--------------------------------------------------------------------------
99117
* @brief Write a single byte to the given register
100118
*

src/sfeTkArdI2C.cpp

+35-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
2323

2424
#include "sfeTkArdI2C.h"
25+
#include <cstddef>
26+
#include <cstdint>
2527

2628
//---------------------------------------------------------------------------------
2729
// init()
@@ -87,7 +89,7 @@ sfeTkError_t sfeTkArdI2C::ping()
8789
//---------------------------------------------------------------------------------
8890
// writeByte()
8991
//
90-
// Writes a single byte to the device.
92+
// Writes a single byte to the device, without indexing to a register.
9193
//
9294
// Returns true on success, false on failure
9395
//
@@ -102,6 +104,33 @@ sfeTkError_t sfeTkArdI2C::writeByte(uint8_t dataToWrite)
102104
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
103105
}
104106

107+
//---------------------------------------------------------------------------------
108+
// writeWord()
109+
//
110+
// Writes a word to the device, without indexing to a register.
111+
//
112+
// Returns true on success, false on failure
113+
//
114+
sfeTkError_t sfeTkArdI2C::writeWord(uint16_t dataToWrite)
115+
{
116+
if (!_i2cPort)
117+
return kSTkErrBusNotInit;
118+
119+
return writeRegion((uint8_t *)&dataToWrite, sizeof(uint16_t));
120+
}
121+
122+
//---------------------------------------------------------------------------------
123+
// writeRegion()
124+
//
125+
// Writes a word to the device, without indexing to a register.
126+
//
127+
// Returns true on success, false on failure
128+
//
129+
sfeTkError_t sfeTkArdI2C::writeRegion(const uint8_t *data, size_t length)
130+
{
131+
return writeRegisterRegionAddress(nullptr, 0, data, length) == 0 ? kSTkErrOk : kSTkErrFail;
132+
}
133+
105134
//---------------------------------------------------------------------------------
106135
// writeRegisterByte()
107136
//
@@ -152,7 +181,10 @@ sfeTkError_t sfeTkArdI2C::writeRegisterRegionAddress(uint8_t *devReg, size_t reg
152181
return kSTkErrBusNotInit;
153182

154183
_i2cPort->beginTransmission(address());
155-
_i2cPort->write(devReg, regLength);
184+
185+
if(devReg != nullptr && regLength > 0)
186+
_i2cPort->write(devReg, regLength);
187+
156188
_i2cPort->write(data, (int)length);
157189

158190
return _i2cPort->endTransmission() ? kSTkErrFail : kSTkErrOk;
@@ -183,7 +215,7 @@ sfeTkError_t sfeTkArdI2C::writeRegister16Region(uint16_t devReg, const uint8_t *
183215
return writeRegisterRegionAddress((uint8_t *)&devReg, 2, data, length);
184216
}
185217

186-
//---------------------------------------------------------------------------------
218+
187219

188220
/**
189221
* @brief Reads an array of bytes to a register on the target address. Supports any address size

src/sfeTkArdI2C.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class sfeTkArdI2C : public sfeTkII2C
109109
sfeTkError_t ping();
110110

111111
/**
112-
@brief Write a single byte to the device
112+
@brief Sends a single byte to the device
113113
@note sfeTkIBus interface method
114114
115115
@param data Data to write.
@@ -118,6 +118,26 @@ class sfeTkArdI2C : public sfeTkII2C
118118
*/
119119
sfeTkError_t writeByte(uint8_t data);
120120

121+
/**
122+
@brief Sends a word to the device.
123+
@note sfeTkIBus interface method
124+
125+
@param data Data to write.
126+
127+
@retval returns kStkErrOk on success
128+
*/
129+
sfeTkError_t writeWord(uint16_t data);
130+
131+
/**
132+
@brief Sends a block of data to the device.
133+
@note sfeTkIBus interface method
134+
135+
@param data Data to write.
136+
137+
@retval returns kStkErrOk on success
138+
*/
139+
sfeTkError_t writeRegion(const uint8_t *data, size_t length);
140+
121141
/**
122142
@brief Write a single byte to the given register
123143
@note sfeTkIBus interface method

src/sfeTkArdSPI.cpp

+43-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ sfeTkError_t sfeTkArdSPI::init(bool bInit)
8282
}
8383

8484
//---------------------------------------------------------------------------------
85-
// writeRegisterByte()
85+
// writeByte()
8686
//
8787
// Writes a single byte to the device.
8888
//
@@ -108,6 +108,46 @@ sfeTkError_t sfeTkArdSPI::writeByte(uint8_t dataToWrite)
108108
return kSTkErrOk;
109109
}
110110

111+
//---------------------------------------------------------------------------------
112+
// writeWord()
113+
//
114+
// Writes a word to the device without indexing to a register.
115+
//
116+
// Returns kSTkErrOk on success
117+
//
118+
sfeTkError_t sfeTkArdSPI::writeWord(uint16_t dataToWrite)
119+
{
120+
return writeRegion((uint8_t *)&dataToWrite, sizeof(uint8_t)) > 0;
121+
}
122+
123+
124+
//---------------------------------------------------------------------------------
125+
// writeRegion()
126+
//
127+
// Writes an array of data to the device without indexing to a register.
128+
//
129+
// Returns kSTkErrOk on success
130+
//
131+
sfeTkError_t sfeTkArdSPI::writeRegion(const uint8_t *dataToWrite, size_t length)
132+
{
133+
134+
if (!_spiPort)
135+
return kSTkErrBusNotInit;
136+
137+
_spiPort->beginTransaction(_sfeSPISettings);
138+
// Signal communication start
139+
digitalWrite(cs(), LOW);
140+
141+
for (size_t i = 0; i < length; i++)
142+
_spiPort->transfer(*dataToWrite++);
143+
144+
// End communication
145+
digitalWrite(cs(), HIGH);
146+
_spiPort->endTransaction();
147+
148+
return kSTkErrOk;
149+
}
150+
111151
//---------------------------------------------------------------------------------
112152
// writeRegisterByte()
113153
//
@@ -164,6 +204,7 @@ sfeTkError_t sfeTkArdSPI::writeRegisterRegion(uint8_t devReg, const uint8_t *dat
164204

165205
// Signal communication start
166206
digitalWrite(cs(), LOW);
207+
167208
_spiPort->transfer(devReg);
168209

169210
for (size_t i = 0; i < length; i++)
@@ -278,4 +319,4 @@ sfeTkError_t sfeTkArdSPI::readRegister16Region(uint16_t devReg, uint8_t *data, s
278319
readBytes = numBytes;
279320

280321
return kSTkErrOk;
281-
}
322+
}

src/sfeTkArdSPI.h

+19
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ class sfeTkArdSPI : public sfeTkISPI
115115
*/
116116
sfeTkError_t writeByte(uint8_t data);
117117

118+
/**
119+
@brief Write a word to the device without indexing to a register.
120+
121+
@param data Data to write.
122+
123+
@retval sfeTkError_t - kSTkErrOk on success
124+
*/
125+
sfeTkError_t writeWord(uint16_t data);
126+
127+
/**
128+
@brief Write an array of data to the device without indexing to a register.
129+
130+
@param data Data to write
131+
@param length Length of Data
132+
133+
@retval sfeTkError_t - kSTkErrOk on success
134+
*/
135+
sfeTkError_t writeRegion(const uint8_t *data, size_t length);
136+
118137
/**
119138
@brief Write a single byte to the given register
120139

0 commit comments

Comments
 (0)