Electrical – Atmel SPI Serial EEPROM Writing Error – Page Write

.net-micro-frameworkatmeleeprommicrocontrollerspi

I'm using this EEPROM: Atmel AT25640B SPI EEPROM DATASHEET

It seems pretty straight forward to operate and I've followed the datasheet closely when coding my driver. I seemed to have some success without too much trouble. Now, I'm having some issue with writing bytes and they're not sticking. For example, I write 10 bytes, but when I immediately read them back(to verify they've been written, I get all 10 bytes back at 0. Is there possibly something fundamental that I'm missing with how to operate this EEPROM?

As I understand Page Write, I can write up to 32 bytes sequentially and then it will roll over to the start address? Are the "pages" physically set 0-31, 32-63, etc or are they determined by where you start writing? ex: If I write 8 bytes at address 29, will it roll over to 0 or will it continue as expected and finish writing at 36? The datasheet is not clear.

The basic steps seem to be to send a WRITE ENABLE Command(1). Then separately, send a WRITE Command followed by address and the bytes to write.

I'm running the at 5v. I've tried to slow down the SPI clock to 1khz from 10mhz with no difference in behavior.

What else can I try?

Best Answer

This device has Page Write functionality. Pages are 32 bytes in size and fixed in address starting from 0. As pointed out by @kkrambo

0-31 32-63 ... etc

If writing a sequence of bytes across a page boundary, we need to break up the bytes into two parts at the point where one page ends and another begins.

Here's some sample code if a sequence spans across two pages(multiple pages are not addressed here):

public void WriteBytesPageSpan(ushort startAddress, byte[] byteBuffer)
        {
            ushort spanAddress = AddressSpansPage(startAddress, byteBuffer.Length);
            if (spanAddress != 0)
            {   
                //perform write on first half
                byte[] splitBuffer= new byte[spanAddress-startAddress];
                Array.Copy(byteBuffer, 0, splitBuffer, 0, spanAddress - startAddress);
                WriteBytes(startAddress, splitBuffer);

                //perform write on second half
                ushort endAddress = (ushort)((startAddress + byteBuffer.Length) - 1);
                splitBuffer = new byte[(endAddress - spanAddress) + 1];
                Array.Copy(byteBuffer, spanAddress-startAddress, splitBuffer, 0, splitBuffer.Length);
                WriteBytes(spanAddress, splitBuffer);
            }

            else WriteBytes(startAddress, byteBuffer);
        }