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

1748 questions
6
votes
2 answers

Probe problem when writing a I2C device driver

I am a newbie in writing linux device driver, forgive me if anything stupid a asked and my poor English^^ I am trying to write a driver for a touch panel, which communicate with CPU via I2C. I tried to add a device driver into linux platform, and…
ChengYing
  • 85
  • 1
  • 2
  • 5
6
votes
5 answers

Reading and writing EEPROM via I2C with Linux

I trying to read and write an Atmel 24C256 EEPROM with a Raspberry Pi B+ over I2C, but I'm having trouble getting it all to work right. Here is the code I have so far: #include #include #include #include…
Jacob Calvert
  • 188
  • 1
  • 2
  • 11
6
votes
0 answers

how to write display driver

I am writing display drivers for micro oled. board is dart4460 (omap4460) which provides dss(display subsystem). so I am writing drivers using dss. but I dont know what I wrote is right or not oled display use dpi interface and i2c for commands I…
Yeol_
  • 93
  • 1
  • 1
  • 9
6
votes
1 answer

i2cdetect won't show device, but it's there

Just a quick question. On my i2c bus 0, I have two devices, 0x32 and 0x20. When I use i2cdetect, only one of them shows up. # ./i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- --…
Andy J
  • 1,479
  • 6
  • 23
  • 40
6
votes
1 answer

How can I access a DDC/CI Display Dependent Device from a Windows application?

I am modifying a monitor controller for a prototype. It would be convenient to send commands to the prototype using DDC/CI. In Windows, I can't find an obvious way to send a DDC/CI command to a "display dependent device". The Monitor Configuration…
joshuanapoli
  • 2,509
  • 3
  • 25
  • 34
6
votes
2 answers

Why I2C_SMBUS_BLOCK_MAX is limited to 32 bytes?

I'm trying to configure a SAA6752HS chip (a MPEG-2 encoder) through I2C bus using a Raspberry Pi as a development kit. It was a piece of cake until I had to write at the address 0xC2 of the chip. For this task, I have to use an I2C command that…
marcelo.guedes
  • 482
  • 5
  • 12
6
votes
1 answer

Configuring STM32 output ports for I2C

I am currently trying to get a pair of stm32l152 (discovery kit) to communicate via I2C. (i am not using the standard peripheral library provided by STM as i want to try to implement the i2c myself..) My configuration is as followed: 7-bit…
foob
  • 293
  • 1
  • 4
  • 12
6
votes
5 answers

What are the disadvantages of bit banging SPI/I2C in embedded applications

I have come to understand that bit banging is horrible practice when it comes to SPI/I2C over GPIO. Why so?
anujdeshpande
  • 313
  • 1
  • 5
  • 11
5
votes
2 answers

I2C slave receiver on stm32f4

I try to implement a i2c slave receiver interrupt service routine on a stm32f4. Here is my smart piece of code. void I2C2_EV_IRQHandler() { switch (I2C_GetLastEvent(I2C2)) { //The address sent by the master matches the own address of…
Jonny Schubert
  • 1,393
  • 2
  • 18
  • 39
5
votes
0 answers

Why is writing to I2C too slow in Python comparing to C?

I am trying to find out why the same code in Python works 25 times slower than C even if I use CDLL, when I try to write into I2C. Below I will describe all the details what I am doing step by step. The version of Raspberry PI: Raspberry PI 3 Model…
Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
5
votes
1 answer

Unknown core dump while accessing driver utilizing i2c bus

Currently I'm developing two drivers for an Arm 9 embedded application. They are both i2c drivers that each utilize an IO expander pcf8575. I have tested the drivers independently, but when I compile them both into the kernel and run my main…
user545199
5
votes
0 answers

How to import MLX90640 thermal camera image data into OpenCV via i2c on Raspberry Pi in C++?

I am using the Melexix MLX90640 32x24 thermal camera sensor connected to a Raspberry Pi 3 via i2c. Using code from Pimoroni I can show the camera data with false colors on the screen through the framebuffer with their fbuf example. Since this is…
lukecv
  • 113
  • 1
  • 11
5
votes
3 answers

STM32F4 I2C with DMA not working

I'm using one STM32F4 and I want to communicate with my LSM303 accelerometer. For that I'm using I2C, and just using I2C works fine but when I try to use DMA with it, it stops working. When I use HAL_I2C_Master_Transmit_DMA it works and I got the…
Victor Douet
  • 161
  • 1
  • 10
5
votes
0 answers

i2cset 16bit register address

I have a device communicates through i2c protocol. The registers addresses of the device are 16 bits but Linux i2c-tools supports only 8-bit addresses. But I have found something to handle it. For example, to read a register at the address on…
mehmetfa
  • 199
  • 3
  • 15
5
votes
2 answers

Is there a general I2C command to see if a device is still present on the bus?

Is there a general I2C command to see if a device is still present on the bus after it is initialized once? For example an OLED display. The reason I ask this is to avoid the main program will freeze (when a device is disconnected) because of…
Codebeat
  • 6,501
  • 6
  • 57
  • 99