Skip to content

Wire.h not able to change buffer length from .ino file #6967

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
1 task done
dharmik768 opened this issue Jul 11, 2022 · 16 comments · Fixed by #7016
Closed
1 task done

Wire.h not able to change buffer length from .ino file #6967

dharmik768 opened this issue Jul 11, 2022 · 16 comments · Fixed by #7016
Assignees
Labels
Area: Libraries Issue is related to Library support. Status: To be implemented Selected for Development Type: Feature request Feature request for Arduino ESP32
Milestone

Comments

@dharmik768
Copy link

Board

ESP32 DEV Module

Device Description

Using generic ESP32 dev board

Hardware Configuration

non

Version

v2.0.3

IDE Name

Arduino IDE

Operating System

Windows 11

Flash frequency

80

PSRAM enabled

no

Upload speed

921600

Description

I am using a vl53l3cx TOF sensor with an ESP32 board.

I am using this library and its example for testing and it works perfectly but I need to change inside the ESP32 package to make it work. To be specific inside the Wire.h file of the ESP32 package.

Initially, the example was not working directly but I read somewhere in the form that it needs to increase the I2C buffer size as the received packet size from the sensor is big therefore default size will not work.

#define I2C_BUFFER_LENGTH 128

First I tested it by changing the I2C_BUFFER_LENGTH to 256 directly in the Wire.h file and the example code is working perfectly. This test was done in the old ESP32 package release V1.0.6

But In the latest releases that I can see after V2.0.3 in Wire.h file it's defined like this

#ifndef I2C_BUFFER_LENGTH
#define I2C_BUFFER_LENGTH 128
#endif

So I thought I can increase the size of the buffer externally directly from the Arduino .ino file itself like this before including the Wire.h

#define I2C_BUFFER_LENGTH 256
#include<Wire.h>

But on uploading in this way the sensor data is not read properly like previously, So I checked by printing the I2C_BUFFER_LENGTH in Arduino code as well as inside the Wire.begin(); library function.
And shockingly it printed 256 in the Arduino code and when begin to function is called it printed 128.
So it seems like it not making any change to the I2C_BUFFER_LENGTH even it's defined before calling the library.

Is there any way to change the I2C_BUFFER_LENGTH without making any modifications to the ESP32 package itself?

As I think the Arduino compile is somehow compiling all ESP32 packagess before even we include them. As I have done the same test with the blank sketch with only Wire.h library included and I2C_BUFFER_LENGTH same results were seen.

Thank you,
Dharmik Patel

Sketch

#define I2C_BUFFER_LENGTH 256
#include<Wire.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(2000);
  Serial.println(I2C_BUFFER_LENGTH);  // here it will print 256 that what we need 
  Wire.begin();  // but in actually its not changed here it will print 128
}

void loop() {
  // put your main code here, to run repeatedly:

}

Debug Message

None

Other Steps to Reproduce

To debug properly add this line inside your Wire.cpp file function

bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)

add this inside that

Serial.println(I2C_BUFFER_LENGTH);

Finally, it will look like this

// Master Begin
bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
{
Serial.println(I2C_BUFFER_LENGTH);

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@dharmik768 dharmik768 added the Status: Awaiting triage Issue is waiting for triage label Jul 11, 2022
@dharmik768 dharmik768 changed the title Wire.h buffer able to change buffer from .ino file Wire.h not able to change buffer length from .ino file Jul 11, 2022
@SuGlider
Copy link
Collaborator

@dharmik768
I have tested it with Arduino Core 2.0.3 and the output is 256 / 128.
But when I test it with Arduino Core 1.0.6 the output is 128 / 128.

If it works for 1.0.6, the buffer size used in Arduino is always 128.

There is no way to change it to any other size just using a simple #define I2C_BUFFER_LENGTH size
This #define only works inside the Sketch.
The library is compiled separately in another step and doesn't use any Arduino Sketch #define value.

Which makes me think that the size of the buffer may not be the problem for the vl53l3cx TOF sensor

IF you want to test with a different Buffer Size, please try it by changing the value directly in Wire.h file.
at https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.h#L38

@SuGlider SuGlider added Type: Question Only question and removed Status: Awaiting triage Issue is waiting for triage labels Jul 13, 2022
@dharmik768
Copy link
Author

I have already tested successfully by changing the buffer length directly in the Wire.h and its works perfectly.

I thought it would be more simple for the end user if they do not need to go and change buffer sizes in their ESP32 packages. Anyway, if there is no way around the possible solution I was going to try out to make a copy of the Wire.h file and change it as per my need and add it locally with my custom library package such that the code used my Wire.h instead of the default one.

@SuGlider
Copy link
Collaborator

I see. In that case we may need to create some interface to fix it.
I'll add a draft PR that addresses this issue.

@SuGlider SuGlider added Type: Feature request Feature request for Arduino ESP32 Status: To be implemented Selected for Development Area: Libraries Issue is related to Library support. and removed Type: Question Only question labels Jul 14, 2022
@SuGlider SuGlider self-assigned this Jul 14, 2022
@SuGlider SuGlider added this to the 2.0.5 milestone Jul 14, 2022
@SuGlider
Copy link
Collaborator

@dharmik768 - Interesting... I just found this saying that it is necessary to change the buffer size directly in the Wire header file:
https://support.arduino.cc/hc/en-us/articles/4406686928786-Modify-the-buffer-size-of-the-Wire-library

@SuGlider SuGlider moved this from Todo to Under investigation in Arduino ESP32 Core Project Roadmap Jul 17, 2022
@dharmik768
Copy link
Author

Yes it is required in some cases as I mentioned the TOF sensor without its not working may be a TOF library side issue but the majority of the library part API is suggested by ST itself.

So it would be very handy if we can directly manage the buffer size in the code itself when needed without modifying the package files.

Repository owner moved this from Under investigation to Done in Arduino ESP32 Core Project Roadmap Jul 19, 2022
@dharmik768 dharmik768 reopened this Jul 19, 2022
@dharmik768
Copy link
Author

Closed by mistake,
Today I tried to add a copy of Wire.h library with modified buffer size to my custom library, but the Arduino IDE somehow still first compiles the package Wire.h file, and then it shows a compilation error as it founds multiple functions so this way is also tricky to accomplish.

@SuGlider
Copy link
Collaborator

I'm currently working on a Wire::setBufferSize().

I will submit soon a PR.

@dharmik768
Copy link
Author

Let me know if you need any helping hand in that, I can test it out.

@SuGlider
Copy link
Collaborator

@dharmik768 - Please test #7016 and let me know. Thanks!

@VojtechBartoska VojtechBartoska moved this from In Progress to In Review in Arduino ESP32 Core Project Roadmap Jul 27, 2022
Repository owner moved this from In Review to Done in Arduino ESP32 Core Project Roadmap Jul 28, 2022
@SuGlider
Copy link
Collaborator

PR #7016 merged.
@dharmik768 - you can try it using Arduino Core cloned from this repository.

@dharmik768
Copy link
Author

Hello @SuGlider,

Sorry for the late response today I got time to test the new modified wire library these are the steps I follow.
I extracted the new modified wire library from the master branch and added that to my ESP32 library package path and removed the old one.

Now I uploaded the test code for the sensor that I have without changing the buffer size, and a strange thing happened without changing the buffer also the code is working perfectly and I am able to read sensor data.

So apart from the new buffer size change function in the library have you changed anything in the older wire library same code is not working without changing the buffer size.

Anyways thank you for adding that function.

@dharmik768
Copy link
Author

Do you think this is happening because in my library.h file I have added this line
#define I2C_BUFFER_LENGTH 256

Will this make changes in the buffer directly without using that function as previously it was not changing, let me comment it out and check it once.

@dharmik768
Copy link
Author

I commented and tested it is still working.

@SuGlider
Copy link
Collaborator

SuGlider commented Aug 8, 2022

Do you think this is happening because in my library.h file I have added this line #define I2C_BUFFER_LENGTH 256

Will this make changes in the buffer directly without using that function as previously it was not changing, let me comment it out and check it once.

Yes, this is used as default size when none is set using setBufferSize() when begin() is executed.
Default size is 128 https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.h#L43
It is used here https://github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.cpp#L41

Make sure you don't have more than one Wire Library in use...

@peteDDD
Copy link

peteDDD commented Aug 28, 2024

As of today... August 28, 2024,
Wire.setBufferSize(255);
Does not change the buffer size. (but keep reading... there is an easy solution...)

Whereas editing the Wire.h as follows does work. (but this "solution" is not acceptable since an update to the library would break that.... (but keep reading... there is an easy solution...)

#ifndef I2C_BUFFER_LENGTH
    #define I2C_BUFFER_LENGTH 255//128  // Default size, if none is set using Wire::setBuffersize(size_t)
#endif

Here is test code... main.cpp
`
#include <Arduino.h>
#include <Wire.h>

#define mySDA 6
#define mySCL 5
#define I2C_FREQUENCY 400000

void setup(void)
{
Serial.begin(115200);
Wire.begin(mySDA, mySCL, I2C_FREQUENCY);
Wire.setBufferSize(255);
}

void loop(void)
{}

`

So, here is a solution that works...
in platformio.ini under build_flags= add the following (to set the buffer size to 255, for example
-DI2C_BUFFER_LENGTH=255

@me-no-dev
Copy link
Member

@peteDDD call Wire.setBufferSize(255); BEFORE Wire.begin(mySDA, mySCL, I2C_FREQUENCY);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Libraries Issue is related to Library support. Status: To be implemented Selected for Development Type: Feature request Feature request for Arduino ESP32
Projects
Development

Successfully merging a pull request may close this issue.

4 participants