From 3d5ebc46e18ee0a8ec86c6cca07627c20a7a058c Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 16 Oct 2017 09:21:20 -0400 Subject: [PATCH 1/3] Add new Wire:sendTo(...) and Wire.requestFrom(...) API's that use external buffer --- libraries/Wire/Wire.cpp | 59 ++++++++++++++++++++++++++++++++++++----- libraries/Wire/Wire.h | 5 ++++ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 0326bf68f..f0473252e 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -62,7 +62,49 @@ void TwoWire::end() { sercom->disableWIRE(); } +uint8_t TwoWire::sentTo(uint8_t address, uint8_t buffer[], size_t quantity) +{ + return sentTo(address, buffer, quantity, true); +} + +uint8_t TwoWire::sentTo(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit) +{ + beginTransmission(address); + endTransmission(false); + + for (size_t i = 0; i < quantity; i++) { + // Try to send data + if (!sercom->sendDataMasterWIRE(buffer[i])) + { + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); + return 3 ; // Nack or error + } + } + + if (stopBit) + { + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); + } + + return 0; +} + +uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity) +{ + return requestFrom(address, quantity, true); +} + uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit) +{ + return requestFrom(address, NULL, quantity, stopBit); +} + +uint8_t TwoWire::requestFrom(uint8_t address, uint8_t buffer[], size_t quantity) +{ + return requestFrom(address, buffer, quantity, true); +} + +uint8_t TwoWire::requestFrom(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit) { if(quantity == 0) { @@ -76,14 +118,22 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit) if(sercom->startTransmissionWIRE(address, WIRE_READ_FLAG)) { // Read first data - rxBuffer.store_char(sercom->readDataWIRE()); + if (buffer == NULL) { + rxBuffer.store_char(sercom->readDataWIRE()); + } else { + buffer[0] = sercom->readDataWIRE(); + } // Connected to slave for (byteRead = 1; byteRead < quantity; ++byteRead) { sercom->prepareAckBitWIRE(); // Prepare Acknowledge sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave - rxBuffer.store_char(sercom->readDataWIRE()); // Read data and send the ACK + if (buffer == NULL) { + rxBuffer.store_char(sercom->readDataWIRE()); // Read data and send the ACK + } else { + buffer[byteRead] = sercom->readDataWIRE(); + } } sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission //sercom->readDataWIRE(); // Clear data register to send NACK @@ -97,11 +147,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit) return byteRead; } -uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity) -{ - return requestFrom(address, quantity, true); -} - void TwoWire::beginTransmission(uint8_t address) { // save address of target and clear buffer txAddress = address; diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index f437db894..54378602b 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -42,8 +42,13 @@ class TwoWire : public Stream uint8_t endTransmission(bool stopBit); uint8_t endTransmission(void); + uint8_t sentTo(uint8_t address, uint8_t buffer[], size_t quantity); + uint8_t sentTo(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit); + uint8_t requestFrom(uint8_t address, size_t quantity, bool stopBit); uint8_t requestFrom(uint8_t address, size_t quantity); + uint8_t requestFrom(uint8_t address, uint8_t buffer[], size_t quantity); + uint8_t requestFrom(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit); size_t write(uint8_t data); size_t write(const uint8_t * data, size_t quantity); From 44032f751c33c61f9b6004ff8c912bdb03d0c671 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 16 Oct 2017 09:34:47 -0400 Subject: [PATCH 2/3] Make Wire.endTransmission(...) use new Wire.sendTo(...) --- libraries/Wire/Wire.cpp | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index f0473252e..67cb1847e 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -69,8 +69,12 @@ uint8_t TwoWire::sentTo(uint8_t address, uint8_t buffer[], size_t quantity) uint8_t TwoWire::sentTo(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit) { - beginTransmission(address); - endTransmission(false); + // Start I2C transmission + if (!sercom->startTransmissionWIRE(address, WIRE_WRITE_FLAG)) + { + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); + return 2 ; // Address error + } for (size_t i = 0; i < quantity; i++) { // Try to send data @@ -165,30 +169,13 @@ uint8_t TwoWire::endTransmission(bool stopBit) { transmissionBegun = false ; - // Start I2C transmission - if ( !sercom->startTransmissionWIRE( txAddress, WIRE_WRITE_FLAG ) ) - { - sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); - return 2 ; // Address error - } + uint8_t result = sentTo(txAddress, txBuffer._aucBuffer, txBuffer.available(), stopBit); - // Send all buffer - while( txBuffer.available() ) - { - // Trying to send data - if ( !sercom->sendDataMasterWIRE( txBuffer.read_char() ) ) - { - sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); - return 3 ; // Nack or error - } + if (result == 0) { + txBuffer.clear(); } - - if (stopBit) - { - sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); - } - return 0; + return result; } uint8_t TwoWire::endTransmission() From 7d4bd13c07e0662248be33fee930a743694410af Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 22 Nov 2017 16:00:33 -0500 Subject: [PATCH 3/3] Rename sentTo -> sendTo --- libraries/Wire/Wire.cpp | 8 ++++---- libraries/Wire/Wire.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 67cb1847e..e7f3c7108 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -62,12 +62,12 @@ void TwoWire::end() { sercom->disableWIRE(); } -uint8_t TwoWire::sentTo(uint8_t address, uint8_t buffer[], size_t quantity) +uint8_t TwoWire::sendTo(uint8_t address, uint8_t buffer[], size_t quantity) { - return sentTo(address, buffer, quantity, true); + return sendTo(address, buffer, quantity, true); } -uint8_t TwoWire::sentTo(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit) +uint8_t TwoWire::sendTo(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit) { // Start I2C transmission if (!sercom->startTransmissionWIRE(address, WIRE_WRITE_FLAG)) @@ -169,7 +169,7 @@ uint8_t TwoWire::endTransmission(bool stopBit) { transmissionBegun = false ; - uint8_t result = sentTo(txAddress, txBuffer._aucBuffer, txBuffer.available(), stopBit); + uint8_t result = sendTo(txAddress, txBuffer._aucBuffer, txBuffer.available(), stopBit); if (result == 0) { txBuffer.clear(); diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index 54378602b..6dc26553d 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -42,8 +42,8 @@ class TwoWire : public Stream uint8_t endTransmission(bool stopBit); uint8_t endTransmission(void); - uint8_t sentTo(uint8_t address, uint8_t buffer[], size_t quantity); - uint8_t sentTo(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit); + uint8_t sendTo(uint8_t address, uint8_t buffer[], size_t quantity); + uint8_t sendTo(uint8_t address, uint8_t buffer[], size_t quantity, bool stopBit); uint8_t requestFrom(uint8_t address, size_t quantity, bool stopBit); uint8_t requestFrom(uint8_t address, size_t quantity);