Is there a bug in the hardware I2C of STM32F103RCT6?

 The STM32F103RCT6, part of the STM32F1 series, does have known hardware issues with the I2C peripheral, especially in early silicon revisions. These issues are well-documented in the ST errata sheets.




 Known I2C Issues in STM32F103RCT6

From the STM32F103xC/D/E errata sheet, here are some notable I2C-related bugs:

1. I2C Busy Flag Stuck (BSY bit never cleared)

  • Issue: If a communication is interrupted (e.g., by a reset or a glitch), the I2C peripheral can think the bus is still busy.

  • Impact: The I2C_SR2.BSY bit stays high indefinitely, preventing further communication.

  • Workaround: You must manually reset the I2C peripheral and reinitialize it.

2. Failure to Detect Stop Condition

  • Issue: Under some conditions, the STOP condition is not correctly detected by the I2C hardware.

  • Impact: The I2C peripheral might hang or misbehave after repeated communications.

  • Workaround: Often handled by carefully placing delays and confirming the STOP bit in software.

3. Stretching or Clock Synchronization Issues

  • Issue: The hardware doesn’t always handle clock stretching or arbitration well, especially under high loads or interrupts.

  • Impact: Loss of synchronization with slave devices.

  • Workaround: Use lower speeds (100 kHz), check timing margins, and avoid DMA for I2C in F1 series.


 Recommended Workarounds

If you're using HAL, and your I2C gets stuck, try this recovery function:

c

void I2C_Reset(I2C_HandleTypeDef *hi2c) { __HAL_RCC_I2C1_FORCE_RESET(); HAL_Delay(1); __HAL_RCC_I2C1_RELEASE_RESET(); MX_I2C1_Init(); // Re-initialize your I2C config }

If using bare-metal or LL, do a similar register-level reset and reconfigure the pins as I2C.

You can also implement a manual I2C bus recovery sequence:

c

// If SDA is stuck low, toggle SCL manually for (int i = 0; i < 9; i++) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); // SCL High HAL_Delay(1); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // SCL Low HAL_Delay(1); }

 Further Suggestions

  • Download the Errata Sheet for STM32F103xC/D/E and check Section 2.11 I2C peripheral limitations.

  • Always use the latest STM32CubeF1 HAL package (some workarounds are built-in).

  • Avoid using I2C in high-speed or DMA mode on STM32F1 unless absolutely needed.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How to add a GPS sensor to ESP32 for Wokwi?