3 ms·
> Yeah, the main issue with I2C for this sort of thing is device discovery. It doesn't have any mechanism for it so you would need to magically know your device
by toast0 3y ago
> Yeah, the main issue with I2C for this sort of thing is device discovery. It doesn't have any mechanism for it so you would need to magically know your device's address.
The typical address space is 7-bits, so you can just try a transaction to everyone... Or have some convention about what device number the display is?
- dmitrygr 3y agoProblem is: every i2c transaction is a READ or a WRITE. The spec is mum on the semantics of what that means. Some devices will get into a weird state or irrevocably pop data off a FIFO if you READ them (eg: some IMUs). Others take a WRITE, even with no data, to have a non-idempotent meaning (eg: SMBUS). Some devices will not even ACK their address on a READ request unless it is preceded by a write and a RESTART. Basically there is no safe way to scan an i2c bus and be sure you did not modify some state or put some device into some weird state.
- buescher 3y agoRight. But if you have a finite space of possible i2c devices it's not quite that bad. You can scan for known addresses and there are frequently mitigations for clogging FIFOs - like clocking out the bus or forcing a reset - that you might have to do anyway, depending on what your failure modes for power sequencing are. i2c is just fine when it works, but there are reasons why it can be worth spending the extra pins for SPI.
- dmitrygr 3y agoThe post I was replying to was suggesting scanning the bus for any kind of device. That is impossible. I could make a i2c device right now and not tell you how it works. You will have no way to scan for it safely (say I specify that unless the first access to it is a write of 0x55 0xAA, it’ll not talk again, but if it is, the byte sent after 0xAA becomes its i2c address) I have seen devices that respond to address zero, which is against the spec. Stopped nobody. I have seen devices that use the bus arbitration meant for multi-master during a READ to allow only one of them to win after responding to a zero-address read. I’ve used an i2c device that had an additional chip select line for some reason. Unless you lowered that, it would not respond to any traffic on the bus. You cannot even imagine the fucked up shit that happens over i2c There is no safe, generic way to scan an i2c bus. If the world of possible devices is limited, it may be possible. But there is no safe generic way.
- buescher 3y agoOh yes. I do not have to imagine. That's why I said "Right".
- utensil4778 3y agoI2C has no concept of a discovery or identification packet. The host can write any data to any address, and you have to already know in advance what address and data structure you need. The device can't tell you what its data format is. An I2C transaction begins with a client address, and the host waits for an acknowledge signal that a client has answered the message. That's it, that's the only standard part. From there, the host typically sends a register address and then some number of data bytes, or the client sends back some number of bytes. What those addresses and bytes mean are totally application-specific. There is no standard at all here. You could absolutely broadcast standard DDC commands to all addresses, but you have no way of knowing what actually happened. You might have turned off one monitor and set another to VGA mode. You might have just crashed some random device on the bus. You have no way to tell. All your host can possibly know is that there is a device at a given address, and it either did or did not send an acknowledge bit after a command. That's all the information available to you, and it is not enough to do what you suggest
- myself248 3y agoIn recognition of this chaos, https://i2cdevices.org/ https://i2cdevices.org/ is putting together a pretty good list.
- FredFS456 3y agoThat's not technically true - you can probe for I2C devices by only sending a START, an address, waiting for the client device to ACK (or not), and then sending a STOP. I don't think the linux kernel driver allows us to do this, but I've implemented this on some microcontrollers with lower-level control over the I2C hardware. It worked on all clients I tried it on.
- joezydeco 3y agoYou can install i2c-tools and run i2cdetect from userspace. https://learn.adafruit.com/scanning-i2c-addresses/raspberry-pi https://learn.adafruit.com/scanning-i2c-addresses/raspberry-...
- 3y ago