Device discovery on serial bus

Hi, everyone. 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 MCUs inside). They communicate with some custom protocol based on serial wire. TX outputs of slave devices connected like this:
All those slave devices have IDs that unique for one bus, so master device can address each of them. Currently I am forced to hardcode those IDs into each slave device. My goal is to flash all those devices with same firmware, without hardcoding any data. So master should automatically register them or something for my idea to be possible. How can I modify or extend my protocol to achieve such functionality? Any ideas, guys?

They communicate with some custom protocol based on serial wire

extend the protocol to include address discovery

That would not solve the problem: slave device still should have unique identifiers to be discovered by master. Or you mean something else? Can you specify or give me any example?

Power up one slave at a time, query the master for an ID and save to EEPROM.

Hang on...

They communicate with some custom protocol

My goal is to flash all those devices with same firmware

So is this a serial bus that has to inter-operate with other poorly-specified equipment or are you writing an Arduino program for every single thing on the bus? If you are in control of every item then it's your custom protocol. You do what you want.

The easy way for different slaves to have the same firmware and different addresses is to store the address in the slave's EEPROM. Write a small sketch that saves an address to the EEPROM. Load that onto the slave. Let it run once and save that address. Then load the main sketch which reads the EEPROM. That way your main sketch can't accidentally reprogram the EEPROM.

Or just read the section of the datasheet for the USART!

Mark

Just an idea.

Start with master, no slaves on the bus.

Master sends a request to devices with e.g. ID 0
Add a slave on the bus; if it has an ID of 0, it will respond and master can assign ID to slave with ID 0; slave stores ID in eeprom (as indicated before) and will obviously no longer respond on successive request for ID 0.
Add next slave and repeat.

To determine a free ID, master either keeps track of assigned IDs or can send request over the bus for all valid IDs; if a reply is received, ID is in use and can't be assigned.

Possible problem; if a slave is moved from one bus to another, you can end with a situation with duplicate IDs. There are possible solutions for that. I don't know if this can happen in your situation.

Not knowing your exact setup, let's assume that you have two masters (with related buses). Each master will have a unique bus ID. This bus ID can be part of the protocol. If you connect a slave with an existing slave ID to the wrong bus, the slave will clear its slave ID (set to 0), store the new bus ID and next respond to a request for slave ID 0.

MorganS:
So is this a serial bus that has to inter-operate with other poorly-specified equipment or are you writing an Arduino program for every single thing on the bus? If you are in control of every item then it's your custom protocol. You do what you want.

Actually I do write sketches for both devices: master and slave. The main idea behind modification I want to do is make master able to enumerate devices on the bus.

sterretje:
Just an idea.

Start with master, no slaves on the bus.

Master sends a request to devices with e.g. ID 0
Add a slave on the bus; if it has an ID of 0, it will respond and master can assign ID to slave with ID 0; slave stores ID in eeprom (as indicated before) and will obviously no longer respond on successive request for ID 0.
Add next slave and repeat.

There is one problem: slaves must be consequently turned on then. If they have initial IDs set to 0, they will simultaneously respond to masters queries and collisions may occur.

As I said, add a slave to the bus, meaning add one slave to the bus.

Possibly you can use some arvitration as I2C does - as long as the slaves are sending the same data they have the same address. If one loose arbitration it switches to another address.

Or you may use daisy chain instead of bus.