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

Usage of Arduino Wire library #2

Open
Koepel opened this issue Feb 20, 2022 · 1 comment
Open

Usage of Arduino Wire library #2

Koepel opened this issue Feb 20, 2022 · 1 comment
Assignees
Labels

Comments

@Koepel
Copy link

Koepel commented Feb 20, 2022

In many files where Wire.requestFrom() is used, I see the comment: "/* blocking call - at least one byte must be available */"
That is not how the Arduino Wire library works.

The Wire.requestFrom() does the whole I2C session and that function itself is blocking. It only returns when the I2C session has completely finished.
After that, the received data is in a buffer in the Wire library. The Wire.read() and Wire.available() operate on that buffer.

So there is no need to wait for something. The data is in the buffer, or not.

Example to check if there was data:

  int n = Wire.requestFrom(0x6f, length);// request length bytes from slave device and end the transmission after this
  if(n==(int)length)    // test if the data was received
  {
    for(i=0;i<length;i++)
    {
      buffer[i] = Wire.read();  // the data is there for sure, because that was tested
    }
  }
  else
  {
    // return some kind of error ?
  }

After a Wire.requestFrom(), there is no need to wait for something. I call that common mistake number 1.
See also my alternative explanation of the Wire library.

@CJoh-NHD CJoh-NHD self-assigned this Feb 22, 2022
@CJoh-NHD CJoh-NHD added the bug label Feb 22, 2022
@CJoh-NHD
Copy link
Contributor

Koepel,

Thank you for bringing this to our attention and the in-depth explanation.
The example programs seen in this repository were written by FTDI/Bridgetek. Therefore, fixing this bug in all the example sketches is time-consuming and low-priority at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants