Build note
SPI in practice: a 25LC512 EEPROM driver
Talking to another ATmega proves the bus works. Talking to a commercial chip proves the driver works. The 25LC512 is a 512 Kbit SPI EEPROM: 64 KB of memory that survives power-off, addressed with 16 bits and written 128 bytes at a time.
It is also the first device in this series that has a command set, a status register and a timing rule you have to respect.
The command set
| Command | Byte | Job |
|---|---|---|
| READ | 0x03 |
Read from an address |
| WRITE | 0x02 |
Write to an address |
| WREN | 0x06 |
Enable writes |
| WRDI | 0x04 |
Disable writes |
| RDSR | 0x05 |
Read the status register |
| WRSR | 0x01 |
Write the status register |
Every command begins with chip select going low and ends with it going high. That is the frame, exactly as in Part 8.
Reading a byte
CS low
0x03
address high
address low
0xFF <- dummy byte, the data comes back during it
CS high
The dummy byte is not padding. SPI is a shift register, so you have to clock a
byte in to get a byte out. Sending 0xFF gives the clock pulses that shift the
stored value back.
Writing a byte takes two frames
CS low, 0x06 (WREN), CS high <- unlock
CS low, 0x02, addr hi, addr lo, data, CS high
wait 5 ms
The write-enable step is a deliberate safety catch. Without it the chip ignores the write command and the memory does not change. That is a common first bug: the read returns the old value and the code looks broken.
After the write, the chip is busy for about 5 ms. You can wait blindly or poll the status register:
while (EEPROM_ReadStatusRegister() & EEPROM_STATUS_WIP)
_delay_ms(1);
WIP is the write-in-progress bit. Polling is faster and more reliable than a
fixed delay tuned to one chip.
The page boundary trap
The EEPROM is organised in 128-byte pages. A write command can start anywhere,
but it wraps inside the page it started in. Write 200 bytes starting at address
0x0070 and the last 72 bytes overwrite the start of that same page instead of
continuing into the next one.
The driver handles this by splitting a long write at the page edge:
uint16_t pageEnd = (currentAddress & 0xFF80) + 128 - 1;
uint16_t chunk = min(length, pageEnd - currentAddress + 1);
The mask 0xFF80 rounds the address down to the start of its 128-byte page,
because 128 is 0x80. That single line is the difference between a driver that
works on small buffers and one that corrupts data on large ones.
Wiring
ATmega328P 25LC512
PB2 CS -------> pin 1 CS
PB4 MISO <------- pin 2 SO
PB3 MOSI -------> pin 5 SI
PB5 SCK -------> pin 6 SCK
5V -------> pin 3 WP (write protect off)
5V -------> pin 7 HOLD (never pause)
5V -------> pin 8 VCC
GND -------> pin 4 GND
WP and HOLD must be tied high. Left floating, one protects the chip and the
other can pause a transfer halfway, and neither failure is obvious.
The failures that cost time
- No WREN before WRITE. The write is ignored.
- Not waiting for
WIP. The next command arrives while the chip is busy and is dropped. - Crossing a page boundary. Data wraps inside the page.
DORDset to 1. The chip expects most significant bit first.- Chip select left low between commands. The chip keeps waiting for more of the previous command.
What the driver gives you
EEPROM_ReadByte, EEPROM_WriteByte, EEPROM_ReadArray, EEPROM_WriteArray,
plus status and erase helpers. The demonstration functions test a single byte, an
array, a page boundary, the full memory and a small config block.
The page-boundary test is the one worth keeping. It is the test that catches the bug above, and it is the reason the driver is trustworthy rather than lucky.
How this was made
- Written by: Rupesh, from his own
c-09-spi/02-eepromdriver and notes, with AI drafting and editing. - Command bytes and timing: from the 25LC512 datasheet.
- AI-assisted sections: structure and copy editing.