Skip to content

I2C slave failed #212

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

Closed
larsstenberg opened this issue Jan 30, 2018 · 2 comments
Closed

I2C slave failed #212

larsstenberg opened this issue Jan 30, 2018 · 2 comments
Labels
bug 🐛 Something isn't working

Comments

@larsstenberg
Copy link

larsstenberg commented Jan 30, 2018

BOARD: NUCLEO-F091RC

Connected a NanoPi Neo Air (master) with the nucleo when running i2cset-y 0 0x09 on the NPI the nucleo hangs. Serial stops, blinking stop. It just freezes.

Code running on the nucleo:

#include <Wire.h>

void setup()
{
  Wire.begin(0x15);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent);
  Serial.begin(115200);           // start serial for output
}

void loop()
{
  delay(100);
}

void receiveEvent(int howMany)
{
  Serial.println(howMany);
  while(1 < Wire.available()) // loop through all but the last
  {
    int c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.print(c);         // print the character
}

void requestEvent()
{
  Wire.write("hello");
}
  1. It seems to be a problem in the Wire.c when running as a slave the allocateRxBuffer never gets called. So first fix seems to be to add:
  // copy twi rx buffer into local read buffer
  // this enables new reads to happen in parallel
+  if(rxBuffer == nullptr){
+    allocateRxBuffer(BUFFER_LENGTH);
+  }
  memcpy(rxBuffer, inBytes, numBytes);

and changing allocateRxBuffer to static.

Now the freeze stopped.

  1. It also seems to be an error with the numBytes received from master.
    Changing line 541 in twi.c
    from: nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferCount;
    to: nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferSize;

Seems to correct the problem.

Am I doing something wrong or this a actual bug?

@fpistm fpistm added the bug 🐛 Something isn't working label Jan 30, 2018
@fpistm
Copy link
Member

fpistm commented Jan 30, 2018

Hi @larsstenberg
yes seems to be a bug :) thanks to report.

fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Feb 2, 2018
@fpistm
Copy link
Member

fpistm commented Feb 2, 2018

Hi @larsstenberg
I've tested Master/receiver Wire example and it is now ok with #216.
Could you review the patch, please?
Thanks in advance

fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Feb 2, 2018
@fpistm fpistm closed this as completed in 86e0bbc Feb 2, 2018
benwaffle pushed a commit to benwaffle/Arduino_Core_STM32 that referenced this issue Apr 10, 2019
benwaffle pushed a commit to benwaffle/Arduino_Core_STM32 that referenced this issue Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants