I2C Bit-Bang Driver

Hard C Interfacing Asked at NXP
Solve this problem

Implement the low-level primitives of a bit-banged I2C master on two open-drain GPIO lines. You must produce a correct START, a byte write with ACK sampling, and a STOP.

Constraints

  • Lines are open-drain: drive low by setting the pin as output-low, release by setting it as input.
  • Provide i2c_start, i2c_stop, and i2c_write_byte returning the ACK bit.
  • SDA may only change while SCL is low, except for START and STOP.
  • Data is sent MSB-first.
  • Use the provided sda_low/sda_release/scl_low/scl_release/sda_read hooks.
  • No blocking longer than one bit period per helper call.
Starter codeC
1#include <stdint.h>2 3extern void sda_low(void);4extern void sda_release(void);5extern void scl_low(void);6extern void scl_release(void);7extern int  sda_read(void);8extern void bit_delay(void);9 10void i2c_start(void);11void i2c_stop(void);12int  i2c_write_byte(uint8_t data); /* returns 0 for ACK, 1 for NACK */