Skip to content

Commit 9e134ee

Browse files
JZimnolDhruvaG2000
authored andcommitted
Wire: don't use rxRingBuffer.buffer directly for i2c_read() calls
Removed a bug from requestFrom() function that caused ring buffer corruption by using rxRingBuffer.buffer directly in i2c_read() calls. Signed-off-by: Jakub Zimnol <zimnol.jakub@gmail.com>
1 parent fe7c9b5 commit 9e134ee

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

libraries/Wire/Wire.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@ uint8_t arduino::ZephyrI2C::endTransmission(void) { // TODO for ADS1115
4242

4343
size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len,
4444
bool stopBit) {
45-
int ret = i2c_read(i2c_dev, rxRingBuffer.buffer, len, address);
45+
/* Use stack-allocated buffer for reading */
46+
uint8_t readbuff[sizeof(rxRingBuffer.buffer)];
47+
if (len > sizeof(readbuff)) {
48+
return 0;
49+
}
50+
51+
int ret = i2c_read(i2c_dev, readbuff, len, address);
4652
if (ret != 0)
4753
{
48-
printk("\n\nERR: i2c burst read fails\n\n\n");
4954
return 0;
5055
}
5156

5257
/* Flush the receive buffer so another read() call returns the correct data */
5358
flush();
54-
ret = ring_buf_put(&rxRingBuffer.rb, rxRingBuffer.buffer, len);
59+
ret = ring_buf_put(&rxRingBuffer.rb, readbuff, len);
5560
if (ret == 0)
5661
{
57-
printk("\n\nERR: buff put fails\n\n\n");
5862
return 0;
5963
}
6064
return len;

0 commit comments

Comments
 (0)