Do we need to write Linux i2c bus atomic operation by ourselves?

I think this is the basic question. I don’t know the I2C protocol details.
I would like to know the linux case.

Do we need to write the atomic operation for i2c bus in Elixir?
Or if we just use cicuits_i2c, it is done by the library or Linux driver?

Thank you

I might need more information on what you’re trying to achieve.

Linux schedules messages to devices in the order that they’re requested. If multiple OS or Erlang processes send messages on an I2C bus, they’ll be interleaved. Circuits.I2C directly uses the Linux APIs so it behaves identically.

There’s a way tell Linux to make more than one I2C bus transaction at a time using the I2C_RDWR ioctl. The Circuits.I2C.write_read/5 function uses this ioctl. The main reason is to do an I2C repeated start which is often expected by I2C devices. It also ensures that the write and read operations are issued back-to-back.

The I2C_RDWR lets you string together more I2C bus operations and this might be what you’re looking for with your question.

A PR that adds a Circuits.I2C.transfer/4 that takes a list of I2C operations would be interesting. But before going down that route, it would be good to confirm that this is the right thing to do. Every time I’ve thought I’ve needed to implement this, I’ve found that making multiple Circuits.I2C calls is sufficient. Also take a look at the error semantics as described in i2c-core-base.c.

Hope this helps.

1 Like

I might need more information on what you’re trying to achieve.

I don’t do achieve something and don’t have any problems.
I just want to know that the parallel accessing the same bus is safe or not.

For example, there are two GenServers, each of them are open same i2c bus, like “i2c-1”, and store the handle to their state. Then both of them write or read the bus with the hundle in parallel.

The bus hundle, reference, includes the file discripter, right?.
I’d like to know that the same timing access to it is safe or not. Does a Linux I2C driver garantee the atomicity?

You can open as many I2C bus references as you’d like. There won’t be any issues. It’s common to call Circuits.I2C.open/2 for each I2C device that you’re using from Elixir. What you’re describing with two GenServers is the normal way, and that’s how most Elixir libraries that use I2C are written.

Trying to pass around I2C bus references to reduce calls to Circuits.I2C.open/2 is unnecessary. I probably would only do that if it were really convenient.

The Linux kernel ensures that each read and write completes before the next transfer starts. The low level I2C hardware controller can only issue one at a time anyway.

Yes, there’s a file handle behind each I2C bus reference.

1 Like

I appreciate your answer!!

Thank you

1 Like