The SCL and SDA pins of stm8 are marked with T, i.e. pure open drain pin. When the pure open drain pin inputs 0, it is low, and the output 1 is in high resistance state instead of high level. Therefore, these two pins need to be added with pull resistance.
There is nothing wrong with setting SCL to output. SDA should be set as input when stm8 reads slave data as the host and output when stm8 writes data to the slave as the host. Here Pb_ CR1 should be set to 0 (open drain at output and floating at input). When SDA is used as output, its output rate is limited to O1 = slow (up to 2MHz), so PB_ It is not necessary to set the corresponding bit of CR2 to 1 (when output). In addition, when it is set to 1 (when SDA pin is input), will an interrupt be triggered? (preliminary thinking: I don’t think so. It’s I2C, not GPIO)
Register I2C_ Bit2 (named ack bit) in CR2 means that stm8 will automatically send ack (pull down SDA) after receiving data, and send no ack without setting this bit (keep it high regardless of SDA). When we read multiple bytes from an external sensor, the last byte must reply to no ack. At this time, when the penultimate byte is read, set the ACK bit to 0. However, this ack must be set before the next reading of the external sensor byte, otherwise it will not be sent automatically. It is recommended that after reading the last byte at a time, set the ACK bit that has just been cleared again.