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 :)

asked Jun 16, 2018 at 13:29

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.

answered Jun 16, 2018 at 13:42
3
  • 1
    other thing is if you do setFrequency(400000L) and then some other library calls Wire.begin() 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 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. Commented Jun 20, 2018 at 10:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.