Skip to content

Commit f1c4cc6

Browse files
stickbreakerfacchinm
authored andcommitted
UnConfigured I2C Slave ISR Causing Reboot
In a MultiMaster I2C environment, The Default value of 0xFE in the TWAR cause the Arduino to respond as an I2C Slave device at address 0x7f. If the Wire.h library was not configured as a I2C Slave, `Wire.begin(slaveID);` the Callbacks for `twi_onSlaveTransmit()` and `twi_onSlaveReceive()` are never initialized. But, they are called during servicing the TWI ISR. This causes a reboot of the Arduino by jumping to an uninitialized function address (0). So, this fix initializes them to the Default Wire.h handler which will respond correctly even during Master Mode operations. A MASTER MODE only Arduino will respond to all Slave Calls that match TWAR, Unless the TWEA bit is disabled outside of Master Transactions. Chuck. It also initialized the TWAR to the General Call ID (0x0) and Disables General Call responses. Chuck.
1 parent 7d4bca5 commit f1c4cc6

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Diff for: libraries/Wire/src/Wire.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
20+
Modified 2017 by Chuck Todd ([email protected]) to correct Unconfigured Slave Mode reboot
2021
*/
2122

2223
extern "C" {
@@ -60,14 +61,14 @@ void TwoWire::begin(void)
6061
txBufferLength = 0;
6162

6263
twi_init();
64+
twi_attachSlaveTxEvent(onRequestService); // default callback must exist
65+
twi_attachSlaveRxEvent(onReceiveService); // default callback must exist
6366
}
6467

6568
void TwoWire::begin(uint8_t address)
6669
{
67-
twi_setAddress(address);
68-
twi_attachSlaveTxEvent(onRequestService);
69-
twi_attachSlaveRxEvent(onReceiveService);
7070
begin();
71+
twi_setAddress(address);
7172
}
7273

7374
void TwoWire::begin(int address)

0 commit comments

Comments
 (0)