Problems with I2C reading and writing eeprom

wiring

When configuring I2C, you need to set the IO port to open-drain mode. Why do you need to set open-drain mode?

Answer: The I2C protocol supports multiple master devices and multiple slave devices on a bus. If open-drain output is not used and push-pull output is used, there will be a short circuit between the master devices, so the bus generally uses open-drain output.

Why do we need to connect a pull-up resistor?

The pull-up resistor is connected because I2C communication requires the ability to output a high level. Generally, open-drain output cannot output a high level. If a pull-up resistor is connected to the drain, the level conversion output can be performed and connected to the power supply through the pull-up resistor. , so that the "line AND" function can be realized.
When the bus is idle, both SDA and SCL are high. When the I2C device is idle, it will output a high-impedance state. When all devices are idle and all output a high-impedance state, the pull-up resistor will pull the bus to a high level. .
Insert image description here
The picture above shows the SPI wiring.

I2C protocol layer

Insert image description here

  1. State 1 is the SPI idle state: SCL and SDA are both in a high-impedance state and are set to a high-impedance state by their own pull-up resistors, becoming a high level.
  2. Starting state: When the bus is at a high level, a falling edge is detected on the SPI data line (SDA) and a start signal is generated . At this time, the device will jump out of this idle state ( I don’t understand this place well, jump out) Idle?? ) Waiting for data input.
  3. The timing diagram for reading and writing data is as follows.
    Insert image description here
    The timing diagram above shows that starting from a certain number is not the idea of ​​starting from the beginning. Before the response, it means that the 8-bit data has been sent.
    When the master device writes data to the slave device, it must follow one principle:
    (1) When the clock SCL is high level, the data bus writes one bit of data to the slave device. During the writing process, the clock signal cannot change, that is, Say stay high.
    If data transmission is compared to crossing a bridge, then the clock is a bridge, and the bridge cannot be dismantled when crossing the bridge. This is why the clock signal cannot be changed.
    (2) After the data is sent, it is necessary to resend the data. You can pull the clock signal (SCL) low, save the data and resend it. That is, the previous data has crossed the bridge, and changing the data is equivalent to changing people
    . Therefore, the bridge needs to be built again. The bridge is to pull the clock line high to allow data to cross the bridge.
    This problem of crossing bridges and destroying bridges has been repeated until 8 bits of data have been transmitted, which is 1 byte. The slave device needs to perform response processing to ensure the accuracy of data transmission.
    (3) The slave device responds.
    If the above 8 data are received correctly, it will pull the SDA data line low and send a 1-bit response signal to the host. If the slave device responds correctly, it can continue to transmit the next word. section, or end data transmission. If the response is wrong, the data should be discarded or read and written again.
    (4) The stop state is
    when all data transmission is completed. When the clock signal is high level, it is detected that the data line SDA changes from low level to high level (a rising edge is detected to generate a stop signal). At this time, I2C End and return to idle state.

In step (3) just now, when the 8-bit data is transmitted, the slave device needs to respond. It needs to send data to the master device. How can it respond? ?
When responding, the data line needs to be released. The steps are as follows: after
the host sends the data, pull the data line low (SDA).
The host remains low for a period of time , which is often called the "reply time" or "delay time". This time should be long enough for the slave to read the low level on the data line.
After the response time, the host releases the data line to high level (SDA)
and then reads whether the signal of this pin is pulled low by the slave to generate an ACK signal. If it is pulled low, it means it is correct. In other words, it takes 9 bits to send one time. .


Thanks to Xiaoyong, the blogger who came here first for reference, study https://blog.csdn.net/weixin_44834094/article/details/121261910

Guess you like

Origin blog.csdn.net/qq_38156743/article/details/132454747