Electronic – Device discovery on simple bus

protocolserialserial-bus

I am currently facing following problem: there is a system, which consist of one master device and some number of slave devices (all those devices have tiny MCUs). They communicate with some custom protocol based on serial wire (looks pretty similar to Modbus).
All slave devices must have unique IDs within one bus, so master device can address each of those slaves. Currently I am forced to hardcode those IDs into each slave device. My goal is to flash all those devices with same firmware, so no hardcoded IDs should be used for this to be possible. I am thinking of master device, which is able to assign unique adresses to all those devices, but cant figure out, how to do that. Can anyone give me some idea or concept how to extend my communication protocol for this to be possible? Maybe someone can give me an example?

Best Answer

To do this sort of dynamic addressing on a common bus you ultimately need to have one of two things:

  • a very small address space so you can brute-force search it efficiently
  • some kind of collision detection/management system

If you don't have either of these two things you will need to hard code addresses into the master and slave devices.

The reason for this is that if you have an address space (of size N) that is too large to search efficiently by stepping through every N address, then any optimised search algorithm that could find the address in less than N steps must include the possibility of more than one slave transmitting a response at the same time. If the physical layer cannot handle this situation, then unfortunately you are out of luck. If the physical layer can detect collisions, then there are many options, such as having a random retry delay when a collision occurs (Ethernet), or using something like CAN bus where collisions are managed in a cooperative way.