I found a header file to test a i2c eeprom 24LC256 , but I used this to test my eeprom CAT24C32.The test sketch works fine.
I have also a FRAM MB85RC256V, I am wondering if can i use the same header to test the FRAM. I saw the command byte write are same like eeprom, but the page write have a different side of buffer, it says have 32Kbytes ...
If I made a mistake please help me. Thanks very much!
here the .h
#ifndef EEPROM_ROUTINES
#define EEPROM_ROUTINES
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#define BLOCKSIZE 8
#define I2CADDR 0x57
#define BLOCKSIZE_READ 30
template <class T>
uint16_t writeObjectSimple(uint8_t i2cAddr, uint16_t addr, const T& value){
const uint8_t* p = (const uint8_t*)(const void*)&value;
uint16_t i;
for (i = 0; i < sizeof(value); i++){
Wire.beginTransmission(i2cAddr);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF));// LSB
Wire.write(*p++);
Wire.endTransmission();
addr++;
delay(5); //max time for writing in 24LC256
}
return i;
}
template <class T>
uint16_t readObjectSimple(uint8_t i2cAddr, uint16_t addr, T& value){
uint8_t* p = (uint8_t*)(void*)&value;
uint8_t objSize = sizeof(value);
uint16_t i;
for (i = 0; i < objSize; i++){
Wire.beginTransmission (i2cAddr);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF));// LSB
Wire.endTransmission();
Wire.requestFrom(i2cAddr, (uint8_t)1);
if(Wire.available()){
*p++ = Wire.read();
}
addr++;
}
return i;
}
template <class T>
uint16_t writeObject(uint16_t addr, const T& value){
const uint8_t* p = (const uint8_t*)(const void*)&value;
Wire.beginTransmission(I2CADDR);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF)); // LSB
//in the loop: counts the bytes we may send before
//our block becomes full
//but initialise it to the number of bytes up to the
//next 16-byte aligned address
uint8_t blockBytes = (addr/BLOCKSIZE + 1)*BLOCKSIZE - addr;
uint16_t i;
for (i = 0; i < sizeof(value); i++){
if (blockBytes == 0){
//block is full;
Wire.endTransmission(); //dispatch the buffer
delay(5);
//restart new block
addr = (addr/BLOCKSIZE + 1)*BLOCKSIZE;
blockBytes = BLOCKSIZE;
Wire.beginTransmission(I2CADDR);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF));// LSB
}
Wire.write(*p++); //dispatch the data byte
blockBytes--; //decrement the block space
}
Wire.endTransmission();
delay(5); //required write delay 5ms
return i;
}
template <class T>
uint16_t readObject(uint16_t addr, T& value){
uint8_t* p = (uint8_t*)(void*)&value;
Wire.beginTransmission(I2CADDR);
Wire.write((uint16_t)(addr >> 8)); // MSB
Wire.write((uint16_t)(addr & 0xFF)); // LSB
Wire.endTransmission();
//counts the bytes we may read before buffer is depleted
uint8_t blockBytes = 0;
uint16_t objSize = sizeof(value);
uint16_t i;
for (i = 0; i < objSize; i++){
if (blockBytes==0){
//we need a new block
blockBytes = BLOCKSIZE_READ;
if (objSize < blockBytes) blockBytes = objSize;
//get the new block
Wire.requestFrom((uint8_t)I2CADDR, blockBytes);
}
if(Wire.available()){
//read a byte from buffer
*p++ = Wire.read();
blockBytes--;
}
}
return i;
}
#endif