UTILS I2C Timing How to Use ¶
- group UTILS_I2C_How_To_Use
-
How to use the utils_i2c module ¶
Configure your I2C peripheral:
Set up the I2C hardware (clock, GPIO, NVIC) as required for your STM32 device.
Choose your timing configuration approach:
Basic mode: Use the
stm32_utils_i2c_timing_basic_config_tstructure for quick setup. This mode supports only standard bus frequencies:100 kHz (Standard mode)
400 kHz (Fast mode)
1 MHz (Fast mode plus) Provide your clock source frequency and desired I2C speed (must match one of the above). The timing calculation will use protocol-compliant defaults.
Advanced mode: Use the
stm32_utils_i2c_timing_advanced_config_tstructure for full customization. This mode allows:Any I2C speed in the supported range (1 Hz to 100 kHz for standard, 101 kHz to 400 kHz for fast, 401 kHz to 1 MHz for fast plus)
Fine-tuning of rising time, falling time, digital noise filter coefficient, analog filter mode. Specify all timing parameters explicitly for precise control.
Calculate timing values:
For basic timing, call
STM32_UTILS_I2C_ComputeTimingBasic()with your basic config.For advanced timing, call
STM32_UTILS_I2C_ComputeAdvanced()with your advanced config.On success, use the timing register value to configure your I2C peripheral. If the function returns an error, check your input parameters.
Apply the timing to your I2C peripheral:
Use the computed timing value to configure your I2C instance using the HAL or LL driver, or by direct register access.
The function
LL_I2C_SetTimingwrites the value to the I2C_TIMINGR register.Alternatively, write the computed value directly to the I2C_TIMINGR register for low-level or performance-critical use cases.
SMBUS timeout calculation (if needed):
For SMBUS TIMEOUTA, fill a
stm32_utils_i2c_smbus_timeouta_config_tstructure and callSTM32_UTILS_I2C_CompTimeoutA().For SMBUS TIMEOUTB, fill a
stm32_utils_i2c_smbus_timeoutb_config_tstructure and callSTM32_UTILS_I2C_CompTimeoutB().On success, use the returned value to configure the SMBUS TIMEOUT registers:
Recommended: Use
LL_I2C_ConfigSMBusTimeout()to set the timeout value, then enable it withLL_I2C_EnableSMBusTimeout()and the appropriate parameter (LL_I2C_SMBUS_TIMEOUTA,LL_I2C_SMBUS_TIMEOUTB, orLL_I2C_SMBUS_ALL_TIMEOUT).Alternatively, for direct register access, write the computed value to the TIMEOUTA and TIMEOUTB fields of the I2C_TIMEOUTR register.
Integrate with your application:
Use the computed timing and timeout values to ensure robust and specification-compliant I2C/SMBUS communication.
Note:
The calculation and application workflow is identical for both basic and advanced modes, and for TIMEOUTA and TIMEOUTB.