79600515

Date: 2025-04-30 14:26:48
Score: 0.5
Natty:
Report link

Modified part:

uint8_t i2c_wait_ack(void)
{
    uint8_t ack = 0;
    uint16_t wait_time = 0;

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = I2C_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    // i2c_sda(1); // 释放SDA
    i2c_delay();
    i2c_scl(1); // 拉高SCL
    i2c_delay();

    while (i2c_read_sda())
    {
        wait_time++;
        if (wait_time > 500) // 超时
        {
            i2c_stop(); // 产生停止信号
            ack = 1;    // 未接收到应答
            break;
        }
    }

    i2c_delay();
    i2c_scl(0); // 拉低SCL

    GPIO_InitStruct.Pin = I2C_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    return ack;
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: chipdynkid