I need to create C code that interfaces with an external EEPROM using bit-banging for I2C. I’ll assume a generic microcontroller and use GPIO functions for setting SDA and SCL. My tasks include implementing start/stop conditions and handling bit/byte reads and writes, along with ACK/NACK and EEPROM operations focused on something like the 24LC256. Although the user didn't ask for it, I think a moderate explanation will be beneficial. I want to ensure the code structure shows the necessary functions without copying from proprietary sources. Let's get started!Creating EEPROM functions
I’m outlining the necessary functions for interfacing with the EEPROM. I’ll start with general I2C functions like i2c_init, i2c_start, i2c_stop, and byte read/write functions that handle ACK and NACK responses. Then, for EEPROM-specific actions, I’ll define functions using a typical address like 0x50 for the 24xx series.
Additionally, I'll need to include ACK polling after write cycles and manage the address space for a 24LC256 EEPROM. Implementing a delay function is also necessary, possibly using a volatile loop. I’ll make sure to manage GPIO states properly and provide placeholders for the user’s implementation.Implementing bit-banging I2C
When using bit-banging, I need to implement the read bit function carefully by releasing SDA for input and raising SCL to read SDA before lowering SCL. I’ll also discuss timing considerations for I2C, ensuring the code is straightforward and includes necessary delays.
I envision crafting a single .c file with relevant sections, perhaps separating them into i2c_bitbang.c/h and eeprom.c/h. While the user didn’t specify a library layout, I think including inline comments will be helpful. I’ll prepare to respond in markdown with code blocks for clarity.Defining I2C macros and functions
I’m working on macros for the I2C implementation to define essential functions, starting with SDA_OUTPUT and SDA_INPUT, as well as high and low settings for SDA and SCL.
For example, in i2c_start, I’ll ensure SDA is set high, followed by setting SCL high, adding delays in between. The stop condition will involve pulling SDA low before raising it again after a delay.
I’ll also implement functions for writing and reading bits. When waiting for an ACK, I’ll define the process and clarify the typical logic for detecting ACK by reading the SDA line.Implementing EEPROM operations
I’ll define a function to return 1 if an ACK is received, meaning the SDA line is pulled low. The AAC detection will occur after reading the byte.
For EEPROM operations, the random read process involves sending a start signal, the device address with a write bit, then the word address, followed by a repeated start signal and a read byte with NACK before stopping. The write process follows a similar pattern, ensuring we wait for an acknowledgment before proceeding.
I’ll keep timeout definitions in mind to prevent infinite loops and ensure page boundaries are respected when writing, using EEPROM_PAGE_SIZE. I’ll phrase this with a clear explanation alongside the code without being too lengthy. Below is a self‑contained example of bit‑banged I²C in C plus simple routines to read/write a typical 24xx‑series EEPROM (e.g., 24LC256).
You must adapt the low‑level GPIO access functions (marked with TODO) to your microcontroller.
1. Low-level I²C bit-bang layer (i2c_bitbang.c)
c
2. EEPROM interface (24xx series, 16-bit address)
Assumes a 24LC256-like device:
7-bit base address 0x50 (A0–A2 = 0)
16-bit word address (two bytes: high then low)
Page size 64 bytes
c
3. Example usage
c
int main(void)
{
uint8_t buf[16];
i2c_init();
// Write single byte
eeprom_write_byte(0x0010, 0x5A);
// Read it back
eeprom_read(0x0010, buf, 1);
// buf[0] should now be 0x5A
// Write a small block (ensure it stays within one page)
const uint8_t data_to_write[4] = { 1, 2, 3, 4 };
eeprom_write_page(0x0040, data_to_write, 4);
// Read it back
eeprom_read(0x0040, buf, 4);
// buf[0..3] should now be {1,2,3,4}
while (1) {
// your loop
}
}
If you tell me your exact microcontroller or SDK (e.g., STM32 HAL, AVR, bare‑metal on a specific chip), I can fill in the GPIO access functions (gpio_sda_*, gpio_scl_*) for that platform.
#include <stdint.h>
#include <stdbool.h>
/* ---------------- Hardware-specific stubs (EDIT THESE) ---------------- */
/* Replace these with your MCU's GPIO functions/macros. */
static inline void gpio_sda_low(void) { /* TODO: drive SDA pin low */ }
static inline void gpio_sda_high(void) { /* TODO: release SDA pin (input / high) */ }
static inline void gpio_scl_low(void) { /* TODO: drive SCL pin low */ }
static inline void gpio_scl_high(void) { /* TODO: release SCL pin (input / high) */ }
static inline void gpio_sda_output(void){ /* TODO: configure SDA as output (open-drain if possible) */ }
static inline void gpio_sda_input(void) { /* TODO: configure SDA as input (hi-Z) */ }
static inline uint8_t gpio_sda_read(void)
{
/* TODO: return 0 or 1 depending on SDA pin level */
return 1;
}
/* Small delay to satisfy I2C timing. Adjust as needed. */
static inline void i2c_delay(void)
{
volatile int i;
for (i = 0; i < 50; i++) {
__asm__ volatile ("nop");
}
}
/* ---------------- I2C bit-bang primitives ---------------- */
static inline void SDA_LOW(void)
{
gpio_sda_output();
gpio_sda_low();
}
static inline void SDA_RELEASE(void)
{
/* Release line so external pull-up pulls it high */
gpio_sda_input();
gpio_sda_high(); /* if needed to disable internal pull-downs */
}
static inline void SCL_LOW(void)
{
gpio_scl_low();
}
static inline void SCL_RELEASE(void)
{
gpio_scl_high(); /* release SCL (becomes high via pull-up) */
}
/* Initialize lines to idle (bus free: SDA=1, SCL=1) */
void i2c_init(void)
{
SDA_RELEASE();
SCL_RELEASE();
}
/* I2C START condition: SDA goes low while SCL is high */
void i2c_start(void)
{
SDA_RELEASE();
SCL_RELEASE();
i2c_delay();
SDA_LOW();
i2c_delay();
SCL_LOW();
}
/* I2C STOP condition: SDA goes high while SCL is high */
void i2c_stop(void)
{
SDA_LOW();
i2c_delay();
SCL_RELEASE();
i2c_delay();
SDA_RELEASE();
i2c_delay();
}
/* Write one bit on SDA (MSB first) */
static void i2c_write_bit(uint8_t bit)
{
if (bit) {
SDA_RELEASE(); // let it float high
} else {
SDA_LOW();
}
i2c_delay();
SCL_RELEASE(); // clock high
i2c_delay();
SCL_LOW(); // clock low
}
/* Read one bit from SDA */
static uint8_t i2c_read_bit(void)
{
uint8_t bit;
SDA_RELEASE(); // release SDA to be read
i2c_delay();
SCL_RELEASE(); // clock high
i2c_delay();
bit = gpio_sda_read();
SCL_LOW(); // clock low
return bit;
}
/* Write a byte, MSB first.
* Returns true if ACK received from slave, false if NACK. */
bool i2c_write_byte(uint8_t byte)
{
for (int i = 7; i >= 0; i--) {
i2c_write_bit((byte >> i) & 0x01);
}
/* Read ACK bit */
SDA_RELEASE(); // release SDA
i2c_delay();
SCL_RELEASE();
i2c_delay();
bool ack = (gpio_sda_read() == 0); // ACK: SDA pulled low by slave
SCL_LOW();
return ack;
}
/* Read a byte from I2C.
* If ack=true, send ACK after reading the byte; else send NACK. */
uint8_t i2c_read_byte(bool ack)
{
uint8_t byte = 0;
for (int i = 7; i >= 0; i--) {
uint8_t bit = i2c_read_bit();
byte |= (bit << i);
}
/* Send ACK/NACK bit */
if (ack) {
SDA_LOW(); // ACK = 0
} else {
SDA_RELEASE(); // NACK = 1
}
i2c_delay();
SCL_RELEASE();
i2c_delay();
SCL_LOW();
SDA_RELEASE();
return byte;
}
#include <stdint.h>
#include <stdbool.h>
/* From the bit-bang implementation above */
void i2c_init(void);
void i2c_start(void);
void i2c_stop(void);
bool i2c_write_byte(uint8_t byte);
uint8_t i2c_read_byte(bool ack);
/* EEPROM parameters (adjust for your device) */
#define EEPROM_I2C_ADDR_BASE 0x50 // 24LC256 base address when A0-A2 = 0
#define EEPROM_PAGE_SIZE 64
#define EEPROM_WRITE_TIMEOUT 10000 // arbitrary loop count for timeout
/* ---------- Internal helper: poll for write completion ---------- */
static bool eeprom_wait_write_complete(void)
{
/* After a write, the EEPROM internally programs the data.
During this time it NACKs its address. We poll until it ACKs. */
for (int i = 0; i < EEPROM_WRITE_TIMEOUT; i++) {
i2c_start();
bool ack = i2c_write_byte((EEPROM_I2C_ADDR_BASE << 1) | 0); // write mode
i2c_stop();
if (ack) {
return true; // ready
}
}
return false; // timed out
}
/* ---------- Byte write ---------- */
bool eeprom_write_byte(uint16_t mem_addr, uint8_t data)
{
i2c_start();
// Send device address (write)
if (!i2c_write_byte((EEPROM_I2C_ADDR_BASE << 1) | 0)) {
i2c_stop();
return false;
}
// Send 16-bit memory address (high then low)
if (!i2c_write_byte((uint8_t)(mem_addr >> 8))) {
i2c_stop();
return false;
}
if (!i2c_write_byte((uint8_t)(mem_addr & 0xFF))) {
i2c_stop();
return false;
}
// Send data byte
if (!i2c_write_byte(data)) {
i2c_stop();
return false;
}
i2c_stop();
// Wait for internal write cycle to finish
return eeprom_wait_write_complete();
}
/* ---------- Sequential read (len bytes) ---------- */
bool eeprom_read(uint16_t mem_addr, uint8_t *buf, uint16_t len)
{
if (buf == NULL || len == 0) {
return false;
}
/* Setup address pointer (write phase) */
i2c_start();
if (!i2c_write_byte((EEPROM_I2C_ADDR_BASE << 1) | 0)) { // write mode
i2c_stop();
return false;
}
// Send 16-bit memory address
if (!i2c_write_byte((uint8_t)(mem_addr >> 8))) {
i2c_stop();
return false;
}
if (!i2c_write_byte((uint8_t)(mem_addr & 0xFF))) {
i2c_stop();
return false;
}
/* Repeated START, then read */
i2c_start();
if (!i2c_write_byte((EEPROM_I2C_ADDR_BASE << 1) | 1)) { // read mode
i2c_stop();
return false;
}
for (uint16_t i = 0; i < len; i++) {
bool ack = (i < (len - 1)); // ACK all but last byte
buf[i] = i2c_read_byte(ack);
}
i2c_stop();
return true;
}
/* ---------- Page write (up to EEPROM_PAGE_SIZE, not crossing boundary) ---------- */
bool eeprom_write_page(uint16_t mem_addr, const uint8_t *data, uint16_t len)
{
if (data == NULL || len == 0) {
return false;
}
if (len > EEPROM_PAGE_SIZE) {
// user should split data to fit within page boundaries
return false;
}
// Ensure we don't cross the page boundary.
uint16_t page_offset = mem_addr % EEPROM_PAGE_SIZE;
if ((page_offset + len) > EEPROM_PAGE_SIZE) {
// Would cross page, not allowed in one op
return false;
}
i2c_start();
// Send device address (write)
if (!i2c_write_byte((EEPROM_I2C_ADDR_BASE << 1) | 0)) {
i2c_stop();
return false;
}
// Send 16-bit memory address
if (!i2c_write_byte((uint8_t)(mem_addr >> 8))) {
i2c_stop();
return false;
}
if (!i2c_write_byte((uint8_t)(mem_addr & 0xFF))) {
i2c_stop();
return false;
}
// Send data bytes
for (uint16_t i = 0; i < len; i++) {
if (!i2c_write_byte(data[i])) {
i2c_stop();
return false;
}
}
i2c_stop();
// Wait for internal write cycle
return eeprom_wait_write_complete();
}