{
  "id": "embedded/i2c-clock-stretch-timeout",
  "signature": "I2C: clock stretch timeout on SCL line, slave holding clock low",
  "signature_zh": "I2C：SCL线时钟拉伸超时，从设备保持时钟低电平",
  "regex": "I2C:\\s*clock\\s+stretch\\s+timeout\\s+on\\s+SCL\\s+line,\\s*slave\\s+holding\\s+clock\\s+low",
  "domain": "embedded",
  "category": "protocol_error",
  "subcategory": null,
  "root_cause": "I2C slave device stretches the clock (holds SCL low) beyond the master's timeout period, often due to slow internal processing or bus lock.",
  "root_cause_type": "generic",
  "root_cause_zh": "I2C从设备拉伸时钟（保持SCL低电平）超过主设备超时周期，通常由内部处理缓慢或总线锁定引起。",
  "versions": [
    {
      "version": "STM32Cube_FW_L4_V1.18.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "IAR EWARM 9.40.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "ARM GCC 10.3.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "FreeRTOSv202212.01",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Reducing I2C bus speed from 400 kHz to 100 kHz",
      "why_fails": "Slower speed may increase stretch duration, making timeout more likely.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Disabling clock stretching in the slave device configuration",
      "why_fails": "Many I2C slaves require clock stretching for proper operation; disabling it may cause data corruption.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Adding a hardware pull-up resistor on SCL line",
      "why_fails": "Pull-up resistors are already present; this does not address the slave holding the line low.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Increase I2C timeout in HAL_I2C_Master_Transmit(): set hi2c1.Init.Timing = 0x20C0E0FF; (for 100 kHz) and use HAL_I2C_Master_Transmit() with a timeout of 10000 ms instead of default 1000 ms.",
      "success_rate": 0.82,
      "how": "Increase I2C timeout in HAL_I2C_Master_Transmit(): set hi2c1.Init.Timing = 0x20C0E0FF; (for 100 kHz) and use HAL_I2C_Master_Transmit() with a timeout of 10000 ms instead of default 1000 ms.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Implement software reset of I2C peripheral after timeout: call HAL_I2C_DeInit(&hi2c1); HAL_I2C_Init(&hi2c1); then retry the transaction up to 3 times.",
      "success_rate": 0.78,
      "how": "Implement software reset of I2C peripheral after timeout: call HAL_I2C_DeInit(&hi2c1); HAL_I2C_Init(&hi2c1); then retry the transaction up to 3 times.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use I2C recovery sequence: toggle SCL 9 times while SDA is high to release the slave: for(int i=0;i<9;i++) { HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_RESET); delay_us(5); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); delay_us(5); }",
      "success_rate": 0.85,
      "how": "Use I2C recovery sequence: toggle SCL 9 times while SDA is high to release the slave: for(int i=0;i<9;i++) { HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_RESET); delay_us(5); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); delay_us(5); }",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Increase I2C timeout in HAL_I2C_Master_Transmit(): set hi2c1.Init.Timing = 0x20C0E0FF; (for 100 kHz) and use HAL_I2C_Master_Transmit() with a timeout of 10000 ms instead of default 1000 ms.",
    "Implement software reset of I2C peripheral after timeout: call HAL_I2C_DeInit(&hi2c1); HAL_I2C_Init(&hi2c1); then retry the transaction up to 3 times.",
    "Use I2C recovery sequence: toggle SCL 9 times while SDA is high to release the slave: for(int i=0;i<9;i++) { HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_RESET); delay_us(5); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); delay_us(5); }"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.st.com/resource/en/application_note/dm00108059-i2c-timing-configuration-on-stm32-mcus-stmicroelectronics.pdf",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.79,
  "resolvable": "true",
  "first_seen": "2024-02-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}