Skip to content

Add writeByte. Add support for I2C restarts #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/sfeTk/sfeTkIBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ const sfeTkError_t kSTkErrBusNotEnabled = kSTkErrBaseBus + 8;
class sfeTkIBus
{
public:
/*--------------------------------------------------------------------------
@brief Write a single byte to the device

@param data Data to write.

@retval sfeTkError_t - kSTkErrOk on successful execution.

*/
virtual sfeTkError_t writeByte(uint8_t data) = 0;

/*--------------------------------------------------------------------------
@brief Write a single byte to the given register

Expand Down
23 changes: 22 additions & 1 deletion src/sfeTk/sfeTkII2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class sfeTkII2C : public sfeTkIBus
sfeTkII2C() : _address{kNoAddress}
{
}
sfeTkII2C(uint8_t addr) : _address{addr}
sfeTkII2C(uint8_t addr) : _address{addr}, _stop{true}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move the initialization of _stop{true} to the default ctor - the constructor without arguments(the method above this one).

And since _stop is a uint8_t, should the default value be 1? I would specify the number, not the value of true in the initializer).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. 1 is the correct choice. The Arduino source uses a mixture of uint8_t but with bool terminology. Let's stick with 1.

{
}

Expand Down Expand Up @@ -68,10 +68,31 @@ class sfeTkII2C : public sfeTkIBus
return _address;
}

/*--------------------------------------------------------------------------
@brief setter for I2C stops (vs restarts)

*/
virtual void setStop(uint8_t stop)
{
_stop = stop;
}

/*--------------------------------------------------------------------------
@brief getter for I2C stops (vs restarts)

@retval uint8_t returns the value of "send stop"

*/
virtual uint8_t getStop(void)
{
return _stop;
}

static constexpr uint8_t kNoAddress = 0;

private:
uint8_t _address;
uint8_t _stop;
};

//};
35 changes: 28 additions & 7 deletions src/sfeTkArdI2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ sfeTkError_t sfeTkArdI2C::ping()
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
}

//---------------------------------------------------------------------------------
// writeByte()
//
// Writes a single byte to the device.
//
// Returns true on success, false on failure
//
sfeTkError_t sfeTkArdI2C::writeByte(uint8_t dataToWrite)
{
if (!_i2cPort)
return kSTkErrBusNotInit;

// do the Arduino I2C work
_i2cPort->beginTransmission(address());
_i2cPort->write(dataToWrite);
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
}

//---------------------------------------------------------------------------------
// writeRegisterByte()
//
Expand All @@ -102,6 +120,7 @@ sfeTkError_t sfeTkArdI2C::writeRegisterByte(uint8_t devReg, uint8_t dataToWrite)
_i2cPort->write(dataToWrite);
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
}

//---------------------------------------------------------------------------------
// writeRegisterWord()
//
Expand Down Expand Up @@ -155,7 +174,7 @@ sfeTkError_t sfeTkArdI2C::readRegisterByte(uint8_t devReg, uint8_t &dataToRead)

_i2cPort->beginTransmission(address());
_i2cPort->write(devReg);
_i2cPort->endTransmission();
_i2cPort->endTransmission((int)getStop());
_i2cPort->requestFrom(address(), (uint8_t)1);

while (_i2cPort->available()) // slave may send less than requested
Expand Down Expand Up @@ -207,21 +226,23 @@ int32_t sfeTkArdI2C::readRegisterRegion(uint8_t devReg, uint8_t *data, size_t nu

while (numBytes > 0)
{
_i2cPort->beginTransmission(address());

if (bFirstInter)
{
_i2cPort->beginTransmission(address());

_i2cPort->write(devReg);

if (_i2cPort->endTransmission(getStop()) != 0)
return kSTkErrFail; // error with the end transmission

bFirstInter = false;
}

if (_i2cPort->endTransmission() != 0)
return kSTkErrFail; // error with the end transmission

// We're chunking in data - keeping the max chunk to kMaxI2CBufferLength
nChunk = numBytes > _bufferChunkSize ? _bufferChunkSize : numBytes;

nReturned = _i2cPort->requestFrom((int)address(), (int)nChunk, (int)true);
// Request the bytes. If this is the last chunk, always send a stop
nReturned = _i2cPort->requestFrom((int)address(), (int)nChunk, (int)(nChunk == numBytes ? true : getStop()));

// No data returned, no dice
if (nReturned == 0)
Expand Down
10 changes: 10 additions & 0 deletions src/sfeTkArdI2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class sfeTkArdI2C : public sfeTkII2C
*/
sfeTkError_t ping();

/*--------------------------------------------------------------------------
@brief Write a single byte to the device
@note sfeTkIBus interface method

@param data Data to write.

@retval returns kStkErrOk on success
*/
sfeTkError_t writeByte(uint8_t data);

/*--------------------------------------------------------------------------
@brief Write a single byte to the given register
@note sfeTkIBus interface method
Expand Down
28 changes: 28 additions & 0 deletions src/sfeTkArdSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ sfeTkError_t sfeTkArdSPI::init(bool bInit)
return init(cs(), bInit);
}

//---------------------------------------------------------------------------------
// writeRegisterByte()
//
// Writes a single byte to the device.
//
// Returns kSTkErrOk on success
//
sfeTkError_t sfeTkArdSPI::writeByte(uint8_t dataToWrite)
{

if (!_spiPort)
return kSTkErrBusNotInit;

// Apply settings
_spiPort->beginTransaction(_sfeSPISettings);
// Signal communication start
digitalWrite(cs(), LOW);

_spiPort->transfer(dataToWrite);

// End communication
digitalWrite(cs(), HIGH);
_spiPort->endTransaction();

return kSTkErrOk;
}

//---------------------------------------------------------------------------------
// writeRegisterByte()
//
Expand Down Expand Up @@ -108,6 +135,7 @@ sfeTkError_t sfeTkArdSPI::writeRegisterByte(uint8_t devReg, uint8_t dataToWrite)

return kSTkErrOk;
}

//---------------------------------------------------------------------------------
// writeRegisterWord()
//
Expand Down
9 changes: 9 additions & 0 deletions src/sfeTkArdSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ class sfeTkArdSPI : public sfeTkISPI
*/
sfeTkError_t init(SPIClass &spiPort, SPISettings &busSPISettings, uint8_t csPin, bool bInit = false);

/*--------------------------------------------------------------------------
@brief Write a single byte to the device

@param data Data to write.

@retval sfeTkError_t - kSTkErrOk on success
*/
sfeTkError_t writeByte(uint8_t data);

/*--------------------------------------------------------------------------
@brief Write a single byte to the given register

Expand Down