1

it's all in the title ; I'm developping a library and use *TwoWire pointers in this library ; but I am using an external library that calls Wire.begin(). I've seen something about TWCR register in arduino but I don't know what it is exaclty.

Thanks in advance :)

1 Answer 1

1

What you are interested in is this bit of the Wire library:

TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);

The important bit is the TWEN bit - TwoWire ENable.

If that bit is set in the TWCR register than Wire.begin() has been called already and you don't need to call it again:

if (TWCR & _BV(TWEN) == 0) {
    Wire.begin();
}

However, it really doesn't matter if you call it multiple times.

3
  • 1
    other thing is if you do setFrequency(400000L) and then some other library calls Wire.begin()
    – Juraj
    Commented Jun 16, 2018 at 13:49
  • Just to precise, it was to avoid such problems github.com/esp8266/Arduino/issues/2607 I don't know if it concerns me but I'll see with the prototype
    – Storca
    Commented Jun 20, 2018 at 10:02
  • @Storca Note that that's for a completely different architecture, so the "fix" I detail above has no relevance to it. The proper place for it to be fixed would be in the Wire library. We do it properly chipKIT: github.com/chipKIT32/chipKIT-core/blob/master/pic32/libraries/… - only allow begin to run the first time it's called.
    – Majenko
    Commented Jun 20, 2018 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.