Writing / Reading 32 bit values to EEPROM using SPI.transfer()

Hi guys/gals,
I'm trying to read/write 32 bit long values to a Micron M25P16 using the SPI Arduino library. As I understand it you can only transfer/receive 8 bits of information at a given time using SPI.transfer().
I'm using a 328 arduino pro mini. I have been successful reading and writing 1 byte of data. But when it comes to a long or float I somehow can't manage to capture or drive the the full 4 byte transmission.
Here is a sample of confirmed working code that will work with a 1 byte write.

void WriteByte() {

digitalWrite(slaveSelect,LOW);
SPI.transfer(pageProgram); //op command for a write command
int address = 3;
SPI.transfer(address >> 16); //page program needs the address to be sent as a 3 byte address MSB
SPI.transfer(address >> 8);
SPI.transfer(address);
SPI.transfer(0b01111111); // takes 1 data byte, decimal 127
digitalWrite(slaveSelect,HIGH);
delay(1000); //write buffer
}

So basically the question is how would I capture or transmit a 32 bit value? my thoughts are something like this but this is just returning a 0 value when I try to perform a read.

float buffer [10] = {70.2, -24.5, 100.23, 10, 65326, 2};

void WriteDataBytes(uint32_t epromStartAddress) {

digitalWrite(chipSelect, LOW);
SPI.transfer(pageProgram);
SPI.transfer(epromStartAddress >> 16); //address to be sent as a 3 byte address MSB
SPI.transfer(epromStartAddress >> 8);
SPI.transfer(epromStartAddress);
for (int I = 0; I < 8; I++) {
SPI.transfer(buffer >> 24);
_ SPI.transfer(buffer >> 16);_
_ SPI.transfer(buffer >> 8);
* SPI.transfer(buffer[0]);
}
digitalWrite(chipSelect, HIGH);
delay(100);
}
I have attached the EPROM spec but my inclination is that the fault lies with how I use SPI.transfer()._

M25P16_OnBoardFlash16Mb.pdf (614 KB)*

You did quite a mess there with the buffer and bit-shifting... :wink:

The principle is correct: a 32-bit variable has to be "broken down" for 8-bit transmission. It's just a question of breaking it down correctly and putting it back together in the right order.

I'm assuming you deal with unsigned 32-bit int (otherwise, the +/- bit may cause some trouble). To access the individual bytes (for reading only), you can do a bit shift:

highestByte = my32BitVariable >> 24; // Moving 24 bits to the right, leaves only the top 8
Byte2         = my32BitVariable >> 16; // Shift leaves top 16 - only last 8 are used when casting to byte
Byte3         = my32BitVariable >> 8;
lowestByte = my32BitVariable; // again, only lowest 8 bits are copied to a byte

To recombine, you can do something like this:

result = highestByte;
result = result << 8 + Byte2; // previous byte shifted up, 2nd added
result = result << 8 + Byte3;
result = result << 8 + lowestByte;

Be careful if you attempt to narrow this to one line, because the implicit conversions will mess you up. It's quite a common bug apparently.