From b2ffe2d90e44a1f43ef60b70f13937172432d720 Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Mon, 24 Jun 2024 23:22:50 +0200 Subject: [PATCH] [i2c] Add more convenience functions to I2cDevice --- .../architecture/interface/i2c_device.hpp | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/modm/architecture/interface/i2c_device.hpp b/src/modm/architecture/interface/i2c_device.hpp index 6442b42328..b151bc3f7a 100644 --- a/src/modm/architecture/interface/i2c_device.hpp +++ b/src/modm/architecture/interface/i2c_device.hpp @@ -80,6 +80,46 @@ class I2cDevice : protected modm::NestedResumable< NestingLevels + 1 > RF_END_RETURN( wasTransactionSuccessful() ); } + /// Starts a write-read transaction and waits until finished. + modm::ResumableResult + writeRead(const uint8_t *writeBuffer, std::size_t writeSize, + uint8_t *readBuffer, std::size_t readSize) + { + RF_BEGIN(); + + RF_WAIT_UNTIL( startWriteRead(writeBuffer, writeSize, readBuffer, readSize) ); + + RF_WAIT_WHILE( isTransactionRunning() ); + + RF_END_RETURN( wasTransactionSuccessful() ); + } + + /// Starts a write transaction and waits until finished. + modm::ResumableResult + write(const uint8_t *buffer, std::size_t size) + { + RF_BEGIN(); + + RF_WAIT_UNTIL( startWrite(buffer, size) ); + + RF_WAIT_WHILE( isTransactionRunning() ); + + RF_END_RETURN( wasTransactionSuccessful() ); + } + + /// Starts a write transaction and waits until finished. + modm::ResumableResult + read(uint8_t *buffer, std::size_t size) + { + RF_BEGIN(); + + RF_WAIT_UNTIL( startRead(buffer, size) ); + + RF_WAIT_WHILE( isTransactionRunning() ); + + RF_END_RETURN( wasTransactionSuccessful() ); + } + protected: /// Configures the transaction with a write/read operation and starts it. bool inline @@ -114,8 +154,8 @@ class I2cDevice : protected modm::NestedResumable< NestingLevels + 1 > return startTransaction(&this->transaction); } - /// Starts the transaction with a seperate transaction object. - bool inline + /// Starts the transaction with a separate transaction object. + static bool inline startTransaction(modm::I2cTransaction *transaction) { return I2cMaster::start(transaction, configuration);