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

I2C Slave Implementation #5746

Merged
merged 4 commits into from
Oct 14, 2021
Merged

I2C Slave Implementation #5746

merged 4 commits into from
Oct 14, 2021

Conversation

me-no-dev
Copy link
Member

@me-no-dev me-no-dev commented Oct 9, 2021

This pull request adds support for I2C Slave to the ESP32 Arduino Framework

Fixes: #118

- Support Wire::end() for Slave
- Prevent Master operations when in Slave mode
@@ -70,6 +77,7 @@ class TwoWire: public Stream
bool setPins(int sda, int scl);

bool begin(int sda=-1, int scl=-1, uint32_t frequency=0); // returns true, if successful init of i2c bus
bool begin(uint8_t slaveAddr, int sda=-1, int scl=-1, uint32_t frequency=0);
Copy link
Contributor

@platypii platypii May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR added an overload for Wire.begin which is now causing errors downstream in Heltec's BMP180.cpp.

.pio/libdeps/heltec_wifi_lora_32_V2/Heltec ESP32 Dev-Boards/src/BMP180.cpp:13:29: error: call of overloaded 'begin(int&, int, int)' is ambiguous
   Wire.begin(sda, 12, 100000);
                             ^
In file included from .pio/libdeps/heltec_wifi_lora_32_V2/Heltec ESP32 Dev-Boards/src/BMP180.h:5,
                 from .pio/libdeps/heltec_wifi_lora_32_V2/Heltec ESP32 Dev-Boards/src/BMP180.cpp:1:
.platformio/packages/framework-arduinoespressif32/libraries/Wire/src/Wire.h:79:10: note: candidate: 'bool TwoWire::begin(int, int, uint32_t)'
     bool begin(int sda=-1, int scl=-1, uint32_t frequency=0); // returns true, if successful init of i2c bus
          ^~~~~
.platformio/packages/framework-arduinoespressif32/libraries/Wire/src/Wire.h:80:10: note: candidate: 'bool TwoWire::begin(uint8_t, int, int, uint32_t)'
     bool begin(uint8_t slaveAddr, int sda=-1, int scl=-1, uint32_t frequency=0);
          ^~~~~

I plan to file an issue and a PR in that repo, but the problem is I can't actually figure out how to disambiguate between these two overloaded constructors. How do I even tell C++ which to prefer?

It seems like it would have been better to add the uint8_t slaveAddr argument to the END of the Wire.begin function, to avoid breaking lots of code in the wild?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the easiest way is to cast the arguments, like Wire.begin((int)SDA, (int)SCL);, but the best would be to use:

Wire.setPins(SDA, SCL);
Wire.begin();
Wire.setClock(frequency); // if necessary

I intend to look into this and see if I can find a better way to resolve it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into it. Actually though, casting the first argument to (int) doesn't work. GCC still thinks the int and uint8_t are ambiguous. The only way I was able to get it to compile was to cast the third argument to unsigned:

-  Wire.begin(13, 12, 100000);
+  Wire.begin(13, 12, 100000U);

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

Successfully merging this pull request may close these issues.

I2C Slave not implemented
3 participants