Folks,
I got some proof-of-concept code for a Microchip 24LC640A 64kb SPI EEPROM working today.
Here is a picture of it on a Sparkfun proto shield (you can't see the Maple, but it is underneath the shield):
Here is the code:
// EEPROM SPI 25LC640
// Demonstrates reading and writing to the EEPROM.
// For the Leaf Labs Maple platform.
//
// Writes a single page (32 bytes),
// then reads it back, printing each byte.
//
// Based on code by Heather Dewey-Hagborg
// http://arduino.cc/en/Tutorial/SPIEEPROM
#define PAGE_SIZE 32
#define SPI_MODE 0
#define CS 10 // chip select
// EEPROM opcodes
#define WREN 6
#define WRDI 4
#define RDSR 5
#define WRSR 1
#define READ 3
#define WRITE 2
// Use SPI 1:
// Pin D11 MOSI
// Pin D12 MISO
// Pin D13 sck
// Pin D10 ss
HardwareSPI Spi(1);
byte eeprom_output_data;
byte eeprom_input_data=0;
int address=0;
// data buffer
uint8 buffer [PAGE_SIZE];
void fill_buffer() {
for (int i=0;i<PAGE_SIZE;i++) {
buffer[i]=i;
}
}
void setup() {
SerialUSB.begin();
SerialUSB.println("starting...");
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH); // disable
Spi.begin(SPI_9MHZ, MSBFIRST, SPI_MODE);
// fill buffer with data
fill_buffer();
// fill eeprom w/ buffer
digitalWrite(CS, LOW); // EEPROM enable
Spi.send(WREN); // EEPROM write enable instruction
digitalWrite(CS, HIGH); // EEPROM disable
delay(10);
digitalWrite(CS, LOW); // EEPROM enable
Spi.send(WRITE); // EEPROM write instruction
address=0;
Spi.send((uint8)(address>>8)); //send MSByte address first
Spi.send((uint8)(address)); //send LSByte address
// write PAGE_SIZE bytes
for (int i=0;i<PAGE_SIZE;i++) {
Spi.send(buffer[i]); // EEPROM write data byte
}
digitalWrite(CS, HIGH); // EEPROM disable
//wait for eeprom to finish writing
delay(3000);
SerialUSB.println("write finished.");
delay(1000);
}
byte read_eeprom(int EEPROM_address) {
//READ EEPROM
int data;
digitalWrite(CS, LOW); // EEPROM enable
Spi.send(READ); //transmit read opcode
Spi.send((uint8)(EEPROM_address>>8)); //send MSByte address first
Spi.send((uint8)(EEPROM_address)); //send LSByte address
data = Spi.send(0xFF); //get data byte
digitalWrite(CS, HIGH); // EEPROM disable
return data;
}
void loop() {
eeprom_output_data = read_eeprom(address);
SerialUSB.print((int) eeprom_output_data);
SerialUSB.println();
address++;
if (address == PAGE_SIZE)
address = 0;
delay(500); // pause for readability
}
It's based off this example on the Arduino website:
http://arduino.cc/en/Tutorial/SPIEEPROM
The circuit is exactly as described in the article.
Here's a link to the Octopart page, with datasheet and pricing:
http://octopart.com/25lc640a-i%2Fp-microchip-7721511
I will eventually turn this into a library; I will let you know when I get that done.