Electrical – MCU to MCU I2C communication

i2cmicrocontrollerserial-bussharedbusslave

I have 2 MCUs and I want to connect them together over I2C bus. MCU1 will be Master while MCU2 will be Slave. Only these 2 MCUs are on the bus. This configuration is fixed and I cannot change it.

There are 2 scenarios. In first scenario only MCU1 and MCU2 are on the bus. While in a second scenario there are some additional I2C devices also connected on this shared bus.

My main purpose to connect them as above is to send data from MCU2 (Slave) to MCU1 (Master).

So far what i understand about I2C is that it is a command-response protocol. The master gives command to slaves, and then the slaves respond to this command and send their data to the master. The slaves cannot send their data on their own without receiving the command first.

If the master wants to receive 32 bytes of data from the slave then what command will it send to the slave and then how will the slave assert that it has received the valid command for data send? I think this is called 'block-mode' data transfer.

My slave address is fixed as 0x08.

Best Answer

Fortunately or unfortunately, it's up to you to come up with a protocol for this scenario as you control both sides. Like you say, the slave can only respond to transfers initiated by the master. Your master then needs to poll the slave (ask for data on a recurring schedule), unless you have another way for the slave to communicate when data is ready. (The latter is commonly solved with a separate interrupt line.) The master and slave need to both agree on a special response when the slave doesn't yet have data available. The slave also needs to reset its response to this "no data" value after being queried by the master, to avoid sending the same data twice.

The easier way would be to switch the roles of master and slave, so it's the master that initiates sending data to the slave.

If you need to also communicate with other slaves on the same I2C bus, you will however be limited in which device can be the master, unless you use the multi-master mode of I2C.

The particular I2C API calls required depends on what device and HAL you are using. On a physical level, it will be a "I2C read" initiated by the master, where the master reads 32 bytes after sending the slave address.

Related Topic