EcmaTC53 / spec

Ecma TC53 spec work
23 stars 11 forks source link

Async I2C and atomicity #34

Open tve opened 1 year ago

tve commented 1 year ago

This issue is related to #33 and to the discussion in #28 about how to expose async in ECMA-419.

The core question I'd like to examine is whether on an I2C bus with multiple devices the "SMBus network layer protocol" style register accesses require atomicity and if so how to achieve that and if not what the requirements on I2C bus implementations are. The question conceptually extends beyond the SMBus-like register accesses but right now I can't name a specific device where this is an issue.

Suppose I'm implementing a register read using ECMA-419 async in a BME280 or Si7021 driver (just to pick two different popular chips). At some point I will have a code fragment that writes the register address without stop condition followed by a read of the register. E.g. using promisified read/write methods I might have:

await i2cAsync.write(register_address, 0)
const value = await i2cAsync.read(register_length)

Clearly the write and read combination is not atomic and specifically, another driver could issue a synchronous bus transaction to a different device right in the middle. Is this a problem? If yes, how can it be avoided or corrected? If not, what does the I2C bus implementation have to do?

In researching the above question I have made a couple of observations:

I'm arriving at the following tentative conclusions:

Even if all this turns into a non-issue I must admit that I would have appreciated if the proposed 2nd edition ECMA-419 spec had said something about atomicity :-) .

Edit: Thinking about this some more, I believe that the source of my confusion comes from not understanding that the I2C start and stop conditions are purely about bus mastership/arbitration and that stop+start vs. repeated start carries no meaning WRT the operations being performed. So the repeated-start between the write and read phase of a SMBus-like register read simply keeps other bus masters at bay and doesn't mean "the following read relates to the preceding write". Apologies if this is obvious to everyone else.