 Hello, and welcome to this presentation of the STM32i²C interface. It covers the main features of this communication interface, which is widely used to connect devices such as microcontrollers, sensors, and serial interface memories. The i²C interface is compliant with the NXPi²C bus specification and user manual, Revision 5. The SM bus system management bus specification, Revision 3, and the PM bus power system management protocol specification, Revision 1.3. The peripheral provides an easy-to-use interface with very simple software programming and full timing flexibility. Additionally, the i²C peripheral is functional in low-power stop modes. The i²C peripheral supports multi-master and slave modes. The i²C IO pins must be configured in open drain mode. The logic high level is driven by an external pull-up. The IO pins support the 20 mA output drive required for fast mode plus. The peripheral controls all i²C bus specific sequencing, protocol, arbitration, and timing values. 7 and 10-bit addressing modes are supported, and multiple 7-bit addresses can be supported in the same application. The peripheral in master mode supports slave clock stretching and clock stretching from slave side. In slave mode configuration of the peripheral, the clock stretching can be disabled by software. The setup and hold times are programmable by software. Analog and digital glitch filters on the data and clock lines can be configured by software. The peripheral can wake up the MCU from stop mode when an address match is detected. The peripheral has an independent clock domain, which allows a communication baud rate independent from the system clock. Here is the i²C block diagram. The registers are accessed through the APB bus, and the peripheral is clocked with the i²C clock, which is independent from the APB clock. The i²C clock can be selected from among the system clock, or SYSCLK, the high speed internal 16 MHz RC oscillator clock, or HSI16, and the APB1 clock, or PCLK1. Analog and digital noise filters are present on the SCL and SDA lines. A 20 mA driving capability is enabled using the control bits in the system configuration registers. In addition, an SM bus alert pin is available in SM bus mode. The i²C setup and hold times can be configured by software through the i²C timing register. The SDA DEL and SCL DEL counters are used during transmission in order to guarantee the minimum data hold and data setup times. The i²C peripheral waits for the programmed data hold time after detecting a falling edge on the clock line before sending the data. After the data is sent, the clock line is stretched low during the programmed data setup time. The total data hold time is greater than the programmed SDA DEL counter. This is due to the fact that SDA DEL delay is only added once the SCL falling edge is internally detected. The time or T-sync 1 needed for this internal detection depends on the SCL falling edge, the input delay due to the filters, and the delay due to the internal SCL synchronization with the i²C clock. However, the setup time is not impacted by these internal delays. The i²C master clock's low and high level durations are configured by software in the i²C timings register. The SCL low and high level counters start after the detection of the edge of the SCL line. This implementation allows the peripheral to support the master clock synchronization mechanism in a multi-master environment as well as the slave clock stretching feature. Therefore, the total SCL period is greater than the sum of the counters. This is linked to the added delays due to the internal detection of the SCL line edge. These delays, T-sync 1 and T-sync 2 depend on the SCL falling or rising edge, the input delay due to the filters, and the delay due to the internal SCL synchronization with the i²C clock. The rising edge depends on pull-up resistor and SCL line capacitance. The falling edge depends on the IO port parameters defined in the datasheet. In order to properly configure clock speed, these edges can be either measured or calculated. They are needed in order to properly configure the i²C peripheral in the STM32QBMX tool. Then, the settings of the timing register can be automatically calculated by this tool. The i²C slave can acknowledge several slave addresses. The slave addresses are programmed into two registers. Own address register 1 can be programmed with a 7 or 10-bit address. Own address register 2 can be programmed with a 7-bit address, but the least significant bits of this address can be masked through the OA2-MSK register in order to acknowledge multiple slave addresses. The two own address registers can be enabled simultaneously. The i²C peripheral supports wake-up from stop mode on address matches. To do this, the i²C peripheral clock must be set to the HSI-16 oscillator. Only the analog noise filter is supported when the wake-up from stop feature is enabled. All addressing modes are supported. When the device is in stop mode, the high-speed internal oscillator is switched off. When a start condition is detected, the i²C peripheral enables the high-speed internal oscillator, which is used to receive the address on the bus. After an address is received in stop mode, a wake-up interrupt is generated if the address matches the programmed slave address. If the address does not match, the high-speed internal oscillator is switched off, no interrupt is generated, and the device remains in stop mode. Clock stretching must be enabled because the i²C peripheral stretches the clock line low after the start condition until the high-speed internal oscillator is started. After having received an address that matches the programmed slave address, the i²C peripheral also stretches the clock line low until the STM32G4 device is awakened. Master mode software management is very simple. Only one right action is needed to handle a master transfer with a payload smaller than 255 bytes. The full protocol is managed by the hardware. In order to start a transfer in master mode, i²C control register 2 must be written with the start condition request, the slave address, the transfer direction, the number of bytes to be transferred, and the end of transfer mode. End of transfer mode is configured by the auto-end bit. If it is set, the stop condition is automatically sent after the programmed number of bytes is transferred. If the auto-end bit is not set, the end of transfer is managed by software. After the programmed number of bytes is transferred, the transfer complete or TC flag is set and an interrupt is generated, if enabled. Then a repeated start or a stop condition can be requested by software. The data transfer can be managed by interrupts or by the DMA. When the payload is greater than 255 bytes, the reload bit must be set in i²C control register 2. In this case, the transfer complete reload or TCR flag is set after the programmed number of bytes has been transferred. The additional number of bytes to be transferred is programmed when the TCR bit is set and then the data transfer will resume. The i²C clock is stretched low as long as the TCR bit is set. The reload bit is used in master mode when the payload is greater than 255 bytes and in slave mode when slave byte control is enabled. When the reload bit is set, the auto-end bit has no effect. By default, the i²C slave uses clock stretching. The clock stretching feature can be disabled by software. In receive mode, the slave acknowledge on received byte behavior can be configured when slave byte control mode is selected together with the reload bit being set. When the SBC bit is set, the number of bytes counter is enabled in slave mode. Clock stretching must be enabled when slave byte control is enabled. In receive mode, when slave byte control is enabled with the reload bit set and the number of bytes to be transferred is 1, the transfer complete reload flag is set after each received byte and the SCL line is stretched. This is done after data reception and before the acknowledge pulse. The receive buffer not empty flag is also set so the data can be read. In the TCR subroutine, an acknowledge or not acknowledge can be programmed to be sent after the byte is received. It is recommended to clear the SBC bit in transmission as there is no use for the byte counter in i²C slave transmitter mode. In SMBus mode, slave byte control mode is used in transmission for sending the PEC or packet error code byte. The i²C peripheral provides hardware support for the SMBus. The SMBus address resolution protocol is supported through the device default address and arbitration in slave mode. The host notify protocol is supported with host address support. The alert protocol is supported through the SMBus alert pin and alert response address. The SMBus clock low timeout and cumulative clock low extend times can be detected with a programmable duration. The bus idle condition can be detected with a programmable duration. Command and data acknowledge control is supported through slave byte control mode. The packet error code or PEC byte is calculated by hardware. The packet error code or PEC byte is automatically sent in transmission and checked in reception. The data transfer counter initialized with the N bytes value is used to automatically check the PEC byte in reception after N bytes minus 1 byte are received. If the received PEC byte does not match the calculation, a not acknowledge is automatically sent after the PEC byte. In transmission, the internally calculated PEC byte is automatically sent after N bytes minus 1 byte. Slave byte control mode must be enabled in slave mode in order to enable the N bytes counter and allow automatic PEC reception or transmission. Several events can trigger and interrupt. The receive buffer not empty flag is set when the receive buffer contains received data and is ready to be read. The transmit buffer interrupt status is set when the transmit buffer is empty and is ready to be written. The stop detection flag is set when a stop condition is detected on the bus. The transfer complete reload flag is set when the reload bit is set and N bytes bytes of data have been transferred. The transfer complete flag is set when the reload and auto end bits are cleared and N bytes bytes of data have been transferred. The address match flag is set when the receive slave address matches one of the enabled slave addresses. The NACK reception flag is set when a not acknowledge is received after a byte transmission. DMA requests can be generated when the receive buffer not empty or transmit buffer empty flag is set. Several error flags can be generated. A bus error detection flag is set when a misplaced start or stop condition is detected. The arbitration loss flag is set in the event of an arbitration loss. An overrun or underrun error flag is set in slave mode with clock stretching disabled when an overrun or an underrun error is detected. In SM bus mode, a PC error flag is set when the receive PEC does not match the calculated PEC register content. A timeout error flag is set when a timeout or extended clock timeout is detected. An alert pin detection flag is set in the SM bus host configuration when alert is enabled and a falling edge is detected on the SMBA pin. The I2C peripheral is active in run and sleep modes. In stop modes, the register's content is kept. If wake up from stop is supported and enabled and if the I2C is clocked by HSI 16 the address recognition is functional. The I2C address match condition causes the device to exit stop mode. In standby and shutdown modes the peripheral is powered down and must be re-initialized after exiting from these modes. For each I2C peripheral, a bit is available for debugging purposes in the MCU debug component that can be used to stop the SM bus timeout counter when the core is halted. Here's an example of a sensor hub application that requires several I2C peripherals. One or several I2C masters are used to interface with external sensors. An I2C slave is used to communicate with the application processor. STM32G4 microcontrollers embed four I2C peripherals all with the same set of features. For more information related to this peripheral you can also refer to these trainings. System configuration controller, reset and clock controller, power controller, interrupts controller, and direct memory access controller. For more details, please refer to the I2C bus specification and user manual from the NXP website. The SM bus specification can be found in the smart battery system implementers forum. The PM bus power system management protocol specification can be found in the power management bus implementers forum.