1
votes

I am writing wrapper API's in user space for controlling the I2C bus in C on Embedded Linux Platform. I am able to read and write data from sensor using read() and write() methods and ioctl() call to select the slave. I know internally read/write do the following steps :

  • Send Start bit
  • Send slave address + R/W bit and get acknowledged
  • Send/Receive Data
  • Send Stop bit

I want to create separate functions to do the following:

  1. To send a Start bit in the I2C bus
  2. To send a Stop bit in the I2C bus
  3. To write one byte to the I2C bus. (Not Generic to a slave)
  4. To read one byte from the I2C bus. (Not Generic to a slave)

Are there any linux functions/API's/ioctl calls which can be used from a C User application to achieve the above functionalities..?

1
This is a bad idea -- I2C busses are designed for multiple slaves, and what you describe would tie up the bus preventing other drivers from communicating with the other slaves, and also likely cause bus timeouts. Use the operations that conduct a complete transaction. - Ben Voigt
Also, usually i2c controllers are just unable to sedn start/stop bits without sending or receiving data. - Alexandre Belloni

1 Answers

1
votes

Why would you need to do this ? It would be helpful if you could shed some light on the this need.

Usually I2C devices are controlled by kernel driver. However, if you are looking for I2C device access from userspace, you can do it via the /dev interface. Here, you can access I2C adapter from user space using i2c-dev.h of i2c-tools for which related information is in the link https://www.kernel.org/doc/Documentation/i2c/dev-interface . Also, note that while using either read() or write(), the entire I2C call flow based on start bit/address/data/stop gets executed accordingly.