Split software I2C code into Master-only and Master-or-Slave parts, enabling multiple instances of TwoWire in master-only role #7911
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Follow-up to the structural change to I2C core support from #6326
What I did is create class hierarchy, where TwiMaster (a subset of the original Twi class in) in "core" does not have any static members, callbacks or IRS, and is exposed in the twi.h header file (in C++ context only).
TwiMasterOrSlave derives from it and adds all the remaining functionality of the original Twi class.
The C interface (declared in twi.h) is rerouted from Twi singleton to TwiMasterOrSlave singleton.
Then, I modified the Wire library in similar manner, but extra measures had to be taken to guarantee compatibility with other Arduino platforms. The default mode of TwoWire class can be switched to master-only via TWOWIRE_MASTER_ONLY define - this implementation does not depend on any static/singleton variables, and also allocates the buffers dynamically (allowing for fine-tuning of necessary buffer size for each I2C interface).
Note: there is already an issue #7894 asking to support multiple I2C instances (with a kinda crude but probably working solution provided already - turning all the static dependencies into 5-tuples so that up-to 5 master/slave I2C interfaces can be used at a time).