Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug #1354: I2C/TWI Library: Buffer Overflow #1363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

neophob
Copy link

@neophob neophob commented Apr 13, 2013

If we send i2c data larger than the arduino buffer (default: 32bytes) the arduino waits infinitely in a while loop (freeze). This fixes this.

@matthijskooijman
Copy link
Collaborator

I'm not sure if this is the proper fix for this issue, it might have side effects or leave different aspects of this issue unfixed. Now that I know this patch fixes the issue, I'm sure I've identified the cause of the issue and I'll have a closer look next week.

@neophob
Copy link
Author

neophob commented Apr 27, 2013

matthijs, do you have a proper fix for this issue?

@matthijskooijman
Copy link
Collaborator

I have been distracted by some fixes in the HardwareSerial driver, so I haven't actually worked on this one yet. It's still on my todolist, though, so I'll get back to you (probably not until next week though).

@neophob
Copy link
Author

neophob commented Jan 6, 2014

Any news about this issue?

@ffissore ffissore added New and removed New labels Feb 27, 2014
@cmaglie cmaglie added Type: Bug Library: Wire The Wire Arduino library Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) labels Apr 15, 2015
@netguy204
Copy link

I've encountered this bug as well and would love to see this merged.

@sandeepmistry
Copy link
Contributor

@cmaglie this looks good to merge to me. It will resolve https://github.com/arduino/Arduino/issues/486 as well.

I've tested with a Uno to Uno Wire setup by artificially lowering the slave RX buffer size:

diff --git a/hardware/arduino/avr/libraries/Wire/utility/twi.c b/hardware/arduino/avr/libraries/Wire/utility/twi.c
index b436e69..600103a 100644
--- a/hardware/arduino/avr/libraries/Wire/utility/twi.c
+++ b/hardware/arduino/avr/libraries/Wire/utility/twi.c
@@ -466,7 +466,7 @@ ISR(TWI_vect)
     case TW_SR_DATA_ACK:       // data received, returned ack
     case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
       // if there is still room in the rx buffer
-      if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
+      if(twi_rxBufferIndex < 10){
         // put byte in buffer and ack
         twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
         twi_reply(1);

and the following sketches:

Master (modified version of master_writer example):

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin(); // join i2c bus (address optional for master)
}

void loop() {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("fedcba9876543210");
  Serial.println(Wire.endTransmission(), HEX);    // stop transmitting

  delay(500);

  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("0123456789");
  Serial.println(Wire.endTransmission(), HEX);

  delay(500);
}

Slave (modified version of slave_receiver example):

#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  Serial.println(howMany);
  while (Wire.available()) {
    char c = Wire.read();
    Serial.print(c);
  }

  Serial.println();
}

Output of master sketch alternates between 0 and 3 (success and data send, NACK received).

@CLAassistant
Copy link

CLAassistant commented Apr 9, 2021

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) Component: Core Related to the code for the standard Arduino API Library: Wire The Wire Arduino library Type: Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants