Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

Commit 32d5dda

Browse files
committed
Wire: added missing error returns; replaced RingBuffer with array for TX
- Added the missing returns values where needed. - Replaced RingBuffer with a plain array for txBuffer: Since Wire.beginTransmission(..) always clears the tx buffer there is no possibility to wrap around, so there is no point in using a ring buffer. - This also remove a now useless buffer copy in Wire.endTransmission().
1 parent bd45947 commit 32d5dda

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

libraries/Wire/Wire.cpp

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "Wire.h"
22

3-
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(sda), _scl(scl) {}
3+
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}
44

55
void arduino::MbedI2C::begin() {
66
master = new mbed::I2C((PinName)_sda, (PinName)_scl);
@@ -37,16 +37,12 @@ void arduino::MbedI2C::setClock(uint32_t freq) {
3737

3838
void arduino::MbedI2C::beginTransmission(uint8_t address) {
3939
_address = address << 1;
40-
txBuffer.clear();
40+
usedTxBuffer = 0;
4141
}
4242

4343
uint8_t arduino::MbedI2C::endTransmission(bool stopBit) {
44-
char buf[256];
45-
int len = txBuffer.available();
46-
for (int i=0; i<len; i++) {
47-
buf[i] = txBuffer.read_char();
48-
}
49-
master->write(_address, buf, len, !stopBit);
44+
if (master->write(_address, (const char *) txBuffer, usedTxBuffer, !stopBit) == 0) return 0;
45+
return 2;
5046
}
5147

5248
uint8_t arduino::MbedI2C::endTransmission(void) {
@@ -70,13 +66,16 @@ uint8_t arduino::MbedI2C::requestFrom(uint8_t address, size_t len) {
7066
}
7167

7268
size_t arduino::MbedI2C::write(uint8_t data) {
73-
txBuffer.store_char(data);
69+
if (usedTxBuffer == 256) return 0;
70+
txBuffer[usedTxBuffer++] = data;
71+
return 1;
7472
}
7573

7674
size_t arduino::MbedI2C::write(uint8_t* data, int len) {
77-
for (int i=0; i<len; i++) {
78-
write(data[i]);
79-
}
75+
if (usedTxBuffer + len > 256) len = 256 - usedTxBuffer;
76+
memcpy(txBuffer + usedTxBuffer, data, len);
77+
usedTxBuffer += len;
78+
return len;
8079
}
8180

8281
int arduino::MbedI2C::read() {

libraries/Wire/Wire.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class MbedI2C : public HardwareI2C
6161
int _scl;
6262
int _address;
6363
RingBufferN<256> rxBuffer;
64-
RingBufferN<256> txBuffer;
64+
uint8_t txBuffer[256];
65+
uint32_t usedTxBuffer;
6566
};
6667

6768
}

0 commit comments

Comments
 (0)