i've build a master-slave combination of two Atmega 328P - So far its not a big problem, but actually i have a strange issue while using I2C for communication.
The communication needs to be stateless and independent, so i do this without using requestFrom - I just use beginTransmission + write + endTransmission for sending a command to the slave and the slave processes this command and sends a response back with beginTransmission + write + endTransmission. Generally it seems to work - but i've discovered that after every write on slave side a reset of slave is being made. My debug protocol of slave looks like:
So after every send on slave side the slave 328P performs a reset. I've checked the circuit and cannot discover any connection to the reset line, so i think the reset results from I2C - Is this possible / a known issue?
The communication needs to be stateless and independent, so i do this without using requestFrom - I just use beginTransmission + write + endTransmission for sending a command to the slave and the slave processes this command and sends a response back with beginTransmission + write + endTransmission.
That is not how I2C works, I am not supprised it crashes, you are probbly blowing a stack.
@PaulS: Thank you for your answer - I've figured the problem out, its what @Grumpy_Mike means ...
The reset occurs when the slave calls Wire.write method after calling Wire.beginTransmission(MASTER_ADDRESS). So i rebuild my code to have the "normal"/correct i2c workflow - only the master sends data via begin transmission and the slave answers if Wire.requestFrom is called. So everything works fine - Sadly its not so nice stateless and independet as originally wanted - but it don't crashes anymore.
Thank you very much for your supprt, @PaulS and @Grumpy_Mike.
Well.. multi-master communication like you described should work.
Your error was somewhere else in the code and you fixed it by accident when changing the way you send/request data.
I bet he had a cycle-intensive process on the twi_attachSlaveRxEvent() callback..
And it gets even better if you start doing a lot of Serial.print() inside the callback..
Wire lib was not designed with multi-master environment in mind but you can live with it and still do two way communication.
but you can live with it and still do two way communication.
No, there is no collision detection in the wire library, if one system starts to transmit when the other is transmitting then the whole thing ends up in a big wobbly heap.