I need to interface with an external EEPROM using bit-banging I2C in C since my MCU has no hardware I2C. Implement clock stretching support where the master waits if the slave holds SCL low, and add a timeout mechanism so the system doesn't hang if the line gets stuck.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Providing bit-banged I2C with clock stretching and timeout
The response plans a generic C implementation of bit-banged I2C supporting clock stretching by waiting for SCL to go high with a timeout, using macros to control pin direction and line state. It includes core functions like start/stop, read/write bits and bytes, plus a timeout-based wait for SCL release to handle stuck line scenarios robustly.
A software master has to emulate the open‑drain behaviour of the bus: driving a line high means releasing it (configure as input and let the pull‑up do the work), driving it low means actively pulling it down. Clock stretching is handled by checking the SCL line after you release it, and a timeout keeps you from blocking forever if the slave never lets go.
One way to structure it is like this:
c
#include <stdint.h>
#include <stdbool.h>
/* Implement these three for your MCU */
static inline void SDA_drive_low(void);
static inline void SDA_release(void); /* high‑Z, pulled up externally */
static inline bool SDA_read(void);
static inline void SCL_drive_low(void);
static inline void SCL_release(void); /* high‑Z, pulled up externally */
static inline bool SCL_read(void);
static void delay_us(unsigned int us); /* ~5 µs for standard mode, etc. */
/* Timeout for clock stretching/stuck bus in microseconds */
#define SCL_TIMEOUT_US 1000
/* Wait for SCL to actually go high, with timeout */
static bool wait_for_scl_high(unsigned int timeout_us)
{
SCL_release();
while (!SCL_read()) {
if (timeout_us-- == 0) {
return false; /* timed out */
}
delay_us(1);
}
return true;
}
static bool i2c_start(void)
{
SDA_release();
SCL_release();
if (!wait_for_scl_high(SCL_TIMEOUT_US)) return false;
/* generate a start condition: SDA goes low while SCL high */
SDA_drive_low();
delay_us(5);
SCL_drive_low();
return true;
}
static bool i2c_stop(void)
{
/* generate stop: SDA goes high while SCL high */
SDA_drive_low();
delay_us(5);
SCL_release();
if (!wait_for_scl_high(SCL_TIMEOUT_US)) return false;
SDA_release();
delay_us(5);
return true;
}
static bool i2c_write_bit(bool bit)
{
if (bit) SDA_release(); else SDA_drive_low();
delay_us(2);
if (!wait_for_scl_high(SCL_TIMEOUT_US)) return false; /* allow stretching */
delay_us(2);
SCL_drive_low();
return true;
}
static bool i2c_read_bit(bool *bit)
{
SDA_release(); /* release SDA so slave can drive it */
delay_us(2);
if (!wait_for_scl_high(SCL_TIMEOUT_US)) return false;
*bit = SDA_read();
delay_us(2);
SCL_drive_low();
return true;
}
static bool i2c_write_byte(uint8_t byte)
{
for (int i = 0; i < 8; i++) {
if (!i2c_write_bit((byte & 0x80) != 0)) return false;
byte <<= 1;
}
/* ACK bit from slave */
bool ack;
if (!i2c_read_bit(&ack)) return false;
return !ack; /* ack is active low */
}
static bool i2c_read_byte(uint8_t *byte, bool ack)
{
uint8_t val = 0;
for (int i = 0; i < 8; i++) {
bool bit;
if (!i2c_read_bit(&bit)) return false;
val = (val << 1) | (bit ? 1 : 0);
}
/* send ACK/NACK */
if (!i2c_write_bit(!ack)) return false;
*byte = val;
return true;
}
You need to fill in the GPIO accessors for your platform. For example, on an AVR using pull‑ups on PORTB0/1:
When you call wait_for_scl_high after releasing SCL, the routine will sit in the loop while the slave stretches the clock. If the line stays low longer than SCL_TIMEOUT_US, it returns false so your higher‑level code can abort or reset the bus instead of hanging forever.
To write to a 24xx‑series EEPROM you would do something like:
and likewise for reads (issue a repeated START and address with the R/W bit set). The important part for your requirements is that every time the master raises SCL it calls wait_for_scl_high, which honors clock stretching and enforces a timeout.