Questions tagged [i2c]

I2C is a two-wire serial bus. It is used to interface with low-speed peripherals in embedded systems and computer motherboards.

Use this tag when asking questions concerning the I2C bus or SMBus, which is a more strictly defined subset of I2C.

Devices you can communicate with using I2C might include the temperature and voltage sensors on your motherboard. In embedded systems, a vast amount of devices ranging from memory chips to camera modules use I2C for control and data transfer.

I2C bus consists of two signals: SCL and SDA. SCL is the clock signal, and SDA is the data signal.

I2C connection schematics

The clock signal is always generated by the current bus master; some slave devices may force the clock low at times to delay the master sending more data (or to require more time to prepare data before the master attempts to clock it out). The common clock frequency of I2C bus is 100KHz (100Kbps) and 400KHz (400 Kbps). There are high speed versions with clock frequency at or greater than 1MHz (1Mbps) available which is product specific by the semiconductor manufacturers.

The bus is a multi-master bus, which means that any number of master nodes can be present. Additionally, master and slave roles may be changed between messages (after a STOP is sent).

At any given time only the master will be able to initiate the communication. Since there is more than one slave in the bus, the master has to refer to each slave using a different address. When addressed only the slave with that particular address will reply back with the information while the others keep quit. This way we can use the same bus to communicate with multiple devices.

The voltage levels of I2C are not predefined. I2C communication is flexible, means the device which is powered by 5v volt, can use 5v for I2C and the 3.3v devices can use 3v for I2C communication. A 5V I2C bus can’t be connected with 3.3V device. In this case voltage shifters are used to match the voltage levels between two I2C buses.

There are some set of conditions which frame a transaction. Initialization of transmission begins with a falling edge of SDA, which is defined as ‘START’ condition in below diagram where master leaves SCL high while setting SDA low. After this all devices on the same bus go into listening mode.

In the same manner, rising edge of SDA stops the transmission which is shown as ‘STOP’ condition in above diagram, where the master leaves SCL high and also releases SDA to go HIGH. So rising edge of SDA stops the transmission.

I2C conditions

With I2C, data is transferred in messages. Messages are broken up into frames of data. Each message has an address frame that contains the binary address of the slave, and one or more data frames that contain the data being transmitted. The message also includes start and stop conditions, read/write bits, and ACK/NACK bits between each data frame:

I2C message format

Address Frame: A 7 or 10 bit sequence unique to each slave that identifies the slave when the master wants to talk to it.

Read/Write Bit: A single bit specifying whether the master is sending data to the slave (low voltage level) or requesting data from it (high voltage level).

ACK/NACK Bit: Each frame in a message is followed by an acknowledge/no-acknowledge bit. If an address frame or data frame was successfully received, an ACK bit is returned to the sender from the receiving device.

More information:

I2C Standards Doc

I2C primer

1459 questions
42
votes
6 answers

How can you flush a write using a file descriptor?

It turns out this whole misunderstanding of the open() versus fopen() stems from a buggy I2C driver in the Linux 2.6.14 kernel on an ARM. Backporting a working bit bashed driver solved the root cause of the problem I was trying to address…
Jamie
  • 6,004
  • 11
  • 52
  • 81
27
votes
3 answers

UART vs I2C vs SPI for inter-processor communication between microcontrollers

I am examining a way to connect two microcontrollers. On the level of serialization I am thinking of using Nano protobuffers (http://code.google.com/p/nanopb/). This way I can encode/decode messages and send them between two processors. Basically,…
Drasko DRASKOVIC
  • 335
  • 1
  • 3
  • 8
17
votes
4 answers

I2C_SLAVE ioctl purpose

I am writing code for implementing a simple i2c read/write function using the general linux i2c driver linux/i2c-dev.h I am confused about the ioctl : I2C_SLAVE The kernel documentation states as follows : You can do plain i2c transactions by using…
zacurry
  • 716
  • 3
  • 8
  • 22
13
votes
4 answers

Reading / writing from using I2C on Linux

I'm trying to read/write to a FM24CL64-GTR FRAM chip that is connected over a I2C bus on address 0b 1010 011. When I'm trying to write 3 bytes (data address 2 bytes, + data one byte), I get a kernel message ([12406.360000] i2c-adapter i2c-0:…
TheSeeker
  • 2,669
  • 5
  • 21
  • 19
13
votes
3 answers

I2C Driver in Linux

I am aware of I2C in a very basic level which relies inside linux kernel, but no clue to implement a basic I2C driver. Trying to moving for a start in I2C device driver. could you please suggest any beginner tutorial with source code mapping !!
San
  • 867
  • 3
  • 13
  • 32
12
votes
2 answers

What makes SPI faster than I2C protocol

I know the basic of I2C and SPI communication. As both are synchronous protocol. I wanted to know that what makes SPI faster than I2C. If I am not wrong using I2C we can go used 400kbps while in SPI we can achieve 10mbps also. Does it because of…
kapilddit
  • 1,565
  • 4
  • 20
  • 42
11
votes
4 answers

I²C bytes received out of order using Raspberry Pi Python SMBus

I am setting up a Raspberry Pi to record data (CO2, humidity, and temperature) from the Sensirion SCD30 sensor. My code is in Python 3, using the SMBus library to communicate with the sensor over the I²C pins in the Raspberry Pi's GPIO. There is a…
Aeolus
  • 746
  • 1
  • 5
  • 19
11
votes
2 answers

How to write multiple slave i2c client device driver?

I am trying to develop a driver for an embedded board. The driver is supposed to open up an interface for v4l2 and communicate with 2 devices using i2c. the driver will act as a master. I can't seem to understand how i2c_device_id array and…
Mert Can Ergün
  • 241
  • 5
  • 14
10
votes
4 answers

Why are i2c_smbus function not available? (I2C – Embedded Linux)

There are many references to using i2c_smbus_ functions when developing embedded Linux software to communicate on the I2C bus. When i2c_smbus functions such as i2c_smbus_read_word_data are referenced in software project for ARM8 processor errors…
Mahendra Gunawardena
  • 1,864
  • 5
  • 23
  • 42
10
votes
1 answer

i2c interrupt handler stm32

I have some problems with I2C2 interrupts, I have enabled the interrupt but the handler interrupt never executes. Here is the i2c2 initialization: void i2c2InitSlave(void) { I2C_DeInit(I2C2); GPIO_InitTypeDef GPIO_InitStructure; …
edgar.epi
  • 175
  • 2
  • 2
  • 7
10
votes
2 answers

I2C concurrent access on Linux, mutex

I'm writing a multithread C program in embedded Linux that accesses from userspace a number of I2C devices (slaves). Also, I access the same I2C device from multiple threads. I'm using SMBUS functions (i2c_smbus_write_byte_data,…
stef
  • 657
  • 3
  • 13
  • 25
10
votes
2 answers

How to read data from Arduino with Raspberry Pi with I2C

I am trying to read data from an Arduino UNO to Raspberry Pi with the python smbus module. The only documentation I could find on the smbus module was here. I am not sure what the cmd means in the module. I can use the write to send data to the…
TheGreenToaster
  • 147
  • 1
  • 3
  • 11
9
votes
1 answer

I2C device linux driver

How to make a character device for i2c device, with open, close, ioctl etc. functions? I was looking for information about it last two weeks and couldn't find anything working. I found some information in Essential Linux Device Drivers, but it was…
mishaskt
  • 231
  • 2
  • 9
8
votes
1 answer

undefined reference to `i2c_smbus_read_word_data(int, unsigned char)

After updating to Ubuntu 18.04 I can't compile my Qt application. The following error occurs: undefined reference to `i2c_smbus_read_word_data(int, unsigned char) As I understood, i2c_smbus_read_word_data is now defined not in linux/i2c-dev.h, but…
vitperov
  • 1,182
  • 15
  • 17
8
votes
1 answer

iPad accessory communication through UART

We manufacture a new accessory for iPad/iPhone which should transfer commands to the iPad. We like to use UART (through a certain Apple-protocol called Lingo). My research shows that I can only use USB (30PIN Connector) and custom…
Sebastian Roth
  • 10,454
  • 13
  • 56
  • 103
1
2 3
97 98