Skip to content

Commit f341755

Browse files
committed
Wire: fix bug on read() backing storage
The ringbuffer backing storage and the read buffer were the same buffer... memcpy over the same location has some nasty implications TODO: move to more complex APIs (like i2c_transfer) to properly implement stop bit
1 parent a3fa5b8 commit f341755

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

libraries/Wire/Wire.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ uint8_t arduino::ZephyrI2C::endTransmission(void) { // TODO for ADS1115
6363

6464
size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len,
6565
bool stopBit) {
66-
int ret = i2c_read(i2c_dev, rxRingBuffer.buffer, len, address);
66+
uint8_t buf[len];
67+
int ret = i2c_read(i2c_dev, buf, len, address);
6768
if (ret != 0)
6869
{
6970
return 0;
7071
}
71-
ret = ring_buf_put(&rxRingBuffer.rb, rxRingBuffer.buffer, len);
72+
ret = ring_buf_put(&rxRingBuffer.rb, buf, len);
7273
if (ret == 0)
7374
{
7475
return 0;
@@ -96,19 +97,19 @@ size_t arduino::ZephyrI2C::write(const uint8_t *buffer, size_t size) {
9697

9798
int arduino::ZephyrI2C::read() {
9899
uint8_t buf[1];
99-
if (ring_buf_peek(&rxRingBuffer.rb, buf, 1) > 0) {
100+
if (ring_buf_size_get(&rxRingBuffer.rb)) {
100101
int ret = ring_buf_get(&rxRingBuffer.rb, buf, 1);
101102
if (ret == 0) {
102-
return 0;
103+
return -1;
103104
}
104105
return (int)buf[0];
105106
}
106-
return EXIT_FAILURE;
107+
return -1;
107108
}
108109

109-
int arduino::ZephyrI2C::available() { // TODO for ADS1115
110-
return ring_buf_size_get(&rxRingBuffer.rb); // ret 0 if empty
111-
}
110+
int arduino::ZephyrI2C::available() {
111+
return ring_buf_size_get(&rxRingBuffer.rb);
112+
}
112113

113114
int arduino::ZephyrI2C::peek() {
114115
uint8_t buf[1];

0 commit comments

Comments
 (0)