I'm struggling to find a proper specification of the I2C receive behaviour using the Wire library. I have an Arduino configured as an I2C slave and I've set up a message handler using Wire.onReceive(OnReceiveEvent);
The question is: What exactly are the restrictions inside the message handler?
From what I've found so far:
-
The handler is called from an interrupt handler (ISR).
-
Use of delay(int) is disallowed because that uses interrupts and these will be disabled inside the handler.
I've defined a message protocol which uses variable-length messages. I assume if the message were all the same length, I could check Wire.available() inside the handler and return if the message was incomplete.However, with variable-length messages I need to read the first byte(s) in order to work out the message size.
What isn't clear is whether the Wire library continues to process I2C input when the app is inside the handler. In particular, can Wire.available() be used inside the handler and will it actually work? For example, if the handler is called after the first byte is received, can I poll Wire.available() until the remaining bytes are in the I2C buffer?
I've found comments online that suggest that 'available() can be used but you shouldn't use it'. That's not super helpful when you're trying to understand how the system works.
6 posts - 5 participants