20. Inter-integrated circuit (I2C) interface

20.1 Introduction

The I 2 C (inter-integrated circuit) bus interface handles communications between the microcontroller and the serial I 2 C bus. It provides multimaster capability, and controls all I 2 C bus-specific sequencing, protocol, arbitration and timing. It supports Standard-mode (Sm), and Fast-mode (Fm).

It is also SMBus (system management bus) and PMBus (power management bus) compatible.

DMA can be used to reduce CPU overload.

20.2 I2C main features

The following additional features are also available depending on the product implementation (see Section 20.3: I2C implementation ):

20.3 I2C implementation

This manual describes the full set of features implemented in I2C1

Table 85. STM32L010xx I2C features

I2C features (1)I2C1
7-bit addressing modeX
10-bit addressing modeX
Standard-mode (up to 100 kbit/s)X
Fast-mode (up to 400 kbit/s)X
Fast-mode Plus with 20mA output drive I/Os (up to 1 Mbit/s)-
Independent clockX
Wakeup from Stop modeX
SMBus/PMBusX

1. X = supported.

20.4 I2C functional description

In addition to receiving and transmitting data, this interface converts it from serial to parallel format and vice versa. The interrupts are enabled or disabled by software. The interface is connected to the I 2 C bus by a data pin (SDA) and by a clock pin (SCL). It can be connected with a standard (up to 100 kHz) or Fast-mode (up to 400 kHz) I 2 C bus.

This interface can also be connected to a SMBus with the data pin (SDA) and clock pin (SCL).

If SMBus feature is supported: the additional optional SMBus Alert pin (SMBA) is also available.

20.4.1 I2C1 block diagram

The block diagram of the I2C1 interface is shown in Figure 150 .

Figure 150. I2C1 block diagram

Block diagram of the I2C1 interface showing internal components and external connections.

The diagram illustrates the internal architecture of the I2C1 interface. It features a central functional block containing several sub-modules: Data control (with a Shift register and SMBUS PEC generation/check), Wakeup on address match, Clock control (with Master clock generation, Slave clock stretching, and SMBus Timeout check), and SMBus Alert control/status. These modules are interconnected and also interface with an external APB bus through a Registers block. The Data control and Clock control blocks are connected to Digital noise filters, which in turn connect to Analog noise filters and finally to GPIO logic for the I2C_SDA and I2C_SCL pins. The SMBus Alert control/status block connects to an I2C_SMBA pin. The interface is powered by two clock sources: i2c_ker_ck (I2C kernel clock) and PCLK (APB clock).

graph TD
    subgraph I2C1_Block [I2C1 Interface]
        DC[Data control]
        WAM[Wakeup on address match]
        CC[Clock control]
        SAC[SMBus Alert control/status]
        REG[Registers]
        
        DC --> WAM
        DC --> CC
        CC --> SAC
        REG <--> APB[APB bus]
        REG --> DC
        REG --> CC
        REG --> SAC
    end
    
    i2c_ker_ck --> I2C1_Block
    i2c_pclk --> I2C1_Block
    
    DC --> DNF1[Digital noise filter]
    DNF1 --> ANF1[Analog noise filter]
    ANF1 --> GL1[GPIO logic]
    GL1 --> I2C_SDA[I2C_SDA]
    
    CC --> DNF2[Digital noise filter]
    DNF2 --> ANF2[Analog noise filter]
    ANF2 --> GL2[GPIO logic]
    GL2 --> I2C_SCL[I2C_SCL]
    
    SAC --> I2C_SMBA[I2C_SMBA]
  

MSV46198V2

Block diagram of the I2C1 interface showing internal components and external connections.

The I2C1 is clocked by an independent clock source which allows the I2C to operate independently from the PCLK frequency.

20.4.2 I2C pins and internal signals

Table 86. I2C input/output pins

Pin nameSignal typeDescription
I2C_SDABidirectionalI2C data
I2C_SCLBidirectionalI2C clock
I2C_SMBABidirectionalSMBus alert

Table 87. I2C internal input/output signals

Internal signal nameSignal typeDescription
i2c_ker_ckInputI2C kernel clock, also named I2CCLK in this document
i2c_pclkInputI2C APB clock
i2c_itOutputI2C interrupts, refer to Table 100: I2C Interrupt requests for the full list of interrupt sources
i2c_rx_dmaOutputI2C receive data DMA request (I2C_RX)
i2c_tx_dmaOutputI2C transmit data DMA request (I2C_TX)

20.4.3 I2C clock requirements

The I2C kernel is clocked by I2CCLK.

The I2CCLK period \( t_{I2CCLK} \) must respect the following conditions:

\[ t_{I2CCLK} < (t_{LOW} - t_{filters}) / 4 \text{ and } t_{I2CCLK} < t_{HIGH} \]

with:

\( t_{LOW} \) : SCL low time and \( t_{HIGH} \) : SCL high time

\( t_{filters} \) : when enabled, sum of the delays brought by the analog filter and by the digital filter.

Analog filter delay is maximum 260 ns. Digital filter delay is \( DNF \times t_{I2CCLK} \) .

The PCLK clock period \( t_{PCLK} \) must respect the following condition:

\[ t_{PCLK} < 4/3 t_{SCL} \]

with \( t_{SCL} \) : SCL period

Caution: When the I2C kernel is clocked by PCLK, this clock must respect the conditions for \( t_{I2CCLK} \) .

20.4.4 Mode selection

The interface can operate in one of the four following modes:

By default, it operates in slave mode. The interface automatically switches from slave to master when it generates a START condition, and from master to slave if an arbitration loss or a STOP generation occurs, allowing multimaster capability.

Communication flow

In Master mode, the I2C interface initiates a data transfer and generates the clock signal. A serial data transfer always begins with a START condition and ends with a STOP condition. Both START and STOP conditions are generated in master mode by software.

In Slave mode, the interface is capable of recognizing its own addresses (7 or 10-bit), and the general call address. The general call address detection can be enabled or disabled by software. The reserved SMBus addresses can also be enabled by software.

Data and addresses are transferred as 8-bit bytes, MSB first. The first byte(s) following the START condition contain the address (one in 7-bit mode, two in 10-bit mode). The address is always transmitted in Master mode.

A ninth clock pulse follows the 8 clock cycles of a byte transfer, during which the receiver must send an acknowledge bit to the transmitter. Refer to the following figure.

Figure 151. I 2 C bus protocol

I2C bus protocol timing diagram showing SDA and SCL signals. It illustrates a Start condition where SDA goes low while SCL is high. Then, 9 clock pulses on SCL are shown. During the first 8 pulses, data bits are transferred on SDA (starting with MSB). On the 9th pulse, an ACK bit is sent. Finally, a Stop condition is shown where SDA goes high while SCL is high.
I2C bus protocol timing diagram showing SDA and SCL signals. It illustrates a Start condition where SDA goes low while SCL is high. Then, 9 clock pulses on SCL are shown. During the first 8 pulses, data bits are transferred on SDA (starting with MSB). On the 9th pulse, an ACK bit is sent. Finally, a Stop condition is shown where SDA goes high while SCL is high.

Acknowledge can be enabled or disabled by software. The I2C interface addresses can be selected by software.

20.4.5 I2C initialization

Enabling and disabling the peripheral

The I2C peripheral clock must be configured and enabled in the clock controller.

Then the I2C can be enabled by setting the PE bit in the I2C_CR1 register.

When the I2C is disabled (PE = 0), the I 2 C performs a software reset. Refer to Section 20.4.6: Software reset for more details.

Noise filters

Before enabling the I2C peripheral by setting the PE bit in I2C_CR1 register, the user must configure the noise filters, if needed. By default, an analog noise filter is present on the SDA and SCL inputs. This analog filter is compliant with the I 2 C specification which requires the

suppression of spikes with a pulse width up to 50 ns in Fast-mode. The user can disable this analog filter by setting the ANFOFF bit, and/or select a digital filter by configuring the DNF[3:0] bit in the I2C_CR1 register.

When the digital filter is enabled, the level of the SCL or the SDA line is internally changed only if it remains stable for more than DNF x I2CCLK periods. This allows spikes with a programmable length of 1 to 15 I2CCLK periods to be suppressed.

Table 88. Comparison of analog vs. digital filters

-Analog filterDigital filter
Pulse width of suppressed spikes≥ 50 nsProgrammable length from 1 to 15 I2C peripheral clocks
BenefitsAvailable in Stop mode
  • – Programmable length: extra filtering capability versus standard requirements
  • – Stable length
DrawbacksVariation vs. temperature, voltage, processWakeup from Stop mode on address match is not available when digital filter is enabled

Caution: Changing the filter configuration is not allowed when the I2C is enabled.

I2C timings

The timings must be configured in order to guarantee a correct data hold and setup time, used in master and slave modes. This is done by programming the PRESC[3:0], SCLDEL[3:0] and SDADEL[3:0] bits in the I2C_TIMINGR register.

The STM32CubeMX tool calculates and provides the I2C_TIMINGR content in the I2C configuration window

Figure 152. Setup and hold timings

Timing diagrams for I2C setup and hold times.

The figure contains two timing diagrams illustrating I2C setup and hold times. The top diagram, titled 'DATA HOLD TIME', shows the SCL and SDA signals. A falling edge on SCL is marked as 'SCL falling edge internal detection'. The time interval from this edge to the start of the SDA output change is labeled \( t_{HD;DAT} \) . The SDA output delay is labeled 'SDA output delay'. The time interval from the start of the SDA output change to the start of the SCL stretched low period is labeled \( t_{SYNC1} \) . The SCL stretched low period is labeled 'SDADEL: SCL stretched low by the I2C'. The bottom diagram, titled 'DATA SETUP TIME', shows the SCL and SDA signals. The SCL stretched low period is labeled 'SCLDEL: SCL stretched low by the I2C'. The time interval from the start of the SCL stretched low period to the start of the SDA output change is labeled \( t_{SU;DAT} \) . The SDA output change is labeled 'SDA output delay'.

Data hold time: in case of transmission, the data is sent on SDA output after the SDADEL delay, if it is already available in I2C_TXDR.

Data setup time: in case of transmission, the SCLDEL counter starts when the data is sent on SDA output.

MS49608V1

Timing diagrams for I2C setup and hold times.

\( t_{\text{SDADEL}} \) impacts the hold time \( t_{\text{HD;DAT}} \) .

The total SDA output delay is:

\[ t_{\text{SYNC1}} + \{[\text{SDADEL} \times (\text{PRESC}+1) + 1] \times t_{\text{I2CCLK}}\} \]

\( t_{\text{SYNC1}} \) duration depends on these parameters:

In order to bridge the undefined region of the SCL falling edge, the user must program SDADEL in such a way that:

\[ \{t_{\text{f(max)}} + t_{\text{HD;DAT(min)}} - t_{\text{AF(min)}} - [(DNF + 3) \times t_{\text{I2CCLK}}]\} / \{(\text{PRESC} + 1) \times t_{\text{I2CCLK}}\} \leq \text{SDADEL} \]
\[ \text{SDADEL} \leq \{t_{\text{HD;DAT(max)}} - t_{\text{AF(max)}} - [(DNF + 4) \times t_{\text{I2CCLK}}]\} / \{(\text{PRESC} + 1) \times t_{\text{I2CCLK}}\} \]

Note: \( t_{\text{AF(min)}} / t_{\text{AF(max)}} \) are part of the equation only when the analog filter is enabled. Refer to device datasheet for \( t_{\text{AF}} \) values.

The maximum \( t_{\text{HD;DAT}} \) can be 3.45 µs, 0.9 µs and 0.45 µs for Standard-mode, Fast-mode, but must be less than the maximum of \( t_{\text{VD;DAT}} \) by a transition time. This maximum must only be met if the device does not stretch the LOW period ( \( t_{\text{LOW}} \) ) of the SCL signal. If the clock stretches the SCL, the data must be valid by the set-up time before it releases the clock.

The SDA rising edge is usually the worst case, so in this case the previous equation becomes:

\[ \text{SDADEL} \leq \{t_{\text{VD;DAT(max)}} - t_{\text{r(max)}} - 260 \text{ ns} - [(DNF + 4) \times t_{\text{I2CCLK}}]\} / \{(\text{PRESC} + 1) \times t_{\text{I2CCLK}}\}. \]

Note: This condition can be violated when \( \text{NOSTRETCH} = 0 \) , because the device stretches SCL low to guarantee the set-up time, according to the SCLDEL value.

Refer to Table 89: I2C-SMBUS specification data setup and hold times for \( t_{\text{f}} \) , \( t_{\text{r}} \) , \( t_{\text{HD;DAT}} \) and \( t_{\text{VD;DAT}} \) standard values.

\( t_{\text{SCLDEL}} \) impacts the setup time \( t_{\text{SU;DAT}} \) .

In order to bridge the undefined region of the SDA transition (rising edge usually worst case), the user must program SCLDEL in such a way that:

\[ \{[t_{\text{r(max)}} + t_{\text{SU;DAT(min)}}] / [(\text{PRESC}+1) \times t_{\text{I2CCLK}}]\} - 1 \leq \text{SCLDEL} \]

Refer to Table 89: I2C-SMBUS specification data setup and hold times for \( t_{\text{r}} \) and \( t_{\text{SU;DAT}} \) standard values.

The SDA and SCL transition time values to be used are the ones in the application. Using the maximum values from the standard increases the constraints for the SDADEL and SCLDEL calculation, but ensures the feature whatever the application.

Note: At every clock pulse, after SCL falling edge detection, the I2C master or slave stretches SCL low during at least \( [(SDADEL+SCLDEL+1) \times (PRESC+1) + 1] \times t_{I2CCLK} \) , in both transmission and reception modes. In transmission mode, in case the data is not yet written in I2C_TXDR when SDADEL counter is finished, the I2C keeps on stretching SCL low until the next data is written. Then new data MSB is sent on SDA output, and SCLDEL counter starts, continuing stretching SCL low to guarantee the data setup time.

If NOSTRETCH = 1 in slave mode, the SCL is not stretched. Consequently the SDADEL must be programmed in such a way to guarantee also a sufficient setup time.

Table 89. I 2 C-SMBUS specification data setup and hold times

SymbolParameterStandard-mode (Sm)Fast-mode (Fm)SMBUSUnit
Min.MaxMin.MaxMin.Max
\( t_{HD;DAT} \)Data hold time0-0-0.3-µs
\( t_{VD;DAT} \)Data valid time-3.45-0.9--
\( t_{SU;DAT} \)Data setup time250-100-250-ns
\( t_r \)Rise time of both SDA and SCL signals-1000-300-1000
\( t_f \)Fall time of both SDA and SCL signals-300-300-300

Additionally, in master mode, the SCL clock high and low levels must be configured by programming the PRESC[3:0], SCLH[7:0] and SCLL[7:0] bits in the I2C_TIMINGR register.

Refer to I2C master initialization for more details.

Caution: Changing the timing configuration is not allowed when the I2C is enabled.

The I2C slave NOSTRETCH mode must also be configured before enabling the peripheral. Refer to I2C slave initialization for more details.

Caution: Changing the NOSTRETCH configuration is not allowed when the I2C is enabled.

Figure 153. I2C initialization flow

Flowchart of I2C initialization flow
graph TD; A([Initial settings]) --> B[Clear PE bit in I2C_CR1]; B --> C[Configure ANFOFF and DNF[3:0] in I2C_CR1]; C --> D["Configure PRESC[3:0],
SDADEL[3:0], SCLDEL[3:0], SCLH[7:0],
SCLL[7:0] in I2C_TIMINGR"]; D --> E[Configure NOSTRETCH in I2C_CR1]; E --> F[Set PE bit in I2C_CR1]; F --> G([End]);

The flowchart illustrates the I2C initialization sequence. It begins with 'Initial settings' (oval), followed by 'Clear PE bit in I2C_CR1' (rectangle), 'Configure ANFOFF and DNF[3:0] in I2C_CR1' (rectangle), 'Configure PRESC[3:0], SDADEL[3:0], SCLDEL[3:0], SCLH[7:0], SCLL[7:0] in I2C_TIMINGR' (rectangle), 'Configure NOSTRETCH in I2C_CR1' (rectangle), 'Set PE bit in I2C_CR1' (rectangle), and finally 'End' (oval). The steps are connected by downward arrows. A small text 'MS19847V2' is located in the bottom right corner of the diagram area.

Flowchart of I2C initialization flow

20.4.6 Software reset

A software reset can be performed by clearing the PE bit in the I2C_CR1 register. In that case I2C lines SCL and SDA are released. Internal states machines are reset and communication control bits, as well as status bits come back to their reset value. The configuration registers are not impacted.

Here is the list of impacted register bits:

  1. 1. I2C_CR2 register: START, STOP, NACK
  2. 2. I2C_ISR register: BUSY, TXE, TXIS, RXNE, ADDR, NACKF, TCR, TC, STOPF, BERR, ARLO, OVR

and in addition when the SMBus feature is supported:

  1. 1. I2C_CR2 register: PECBYTE
  2. 2. I2C_ISR register: PECERR, TIMEOUT, ALERT

PE must be kept low during at least three APB clock cycles in order to perform the software reset. This is ensured by writing the following software sequence:

  1. 1. Write PE = 0
  2. 2. Check PE = 0
  3. 3. Write PE = 1.

20.4.7 Data transfer

The data transfer is managed through transmit and receive data registers and a shift register.

Reception

The SDA input fills the shift register. After the eighth SCL pulse (when the complete data byte is received), the shift register is copied into I2C_RXDR register if it is empty (RXNE = 0). If RXNE = 1, meaning that the previous received data byte has not yet been read, the SCL line is stretched low until I2C_RXDR is read. The stretch is inserted between the eighth and ninth SCL pulse (before the acknowledge pulse).

Figure 154. Data reception

Timing diagram for I2C data reception showing SCL, Shift register, RXNE, and I2C_RXDR signals over time.

The diagram illustrates the timing for data reception in an I2C interface. It shows four horizontal timelines: SCL (Serial Clock), Shift register, RXNE (Receive Not Empty), and I2C_RXDR (Receive Data Register). The SCL signal is a periodic square wave. The Shift register is a horizontal bar divided into segments: 'xx', 'data1', 'xx', 'data2', 'xx'. The RXNE signal is a line that goes high when a byte is received and low when it is read. The I2C_RXDR is a horizontal bar divided into segments: 'data0', 'data1', 'data2'. Vertical dashed lines mark key events. The first dashed line is at the start. The second dashed line is after the eighth SCL pulse, where 'data1' is shifted into the register and RXNE goes high. The third dashed line is after the first ACK pulse, where 'data0' is copied to I2C_RXDR and RXNE goes low. The fourth dashed line is after the ninth SCL pulse, where 'data1' is shifted into the register and RXNE goes high again. The fifth dashed line is after the second ACK pulse, where 'data1' is copied to I2C_RXDR and RXNE goes low. The sixth dashed line is after the tenth SCL pulse. A legend indicates that a blue line represents an 'SCL stretch', which occurs when the SCL line is held low between the eighth and ninth pulses while RXNE is high. Arrows show the flow of data: from SCL to Shift register, from Shift register to RXNE, and from RXNE to I2C_RXDR. Labels 'rd data0' and 'rd data1' indicate the reading of data from I2C_RXDR. The diagram is labeled 'MS19848V1' in the bottom right corner.

Timing diagram for I2C data reception showing SCL, Shift register, RXNE, and I2C_RXDR signals over time.

Transmission

If the I2C_TXDR register is not empty (TXE=0), its content is copied into the shift register after the ninth SCL pulse (the Acknowledge pulse). Then the shift register content is shifted out on SDA line. If TXE = 1, meaning that no data is written yet in I2C_TXDR, SCL line is stretched low until I2C_TXDR is written. The stretch is done after the ninth SCL pulse.

Figure 155. Data transmission

Timing diagram for I2C data transmission showing SCL, Shift register, TXE, and I2C_TXDR signals over time. The diagram illustrates the flow of data from the I2C_TXDR register through the shift register to the SDA line, synchronized with SCL pulses. ACK pulses are shown on the SCL line. The TXE flag is shown toggling between 0 and 1. The I2C_TXDR register is shown being updated with data0, data1, and data2. The shift register is shown with 'xx' (unknown) and 'data' (data) segments. A legend indicates that a blue line represents an SCL stretch.

The diagram illustrates the timing of data transmission in an I2C interface. It shows four horizontal timelines: SCL, Shift register, TXE, and I2C_TXDR. - The SCL line shows a series of pulses. Two 'ACK pulse' labels are present, each with a circle around a low pulse on the SCL line. - The Shift register is shown as a horizontal bar divided into segments. It starts with 'xx', followed by 'data1', then 'xx', then 'data2', and finally 'xx'. - The TXE line shows a high level initially, then drops to low when 'data1' is written to I2C_TXDR, and returns to high when 'data2' is written. - The I2C_TXDR register shows 'data0' initially, then 'data1' is written, and finally 'data2' is written. - Arrows indicate the flow of data: from I2C_TXDR to the Shift register (labeled 'wr data1' and 'wr data2'), and from the Shift register to the SDA line (implied by the shift register segments). - Vertical dashed lines mark key events: the first after the first ACK pulse, and the second after the second ACK pulse. - A legend on the right shows a blue line segment labeled 'SCL stretch'.

Timing diagram for I2C data transmission showing SCL, Shift register, TXE, and I2C_TXDR signals over time. The diagram illustrates the flow of data from the I2C_TXDR register through the shift register to the SDA line, synchronized with SCL pulses. ACK pulses are shown on the SCL line. The TXE flag is shown toggling between 0 and 1. The I2C_TXDR register is shown being updated with data0, data1, and data2. The shift register is shown with 'xx' (unknown) and 'data' (data) segments. A legend indicates that a blue line represents an SCL stretch.

MS19849V1

Hardware transfer management

The I2C has a byte counter embedded in hardware in order to manage byte transfer and to close the communication in various modes such as:

The byte counter is always used in master mode. By default it is disabled in slave mode, but it can be enabled by software by setting the SBC (Slave Byte Control) bit in the I2C_CR2 register.

The number of bytes to be transferred is programmed in the NBBYTES[7:0] bit field in the I2C_CR2 register. If the number of bytes to be transferred (NBBYTES) is greater than 255, or if a receiver wants to control the acknowledge value of a received data byte, the reload mode must be selected by setting the RELOAD bit in the I2C_CR2 register. In this mode, the TCR flag is set when the number of bytes programmed in NBBYTES is transferred, and an interrupt is generated if TCIE is set. SCL is stretched as long as TCR flag is set. TCR is cleared by software when NBBYTES is written to a non-zero value.

When the NBBYTES counter is reloaded with the last number of bytes, RELOAD bit must be cleared.

When RELOAD=0 in master mode, the counter can be used in 2 modes:

Caution: The AUTOEND bit has no effect when the RELOAD bit is set.

Table 90. I2C configuration

FunctionSBC bitRELOAD bitAUTOEND bit
Master Tx/Rx NBBYTES + STOPx01
Master Tx/Rx + NBBYTES + RESTARTx00
Slave Tx/Rx
all received bytes ACKed
0xx
Slave Rx with ACK control11x

20.4.8 I2C slave mode

I2C slave initialization

In order to work in slave mode, the user must enable at least one slave address. Two registers I2C_OAR1 and I2C_OAR2 are available in order to program the slave own addresses OA1 and OA2.

OA1 is enabled by setting the OA1EN bit in the I2C_OAR1 register.

These reserved addresses can be acknowledged if they are enabled by the specific enable bit, if they are programmed in the I2C_OAR1 or I2C_OAR2 register with OA2MSK=0.

OA2 is enabled by setting the OA2EN bit in the I2C_OAR2 register.

When the I2C is selected by one of its enabled addresses, the ADDR interrupt status flag is set, and an interrupt is generated if the ADDRIE bit is set.

By default, the slave uses its clock stretching capability, which means that it stretches the SCL signal at low level when needed, in order to perform software actions. If the master does not support clock stretching, the I2C must be configured with NOSTRETCH = 1 in the I2C_CR1 register.

After receiving an ADDR interrupt, if several addresses are enabled the user must read the ADDCODE[6:0] bits in the I2C_ISR register in order to check which address matched. DIR flag must also be checked in order to know the transfer direction.

Slave clock stretching (NOSTRETCH = 0)

In default mode, the I2C slave stretches the SCL clock in the following situations:

Slave without clock stretching (NOSTRETCH = 1)

When NOSTRETCH = 1 in the I2C_CR1 register, the I2C slave does not stretch the SCL signal.

Slave byte control mode

In order to allow byte ACK control in slave reception mode, The Slave byte control mode must be enabled by setting the SBC bit in the I2C_CR1 register. This is required to be compliant with SMBus standards.

The Reload mode must be selected in order to allow byte ACK control in slave reception mode (RELOAD = 1). To get control of each byte, NBBYTES must be initialized to 0x1 in the ADDR interrupt subroutine, and reloaded to 0x1 after each received byte. When the byte is received, the TCR bit is set, stretching the SCL signal low between the eighth and ninth SCL pulses. The user can read the data from the I2C_RXDR register, and then decide to acknowledge it or not by configuring the ACK bit in the I2C_CR2 register. The SCL stretch is released by programming NBBYTES to a non-zero value: the acknowledge or not-acknowledge is sent and next byte can be received.

NBYTES can be loaded with a value greater than 0x1, and in this case, the reception flow is continuous during NBBYTES data reception.

Note: The SBC bit must be configured when the I2C is disabled, or when the slave is not addressed, or when ADDR = 1.

The RELOAD bit value can be changed when ADDR = 1, or when TCR = 1.

Caution: The Slave byte control mode is not compatible with NOSTRETCH mode. Setting SBC when NOSTRETCH = 1 is not allowed.

Figure 156. Slave initialization flow

Flowchart of Slave initialization flow
graph TD; A([Slave initialization]) --> B[Initial settings]; B --> C[Clear {OA1EN, OA2EN} in I2C_OAR1 and I2C_OAR2]; C --> D["Configure {OA1[9:0], OA1MODE, OA1EN, OA2[6:0], OA2MSK[2:0], OA2EN, GCEN}"]; D --> E[Configure SBC in I2C_CR1*]; E --> F[Enable interrupts and/or DMA in I2C_CR1]; F --> G([End]);

*SBC must be set to support SMBus features

MS19850V2

Flowchart of Slave initialization flow

For code example, refer to A.14.1: I2C configured in slave mode code example .

Slave transmitter

A transmit interrupt status (TXIS) is generated when the I2C_TXDR register becomes empty. An interrupt is generated if the TXIE bit is set in the I2C_CR1 register.

The TXIS bit is cleared when the I2C_TXDR register is written with the next data byte to be transmitted.

When a NACK is received, the NACKF bit is set in the I2C_ISR register and an interrupt is generated if the NACKIE bit is set in the I2C_CR1 register. The slave automatically releases the SCL and SDA lines in order to let the master perform a STOP or a RESTART condition. The TXIS bit is not set when a NACK is received.

When a STOP is received and the STOPIE bit is set in the I2C_CR1 register, the STOPF flag is set in the I2C_ISR register and an interrupt is generated. In most applications, the SBC bit is usually programmed to '0'. In this case, If TXE = 0 when the slave address is received (ADDR = 1), the user can choose either to send the content of the I2C_TXDR register as the first data byte, or to flush the I2C_TXDR register by setting the TXE bit in order to program a new data byte.

In Slave byte control mode (SBC = 1), the number of bytes to be transmitted must be programmed in NBBYTES in the address match interrupt subroutine (ADDR = 1). In this case, the number of TXIS events during the transfer corresponds to the value programmed in NBBYTES.

Caution: When NOSTRETCH = 1, the SCL clock is not stretched while the ADDR flag is set, so the user cannot flush the I2C_TXDR register content in the ADDR subroutine, in order to program the first data byte. The first data byte to be sent must be previously programmed in the I2C_TXDR register:

If STOPF is still set when the first data transmission starts, an underrun error is generated (the OVR flag is set).

If a TXIS event is needed, (transmit interrupt or transmit DMA request), the user must set the TXIS bit in addition to the TXE bit, in order to generate a TXIS event.

Figure 157. Transfer sequence flow for I2C slave transmitter, NOSTRETCH = 0

Flowchart of I2C slave transmitter transfer sequence. It starts with 'Slave transmission' (oval), followed by 'Slave initialization' (rectangle). A decision diamond 'I2C_ISR.ADDR = 1?' follows. If 'No', it loops back to 'Slave initialization'. If 'Yes', it proceeds to a rectangle: 'Read ADDCODE and DIR in I2C_ISR. Optional: Set I2C_ISR.TXE = 1. Set I2C_ICR.ADDRCF'. A vertical double-headed arrow labeled 'SCL stretched' is next to this step. Next is a decision diamond 'I2C_ISR.TXIS = 1?'. If 'No', it loops back to the entry point of the 'Read ADDCODE...' step. If 'Yes', it proceeds to 'Write I2C_TXDR.TXDATA' (rectangle), which then loops back to the entry point of the 'Read ADDCODE...' step.
graph TD; Start([Slave transmission]) --> Init[Slave initialization]; Init --> ADDR{I2C_ISR.ADDR = 1?}; ADDR -- No --> Init; ADDR -- Yes --> Read[Read ADDCODE and DIR in I2C_ISR<br/>Optional: Set I2C_ISR.TXE = 1<br/>Set I2C_ICR.ADDRCF]; Read --> TXIS{I2C_ISR.TXIS = 1?}; TXIS -- No --> Read; TXIS -- Yes --> Write[Write I2C_TXDR.TXDATA]; Write --> Read;

MS19851V2

Flowchart of I2C slave transmitter transfer sequence. It starts with 'Slave transmission' (oval), followed by 'Slave initialization' (rectangle). A decision diamond 'I2C_ISR.ADDR = 1?' follows. If 'No', it loops back to 'Slave initialization'. If 'Yes', it proceeds to a rectangle: 'Read ADDCODE and DIR in I2C_ISR. Optional: Set I2C_ISR.TXE = 1. Set I2C_ICR.ADDRCF'. A vertical double-headed arrow labeled 'SCL stretched' is next to this step. Next is a decision diamond 'I2C_ISR.TXIS = 1?'. If 'No', it loops back to the entry point of the 'Read ADDCODE...' step. If 'Yes', it proceeds to 'Write I2C_TXDR.TXDATA' (rectangle), which then loops back to the entry point of the 'Read ADDCODE...' step.

Figure 158. Transfer sequence flow for I2C slave transmitter, NOSTRETCH = 1

Flowchart for I2C slave transmitter transfer sequence. It starts with 'Slave transmission' (oval), followed by 'Slave initialization' (rectangle). A loop begins with a decision 'I2C_ISR.TXIS = 1?'. If 'Yes', it goes to 'Write I2C_TXDR.TXDATA' (rectangle) and loops back. If 'No', it goes to another decision 'I2C_ISR.STOPF = 1?'. If 'Yes', it goes to 'Optional: Set I2C_ISR.TXE = 1 and I2C_ISR.TXIS=1' (rectangle), then 'Set I2C_ICR.STOPCF' (rectangle), and loops back to the start of the loop. If 'No', it loops back directly.
graph TD; Start([Slave transmission]) --> Init[Slave initialization]; Init --> LoopStart(( )); LoopStart --> TXIS{I2C_ISR.TXIS = 1?}; TXIS -- Yes --> Write[Write I2C_TXDR.TXDATA]; Write --> LoopStart; TXIS -- No --> STOPF{I2C_ISR.STOPF = 1?}; STOPF -- Yes --> Optional["Optional: Set I2C_ISR.TXE = 1 and I2C_ISR.TXIS=1"]; Optional --> STOPCF[Set I2C_ICR.STOPCF]; STOPCF --> LoopStart; STOPF -- No --> LoopStart;

MS19852V2

Flowchart for I2C slave transmitter transfer sequence. It starts with 'Slave transmission' (oval), followed by 'Slave initialization' (rectangle). A loop begins with a decision 'I2C_ISR.TXIS = 1?'. If 'Yes', it goes to 'Write I2C_TXDR.TXDATA' (rectangle) and loops back. If 'No', it goes to another decision 'I2C_ISR.STOPF = 1?'. If 'Yes', it goes to 'Optional: Set I2C_ISR.TXE = 1 and I2C_ISR.TXIS=1' (rectangle), then 'Set I2C_ICR.STOPCF' (rectangle), and loops back to the start of the loop. If 'No', it loops back directly.

Figure 159. Transfer bus diagrams for I2C slave transmitter

Timing diagram for I2C slave transmitter with 3 bytes and 1st data flushed. The diagram shows a sequence of bytes: S (reception), Address (reception), A (acknowledge), data1 (transmission), A (acknowledge), data2 (transmission), A (acknowledge), data3 (transmission), NA (no acknowledge), and P (stop). Events are marked: EV1 at ADDR, EV2 at TXIS (data1), EV3 at TXIS (data2), EV4 at TXIS (data3), and EV5 at TXIS (data4, not sent). The TXE line is shown as a pulse. A legend indicates transmission (white), reception (yellow), and SCL stretch (blue line). Timing diagram for I2C slave transmitter with 3 bytes without 1st data flush. The diagram shows a sequence of bytes: S (reception), Address (reception), A (acknowledge), data1 (transmission), A (acknowledge), data2 (transmission), A (acknowledge), data3 (transmission), NA (no acknowledge), and P (stop). Events are marked: EV1 at ADDR, EV2 at TXIS (data2), EV3 at TXIS (data3), and EV4 at TXIS (data4, not sent). The TXE line is shown as a pulse. A legend indicates transmission (white), reception (yellow), and SCL stretch (blue line). Timing diagram for I2C slave transmitter with 3 bytes and NOSTRETCH=1. The diagram shows a sequence of bytes: S (reception), Address (reception), A (acknowledge), data1 (transmission), A (acknowledge), data2 (transmission), A (acknowledge), data3 (transmission), NA (no acknowledge), and P (stop). Events are marked: EV1 at ADDR, EV2 at TXIS (data1), EV3 at TXIS (data2), EV4 at TXIS (data3), and EV5 at STOPF. The TXE line is shown as a pulse. A legend indicates transmission (white), reception (yellow), and SCL stretch (blue line).

Example I2C slave transmitter 3 bytes with 1st data flushed, NOSTRETCH=0:

EV1: ADDR ISR: check ADDCODE and DIR, set TXE, set ADDRCF
EV2: TXIS ISR: wr data1
EV3: TXIS ISR: wr data2
EV4: TXIS ISR: wr data3
EV5: TXIS ISR: wr data4 (not sent)

Example I2C slave transmitter 3 bytes without 1st data flush, NOSTRETCH=0:

EV1: ADDR ISR: check ADDCODE and DIR, set ADDRCF
EV2: TXIS ISR: wr data2
EV3: TXIS ISR: wr data3
EV4: TXIS ISR: wr data4 (not sent)

Example I2C slave transmitter 3 bytes, NOSTRETCH=1:

EV1: wr data1
EV2: TXIS ISR: wr data2
EV3: TXIS ISR: wr data3
EV4: TXIS ISR: wr data4 (not sent)
EV5: STOPF ISR: (optional: set TXE and TXIS), set STOPCF

MS19853V2

Timing diagram for I2C slave transmitter with 3 bytes and 1st data flushed. The diagram shows a sequence of bytes: S (reception), Address (reception), A (acknowledge), data1 (transmission), A (acknowledge), data2 (transmission), A (acknowledge), data3 (transmission), NA (no acknowledge), and P (stop). Events are marked: EV1 at ADDR, EV2 at TXIS (data1), EV3 at TXIS (data2), EV4 at TXIS (data3), and EV5 at TXIS (data4, not sent). The TXE line is shown as a pulse. A legend indicates transmission (white), reception (yellow), and SCL stretch (blue line). Timing diagram for I2C slave transmitter with 3 bytes without 1st data flush. The diagram shows a sequence of bytes: S (reception), Address (reception), A (acknowledge), data1 (transmission), A (acknowledge), data2 (transmission), A (acknowledge), data3 (transmission), NA (no acknowledge), and P (stop). Events are marked: EV1 at ADDR, EV2 at TXIS (data2), EV3 at TXIS (data3), and EV4 at TXIS (data4, not sent). The TXE line is shown as a pulse. A legend indicates transmission (white), reception (yellow), and SCL stretch (blue line). Timing diagram for I2C slave transmitter with 3 bytes and NOSTRETCH=1. The diagram shows a sequence of bytes: S (reception), Address (reception), A (acknowledge), data1 (transmission), A (acknowledge), data2 (transmission), A (acknowledge), data3 (transmission), NA (no acknowledge), and P (stop). Events are marked: EV1 at ADDR, EV2 at TXIS (data1), EV3 at TXIS (data2), EV4 at TXIS (data3), and EV5 at STOPF. The TXE line is shown as a pulse. A legend indicates transmission (white), reception (yellow), and SCL stretch (blue line).

For code example, refer to A.14.2: I2C slave transmitter code example .

Slave receiver

RXNE is set in I2C_ISR when the I2C_RXDR is full, and generates an interrupt if RXIE is set in I2C_CR1. RXNE is cleared when I2C_RXDR is read.

When a STOP is received and STOPIE is set in I2C_CR1, STOPF is set in I2C_ISR and an interrupt is generated.

Figure 160. Transfer sequence flow for slave receiver with NOSTRETCH = 0

Flowchart of slave receiver transfer sequence. It starts with 'Slave reception' (oval), followed by 'Slave initialization' (rectangle). A decision diamond 'I2C_ISR.ADDR = 1?' follows. If 'No', it loops back to 'Slave initialization'. If 'Yes', it proceeds to 'Read ADDCODE and DIR in I2C_ISR Set I2C_ICR.ADDRCF' (rectangle). Next is a decision diamond 'I2C_ISR.RXNE = 1?'. If 'No', it loops back to the entry point of the previous step. If 'Yes', it proceeds to 'Write I2C_RXDR.RXDATA' (rectangle), which then loops back to the entry point of the previous step. A vertical double-headed arrow on the right is labeled 'SCL stretched'.
graph TD; A([Slave reception]) --> B[Slave initialization]; B --> C{I2C_ISR.ADDR = 1?}; C -- No --> B; C -- Yes --> D[Read ADDCODE and DIR in I2C_ISR<br/>Set I2C_ICR.ADDRCF]; D --> E{I2C_ISR.RXNE = 1?}; E -- No --> D; E -- Yes --> F[Write I2C_RXDR.RXDATA]; F --> E;

MS19855V2

Flowchart of slave receiver transfer sequence. It starts with 'Slave reception' (oval), followed by 'Slave initialization' (rectangle). A decision diamond 'I2C_ISR.ADDR = 1?' follows. If 'No', it loops back to 'Slave initialization'. If 'Yes', it proceeds to 'Read ADDCODE and DIR in I2C_ISR Set I2C_ICR.ADDRCF' (rectangle). Next is a decision diamond 'I2C_ISR.RXNE = 1?'. If 'No', it loops back to the entry point of the previous step. If 'Yes', it proceeds to 'Write I2C_RXDR.RXDATA' (rectangle), which then loops back to the entry point of the previous step. A vertical double-headed arrow on the right is labeled 'SCL stretched'.

Figure 161. Transfer sequence flow for slave receiver with NOSTRETCH = 1

Flowchart for slave receiver transfer sequence with NOSTRETCH = 1. It starts with 'Slave reception' leading to 'Slave initialization'. A loop exists between 'Slave initialization' and a decision diamond 'I2C_ISR.RXNE = 1?'. If 'Yes', it goes to 'Read I2C_RXDR.RXDATA' and loops back to 'Slave initialization'. If 'No', it goes to another decision diamond 'I2C_ISR.STOPF = 1?'. If 'Yes', it goes to 'Set I2C_ICR.STOPCF' and loops back to 'Slave initialization'. If 'No', it loops back to 'Slave initialization'.
graph TD
    Start([Slave reception]) --> Init[Slave initialization]
    Init --> RXNE{I2C_ISR.RXNE = 1?}
    RXNE -- Yes --> Read[Read I2C_RXDR.RXDATA]
    Read --> Init
    RXNE -- No --> STOPF{I2C_ISR.STOPF = 1?}
    STOPF -- Yes --> StopCF[Set I2C_ICR.STOPCF]
    StopCF --> Init
    STOPF -- No --> Init
    

MS19856V2

Flowchart for slave receiver transfer sequence with NOSTRETCH = 1. It starts with 'Slave reception' leading to 'Slave initialization'. A loop exists between 'Slave initialization' and a decision diamond 'I2C_ISR.RXNE = 1?'. If 'Yes', it goes to 'Read I2C_RXDR.RXDATA' and loops back to 'Slave initialization'. If 'No', it goes to another decision diamond 'I2C_ISR.STOPF = 1?'. If 'Yes', it goes to 'Set I2C_ICR.STOPCF' and loops back to 'Slave initialization'. If 'No', it loops back to 'Slave initialization'.

Figure 162. Transfer bus diagrams for I2C slave receiver

Timing diagram for I2C slave receiver with NOSTRETCH=0. It shows a sequence of Address, Acknowledge (A), data1, Acknowledge (A), data2, Acknowledge (A), data3, Acknowledge (A). Events EV1 (ADDR ISR), EV2 (RXNE ISR), EV3 (RXNE ISR), and EV4 (RXNE ISR) are marked. The RXNE signal is shown as a pulse that goes high at EV1 and low at EV2, EV3, and EV4. Timing diagram for I2C slave receiver with NOSTRETCH=1. It shows a sequence of Address, Acknowledge (A), data 1, Acknowledge (A), data 2, Acknowledge (A), data 3, Acknowledge (A), Stop (P). Events EV1 (RXNE ISR), EV2 (RXNE ISR), EV3 (RXNE ISR), and EV4 (STOPF ISR) are marked. The RXNE signal is shown as a pulse that goes high at EV1 and low at EV2, EV3, and EV4.

Example I2C slave receiver 3 bytes, NOSTRETCH=0:

EV1: ADDR ISR: check ADDCODE and DIR, set ADDRCF
EV2: RXNE ISR: rd data1
EV3 : RXNE ISR: rd data2
EV4: RXNE ISR: rd data3

Example I2C slave receiver 3 bytes, NOSTRETCH=1:

EV1: RXNE ISR: rd data1
EV2: RXNE ISR: rd data2
EV3: RXNE ISR: rd data3
EV4: STOPF ISR: set STOPCF

legend:
transmission
reception
SCL stretch

MS19857V3

Timing diagram for I2C slave receiver with NOSTRETCH=0. It shows a sequence of Address, Acknowledge (A), data1, Acknowledge (A), data2, Acknowledge (A), data3, Acknowledge (A). Events EV1 (ADDR ISR), EV2 (RXNE ISR), EV3 (RXNE ISR), and EV4 (RXNE ISR) are marked. The RXNE signal is shown as a pulse that goes high at EV1 and low at EV2, EV3, and EV4. Timing diagram for I2C slave receiver with NOSTRETCH=1. It shows a sequence of Address, Acknowledge (A), data 1, Acknowledge (A), data 2, Acknowledge (A), data 3, Acknowledge (A), Stop (P). Events EV1 (RXNE ISR), EV2 (RXNE ISR), EV3 (RXNE ISR), and EV4 (STOPF ISR) are marked. The RXNE signal is shown as a pulse that goes high at EV1 and low at EV2, EV3, and EV4.

For code example, refer to A.14.3: I2C slave receiver code example .

20.4.9 I2C master mode

I2C master initialization

Before enabling the peripheral, the I2C master clock must be configured by setting the SCLH and SCLL bits in the I2C_TIMINGR register.

The STM32CubeMX tool calculates and provides the I2C_TIMINGR content in the I2C Configuration window.

A clock synchronization mechanism is implemented in order to support multi-master environment and slave clock stretching.

In order to allow clock synchronization:

The I2C detects its own SCL low level after a \( t_{\text{SYNC1}} \) delay depending on the SCL falling edge, SCL input noise filters (analog + digital) and SCL synchronization to the I2CxCLK clock. The I2C releases SCL to high level once the SCLL counter reaches the value programmed in the SCLL[7:0] bits in the I2C_TIMINGR register.

The I2C detects its own SCL high level after a \( t_{\text{SYNC2}} \) delay depending on the SCL rising edge, SCL input noise filters (analog + digital) and SCL synchronization to I2CxCLK clock. The I2C ties SCL to low level once the SCLH counter is reached reaches the value programmed in the SCLH[7:0] bits in the I2C_TIMINGR register.

Consequently the master clock period is:

\[ t_{\text{SCL}} = t_{\text{SYNC1}} + t_{\text{SYNC2}} + \{[(\text{SCLH}+1) + (\text{SCLL}+1)] \times (\text{PRESC}+1) \times t_{\text{I2CCLK}}\} \]

The duration of \( t_{\text{SYNC1}} \) depends on these parameters:

The duration of \( t_{\text{SYNC2}} \) depends on these parameters:

Figure 163. Master clock generation

Timing diagram for SCL master clock generation. It shows the SCL signal transitioning from a released state to a high level, then being driven low. The high level duration is SCLH and the low level duration is SCLL. The time from SCL released to SCL high level detected is tSYNC2. The time from SCL driven low to SCL low level detected is tSYNC1. The SCLH counter starts when SCL high level is detected, and the SCLL counter starts when SCL low level is detected. Timing diagram for SCL master clock synchronization. It shows a continuous SCL signal with multiple high and low periods. The high level duration is SCLH and the low level duration is SCLL. The SCLH counter starts when SCL high level is detected, and the SCLL counter starts when SCL low level is detected. The signal is driven low by another device during the low periods.

SCL master clock generation

SCL high level detected
SCLH counter starts

\( t_{SYNC2} \) SCLH SCLL \( t_{SYNC1} \)

SCL

SCL released

SCL driven low

SCL low level detected
SCLL counter starts

SCL master clock synchronization

SCL high level detected
SCLH counter starts

SCLH SCLL

SCL driven low by another device

SCL low level detected
SCLL counter starts

SCL released

SCL high level detected
SCLH counter starts

SCLH SCLL

SCL driven low by another device

SCL low level detected
SCLL counter starts

SCL high level detected
SCLH counter starts

SCLH SCLL

MS19858V1

Timing diagram for SCL master clock generation. It shows the SCL signal transitioning from a released state to a high level, then being driven low. The high level duration is SCLH and the low level duration is SCLL. The time from SCL released to SCL high level detected is tSYNC2. The time from SCL driven low to SCL low level detected is tSYNC1. The SCLH counter starts when SCL high level is detected, and the SCLL counter starts when SCL low level is detected. Timing diagram for SCL master clock synchronization. It shows a continuous SCL signal with multiple high and low periods. The high level duration is SCLH and the low level duration is SCLL. The SCLH counter starts when SCL high level is detected, and the SCLL counter starts when SCL low level is detected. The signal is driven low by another device during the low periods.

Caution: In order to be I 2 C or SMBus compliant, the master clock must respect the timings given the table below.

Table 91. I 2 C-SMBUS specification clock timings
SymbolParameterStandard-mode (Sm)Fast-mode (Fm)SMBUSUnit
MinMaxMinMaxMinMax
f SCLSCL clock frequency-100-400-100kHz
t HD:STAHold time (repeated) START condition4.0-0.6-4.0-µs
t SU:STASet-up time for a repeated START condition4.7-0.6-4.7-µs
t SU:STOSet-up time for STOP condition4.0-0.6-4.0-µs
t BUFBus free time between a STOP and START condition4.7-1.3-4.7-µs
t LOWLow period of the SCL clock4.7-1.3-4.7-µs
t HIGHPeriod of the SCL clock4.0-0.6-4.050µs
t rRise time of both SDA and SCL signals-1000-300-1000ns
t fFall time of both SDA and SCL signals-300-300-300ns

Note: SCLL is also used to generate the t BUF and t SU:STA timings.
SCLH is also used to generate the t HD:STA and t SU:STO timings.

Refer to Section 20.4.10: I2C_TIMINGR register configuration examples for examples of I2C_TIMINGR settings vs. I2CCLK frequency.

Master communication initialization (address phase)

In order to initiate the communication, the user must program the following parameters for the addressed slave in the I2C_CR2 register:

The user must then set the START bit in I2C_CR2 register. Changing all the above bits is not allowed when START bit is set.

Then the master automatically sends the START condition followed by the slave address as soon as it detects that the bus is free (BUSY = 0) and after a delay of t BUF .

In case of an arbitration loss, the master automatically switches back to slave mode and can acknowledge its own address if it is addressed as a slave.

  1. Note: The START bit is reset by hardware when the slave address has been sent on the bus, whatever the received acknowledge value. The START bit is also reset by hardware if an arbitration loss occurs.
    In 10-bit addressing mode, when the Slave Address first 7 bits is NACKed by the slave, the master re-launches automatically the slave address transmission until ACK is received. In this case ADDRCF must be set if a NACK is received from the slave, in order to stop sending the slave address.
    If the I2C is addressed as a slave (ADDR = 1) while the START bit is set, the I2C switches to slave mode and the START bit is cleared, when the ADDRGF bit is set.
  2. Note: The same procedure is applied for a Repeated Start condition. In this case BUSY = 1.

Figure 164. Master initialization flow

Flowchart of Master initialization flow
graph TD
    A([Master initialization]) --> B[Initial settings]
    B --> C[Enable interrupts and/or DMA in I2C_CR1]
    C --> D([End])
  

MS19859V2

Flowchart of Master initialization flow

For code example, refer to A.14.5: I2C configured in master mode to transmit code example .

Initialization of a master receiver addressing a 10-bit address slave

Figure 165. 10-bit address read access with HEAD10R = 0

Timing diagram for 10-bit address read access with HEAD10R = 0
1 1 1 1 0 X X 01 1 1 1 0 X X 1
SSlave address 1st 7 bitsR/WA1Slave address 2nd byteA2SrSlave address 1st 7 bitsR/WA3DATAADATAĀP
WriteRead

MSv41066V1

Timing diagram for 10-bit address read access with HEAD10R = 0

Figure 166. 10-bit address read access with HEAD10R = 1

Timing diagram for 10-bit address read access with HEAD10R = 1. The diagram shows two sequences of I2C transactions. The first sequence is a 'Write' transaction starting with a Start (S) condition, followed by a 10-bit slave address (1st 7 bits: 11110XX, 0; R/W bit: 0), an Acknowledge (A), the 2nd byte of the 10-bit address, another Acknowledge (A), a data byte (DATA), and a final Acknowledge (A/A). The second sequence is a 'Read' transaction starting with a Repeated Start (Sr) condition, followed by the same 10-bit slave address (1st 7 bits: 11110XX, 1; R/W bit: 1), an Acknowledge (A), a data byte (DATA), a Not Acknowledge (A-bar), and a Stop (P) condition. The diagram is labeled MS19823V1.
Timing diagram for 10-bit address read access with HEAD10R = 1. The diagram shows two sequences of I2C transactions. The first sequence is a 'Write' transaction starting with a Start (S) condition, followed by a 10-bit slave address (1st 7 bits: 11110XX, 0; R/W bit: 0), an Acknowledge (A), the 2nd byte of the 10-bit address, another Acknowledge (A), a data byte (DATA), and a final Acknowledge (A/A). The second sequence is a 'Read' transaction starting with a Repeated Start (Sr) condition, followed by the same 10-bit slave address (1st 7 bits: 11110XX, 1; R/W bit: 1), an Acknowledge (A), a data byte (DATA), a Not Acknowledge (A-bar), and a Stop (P) condition. The diagram is labeled MS19823V1.

Master transmitter

In the case of a write transfer, the TXIS flag is set after each byte transmission, after the ninth SCL pulse when an ACK is received.

A TXIS event generates an interrupt if the TXIE bit is set in the I2C_CR1 register. The flag is cleared when the I2C_TXDR register is written with the next data byte to be transmitted.

The number of TXIS events during the transfer corresponds to the value programmed in NBBYTES[7:0]. If the total number of data bytes to be sent is greater than 255, reload mode must be selected by setting the RELOAD bit in the I2C_CR2 register. In this case, when NBBYTES data have been transferred, the TCR flag is set and the SCL line is stretched low until NBBYTES[7:0] is written to a non-zero value.

The TXIS flag is not set when a NACK is received.

A RESTART condition can be requested by setting the START bit in the I2C_CR2 register with the proper slave address configuration, and number of bytes to be transferred. Setting the START bit clears the TC flag and the START condition is sent on the bus.

A STOP condition can be requested by setting the STOP bit in the I2C_CR2 register. Setting the STOP bit clears the TC flag and the STOP condition is sent on the bus.

Figure 167. Transfer sequence flow for I2C master transmitter for N≤255 bytes

Flowchart for I2C master transmitter transfer sequence. It starts with 'Master transmission' (oval), followed by 'Master initialization' (rectangle), and then a configuration block: 'NBYS = N, AUTOEND = 0 for RESTART; 1 for STOP, Configure slave address, Set I2C_CR2.START'. A loop begins with a decision 'I2C_ISR.NACKF = 1?'. If 'Yes', it ends at 'End' (oval). If 'No', it proceeds to 'I2C_ISR.TXIS = 1?'. If 'Yes', it goes to 'Write I2C_TXDR' (rectangle), then to 'NBYS transmitted?' (decision). If 'Yes', it goes to 'I2C_ISR.TC = 1?' (decision). If 'Yes', it goes to 'Set I2C_CR2.START with slave address NBYS ...' (rectangle), then to 'End' (oval). If 'No', it loops back to the start of the loop. If 'No' at 'I2C_ISR.TXIS = 1?', it loops back to the start of the loop. If 'No' at 'NBYS transmitted?', it loops back to the start of the loop.
graph TD; Start([Master transmission]) --> Init[Master initialization]; Init --> Config["NBYS = N<br/>AUTOEND = 0 for RESTART; 1 for STOP<br/>Configure slave address<br/>Set I2C_CR2.START"]; Config --> LoopStart(( )); LoopStart --> NACKF{"I2C_ISR.NACKF = 1?"}; NACKF -- Yes --> End1([End]); NACKF -- No --> TXIS{"I2C_ISR.TXIS = 1?"}; TXIS -- Yes --> Write[Write I2C_TXDR]; TXIS -- No --> LoopStart; Write --> NBYSTrans{"NBYS transmitted?"}; NBYSTrans -- Yes --> TC{"I2C_ISR.TC = 1?"}; NBYSTrans -- No --> LoopStart; TC -- Yes --> SetStart["Set I2C_CR2.START with<br/>slave address NBYS ..."]; TC -- No --> End2([End]); SetStart --> End2; style LoopStart fill:none,stroke:none
Flowchart for I2C master transmitter transfer sequence. It starts with 'Master transmission' (oval), followed by 'Master initialization' (rectangle), and then a configuration block: 'NBYS = N, AUTOEND = 0 for RESTART; 1 for STOP, Configure slave address, Set I2C_CR2.START'. A loop begins with a decision 'I2C_ISR.NACKF = 1?'. If 'Yes', it ends at 'End' (oval). If 'No', it proceeds to 'I2C_ISR.TXIS = 1?'. If 'Yes', it goes to 'Write I2C_TXDR' (rectangle), then to 'NBYS transmitted?' (decision). If 'Yes', it goes to 'I2C_ISR.TC = 1?' (decision). If 'Yes', it goes to 'Set I2C_CR2.START with slave address NBYS ...' (rectangle), then to 'End' (oval). If 'No', it loops back to the start of the loop. If 'No' at 'I2C_ISR.TXIS = 1?', it loops back to the start of the loop. If 'No' at 'NBYS transmitted?', it loops back to the start of the loop.

MS19860V2

Figure 168. Transfer sequence flow for I2C master transmitter for N>255 bytes

Flowchart for I2C master transmitter transfer sequence for N>255 bytes. The process starts with 'Master transmission' (oval), followed by 'Master initialization' (rectangle). A loop begins with 'NBYS = 0xFF; N=N-255; RELOAD = 1; Configure slave address; Set I2C_CR2.START' (rectangle). A decision diamond 'I2C_ISR.TXIS = 1?' leads to 'Write I2C_TXDR' (rectangle) if 'Yes', or back to the start of the loop if 'No'. From 'Write I2C_TXDR', a decision diamond 'NBYS transmitted ?' leads to 'I2C_ISR.TC = 1?' (diamond) if 'Yes', or back to the start of the loop if 'No'. From 'I2C_ISR.TC = 1?', a 'Yes' leads to 'Set I2C_CR2.START with slave address NBYS ...' (rectangle), which then leads to an 'End' (oval) via a dotted line. A 'No' leads to 'I2C_ISR.TCR = 1?' (diamond). From 'I2C_ISR.TCR = 1?', a 'Yes' leads to a large rectangle containing: 'IF N< 256: NBYS = N; N = 0; RELOAD = 0; AUTOEND = 0 for RESTART; 1 for STOP; ELSE: NBYS = 0xFF; N = N-255; RELOAD = 1'. A 'No' from 'I2C_ISR.TCR = 1?' also leads to the 'End' (oval). A 'No' from the initial 'I2C_ISR.TXIS = 1?' decision also leads to the 'End' (oval). A 'No' from 'I2C_ISR.NACKF = 1?' leads to the 'End' (oval). A 'Yes' from 'I2C_ISR.NACKF = 1?' leads to 'End' (oval).
graph TD; Start([Master transmission]) --> Init[Master initialization]; Init --> LoopStart[NBYS = 0xFF; N=N-255; RELOAD = 1; Configure slave address; Set I2C_CR2.START]; LoopStart --> TXIS{I2C_ISR.TXIS = 1?}; TXIS -- No --> LoopStart; TXIS -- Yes --> WriteTXDR[Write I2C_TXDR]; WriteTXDR --> NBYSTrans{NBYS transmitted ?}; NBYSTrans -- No --> LoopStart; NBYSTrans -- Yes --> TC{I2C_ISR.TC = 1?}; TC -- Yes --> SetStart[Set I2C_CR2.START with slave address NBYS ...]; SetStart -.-> End1([End]); TC -- No --> TCR{I2C_ISR.TCR = 1?}; TCR -- Yes --> EndCondition[IF N< 256: NBYS = N; N = 0; RELOAD = 0; AUTOEND = 0 for RESTART; 1 for STOP; ELSE: NBYS = 0xFF; N = N-255; RELOAD = 1]; EndCondition --> LoopStart; TCR -- No --> End2([End]); NACKF{I2C_ISR.NACKF = 1?} -- No --> End3([End]); NACKF -- Yes --> End4([End]);
Flowchart for I2C master transmitter transfer sequence for N>255 bytes. The process starts with 'Master transmission' (oval), followed by 'Master initialization' (rectangle). A loop begins with 'NBYS = 0xFF; N=N-255; RELOAD = 1; Configure slave address; Set I2C_CR2.START' (rectangle). A decision diamond 'I2C_ISR.TXIS = 1?' leads to 'Write I2C_TXDR' (rectangle) if 'Yes', or back to the start of the loop if 'No'. From 'Write I2C_TXDR', a decision diamond 'NBYS transmitted ?' leads to 'I2C_ISR.TC = 1?' (diamond) if 'Yes', or back to the start of the loop if 'No'. From 'I2C_ISR.TC = 1?', a 'Yes' leads to 'Set I2C_CR2.START with slave address NBYS ...' (rectangle), which then leads to an 'End' (oval) via a dotted line. A 'No' leads to 'I2C_ISR.TCR = 1?' (diamond). From 'I2C_ISR.TCR = 1?', a 'Yes' leads to a large rectangle containing: 'IF N< 256: NBYS = N; N = 0; RELOAD = 0; AUTOEND = 0 for RESTART; 1 for STOP; ELSE: NBYS = 0xFF; N = N-255; RELOAD = 1'. A 'No' from 'I2C_ISR.TCR = 1?' also leads to the 'End' (oval). A 'No' from the initial 'I2C_ISR.TXIS = 1?' decision also leads to the 'End' (oval). A 'No' from 'I2C_ISR.NACKF = 1?' leads to the 'End' (oval). A 'Yes' from 'I2C_ISR.NACKF = 1?' leads to 'End' (oval).

MS19861V3

Figure 169. Transfer bus diagrams for I2C master transmitter

Timing diagram for I2C master transmitter with 2 bytes in automatic end mode. The bus sequence is: S (Start), Address (reception), A (Ack, transmission), data1 (reception), A (Ack, transmission), data2 (reception), A (Ack, transmission), P (Stop, transmission). Signal lines show: TXE (Transmit data register empty) goes high after INIT, low when data1 is written at EV1, high again, low when data2 is written at EV2, and high after transmission. NBYTES register transitions from XX to 2 at INIT. Event markers: INIT (Start), EV1 (TXIS), EV2 (TXIS). Timing diagram for I2C master transmitter with 2 bytes in software end mode. The bus sequence is: S (Start), Address (reception), A (Ack, transmission), data1 (reception), A (Ack, transmission), data2 (reception), A (Ack, transmission), TC (Transfer Complete), ReS (Restart, transmission), Address (reception). Signal lines show: TXE transitions similarly to the first diagram. NBYTES transitions from XX to 2 at INIT. Event markers: INIT, EV1 (TXIS), EV2 (TXIS), EV3 (TC).

Example I2C master transmitter 2 bytes, automatic end mode (STOP)

legend:
transmission
reception
SCL stretch

INIT: program Slave address, program NBYTES = 2, AUTOEND=1, set START
EV1: TXIS ISR: wr data1
EV2: TXIS ISR: wr data2


Example I2C master transmitter 2 bytes, software end mode (RESTART)

legend:
transmission
reception
SCL stretch

INIT: program Slave address, program NBYTES = 2, AUTOEND=0, set START
EV1: TXIS ISR: wr data1
EV2: TXIS ISR: wr data2
EV3: TC ISR: program Slave address, program NBYTES = N, set START

MS19862V2

Timing diagram for I2C master transmitter with 2 bytes in automatic end mode. The bus sequence is: S (Start), Address (reception), A (Ack, transmission), data1 (reception), A (Ack, transmission), data2 (reception), A (Ack, transmission), P (Stop, transmission). Signal lines show: TXE (Transmit data register empty) goes high after INIT, low when data1 is written at EV1, high again, low when data2 is written at EV2, and high after transmission. NBYTES register transitions from XX to 2 at INIT. Event markers: INIT (Start), EV1 (TXIS), EV2 (TXIS). Timing diagram for I2C master transmitter with 2 bytes in software end mode. The bus sequence is: S (Start), Address (reception), A (Ack, transmission), data1 (reception), A (Ack, transmission), data2 (reception), A (Ack, transmission), TC (Transfer Complete), ReS (Restart, transmission), Address (reception). Signal lines show: TXE transitions similarly to the first diagram. NBYTES transitions from XX to 2 at INIT. Event markers: INIT, EV1 (TXIS), EV2 (TXIS), EV3 (TC).

For code example, refer to A.14.6: I2C master transmitter code example .

Master receiver

In the case of a read transfer, the RXNE flag is set after each byte reception, after the eighth SCL pulse. An RXNE event generates an interrupt if the RXIE bit is set in the I2C_CR1 register. The flag is cleared when I2C_RXDR is read.

If the total number of data bytes to be received is greater than 255, reload mode must be selected by setting the RELOAD bit in the I2C_CR2 register. In this case, when NBBYTES[7:0] data have been transferred, the TCR flag is set and the SCL line is stretched low until NBBYTES[7:0] is written to a non-zero value.

A RESTART condition can be requested by setting the START bit in the I2C_CR2 register with the proper slave address configuration, and number of bytes to be transferred. Setting the START bit clears the TC flag and the START condition, followed by slave address, are sent on the bus.

A STOP condition can be requested by setting the STOP bit in the I2C_CR2 register. Setting the STOP bit clears the TC flag and the STOP condition is sent on the bus.

Figure 170. Transfer sequence flow for I2C master receiver for N≤255 bytes

Flowchart for I2C master receiver transfer sequence. Steps include: Master reception (start), Master initialization, NBYPES = N, AUTOEND = 0 for RESTART; 1 for STOP, Configure slave address, Set I2C_CR2.START. Loop: I2C_ISR.RXNE = 1? (No loops back to start of loop, Yes leads to Read I2C_RXDR). Loop: NBYPES received? (No loops back to start of loop, Yes leads to I2C_ISR.TC = 1?). I2C_ISR.TC = 1? (Yes leads to Set I2C_CR2.START with slave address NBYPES ..., No leads to End).
graph TD; Start([Master reception]) --> Init[Master initialization]; Init --> Config["NBYPES = N<br/>AUTOEND = 0 for RESTART; 1 for STOP<br/>Configure slave address<br/>Set I2C_CR2.START"]; Config --> RXNE{"I2C_ISR.RXNE<br/>=1?"}; RXNE -- No --> RXNE; RXNE -- Yes --> Read[Read I2C_RXDR]; Read --> NBYPES{"NBYPES<br/>received?"}; NBYPES -- No --> RXNE; NBYPES -- Yes --> TC{"I2C_ISR.TC<br/>=1?"}; TC -- Yes --> SetCR2["Set I2C_CR2.START with<br/>slave address NBYPES ..."]; TC -- No --> End([End]); SetCR2 -.-> End;
Flowchart for I2C master receiver transfer sequence. Steps include: Master reception (start), Master initialization, NBYPES = N, AUTOEND = 0 for RESTART; 1 for STOP, Configure slave address, Set I2C_CR2.START. Loop: I2C_ISR.RXNE = 1? (No loops back to start of loop, Yes leads to Read I2C_RXDR). Loop: NBYPES received? (No loops back to start of loop, Yes leads to I2C_ISR.TC = 1?). I2C_ISR.TC = 1? (Yes leads to Set I2C_CR2.START with slave address NBYPES ..., No leads to End).

MS19863V2

Figure 171. Transfer sequence flow for I2C master receiver for N >255 bytes

Flowchart for I2C master receiver transfer sequence for N > 255 bytes.
graph TD; Start([Master reception]) --> Init[Master initialization]; Init --> Config["NBYTES = 0xFF; N=N-255<br>RELOAD =1<br>Configure slave address<br>Set I2C_CR2.START"]; Config --> RXNE{I2C_ISR.RXNE =1?}; RXNE -- No --> RXNE; RXNE -- Yes --> Read[Read I2C_RXDR]; Read --> Nbytes{NBYTES received?}; Nbytes -- No --> RXNE; Nbytes -- Yes --> TC{I2C_ISR.TC = 1?}; TC -- Yes --> SetStart["Set I2C_CR2.START with<br>slave address NBYTES ..."]; SetStart -.-> Dots[...]; Dots -.-> TCR{I2C_ISR.TCR = 1?}; TC -- No --> TCR; TCR -- Yes --> EndCond["IF N< 256<br>NBYTES =N; N=0;RELOAD=0<br>AUTOEND=0 for RESTART; 1 for STOP<br>ELSE<br>NBYTES =0xFF;N=N-255<br>RELOAD=1"]; TCR -- No --> End([End]); EndCond --> End; EndCond --> RXNE;

The flowchart illustrates the transfer sequence for an I2C master receiver when the number of bytes to receive (N) is greater than 255. It begins with 'Master reception', followed by 'Master initialization'. The next step is a configuration block: 'NBYTES = 0xFF; N=N-255', 'RELOAD =1', 'Configure slave address', and 'Set I2C_CR2.START'. A loop begins with a decision 'I2C_ISR.RXNE =1?'. If 'No', it loops back to the same point. If 'Yes', it proceeds to 'Read I2C_RXDR'. Another decision 'NBYTES received?' follows. If 'No', it loops back to the 'I2C_ISR.RXNE =1?' decision. If 'Yes', it proceeds to 'I2C_ISR.TC = 1?'. If 'Yes', it goes to 'Set I2C_CR2.START with slave address NBYTES ...', which then leads via a dashed line to 'I2C_ISR.TCR = 1?'. If 'No' from 'I2C_ISR.TC = 1?', it also leads to 'I2C_ISR.TCR = 1?'. If 'Yes' from 'I2C_ISR.TCR = 1?', it enters a conditional block: 'IF N< 256' (then 'NBYTES =N; N=0;RELOAD=0' and 'AUTOEND=0 for RESTART; 1 for STOP'), 'ELSE' (then 'NBYTES =0xFF;N=N-255' and 'RELOAD=1'). Finally, it reaches 'End' and loops back to the 'I2C_ISR.RXNE =1?' decision.

Flowchart for I2C master receiver transfer sequence for N > 255 bytes.

MS19864V2

Figure 172. Transfer bus diagrams for I2C master receiver

Timing diagram for I2C master receiver in automatic end mode (STOP). The sequence shows a start condition (S), slave address, acknowledge (A), data1, acknowledge (A), data2, not-acknowledge (NA), and stop condition (P). RXNE flags are raised after data1 and data2. NBYTES is set to 2. Legend indicates white for transmission, yellow for reception, and a blue line for SCL stretch. Timing diagram for I2C master receiver in software end mode (RESTART). The sequence shows a start condition (S), slave address, acknowledge (A), data1, acknowledge (A), data2, not-acknowledge (NA), and a Transfer Complete (TC) flag. This is followed by a Restart (ReS) and a new Address. NBYTES is initially 2, then changed to N. Legend indicates white for transmission, yellow for reception, and a blue line for SCL stretch.

Example I2C master receiver 2 bytes, automatic end mode (STOP)

INIT: program Slave address, program NBYTES = 2, AUTOEND=1, set START
EV1: RXNE ISR: rd data1
EV2: RXNE ISR: rd data2

Example I2C master receiver 2 bytes, software end mode (RESTART)

INIT: program Slave address, program NBYTES = 2, AUTOEND=0, set START
EV1: RXNE ISR: rd data1
EV2: RXNE ISR: read data2
EV3: TC ISR: program Slave address, program NBYTES = N, set START

MS19865V1

Timing diagram for I2C master receiver in automatic end mode (STOP). The sequence shows a start condition (S), slave address, acknowledge (A), data1, acknowledge (A), data2, not-acknowledge (NA), and stop condition (P). RXNE flags are raised after data1 and data2. NBYTES is set to 2. Legend indicates white for transmission, yellow for reception, and a blue line for SCL stretch. Timing diagram for I2C master receiver in software end mode (RESTART). The sequence shows a start condition (S), slave address, acknowledge (A), data1, acknowledge (A), data2, not-acknowledge (NA), and a Transfer Complete (TC) flag. This is followed by a Restart (ReS) and a new Address. NBYTES is initially 2, then changed to N. Legend indicates white for transmission, yellow for reception, and a blue line for SCL stretch.

For code example refer to A.14.7: I2C master receiver code example .

20.4.10 I2C_TIMINGR register configuration examples

The tables below provide examples of how to program the I2C_TIMINGR to obtain timings compliant with the I 2 C specification. In order to get more accurate configuration values, the STM32CubeMX tool (I2C Configuration window) must be used.

Table 92. Examples of timing settings for \( f_{I2CCLK} = 8 \) MHz

ParameterStandard-mode (Sm)Fast-mode (Fm)
10 kHz100 kHz400 kHz
PRESC110
SCLL0xC70x130x9
\( t_{SCLL} \)200x250 ns = 50 µs20x250 ns = 5.0 µs10x125 ns = 1250 ns
SCLH0xC30xF0x3
\( t_{SCLH} \)196x250 ns = 49 µs16x250 ns = 4.0µs4x125ns = 500ns
\( t_{SCL}^{(1)} \)~100 µs (2)~10 µs (2)~2500 ns (3)
SDADEL0x20x20x1
\( t_{SDADEL} \)2x250 ns = 500 ns2x250 ns = 500 ns1x125 ns = 125 ns
SCLDEL0x40x40x3
\( t_{SCLDEL} \)5x250 ns = 1250 ns5x250 ns = 1250 ns4x125 ns = 500 ns
  1. 1. SCL period \( t_{SCL} \) is greater than \( t_{SCLL} + t_{SCLH} \) due to SCL internal detection delay. Values provided for \( t_{SCL} \) are examples only.
  2. 2. \( t_{SYNC1} + t_{SYNC2} \) minimum value is \( 4 \times t_{I2CCLK} = 500 \) ns. Example with \( t_{SYNC1} + t_{SYNC2} = 1000 \) ns
  3. 3. \( t_{SYNC1} + t_{SYNC2} \) minimum value is \( 4 \times t_{I2CCLK} = 500 \) ns. Example with \( t_{SYNC1} + t_{SYNC2} = 750 \) ns

Table 93. Examples of timings settings for \( f_{I2CCLK} = 16 \) MHz

ParameterStandard-mode (Sm)Fast-mode (Fm)
10 kHz100 kHz400 kHz
PRESC331
SCLL0xC70x130x9
\( t_{SCLL} \)200 x 250 ns = 50 µs20 x 250 ns = 5.0 µs10 x 125 ns = 1250 ns
SCLH0xC30xF0x3
\( t_{SCLH} \)196 x 250 ns = 49 µs16 x 250 ns = 4.0 µs4 x 125ns = 500 ns
\( t_{SCL}^{(1)} \)~100 µs (2)~10 µs (2)~2500 ns (3)
SDADEL0x20x20x2
\( t_{SDADEL} \)2 x 250 ns = 500 ns2 x 250 ns = 500 ns2 x 125 ns = 250 ns
SCLDEL0x40x40x3
\( t_{SCLDEL} \)5 x 250 ns = 1250 ns5 x 250 ns = 1250 ns4 x 125 ns = 500 ns
  1. 1. SCL period \( t_{SCL} \) is greater than \( t_{SCLL} + t_{SCLH} \) due to SCL internal detection delay. Values provided for \( t_{SCL} \) are examples only.
  2. 2. \( t_{SYNC1} + t_{SYNC2} \) minimum value is \( 4 \times t_{I2CCLK} = 250 \) ns. Example with \( t_{SYNC1} + t_{SYNC2} = 1000 \) ns
  1. 3. \( t_{SYNC1} + t_{SYNC2} \) minimum value is \( 4 \times t_{I2CCLK} = 250 \) ns. Example with \( t_{SYNC1} + t_{SYNC2} = 750 \) ns

20.4.11 SMBus specific features

This section is relevant only when SMBus feature is supported. Refer to Section 20.3: I2C implementation .

Introduction

The system management bus (SMBus) is a two-wire interface through which various devices can communicate with each other and with the rest of the system. It is based on I 2 C principles of operation. The SMBus provides a control bus for system and power management related tasks.

This peripheral is compatible with the SMBus specification ( http://smbus.org ).

The System Management Bus Specification refers to three types of devices.

This peripheral can be configured as master or slave device, and also as a host.

Bus protocols

There are eleven possible command protocols for any given device. A device may use any or all of the eleven protocols to communicate. The protocols are Quick Command, Send Byte, Receive Byte, Write Byte, Write Word, Read Byte, Read Word, Process Call, Block Read, Block Write and Block Write-Block Read Process Call. These protocols should be implemented by the user software.

For more details of these protocols, refer to SMBus specification ( http://smbus.org ).

Address resolution protocol (ARP)

SMBus slave address conflicts can be resolved by dynamically assigning a new unique address to each slave device. In order to provide a mechanism to isolate each device for the purpose of address assignment each device must implement a unique device identifier (UDID). This 128-bit number is implemented by software.

This peripheral supports the Address Resolution Protocol (ARP). The SMBus Device Default Address (0b1100 001) is enabled by setting SMBDEN bit in I2C_CR1 register. The ARP commands should be implemented by the user software.

Arbitration is also performed in slave mode for ARP support.

For more details of the SMBus address resolution protocol, refer to SMBus specification ( http://smbus.org ).

Received command and data acknowledge control

A SMBus receiver must be able to NACK each received command or data. In order to allow the ACK control in slave mode, the Slave Byte Control mode must be enabled by setting SBC bit in I2C_CR1 register. Refer to Slave byte control mode on page 519 for more details.

Host notify protocol

This peripheral supports the host notify protocol by setting the SMBHEN bit in the I2C_CR1 register. In this case the host acknowledges the SMBus host address (0b0001 000).

When this protocol is used, the device acts as a master and the host as a slave.

SMBus alert

The SMBus ALERT optional signal is supported. A slave-only device can signal the host through the SMBALERT# pin that it wants to talk. The host processes the interrupt and simultaneously accesses all SMBALERT# devices through the alert response address (0b0001 100). Only the device(s) which pulled SMBALERT# low acknowledges the alert response address.

When configured as a slave device (SMBHEN=0), the SMBA pin is pulled low by setting the ALERTEN bit in the I2C_CR1 register. The Alert Response Address is enabled at the same time.

When configured as a host (SMBHEN=1), the ALERT flag is set in the I2C_ISR register when a falling edge is detected on the SMBA pin and ALERTEN=1. An interrupt is generated if the ERRIE bit is set in the I2C_CR1 register. When ALERTEN=0, the ALERT line is considered high even if the external SMBA pin is low.

If the SMBus ALERT pin is not needed, the SMBA pin can be used as a standard GPIO if ALERTEN=0.

Packet error checking

A packet error checking mechanism has been introduced in the SMBus specification to improve reliability and communication robustness. The packet error checking is implemented by appending a packet error code (PEC) at the end of each message transfer. The PEC is calculated by using the \( C(x) = x^8 + x^2 + x + 1 \) CRC-8 polynomial on all the message bytes (including addresses and read/write bits).

The peripheral embeds a hardware PEC calculator and allows a not acknowledge to be sent automatically when the received byte does not match with the hardware calculated PEC.

Timeouts

This peripheral embeds hardware timers in order to be compliant with the 3 timeouts defined in SMBus specification.

Table 94. SMBus timeout specifications

SymbolParameterLimitsUnit
MinMax
\( t_{\text{TIMEOUT}} \)Detect clock low timeout2535ms

Table 94. SMBus timeout specifications (continued)

SymbolParameterLimitsUnit
MinMax
\( t_{LOW:SEXT}^{(1)} \)Cumulative clock low extend time (slave device)-25ms
\( t_{LOW:MEXT}^{(2)} \)Cumulative clock low extend time (master device)-10ms
  1. \( t_{LOW:SEXT} \) is the cumulative time a given slave device is allowed to extend the clock cycles in one message from the initial START to the STOP. It is possible that, another slave device or the master also extends the clock causing the combined clock low extend time to be greater than \( t_{LOW:SEXT} \) . Therefore, this parameter is measured with the slave device as the sole target of a full-speed master.
  2. \( t_{LOW:MEXT} \) is the cumulative time a master device is allowed to extend its clock cycles within each byte of a message as defined from START-to-ACK, ACK-to-ACK, or ACK-to-STOP. It is possible that a slave device or another master also extends the clock causing the combined clock low time to be greater than \( t_{LOW:MEXT} \) on a given byte. Therefore, this parameter is measured with a full speed slave device as the sole target of the master.
Figure 173. Timeout intervals for\( t_{LOW:SEXT} \) , \( t_{LOW:MEXT} \) Timing diagram showing SMBus signals (SMBCLK and SMBDAT) over time. The diagram illustrates the timeout intervals for t_LOW:SEXT (slave device) and t_LOW:MEXT (master device). The t_LOW:SEXT interval is shown as the cumulative time from the initial START to the STOP. The t_LOW:MEXT interval is shown as the cumulative time within each byte of a message, specifically from START-to-ACK, ACK-to-ACK, or ACK-to-STOP. The diagram includes labels for Start, Stop, ClkAck, and the corresponding signal levels for SMBCLK and SMBDAT.

The diagram shows two signal lines, SMBCLK and SMBDAT, over time. The SMBCLK line is a periodic square wave. The SMBDAT line shows data transitions. The diagram is divided into three main sections by vertical dashed lines: Start, ClkAck, and Stop. The first section (Start to ClkAck) shows the initial clock cycle. The second section (ClkAck to ClkAck) shows a subsequent clock cycle. The third section (ClkAck to Stop) shows the final clock cycle. The \( t_{LOW:SEXT} \) interval is indicated by a long double-headed arrow spanning from the Start line to the Stop line. The \( t_{LOW:MEXT} \) intervals are indicated by three shorter double-headed arrows, each spanning from a rising edge of SMBCLK to the next rising edge of SMBCLK (or Stop line). The first \( t_{LOW:MEXT} \) interval is from the first rising edge to the first ClkAck. The second is from the first ClkAck to the second ClkAck. The third is from the second ClkAck to the Stop line. The ClkAck labels are placed above the rising edges of the SMBCLK line.

Timing diagram showing SMBus signals (SMBCLK and SMBDAT) over time. The diagram illustrates the timeout intervals for t_LOW:SEXT (slave device) and t_LOW:MEXT (master device). The t_LOW:SEXT interval is shown as the cumulative time from the initial START to the STOP. The t_LOW:MEXT interval is shown as the cumulative time within each byte of a message, specifically from START-to-ACK, ACK-to-ACK, or ACK-to-STOP. The diagram includes labels for Start, Stop, ClkAck, and the corresponding signal levels for SMBCLK and SMBDAT.

MS19866V1

Bus idle detection

A master can assume that the bus is free if it detects that the clock and data signals have been high for \( t_{IDLE} \) greater than \( t_{HIGH,MAX} \) . (refer to Table 89: I2C-SMBUS specification data setup and hold times )

This timing parameter covers the condition where a master has been dynamically added to the bus and may not have detected a state transition on the SMBCLK or SMBDAT lines. In this case, the master must wait long enough to ensure that a transfer is not currently in progress. The peripheral supports a hardware bus idle detection.

20.4.12 SMBus initialization

This section is relevant only when SMBus feature is supported. Refer to Section 20.3: I2C implementation .

In addition to I2C initialization, some other specific initialization must be done in order to perform SMBus communication:

Received command and data acknowledge control (Slave mode)

A SMBus receiver must be able to NACK each received command or data. In order to allow ACK control in slave mode, the Slave byte control mode must be enabled by setting the SBC bit in the I2C_CR1 register. Refer to Slave byte control mode on page 519 for more details.

Specific address (Slave mode)

The specific SMBus addresses must be enabled if needed. Refer to Bus idle detection on page 542 for more details.

Packet error checking

PEC calculation is enabled by setting the PECEN bit in the I2C_CR1 register. Then the PEC transfer is managed with the help of a hardware byte counter: NBBYTES[7:0] in the I2C_CR2 register. The PECEN bit must be configured before enabling the I2C.

The PEC transfer is managed with the hardware byte counter, so the SBC bit must be set when interfacing the SMBus in slave mode. The PEC is transferred after NBBYTES - 1 data have been transferred when the PECBYTE bit is set and the RELOAD bit is cleared. If RELOAD is set, PECBYTE has no effect.

Caution: Changing the PECEN configuration is not allowed when the I2C is enabled.

Table 95. SMBus with PEC configuration

ModeSBC bitRELOAD bitAUTOEND bitPECBYTE bit
Master Tx/Rx NBYTES + PEC+ STOPx011
Master Tx/Rx NBYTES + PEC + ReSTARTx001
Slave Tx/Rx with PEC10x1

Timeout detection

The timeout detection is enabled by setting the TIMOUTEN and TEXTEN bits in the I2C_TIMEOUTR register. The timers must be programmed in such a way that they detect a timeout before the maximum time given in the SMBus specification.

In order to enable the \( t_{\text{TIMEOUT}} \) check, the 12-bit TIMEOUTA[11:0] bits must be programmed with the timer reload value in order to check the \( t_{\text{TIMEOUT}} \) parameter. The TIDLE bit must be configured to '0' in order to detect the SCL low level timeout.

Then the timer is enabled by setting the TIMOUTEN in the I2C_TIMEOUTR register.

If SCL is tied low for a time greater than \( (\text{TIMEOUTA}+1) \times 2048 \times t_{\text{I2CCLK}} \) , the TIMEOUT flag is set in the I2C_ISR register.

Refer to Table 96: Examples of TIMEOUTA settings for various I2CCLK frequencies (max \( t_{\text{TIMEOUT}} = 25 \text{ ms} \) ) .

Caution: Changing the TIMEOUTA[11:0] bits and TIDLE bit configuration is not allowed when the TIMOUTEN bit is set.

Depending on if the peripheral is configured as a master or as a slave, The 12-bit TIMEOUTB timer must be configured in order to check \( t_{\text{LOW:SEXT}} \) for a slave and \( t_{\text{LOW:MEXT}} \) for a master. As the standard specifies only a maximum, the user can choose the same value for the both.

Then the timer is enabled by setting the TEXTEN bit in the I2C_TIMEOUTR register.

If the SMBus peripheral performs a cumulative SCL stretch for a time greater than \( (\text{TIMEOUTB}+1) \times 2048 \times t_{\text{I2CCLK}} \) , and in the timeout interval described in Bus idle detection on page 542 section, the TIMEOUT flag is set in the I2C_ISR register.

Refer to Table 97: Examples of TIMEOUTB settings for various I2CCLK frequencies

Caution: Changing the TIMEOUTB configuration is not allowed when the TEXTEN bit is set.

Bus idle detection

In order to enable the \( t_{\text{IDLE}} \) check, the 12-bit TIMEOUTA[11:0] field must be programmed with the timer reload value in order to obtain the \( t_{\text{IDLE}} \) parameter. The TIDLE bit must be configured to '1' in order to detect both SCL and SDA high level timeout.

Then the timer is enabled by setting the TIMOUTEN bit in the I2C_TIMEOUTR register.

If both the SCL and SDA lines remain high for a time greater than \( (\text{TIMEOUTA}+1) \times 4 \times t_{\text{I2CCLK}} \) , the TIMEOUT flag is set in the I2C_ISR register.

Refer to Table 98: Examples of TIMEOUTA settings for various I2CCLK frequencies (max \( t_{\text{IDLE}} = 50 \text{ } \mu\text{s} \) )

Caution: Changing the TIMEOUTA and TIDLE configuration is not allowed when the TIMEOUTEN is set.

20.4.13 SMBus: I2C_TIMEOUTR register configuration examples

This section is relevant only when SMBus feature is supported. Refer to Section 20.3: I2C implementation .

Table 96. Examples of TIMEOUTA settings for various I2CCLK frequencies
(max \( t_{\text{TIMEOUT}} = 25 \) ms)

\( f_{\text{I2CCLK}} \)TIMEOUTA[11:0] bitsTIDLE bitTIMEOUTEN bit\( t_{\text{TIMEOUT}} \)
8 MHz0x6101\( 98 \times 2048 \times 125 \text{ ns} = 25 \text{ ms} \)
16 MHz0xC301\( 196 \times 2048 \times 62.5 \text{ ns} = 25 \text{ ms} \)
32 MHz0x18601\( 391 \times 2048 \times 31.25 \text{ ns} = 25 \text{ ms} \)

Table 97. Examples of TIMEOUTB settings for various I2CCLK frequencies

\( f_{\text{I2CCLK}} \)TIMEOUTB[11:0] bitsTEXTEN bit\( t_{\text{LOW:EXT}} \)
8 MHz0x1F1\( 32 \times 2048 \times 125 \text{ ns} = 8 \text{ ms} \)
16 MHz0x3F1\( 64 \times 2048 \times 62.5 \text{ ns} = 8 \text{ ms} \)
32 MHz0x7C1\( 125 \times 2048 \times 31.25 \text{ ns} = 8 \text{ ms} \)

Table 98. Examples of TIMEOUTA settings for various I2CCLK frequencies
(max \( t_{\text{TIDLE}} = 50 \mu\text{s} \) )

\( f_{\text{I2CCLK}} \)TIMEOUTA[11:0] bitsTIDLE bitTIMEOUTEN bit\( t_{\text{TIDLE}} \)
8 MHz0x6311\( 100 \times 4 \times 125 \text{ ns} = 50 \mu\text{s} \)
16 MHz0xC711\( 200 \times 4 \times 62.5 \text{ ns} = 50 \mu\text{s} \)
32 MHz0x18F11\( 400 \times 4 \times 31.25 \text{ ns} = 50 \mu\text{s} \)

20.4.14 SMBus slave mode

This section is relevant only when the SMBus feature is supported. Refer to Section 20.3: I2C implementation .

In addition to I2C slave transfer management (refer to Section 20.4.8: I2C slave mode ) some additional software flows are provided to support the SMBus.

SMBus slave transmitter

When the IP is used in SMBus, SBC must be programmed to '1' in order to allow the PEC transmission at the end of the programmed number of data bytes. When the PECBYTE bit is set, the number of bytes programmed in NBYPES[7:0] includes the PEC transmission. In

that case the total number of TXIS interrupts is NBBYTES - 1 and the content of the I2C_PECR register is automatically transmitted if the master requests an extra byte after the NBBYTES - 1 data transfer.

Caution: The PECBYTE bit has no effect when the RELOAD bit is set.

Figure 174. Transfer sequence flow for SMBus slave transmitter N bytes + PEC

Flowchart of SMBus slave transmitter transfer sequence for N bytes + PEC.
graph TD; Start([SMBus slave transmission]) --> Init[Slave initialization]; Init --> Addr{I2C_ISR.ADDR = 1?}; Addr -- No --> Init; Addr -- Yes --> Read[Read ADDCODE and DIR in I2C_ISR<br/>I2C_CR2.NBYTES = N + 1<br/>PECBYTE=1<br/>Set I2C_ICR.ADDRCF]; Read --> TxIs{I2C_ISR.TXIS = 1?}; TxIs -- No --> Read; TxIs -- Yes --> Write[Write I2C_TXDR.TXDATA]; Write --> Read;

The flowchart illustrates the transfer sequence for an SMBus slave transmitter. It begins with an oval labeled "SMBus slave transmission", which points to a rectangle "Slave initialization". From there, a decision diamond asks "I2C_ISR.ADDR = 1?". If "No", the flow loops back to "Slave initialization". If "Yes", it proceeds to a rectangle: "Read ADDCODE and DIR in I2C_ISR", "I2C_CR2.NBYTES = N + 1", "PECBYTE=1", and "Set I2C_ICR.ADDRCF". To the right of this block is a vertical double-headed arrow labeled "SCL stretched". Below this is another decision diamond: "I2C_ISR.TXIS = 1?". If "No", the flow loops back to the entry point of the previous block. If "Yes", it proceeds to a rectangle "Write I2C_TXDR.TXDATA", which then loops back to the entry point of the previous block. The identifier "MS19867V2" is in the bottom right corner.

Flowchart of SMBus slave transmitter transfer sequence for N bytes + PEC.

Figure 175. Transfer bus diagrams for SMBus slave transmitter (SBC=1)

Figure 175. Transfer bus diagrams for SMBus slave transmitter (SBC=1). The diagram shows a sequence of bytes: S (Start), Address, A (ACK), data1, A (ACK), data2, A (ACK), PEC, NA (NACK), P (Stop). Above the sequence, 'ADDR' points to the Address byte, 'TXIS' points to the data1 and data2 bytes. Below the sequence, 'EV1' points to the Address byte, 'EV2' points to the data1 byte, and 'EV3' points to the data2 byte. A legend indicates: transmission (white box), reception (yellow box), and SCL stretch (blue line). The NBYTES register is shown with a value of 3. The example is for an SMBus slave transmitter with 2 bytes + PEC. The legend also shows a yellow box for reception and a blue line for SCL stretch. The diagram is labeled MS19869V2.

Example SMBus slave transmitter 2 bytes + PEC,

legend:
transmission
reception
SCL stretch

ADDR TXIS TXIS

S Address A data1 A data2 A PEC NA P

EV1 EV2 EV3

NBYTES 3

EV1: ADDR ISR: check ADDCODE, program NBYTES=3, set PECBYTE, set ADDRCF
EV2: TXIS ISR: wr data1
EV3: TXIS ISR: wr data2

MS19869V2

Figure 175. Transfer bus diagrams for SMBus slave transmitter (SBC=1). The diagram shows a sequence of bytes: S (Start), Address, A (ACK), data1, A (ACK), data2, A (ACK), PEC, NA (NACK), P (Stop). Above the sequence, 'ADDR' points to the Address byte, 'TXIS' points to the data1 and data2 bytes. Below the sequence, 'EV1' points to the Address byte, 'EV2' points to the data1 byte, and 'EV3' points to the data2 byte. A legend indicates: transmission (white box), reception (yellow box), and SCL stretch (blue line). The NBYTES register is shown with a value of 3. The example is for an SMBus slave transmitter with 2 bytes + PEC. The legend also shows a yellow box for reception and a blue line for SCL stretch. The diagram is labeled MS19869V2.

SMBus Slave receiver

When the I2C is used in SMBus mode, SBC must be programmed to '1' in order to allow the PEC checking at the end of the programmed number of data bytes. In order to allow the ACK control of each byte, the reload mode must be selected (RELOAD=1). Refer to Slave byte control mode on page 519 for more details.

In order to check the PEC byte, the RELOAD bit must be cleared and the PECBYTE bit must be set. In this case, after NBYTES - 1 data have been received, the next received byte is compared with the internal I2C_PECR register content. A NACK is automatically generated if the comparison does not match, and an ACK is automatically generated if the comparison matches, whatever the ACK bit value. Once the PEC byte is received, it is copied into the I2C_RXDR register like any other data, and the RXNE flag is set.

In the case of a PEC mismatch, the PECERR flag is set and an interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.

If no ACK software control is needed, the user can program PECBYTE=1 and, in the same write operation, program NBYTES with the number of bytes to be received in a continuous flow. After NBYTES - 1 are received, the next received byte is checked as being the PEC.

Caution: The PECBYTE bit has no effect when the RELOAD bit is set.

Figure 176. Transfer sequence flow for SMBus slave receiver N Bytes + PEC

Flowchart showing the transfer sequence flow for SMBus slave receiver N Bytes + PEC. The process starts with SMBus slave reception, followed by slave initialization. It then enters a loop checking for I2C_ISR.ADDR = 1. If yes, it reads ADDCODE and DIR, sets I2C_CR2.NBYTES = 1, RELOAD = 1, PECBYTE = 1, and sets I2C_ICR.ADDRCF. It then checks if I2C_ISR.RXNE = 1 or I2C_ISR.TCR = 1. If yes, it reads I2C_RXDR.RXDATA, programs I2C_CR2.NACK = 0, I2C_CR2.NBYTES = 1, and updates N = N - 1. It then checks if N = 1. If yes, it reads I2C_RXDR.RXDATA, programs RELOAD = 0, NACK = 0 and NBYTES = 1, and checks if I2C_ISR.RXNE = 1. If yes, it reads I2C_RXDR.RXDATA and ends. If no at any point, it loops back to the address check.
graph TD
    Start([SMBus slave reception]) --> Init[Slave initialization]
    Init --> AddrCheck{I2C_ISR.ADDR = 1?}
    AddrCheck -- No --> AddrCheck
    AddrCheck -- Yes --> InitParams[Read ADDCODE and DIR in I2C_ISR
I2C_CR2.NBYTES = 1, RELOAD = 1
PECBYTE = 1
Set I2C_ICR.ADDRCF] InitParams --> RxneTcrCheck{I2C_ISR.RXNE = 1?
I2C_ISR.TCR = 1?} RxneTcrCheck -- No --> AddrCheck RxneTcrCheck -- Yes --> ReadData1[Read I2C_RXDR.RXDATA
Program I2C_CR2.NACK = 0
I2C_CR2.NBYTES = 1
N = N - 1] ReadData1 --> Ncheck{N = 1?} Ncheck -- No --> RxneTcrCheck Ncheck -- Yes --> ReadData2[Read I2C_RXDR.RXDATA
Program RELOAD = 0
NACK = 0 and NBYTES = 1] ReadData2 --> RxneCheck{I2C_ISR.RXNE = 1?} RxneCheck -- No --> ReadData2 RxneCheck -- Yes --> ReadData3[Read I2C_RXDR.RXDATA] ReadData3 --> End([End])

SCL stretched

MS19868V2

Flowchart showing the transfer sequence flow for SMBus slave receiver N Bytes + PEC. The process starts with SMBus slave reception, followed by slave initialization. It then enters a loop checking for I2C_ISR.ADDR = 1. If yes, it reads ADDCODE and DIR, sets I2C_CR2.NBYTES = 1, RELOAD = 1, PECBYTE = 1, and sets I2C_ICR.ADDRCF. It then checks if I2C_ISR.RXNE = 1 or I2C_ISR.TCR = 1. If yes, it reads I2C_RXDR.RXDATA, programs I2C_CR2.NACK = 0, I2C_CR2.NBYTES = 1, and updates N = N - 1. It then checks if N = 1. If yes, it reads I2C_RXDR.RXDATA, programs RELOAD = 0, NACK = 0 and NBYTES = 1, and checks if I2C_ISR.RXNE = 1. If yes, it reads I2C_RXDR.RXDATA and ends. If no at any point, it loops back to the address check.

Figure 177. Bus transfer diagrams for SMBus slave receiver (SBC=1)

Diagram of SMBus slave receiver transfer with 2 bytes + PEC. The sequence is: S (Start), Address (transmission), A (ACK, reception), data1 (reception), A (ACK, reception), data2 (reception), A (ACK, reception), PEC (reception), A (ACK, reception), P (Stop, transmission). Interrupts are triggered at EV1 (ADDR), EV2 (RXNE), EV3 (RXNE), and EV4 (RXNE). The NBYTES register is set to 3. Diagram of SMBus slave receiver transfer with 2 bytes + PEC and ACK control. The sequence is: S (Start), Address (transmission), A (ACK, reception), data1 (reception), A (ACK, reception), data2 (reception), A (ACK, reception), PEC (reception), A (ACK, reception), P (Stop, transmission). Interrupts are triggered at EV1 (ADDR), EV2 (RXNE,TCR), EV3 (RXNE,TCR), and EV4 (RXNE). The NBYTES register is set to 1.

Example SMBus slave receiver 2 bytes + PEC

legend:
transmission
reception
SCL stretch

NBYTES 3

EV1: ADDR ISR: check ADDCODE and DIR, program NBYTES = 3, PECBYTE=1, RELOAD=0, set ADDRCF
EV2: RXNE ISR: rd data1
EV3: RXNE ISR: rd data2
EV4: RXNE ISR: rd PEC

Example SMBus slave receiver 2 bytes + PEC, with ACK control (RELOAD=1/0)

legend :
transmission
reception
SCL stretch

NBYTES 1

EV1: ADDR ISR: check ADDCODE and DIR, program NBYTES = 1, PECBYTE=1, RELOAD=1, set ADDRCF
EV2: RXNE-TCR ISR: rd data1, program NACK=0 and NBYTES = 1
EV3: RXNE-TCR ISR: rd data2, program NACK=0, NBYTES = 1 and RELOAD=0
EV4: RXNE-TCR ISR: rd PEC

MS19870V2

Diagram of SMBus slave receiver transfer with 2 bytes + PEC. The sequence is: S (Start), Address (transmission), A (ACK, reception), data1 (reception), A (ACK, reception), data2 (reception), A (ACK, reception), PEC (reception), A (ACK, reception), P (Stop, transmission). Interrupts are triggered at EV1 (ADDR), EV2 (RXNE), EV3 (RXNE), and EV4 (RXNE). The NBYTES register is set to 3. Diagram of SMBus slave receiver transfer with 2 bytes + PEC and ACK control. The sequence is: S (Start), Address (transmission), A (ACK, reception), data1 (reception), A (ACK, reception), data2 (reception), A (ACK, reception), PEC (reception), A (ACK, reception), P (Stop, transmission). Interrupts are triggered at EV1 (ADDR), EV2 (RXNE,TCR), EV3 (RXNE,TCR), and EV4 (RXNE). The NBYTES register is set to 1.

This section is relevant only when the SMBus feature is supported. Refer to Section 20.3: I2C implementation .

In addition to I2C master transfer management (refer to Section 20.4.9: I2C master mode ), some additional software flows are provided to support the SMBus.

SMBus master transmitter

When the SMBus master wants to transmit the PEC, the PECBYTE bit must be set and the number of bytes must be programmed in the NBYTES[7:0] field, before setting the START bit. In this case the total number of TXIS interrupts is NBYTES - 1. So if the PECBYTE bit is set when NBYTES = 0x1, the content of the I2C_PECR register is automatically transmitted.

If the SMBus master wants to send a STOP condition after the PEC, automatic end mode must be selected (AUTOEND = 1). In this case, the STOP condition automatically follows the PEC transmission.

When the SMBus master wants to send a RESTART condition after the PEC, software mode must be selected ( AUTOEND =0). In this case, once NBYTES - 1 have been transmitted, the I2C_PECR register content is transmitted and the TC flag is set after the PEC transmission, stretching the SCL line low. The RESTART condition must be programmed in the TC interrupt subroutine.

Caution: The PECBYTE bit has no effect when the RELOAD bit is set.

Figure 178. Bus transfer diagrams for SMBus master transmitter

Timing diagram for SMBus master transmitter in automatic end mode (STOP). The sequence shows: S (Start), Address, A (Ack), data1, A, data2, A, PEC, A, P (Stop). Event markers: INIT (program Slave address, NBYTES=3, AUTOEND=1, set PECBYTE, set START), EV1 (TXIS ISR: wr data1), EV2 (TXIS ISR: wr data2). Signals shown: TXE (goes low during data writes), NBYTES (starts at xx, then 3). Legend: white box for transmission, yellow box for reception, blue line for SCL stretch. Timing diagram for SMBus master transmitter in software end mode (RESTART). The sequence shows: S, Address, A, data1, A, data2, A, PEC, A, Rstart, Address. Event markers: INIT (program Slave address, NBYTES=3, AUTOEND=0, set PECBYTE, set START), EV1 (TXIS ISR: wr data1), EV2 (TXIS ISR: wr data2), TC (Transfer Complete flag set), EV3 (TC ISR: program Slave address, NBYTES=N, set START). Signals shown: TXE, NBYTES (starts at xx, then 3, then N). Legend: white box for transmission, yellow box for reception, blue line for SCL stretch.

Example SMBus master transmitter 2 bytes + PEC, automatic end mode (STOP)

INIT: program Slave address, program NBYTES = 3, AUTOEND =1, set PECBYTE , set START
EV1: TXIS ISR: wr data1
EV2: TXIS ISR: wr data2


Example SMBus master transmitter 2 bytes + PEC, software end mode (RESTART)

INIT: program Slave address, program NBYTES = 3, AUTOEND =0, set PECBYTE , set START
EV1: TXIS ISR: wr data1
EV2: TXIS ISR: wr data2
EV3: TC ISR: program Slave address, program NBYTES = N, set START

MS19871V2

Timing diagram for SMBus master transmitter in automatic end mode (STOP). The sequence shows: S (Start), Address, A (Ack), data1, A, data2, A, PEC, A, P (Stop). Event markers: INIT (program Slave address, NBYTES=3, AUTOEND=1, set PECBYTE, set START), EV1 (TXIS ISR: wr data1), EV2 (TXIS ISR: wr data2). Signals shown: TXE (goes low during data writes), NBYTES (starts at xx, then 3). Legend: white box for transmission, yellow box for reception, blue line for SCL stretch. Timing diagram for SMBus master transmitter in software end mode (RESTART). The sequence shows: S, Address, A, data1, A, data2, A, PEC, A, Rstart, Address. Event markers: INIT (program Slave address, NBYTES=3, AUTOEND=0, set PECBYTE, set START), EV1 (TXIS ISR: wr data1), EV2 (TXIS ISR: wr data2), TC (Transfer Complete flag set), EV3 (TC ISR: program Slave address, NBYTES=N, set START). Signals shown: TXE, NBYTES (starts at xx, then 3, then N). Legend: white box for transmission, yellow box for reception, blue line for SCL stretch.

SMBus master receiver

When the SMBus master wants to receive the PEC followed by a STOP at the end of the transfer, automatic end mode can be selected (AUTOEND = 1). The PECBYTE bit must be set and the slave address must be programmed, before setting the START bit. In this case, after NBYTES - 1 data have been received, the next received byte is automatically checked versus the I2C_PECR register content. A NACK response is given to the PEC byte, followed by a STOP condition.

When the SMBus master receiver wants to receive the PEC byte followed by a RESTART condition at the end of the transfer, software mode must be selected (AUTOEND=0). The PECBYTE bit must be set and the slave address must be programmed, before setting the START bit. In this case, after NBYTES - 1 data have been received, the next received byte is automatically checked versus the I2C_PECR register content. The TC flag is set after the PEC byte reception, stretching the SCL line low. The RESTART condition can be programmed in the TC interrupt subroutine.

Caution: The PECBYTE bit has no effect when the RELOAD bit is set.

Figure 179. Bus transfer diagrams for SMBus master receiver

Timing diagram for SMBus master receiver in automatic end mode (STOP). The sequence starts with INIT (Slave address), followed by reception of data1, data2, and PEC. Events RXNE occur after each data byte. The sequence ends with NA and P. NBYTES is set to 3. Timing diagram for SMBus master receiver in software end mode (RESTART). The sequence starts with INIT (Slave address), followed by reception of data1, data2, and PEC. Events RXNE occur after each data byte. After PEC, there is a NA and TC event, followed by a RESTART and a new Slave address. NBYTES is initially 3, then changed to N.

Example SMBus master receiver 2 bytes + PEC, automatic end mode (STOP)

Legend:

NBYTES: xx | 3

INIT: program Slave address, program NBYTES = 3, AUTOEND=1, set PECBYTE, set START
EV1: RXNE ISR: rd data1
EV2: RXNE ISR: rd data2
EV3: RXNE ISR: rd PEC

Example SMBus master receiver 2 bytes + PEC, software end mode (RESTART)

Legend:

NBYTES: xx | 3 | N

INIT: program Slave address, program NBYTES = 3, AUTOEND=0, set PECBYTE, set START
EV1: RXNE ISR: rd data1
EV2: RXNE ISR: rd data2
EV3: RXNE ISR: read PEC
EV4: TC ISR: program Slave address, program NBYTES = N, set START

MS19872V2

Timing diagram for SMBus master receiver in automatic end mode (STOP). The sequence starts with INIT (Slave address), followed by reception of data1, data2, and PEC. Events RXNE occur after each data byte. The sequence ends with NA and P. NBYTES is set to 3. Timing diagram for SMBus master receiver in software end mode (RESTART). The sequence starts with INIT (Slave address), followed by reception of data1, data2, and PEC. Events RXNE occur after each data byte. After PEC, there is a NA and TC event, followed by a RESTART and a new Slave address. NBYTES is initially 3, then changed to N.

20.4.15 Wakeup from Stop mode on address match

This section is relevant only when wakeup from Stop mode feature is supported. Refer to Section 20.3: I2C implementation .

The I2C is able to wakeup the MCU from Stop mode (APB clock is off), when it is addressed. All addressing modes are supported.

Wakeup from Stop mode is enabled by setting the WUPEN bit in the I2C_CR1 register. The HSI16 oscillator must be selected as the clock source for I2CCLK in order to allow wakeup from Stop mode.

During Stop mode, the HSI16 is switched off. When a START is detected, the I2C interface switches the HSI16 on, and stretches SCL low until HSI16 is woken up.

HSI16 is then used for the address reception.

In case of an address match, the I2C stretches SCL low during MCU wakeup time. The stretch is released when ADDR flag is cleared by software, and the transfer goes on normally.

If the address does not match, the HSI16 is switched off again and the MCU is not woken up.

Note: If the I2C clock is the system clock, or if WUPEN = 0, the HSI16 is not switched on after a START is received.

Only an ADDR interrupt can wakeup the MCU. Therefore do not enter Stop mode when the I2C is performing a transfer as a master, or as an addressed slave after the ADDR flag is set. This can be managed by clearing SLEEPDEEP bit in the ADDR interrupt routine and setting it again only after the STOPF flag is set.

Caution: The digital filter is not compatible with the wakeup from Stop mode feature. If the DNF bit is not equal to 0, setting the WUPEN bit has no effect.

Caution: This feature is available only when the I2C clock source is the HSI16 oscillator.

Caution: Clock stretching must be enabled (NOSTRETCH = 0) to ensure proper operation of the wakeup from Stop mode feature.

Caution: If wakeup from Stop mode is disabled (WUPEN = 0), the I2C peripheral must be disabled before entering Stop mode (PE = 0).

20.4.16 Error conditions

The following errors are the error conditions which may cause communication to fail.

Bus error (BERR)

A bus error is detected when a START or a STOP condition is detected and is not located after a multiple of 9 SCL clock pulses. A START or a STOP condition is detected when a SDA edge occurs while SCL is high.

The bus error flag is set only if the I2C is involved in the transfer as master or addressed slave (i.e not during the address phase in slave mode).

In case of a misplaced START or RESTART detection in slave mode, the I2C enters address recognition state like for a correct START condition.

When a bus error is detected, the BERR flag is set in the I2C_ISR register, and an interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.

Arbitration lost (ARLO)

An arbitration loss is detected when a high level is sent on the SDA line, but a low level is sampled on the SCL rising edge.

When an arbitration loss is detected, the ARLO flag is set in the I2C_ISR register, and an interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.

Overrun/underrun error (OVR)

An overrun or underrun error is detected in slave mode when NOSTRETCH = 1 and:

When an overrun or underrun error is detected, the OVR flag is set in the I2C_ISR register, and an interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.

Packet error checking error (PECERR)

This section is relevant only when the SMBus feature is supported. Refer to Section 20.3: I2C implementation .

A PEC error is detected when the received PEC byte does not match with the I2C_PECR register content. A NACK is automatically sent after the wrong PEC reception.

When a PEC error is detected, the PECERR flag is set in the I2C_ISR register, and an interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.

Timeout Error (TIMEOUT)

This section is relevant only when the SMBus feature is supported. Refer to Section 20.3: I2C implementation .

A timeout error occurs for any of these conditions:

When a timeout violation is detected in master mode, a STOP condition is automatically sent.

When a timeout violation is detected in slave mode, SDA and SCL lines are automatically released.

When a timeout error is detected, the TIMEOUT flag is set in the I2C_ISR register, and an interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.

Alert (ALERT)

This section is relevant only when the SMBus feature is supported. Refer to Section 20.3: I2C implementation .

The ALERT flag is set when the I2C interface is configured as a Host (SMBHEN=1), the alert pin detection is enabled (ALERTEN=1) and a falling edge is detected on the SMBALERT pin. An interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.

20.4.17 DMA requests

Transmission using DMA

DMA (direct memory access) can be enabled for transmission by setting the TXDMAEN bit in the I2C_CR1 register. Data is loaded from an SRAM area configured using the DMA peripheral (see Section 11: Direct memory access controller (DMA) on page 361 ) to the I2C_TXDR register whenever the TXIS bit is set.

Only the data are transferred with DMA.

For code example refer to A.14.8: I2C configured in master mode to transmit with DMA code example .

Note: If DMA is used for transmission, the TXIE bit does not need to be enabled.

Reception using DMA

DMA (direct memory access) can be enabled for reception by setting the RXDMAEN bit in the I2C_CR1 register. Data is loaded from the I2C_RXDR register to an SRAM area configured using the DMA peripheral (refer to Section 10: Direct memory access controller (DMA) on page 216 ) whenever the RXNE bit is set. Only the data (including PEC) are transferred with DMA.

Note: If DMA is used for reception, the RXIE bit does not need to be enabled.

20.4.18 Debug mode

When the microcontroller enters debug mode (core halted), the SMBus timeout either continues to work normally or stops, depending on the DBG_I2Cx_SMBUS_TIMEOUT configuration bits in the DBG module.

20.5 I2C low-power modes

Table 99. Effect of low-power modes on the I2C

ModeDescription
SleepNo effect. I2C interrupts cause the device to exit the Sleep mode.
Stop (1)The I2C registers content is kept. If WUPEN = 1 and I2C is clocked by an internal oscillator (HSI16): the address recognition is functional. The I2C address match condition causes the device to exit the Stop mode. If WUPEN=0: the I2C must be disabled before entering Stop mode
StandbyThe I2C peripheral is powered down and must be reinitialized after exiting Standby mode.
  1. 1. Refer to Section 20.3: I2C implementation for information about the Stop modes supported by each instance. If wakeup from a specific Stop mode is not supported, the instance must be disabled before entering this Stop mode.

20.6 I2C interrupts

The table below gives the list of I2C interrupt requests.

Table 100. I2C Interrupt requests

Interrupt acronymInterrupt eventEvent flagEnable control bitInterrupt clear methodExit the Sleep modeExit the Stop modeExit the Standby mode
I2CI2C_EVReceive buffer not emptyRXNERXIERead I2C_RXDR registerYesNoNo
Transmit buffer interrupt statusTXISTXIEWrite I2C_TXDR register
Stop detection interrupt flagSTOPFSTOPIEWrite STOPCF=1
Transfer complete reloadTCRTCIEWrite I2C_CR2 with NBBYTES[7:0] ≠ 0
Transfer completeTCWrite START=1 or STOP=1
Address matchedADDRADDRIEWrite ADDRCF=1Yes (1)
NACK receptionNACKFNACKIEWrite NACKCF=1No
I2C_ERBus errorBERRERRIEWrite BERRCF=1YesNoNo
Arbitration lossARLOWrite ARLOCF=1
Overrun/UnderrunOVRWrite OVRCF=1
PEC errorPECERRWrite PECERRCF=1
Timeout/t low errorTIMEOUTWrite TIMEOUTCF=1
SMBus alertALERTWrite ALERTCF=1

1. The ADDR match event can wake up the device from Stop mode only if the I2C instance supports the Wakeup from Stop mode feature. Refer to Section 20.3: I2C implementation .

20.7 I2C registers

Refer to Section 1.2 on page 33 for a list of abbreviations used in register descriptions.

The peripheral registers are accessed by words (32-bit).

20.7.1 I2C control register 1 (I2C_CR1)

Address offset: 0x00

Reset value: 0x0000 0000

Access: No wait states, except if a write access occurs while a write access to this register is ongoing. In this case, wait states are inserted in the second write access until the previous one is completed. The latency of the second write access can be up to \( 2 \times PCLK1 + 6 \times I2CCLK \) .

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.PECENALERTENSMBDENSMBHENGCENWUPENNOSTRETCHSBC
rwrwrwrwrwrwrwrw
1514131211109876543210
RXDMAENTXDMAENRes.ANFOFFDNF[3:0]ERRIETCIESTOPIENACKIEADDRIERXIETXIEPE
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:24 Reserved, must be kept at reset value.

Bit 23 PECEN : PEC enable

0: PEC calculation disabled

1: PEC calculation enabled

Note: If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.
Refer to Section 20.3: I2C implementation .

Bit 22 ALERTEN : SMBus alert enable

0: The SMBus alert pin (SMBA) is not supported in host mode (SMBHEN=1). In device mode (SMBHEN=0), the SMBA pin is released and the Alert Response Address header is disabled (0001100x followed by NACK).

1: The SMBus alert pin is supported in host mode (SMBHEN=1). In device mode (SMBHEN=0), the SMBA pin is driven low and the Alert Response Address header is enabled (0001100x followed by ACK).

Note: When ALERTEN=0, the SMBA pin can be used as a standard GPIO.
If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.
Refer to Section 20.3: I2C implementation .

Bit 21 SMBDEN : SMBus device default address enable

0: Device default address disabled. Address 0b1100001x is NACKed.

1: Device default address enabled. Address 0b1100001x is ACKed.

Note: If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.
Refer to Section 20.3: I2C implementation .

Bit 20 SMBHEN : SMBus host address enable

0: Host address disabled. Address 0b0001000x is NACKed.

1: Host address enabled. Address 0b0001000x is ACKed.

Note: If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.
Refer to Section 20.3: I2C implementation .

Bit 19 GCEN : General call enable

0: General call disabled. Address 0b00000000 is NACKed.

1: General call enabled. Address 0b00000000 is ACKed.

Bit 18 WUPEN : Wakeup from Stop mode enable

0: Wakeup from Stop mode disable.

1: Wakeup from Stop mode enable.

Note: If the Wakeup from Stop mode feature is not supported, this bit is reserved and forced by hardware to '0'. Refer to Section 20.3: I2C implementation .

Note: WUPEN can be set only when DNF = '0000'

Bit 17 NOSTRETCH : Clock stretching disable

This bit is used to disable clock stretching in slave mode. It must be kept cleared in master mode.

0: Clock stretching enabled

1: Clock stretching disabled

Note: This bit can only be programmed when the I2C is disabled (PE = 0).

Bit 16 SBC : Slave byte control

This bit is used to enable hardware byte control in slave mode.

0: Slave byte control disabled

1: Slave byte control enabled

Bit 15 RXDMAEN : DMA reception requests enable

0: DMA mode disabled for reception

1: DMA mode enabled for reception

Bit 14 TXDMAEN : DMA transmission requests enable

0: DMA mode disabled for transmission

1: DMA mode enabled for transmission

Bit 13 Reserved, must be kept at reset value.

Bit 12 ANFOFF : Analog noise filter OFF

0: Analog noise filter enabled

1: Analog noise filter disabled

Note: This bit can only be programmed when the I2C is disabled (PE = 0).

Bits 11:8 DNF[3:0] : Digital noise filter

These bits are used to configure the digital noise filter on SDA and SCL input. The digital filter, filters spikes with a length of up to \( DNF[3:0] * t_{I2CCLK} \)

0000: Digital filter disabled

0001: Digital filter enabled and filtering capability up to \( 1 t_{I2CCLK} \)

1111: digital filter enabled and filtering capability up to \( 15 t_{I2CCLK} \)

Note: If the analog filter is also enabled, the digital filter is added to the analog filter.

This filter can only be programmed when the I2C is disabled (PE = 0).

Bit 7 ERRIE : Error interrupts enable

0: Error detection interrupts disabled

1: Error detection interrupts enabled

Note: Any of these errors generate an interrupt:

Arbitration Loss (ARLO)

Bus Error detection (BERR)

Overrun/Underrun (OVR)

Timeout detection (TIMEOUT)

PEC error detection (PECERR)

Alert pin event detection (ALERT)

Bit 6 TCIE : Transfer Complete interrupt enable

0: Transfer Complete interrupt disabled

1: Transfer Complete interrupt enabled

Note: Any of these events generate an interrupt:

Transfer Complete (TC)

Transfer Complete Reload (TCR)

Bit 5 STOPIE : Stop detection Interrupt enable

0: Stop detection (STOPF) interrupt disabled

1: Stop detection (STOPF) interrupt enabled

Bit 4 NACKIE : Not acknowledge received Interrupt enable

0: Not acknowledge (NACKF) received interrupts disabled

1: Not acknowledge (NACKF) received interrupts enabled

Bit 3 ADDRIE : Address match Interrupt enable (slave only)

0: Address match (ADDR) interrupts disabled

1: Address match (ADDR) interrupts enabled

Bit 2 RXIE : RX Interrupt enable

0: Receive (RXNE) interrupt disabled

1: Receive (RXNE) interrupt enabled

Bit 1 TXIE : TX Interrupt enable

0: Transmit (TXIS) interrupt disabled

1: Transmit (TXIS) interrupt enabled

Bit 0 PE : Peripheral enable

0: Peripheral disable

1: Peripheral enable

Note: When PE = 0, the I2C SCL and SDA lines are released. Internal state machines and status bits are put back to their reset value. When cleared, PE must be kept low for at least 3 APB clock cycles.

20.7.2 I2C control register 2 (I2C_CR2)

Address offset: 0x04

Reset value: 0x0000 0000

Access: No wait states, except if a write access occurs while a write access to this register is ongoing. In this case, wait states are inserted in the second write access until the previous one is completed. The latency of the second write access can be up to \( 2 \times PCLK1 + 6 \times I2CCLK \) .

31302928272625242322212019181716
Res.Res.Res.Res.Res.PEC
BYTE
AUTOE
ND
RE
LOAD
NBYPES[7:0]
rsrwrwrwrwrwrwrwrwrwrw

1514131211109876543210
NACKSTOPSTARTHEAD1
OR
ADD10RD
WRN
SADD[9:0]
rsrsrsrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:27 Reserved, must be kept at reset value.

Bit 26 PECBYTE : Packet error checking byte

This bit is set by software, and cleared by hardware when the PEC is transferred, or when a STOP condition or an Address matched is received, also when PE = 0.

0: No PEC transfer.

1: PEC transmission/reception is requested

Note: Writing '0' to this bit has no effect.

This bit has no effect when RELOAD is set.

This bit has no effect is slave mode when SBC=0.

If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.

Refer to Section 20.3: I2C implementation .

Bit 25 AUTOEND : Automatic end mode (master mode)

This bit is set and cleared by software.

0: software end mode: TC flag is set when NBYPES data are transferred, stretching SCL low.

1: Automatic end mode: a STOP condition is automatically sent when NBYPES data are transferred.

Note: This bit has no effect in slave mode or when the RELOAD bit is set.

Bit 24 RELOAD : NBYPES reload mode

This bit is set and cleared by software.

0: The transfer is completed after the NBYPES data transfer (STOP or RESTART follows).

1: The transfer is not completed after the NBYPES data transfer (NBYPES is reloaded). TCR flag is set when NBYPES data are transferred, stretching SCL low.

Bits 23:16 NBYTES[7:0] : Number of bytes

The number of bytes to be transmitted/received is programmed there. This field is don't care in slave mode with SBC=0.

Note: Changing these bits when the START bit is set is not allowed.

Bit 15 NACK: NACK generation (slave mode)

The bit is set by software, cleared by hardware when the NACK is sent, or when a STOP condition or an Address matched is received, or when PE = 0.

0: an ACK is sent after current received byte.

1: a NACK is sent after current received byte.

Note: Writing '0' to this bit has no effect.

This bit is used in slave mode only: in master receiver mode, NACK is automatically generated after last byte preceding STOP or RESTART condition, whatever the NACK bit value.

When an overrun occurs in slave receiver NOSTRETCH mode, a NACK is automatically generated whatever the NACK bit value.

When hardware PEC checking is enabled (PECBYTE=1), the PEC acknowledge value does not depend on the NACK value.

Bit 14 STOP: Stop generation (master mode)

The bit is set by software, cleared by hardware when a STOP condition is detected, or when PE = 0.

In Master Mode:

0: No Stop generation.

1: Stop generation after current byte transfer.

Note: Writing '0' to this bit has no effect.

Bit 13 START: Start generation

This bit is set by software, and cleared by hardware after the Start followed by the address sequence is sent, by an arbitration loss, by a timeout error detection, or when PE = 0. It can also be cleared by software by writing '1' to the ADDRCF bit in the I2C_ICR register.

0: No Start generation.

1: Restart/Start generation:

If the I2C is already in master mode with AUTOEND = 0, setting this bit generates a Repeated Start condition when RELOAD=0, after the end of the NBYTES transfer.

Otherwise setting this bit generates a START condition once the bus is free.

Note: Writing '0' to this bit has no effect.

The START bit can be set even if the bus is BUSY or I2C is in slave mode.

This bit has no effect when RELOAD is set.

Bit 12 HEAD10R: 10-bit address header only read direction (master receiver mode)

0: The master sends the complete 10 bit slave address read sequence: Start + 2 bytes 10bit address in write direction + Restart + 1st 7 bits of the 10 bit address in read direction.

1: The master only sends the 1st 7 bits of the 10 bit address, followed by Read direction.

Note: Changing this bit when the START bit is set is not allowed.

Bit 11 ADD10: 10-bit addressing mode (master mode)

0: The master operates in 7-bit addressing mode,

1: The master operates in 10-bit addressing mode

Note: Changing this bit when the START bit is set is not allowed.

Bit 10 RD_WRN: Transfer direction (master mode)

0: Master requests a write transfer.

1: Master requests a read transfer.

Note: Changing this bit when the START bit is set is not allowed.

Bits 9:0 SADD[9:0] : Slave address (master mode)

In 7-bit addressing mode (ADD10 = 0):

SADD[7:1] should be written with the 7-bit slave address to be sent. The bits SADD[9], SADD[8] and SADD[0] are don't care.

In 10-bit addressing mode (ADD10 = 1):

SADD[9:0] should be written with the 10-bit slave address to be sent.

Note: Changing these bits when the START bit is set is not allowed.

20.7.3 I2C own address 1 register (I2C_OAR1)

Address offset: 0x08

Reset value: 0x0000 0000

Access: No wait states, except if a write access occurs while a write access to this register is ongoing. In this case, wait states are inserted in the second write access until the previous one is completed. The latency of the second write access can be up to \( 2 \times PCLK1 + 6 \times I2CCLK \) .

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
OA1ENRes.Res.Res.Res.OA1 MODEOA1[9:0]
rwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:16 Reserved, must be kept at reset value.

Bit 15 OA1EN : Own Address 1 enable

0: Own address 1 disabled. The received slave address OA1 is NACKed.

1: Own address 1 enabled. The received slave address OA1 is ACKed.

Bits 14:11 Reserved, must be kept at reset value.

Bit 10 OA1MODE : Own Address 1 10-bit mode

0: Own address 1 is a 7-bit address.

1: Own address 1 is a 10-bit address.

Note: This bit can be written only when OA1EN=0.

Bits 9:0 OA1[9:0] : Interface own slave address

7-bit addressing mode: OA1[7:1] contains the 7-bit own slave address. The bits OA1[9], OA1[8] and OA1[0] are don't care.

10-bit addressing mode: OA1[9:0] contains the 10-bit own slave address.

Note: These bits can be written only when OA1EN=0.

20.7.4 I2C own address 2 register (I2C_OAR2)

Address offset: 0x0C

Reset value: 0x0000 0000

Access: No wait states, except if a write access occurs while a write access to this register is ongoing. In this case, wait states are inserted in the second write access until the previous one is completed. The latency of the second write access can be up to \( 2 \times PCLK1 + 6 \times I2CCLK \) .

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
OA2ENRes.Res.Res.Res.OA2MSK[2:0]OA2[7:1]Res.
rwrwrwrwrwrwrwrwrwrwrw

Bits 31:16 Reserved, must be kept at reset value.

Bit 15 OA2EN : Own Address 2 enable

0: Own address 2 disabled. The received slave address OA2 is NACKed.

1: Own address 2 enabled. The received slave address OA2 is ACKed.

Bits 14:11 Reserved, must be kept at reset value.

Bits 10:8 OA2MSK[2:0] : Own Address 2 masks

000: No mask

001: OA2[1] is masked and don't care. Only OA2[7:2] are compared.

010: OA2[2:1] are masked and don't care. Only OA2[7:3] are compared.

011: OA2[3:1] are masked and don't care. Only OA2[7:4] are compared.

100: OA2[4:1] are masked and don't care. Only OA2[7:5] are compared.

101: OA2[5:1] are masked and don't care. Only OA2[7:6] are compared.

110: OA2[6:1] are masked and don't care. Only OA2[7] is compared.

111: OA2[7:1] are masked and don't care. No comparison is done, and all (except reserved) 7-bit received addresses are acknowledged.

Note: These bits can be written only when OA2EN=0.

As soon as OA2MSK is not equal to 0, the reserved I2C addresses (0b0000xxx and 0b1111xxx) are not acknowledged even if the comparison matches.

Bits 7:1 OA2[7:1] : Interface address

7-bit addressing mode: 7-bit address

Note: These bits can be written only when OA2EN=0.

Bit 0 Reserved, must be kept at reset value.

20.7.5 I2C timing register (I2C_TIMINGR)

Address offset: 0x10

Reset value: 0x0000 0000

Access: No wait states

31302928272625242322212019181716
PRESC[3:0]Res.Res.Res.Res.SCLDEL[3:0]SDADEL[3:0]
rwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
SCLH[7:0]SCLL[7:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:28 PRESC[3:0] : Timing prescaler

This field is used to prescale I2CCLK in order to generate the clock period \( t_{PRESC} \) used for data setup and hold counters (refer to I2C timings on page 511 ) and for SCL high and low level counters (refer to I2C master initialization on page 526 ).

\[ t_{PRESC} = (PRESC+1) \times t_{I2CCLK} \]

Bits 27:24 Reserved, must be kept at reset value.

Bits 23:20 SCLDEL[3:0] : Data setup time

This field is used to generate a delay \( t_{SCLDEL} \) between SDA edge and SCL rising edge. In master mode and in slave mode with NOSTRETCH = 0, the SCL line is stretched low during

\( t_{SCLDEL} \) :

\[ t_{SCLDEL} = (SCLDEL+1) \times t_{PRESC} \]

Note: \( t_{SCLDEL} \) is used to generate \( t_{SU:DAT} \) timing.

Bits 19:16 SDADEL[3:0] : Data hold time

This field is used to generate the delay \( t_{SDADEL} \) between SCL falling edge and SDA edge. In master mode and in slave mode with NOSTRETCH = 0, the SCL line is stretched low during

\( t_{SDADEL} \) :

\[ t_{SDADEL} = SDADEL \times t_{PRESC} \]

Note: \( t_{SDADEL} \) is used to generate \( t_{HD:DAT} \) timing.

Bits 15:8 SCLH[7:0] : SCL high period (master mode)

This field is used to generate the SCL high period in master mode.

\[ t_{SCLH} = (SCLH+1) \times t_{PRESC} \]

Note: \( t_{SCLH} \) is also used to generate \( t_{SU:STO} \) and \( t_{HD:STA} \) timing.

Bits 7:0 SCLL[7:0] : SCL low period (master mode)

This field is used to generate the SCL low period in master mode.

\[ t_{SCLL} = (SCLL+1) \times t_{PRESC} \]

Note: \( t_{SCLL} \) is also used to generate \( t_{BUF} \) and \( t_{SU:STA} \) timings.

Note: This register must be configured when the I2C is disabled (PE = 0).

Note: The STM32CubeMX tool calculates and provides the I2C_TIMINGR content in the I2C Configuration window.

20.7.6 I2C timeout register (I2C_TIMEOUTR)

Address offset: 0x14

Reset value: 0x0000 0000

Access: No wait states, except if a write access occurs while a write access to this register is ongoing. In this case, wait states are inserted in the second write access until the previous one is completed. The latency of the second write access can be up to \( 2 \times PCLK1 + 6 \times I2CCLK \) .

31302928272625242322212019181716
TEXTENRes.Res.Res.TIMEOUTB[11:0]
rwrwrwrwrwrwrwrwrwrwrwrwrw

1514131211109876543210
TIMOUTENRes.Res.TIDLETIMEOUTA[11:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 31 TEXTEN : Extended clock timeout enable

Bits 30:28 Reserved, must be kept at reset value.

Bits 27:16 TIMEOUTB[11:0] : Bus timeout B

This field is used to configure the cumulative clock extension timeout:
In master mode, the master cumulative clock low extend time ( \( t_{LOW:MEXT} \) ) is detected
In slave mode, the slave cumulative clock low extend time ( \( t_{LOW:SEXT} \) ) is detected
\( t_{LOW:EXT} = (TIMEOUTB+1) \times 2048 \times t_{I2CCLK} \)

Note: These bits can be written only when TEXTEN=0.

Bit 15 TIMOUTEN : Clock timeout enable

Bits 14:13 Reserved, must be kept at reset value.

Bit 12 TIDLE : Idle clock timeout detection

Note: This bit can be written only when TIMOUTEN=0.

Bits 11:0 TIMEOUTA[11:0] : Bus Timeout A

This field is used to configure:
The SCL low timeout condition \( t_{TIMEOUT} \) when TIDLE=0
\( t_{TIMEOUT} = (TIMEOUTA+1) \times 2048 \times t_{I2CCLK} \)
The bus idle condition (both SCL and SDA high) when TIDLE=1
\( t_{IDLE} = (TIMEOUTA+1) \times 4 \times t_{I2CCLK} \)

Note: These bits can be written only when TIMOUTEN=0.

Note: If the SMBus feature is not supported, this register is reserved and forced by hardware to "0x00000000". Refer to Section 20.3: I2C implementation.

20.7.7 I2C interrupt and status register (I2C_ISR)

Address offset: 0x18

Reset value: 0x0000 0001

Access: No wait states

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.ADDCODE[6:0]DIR
rrrrrrrr
1514131211109876543210
BUSYRes.ALERTTIME
OUT
PEC
ERR
OVRARLOBERRTCRTCSTOPFNACKFADDRRXNETXISTXE
rrrrrrrrrrrrrrsrs

Bits 31:24 Reserved, must be kept at reset value.

Bits 23:17 ADDCODE[6:0] : Address match code (Slave mode)

These bits are updated with the received address when an address match event occurs (ADDR = 1).

In the case of a 10-bit address, ADDCODE provides the 10-bit header followed by the 2 MSBs of the address.

Bit 16 DIR : Transfer direction (Slave mode)

This flag is updated when an address match event occurs (ADDR = 1).
0: Write transfer, slave enters receiver mode.
1: Read transfer, slave enters transmitter mode.

Bit 15 BUSY : Bus busy

This flag indicates that a communication is in progress on the bus. It is set by hardware when a START condition is detected. It is cleared by hardware when a STOP condition is detected, or when PE = 0.

Bit 14 Reserved, must be kept at reset value.

Bit 13 ALERT : SMBus alert

This flag is set by hardware when SMBHEN=1 (SMBus host configuration), ALERTEN=1 and a SMBALERT event (falling edge) is detected on SMBA pin. It is cleared by software by setting the ALERTCF bit.

Note: This bit is cleared by hardware when PE = 0.

If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.

Refer to Section 20.3: I2C implementation .

Bit 12 TIMEOUT : Timeout or \( t_{LOW} \) detection flag

This flag is set by hardware when a timeout or extended clock timeout occurred. It is cleared by software by setting the TIMEOUTCF bit.

Note: This bit is cleared by hardware when PE = 0.

If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.

Refer to Section 20.3: I2C implementation .

Bit 11 PECERR : PEC Error in reception

This flag is set by hardware when the received PEC does not match with the PEC register content. A NACK is automatically sent after the wrong PEC reception. It is cleared by software by setting the PECCF bit.

Note: This bit is cleared by hardware when PE = 0.

If the SMBus feature is not supported, this bit is reserved and forced by hardware to '0'.

Refer to Section 20.3: I2C implementation .

Bit 10 OVR : Overrun/Underrun (slave mode)

This flag is set by hardware in slave mode with NOSTRETCH = 1, when an overrun/underrun error occurs. It is cleared by software by setting the OVRCF bit.

Note: This bit is cleared by hardware when PE = 0.

Bit 9 ARLO : Arbitration lost

This flag is set by hardware in case of arbitration loss. It is cleared by software by setting the ARLOCF bit.

Note: This bit is cleared by hardware when PE = 0.

Bit 8 BERR : Bus error

This flag is set by hardware when a misplaced Start or STOP condition is detected whereas the peripheral is involved in the transfer. The flag is not set during the address phase in slave mode. It is cleared by software by setting BERRCF bit.

Note: This bit is cleared by hardware when PE = 0.

Bit 7 TCR : Transfer Complete Reload

This flag is set by hardware when RELOAD=1 and NBBYTES data have been transferred. It is cleared by software when NBBYTES is written to a non-zero value.

Note: This bit is cleared by hardware when PE = 0.

This flag is only for master mode, or for slave mode when the SBC bit is set.

Bit 6 TC : Transfer Complete (master mode)

This flag is set by hardware when RELOAD=0, AUTOEND=0 and NBBYTES data have been transferred. It is cleared by software when START bit or STOP bit is set.

Note: This bit is cleared by hardware when PE = 0.

Bit 5 STOPF : Stop detection flag

This flag is set by hardware when a STOP condition is detected on the bus and the peripheral is involved in this transfer:

It is cleared by software by setting the STOPCF bit.

Note: This bit is cleared by hardware when PE = 0.

Bit 4 NACKF : Not Acknowledge received flag

This flag is set by hardware when a NACK is received after a byte transmission. It is cleared by software by setting the NACKCF bit.

Note: This bit is cleared by hardware when PE = 0.

Bit 3 ADDR : Address matched (slave mode)

This bit is set by hardware as soon as the received slave address matched with one of the enabled slave addresses. It is cleared by software by setting ADDRCF bit.

Note: This bit is cleared by hardware when PE = 0.

Bit 2 RXNE : Receive data register not empty (receivers)

This bit is set by hardware when the received data is copied into the I2C_RXDR register, and is ready to be read. It is cleared when I2C_RXDR is read.
Note: This bit is cleared by hardware when PE = 0.

Bit 1 TXIS : Transmit interrupt status (transmitters)

This bit is set by hardware when the I2C_TXDR register is empty and the data to be transmitted must be written in the I2C_TXDR register. It is cleared when the next data to be sent is written in the I2C_TXDR register.

This bit can be written to ‘1’ by software when NOSTRETCH = 1 only, in order to generate a TXIS event (interrupt if TXIE=1 or DMA request if TXDMAEN = 1).
Note: This bit is cleared by hardware when PE = 0.

Bit 0 TXE : Transmit data register empty (transmitters)

This bit is set by hardware when the I2C_TXDR register is empty. It is cleared when the next data to be sent is written in the I2C_TXDR register.

This bit can be written to ‘1’ by software in order to flush the transmit data register I2C_TXDR.
Note: This bit is set by hardware when PE = 0.

20.7.8 I2C interrupt clear register (I2C_ICR)

Address offset: 0x1C

Reset value: 0x0000 0000

Access: No wait states

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.ALERT CFTIMOU TCFPECCFOVRCFARLOC FBERRC FRes.Res.STOPC FNACKC FADDR CFRes.Res.Res.
wwwwwwwww

Bits 31:14 Reserved, must be kept at reset value.

Bit 13 ALERTCF : Alert flag clear

Writing 1 to this bit clears the ALERT flag in the I2C_ISR register.
Note: If the SMBus feature is not supported, this bit is reserved and forced by hardware to ‘0’.
Refer to Section 20.3: I2C implementation.

Bit 12 TIMOUTCF : Timeout detection flag clear

Writing 1 to this bit clears the TIMEOUT flag in the I2C_ISR register.
Note: If the SMBus feature is not supported, this bit is reserved and forced by hardware to ‘0’.
Refer to Section 20.3: I2C implementation.

Bit 11 PECCF : PEC Error flag clear

Writing 1 to this bit clears the PECERR flag in the I2C_ISR register.
Note: If the SMBus feature is not supported, this bit is reserved and forced by hardware to ‘0’.
Refer to Section 20.3: I2C implementation.

Bit 10 OVRCF : Overrun/Underrun flag clear

Writing 1 to this bit clears the OVR flag in the I2C_ISR register.

Bit 9 ARLOCF : Arbitration lost flag clear

Writing 1 to this bit clears the ARLO flag in the I2C_ISR register.

Bit 8 BERRCF : Bus error flag clear

Writing 1 to this bit clears the BERRF flag in the I2C_ISR register.

Bits 7:6 Reserved, must be kept at reset value.

Bit 5 STOPCF : STOP detection flag clear

Writing 1 to this bit clears the STOPF flag in the I2C_ISR register.

Bit 4 NACKCF : Not Acknowledge flag clear

Writing 1 to this bit clears the NACKF flag in I2C_ISR register.

Bit 3 ADDRCF : Address matched flag clear

Writing 1 to this bit clears the ADDR flag in the I2C_ISR register. Writing 1 to this bit also clears the START bit in the I2C_CR2 register.

Bits 2:0 Reserved, must be kept at reset value.

20.7.9 I2C PEC register (I2C_PECR)

Address offset: 0x20

Reset value: 0x0000 0000

Access: No wait states

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.PEC[7:0]
rrrrrrrr

Bits 31:8 Reserved, must be kept at reset value.

Bits 7:0 PEC[7:0] : Packet error checking register

This field contains the internal PEC when PECEN=1.

The PEC is cleared by hardware when PE = 0.

Note: If the SMBus feature is not supported, this register is reserved and forced by hardware to "0x00000000". Refer to Section 20.3: I2C implementation .

20.7.10 I2C receive data register (I2C_RXDR)

Address offset: 0x24

Reset value: 0x0000 0000

Access: No wait states

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.RXDATA[7:0]
rrrrrrrr

Bits 31:8 Reserved, must be kept at reset value.

Bits 7:0 RXDATA[7:0] : 8-bit receive data

Data byte received from the I 2 C bus

20.7.11 I2C transmit data register (I2C_TXDR)

Address offset: 0x28

Reset value: 0x0000 0000

Access: No wait states

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.TXDATA[7:0]
rwrwrwrwrwrwrwrw

Bits 31:8 Reserved, must be kept at reset value.

Bits 7:0 TXDATA[7:0] : 8-bit transmit data

Data byte to be transmitted to the I 2 C bus

Note: These bits can be written only when TXE = 1.

20.7.12 I2C register map

The table below provides the I2C register map and reset values.

Table 101. I2C register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x0I2C_CR1Res.Res.Res.Res.Res.Res.Res.Res.PECENALERTENSMBDENSMBHENGCENWUPENNOSTRETCHSBCRXDMAENTXDMAENRes.ANFOFFDNF[3:0]ERRIETCIESTOPIENACKIEADDRIERXIETXIEPE
Reset value00000000000000000000000
0x4I2C_CR2Res.Res.Res.Res.Res.PECBYTEAUTOENDRELOADNBYTES[7:0]NACKSTOPSTARTHEAD10RADD10RD_WRNSADD[9:0]
Reset value00000000000000000000000000
0x8I2C_OAR1Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.OA1ENRes.Res.Res.Res.OA1MODEOA1[9:0]
Reset value00000000000
0xCI2C_OAR2Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.OA2ENRes.Res.Res.Res.OA2MSK[2:0]OA2[7:1]Res.
Reset value00000000000
0x10I2C_TIMINGRPRESC[3:0]Res.Res.Res.Res.SCLDEL[3:0]SDADEL[3:0]SCLH[7:0]SCLL[7:0]
Reset value000000000000000000000000000
0x14I2C_TIMEOUTRTEXTENRes.Res.Res.TIMEOUTB[11:0]TIMOUTENRes.Res.TIDLETIMEOUTA[11:0]
Reset value00000000000000000000000000
0x18I2C_ISRRes.Res.Res.Res.Res.Res.Res.Res.ADDCODE[6:0]DIRBUSYRes.ALERTTIMEOUTPECERROVRARLOBERRTCRTCSTOPFNACKFADDRRXNETXISTXE
Reset value0000000000000000000001
0x1CI2C_ICRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ALERTCFTIMOUTCFPECFCFOVRCFARLOCFBERRCFRes.Res.STOPCFNACKCFADDRCFRes.Res.Res.
Reset value000000000
0x20I2C_PECRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.PEC[7:0]
Reset value00000000
0x24I2C_RXDRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.RXDATA[7:0]
Reset value00000000

Table 101. I2C register map and reset values (continued)

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x28I2C_TXDRResResResResResResResResResResResResResResResResResResResResResResResResTXDATA[7:0]
Reset value00000000

Refer to Section 2.2 on page 39 for the register boundary addresses.