Hi All,
I have a weird question on I2C. I have to develop an I2C slave module to join an existing I2C network and it is required that the new module will have to register its address uniquely and automatically. Can it work like this:
I2C module (MCU_s) is plugged in existing I2C network and power on;
MCU_s firstly act as a master and search for existing slave devices on the network,
2.1 Found: the last highest address of 5 (as an example), it will create an address of 6 and store to EEPROM, MCU_s conduct power reset and start to act as slave from now on with the address it assigned;
2.2 Not found: it assigns itself with 1 (starting address), store in EEPROM and recycle its power and start acting as slave with this 1 address.
The MCU_s has attached with a button (INPUT) and an OUTPUT. It will:
3.1 Accepts INPUT from the Button for controlling the OUTPUT and notifies the OUTPUT state to the Master MCU when OUTPUT state changed due to Button action;
3.2 Accept Controlling signal via I2C from the master MCU to control state of the OUTPUT.
Thank you very much for your attention and your supports!
Ngoc
The I2C bus is not hot-swappable.
When something is connected to the I2C bus with no power, then the I2C bus does not work.
When a new module becomes a Master, there could be a collision with the real Master.
Storing something in EEPROM is okay, but recycling power after that is not okay. Solve it in software without resetting.
How can you tell if the EEPROM contains nothing or a I2C address ? How can you tell if that I2C address is from an other network ?
How can a Slave tell the Master something ?
How does a Master know that a new module was added ?
I prefer that the Master controls everything. That means I prefer that the Master assigns an address to a Slave.
The Master could send a broadcast with the first free I2C address as its data twice a second and check that free I2C address also twice a second.
In the broadcast should also be an identifier for the network. So a Slave knows when it is connected to a new network with a new Master and should start to use the new free I2C address.
But in the end, there is always one way or an other that anything you can think of will go wrong. It is not simple, and the more code is used, the more bugs can be in it.