Skip to content

Commit 1e24077

Browse files
authored
Merge pull request #25 from sparkfun/Add_Non-Register_Write
Add writeByte. Add support for I2C restarts
2 parents c17b3a0 + 23d7d9a commit 1e24077

File tree

6 files changed

+107
-8
lines changed

6 files changed

+107
-8
lines changed

src/sfeTk/sfeTkIBus.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ const sfeTkError_t kSTkErrBusNotEnabled = kSTkErrBaseBus + 8;
4646
class sfeTkIBus
4747
{
4848
public:
49+
/*--------------------------------------------------------------------------
50+
@brief Write a single byte to the device
51+
52+
@param data Data to write.
53+
54+
@retval sfeTkError_t - kSTkErrOk on successful execution.
55+
56+
*/
57+
virtual sfeTkError_t writeByte(uint8_t data) = 0;
58+
4959
/*--------------------------------------------------------------------------
5060
@brief Write a single byte to the given register
5161

src/sfeTk/sfeTkII2C.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class sfeTkII2C : public sfeTkIBus
3434
sfeTkII2C() : _address{kNoAddress}
3535
{
3636
}
37-
sfeTkII2C(uint8_t addr) : _address{addr}
37+
sfeTkII2C(uint8_t addr) : _address{addr}, _stop{true}
3838
{
3939
}
4040

@@ -68,10 +68,31 @@ class sfeTkII2C : public sfeTkIBus
6868
return _address;
6969
}
7070

71+
/*--------------------------------------------------------------------------
72+
@brief setter for I2C stops (vs restarts)
73+
74+
*/
75+
virtual void setStop(uint8_t stop)
76+
{
77+
_stop = stop;
78+
}
79+
80+
/*--------------------------------------------------------------------------
81+
@brief getter for I2C stops (vs restarts)
82+
83+
@retval uint8_t returns the value of "send stop"
84+
85+
*/
86+
virtual uint8_t getStop(void)
87+
{
88+
return _stop;
89+
}
90+
7191
static constexpr uint8_t kNoAddress = 0;
7292

7393
private:
7494
uint8_t _address;
95+
uint8_t _stop;
7596
};
7697

7798
//};

src/sfeTkArdI2C.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

src/sfeTkArdI2C.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ class sfeTkArdI2C : public sfeTkII2C
9292
*/
9393
sfeTkError_t ping();
9494

95+
/*--------------------------------------------------------------------------
96+
@brief Write a single byte to the device
97+
@note sfeTkIBus interface method
98+
99+
@param data Data to write.
100+
101+
@retval returns kStkErrOk on success
102+
*/
103+
sfeTkError_t writeByte(uint8_t data);
104+
95105
/*--------------------------------------------------------------------------
96106
@brief Write a single byte to the given register
97107
@note sfeTkIBus interface method

src/sfeTkArdSPI.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ sfeTkError_t sfeTkArdSPI::init(bool bInit)
8181
return init(cs(), bInit);
8282
}
8383

84+
//---------------------------------------------------------------------------------
85+
// writeRegisterByte()
86+
//
87+
// Writes a single byte to the device.
88+
//
89+
// Returns kSTkErrOk on success
90+
//
91+
sfeTkError_t sfeTkArdSPI::writeByte(uint8_t dataToWrite)
92+
{
93+
94+
if (!_spiPort)
95+
return kSTkErrBusNotInit;
96+
97+
// Apply settings
98+
_spiPort->beginTransaction(_sfeSPISettings);
99+
// Signal communication start
100+
digitalWrite(cs(), LOW);
101+
102+
_spiPort->transfer(dataToWrite);
103+
104+
// End communication
105+
digitalWrite(cs(), HIGH);
106+
_spiPort->endTransaction();
107+
108+
return kSTkErrOk;
109+
}
110+
84111
//---------------------------------------------------------------------------------
85112
// writeRegisterByte()
86113
//
@@ -108,6 +135,7 @@ sfeTkError_t sfeTkArdSPI::writeRegisterByte(uint8_t devReg, uint8_t dataToWrite)
108135

109136
return kSTkErrOk;
110137
}
138+
111139
//---------------------------------------------------------------------------------
112140
// writeRegisterWord()
113141
//

src/sfeTkArdSPI.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ class sfeTkArdSPI : public sfeTkISPI
8282
*/
8383
sfeTkError_t init(SPIClass &spiPort, SPISettings &busSPISettings, uint8_t csPin, bool bInit = false);
8484

85+
/*--------------------------------------------------------------------------
86+
@brief Write a single byte to the device
87+
88+
@param data Data to write.
89+
90+
@retval sfeTkError_t - kSTkErrOk on success
91+
*/
92+
sfeTkError_t writeByte(uint8_t data);
93+
8594
/*--------------------------------------------------------------------------
8695
@brief Write a single byte to the given register
8796

0 commit comments

Comments
 (0)