Cross-platform EEPROM routines for D21G
Hello all,
today I started working on D21G with the purpose to update existing installations.
After a few preliminary tests, I decided to make my software cross-platform, to avoid future problems with upgrades.
So I want to share with you my solution for the EEPROM library, hoping this will save time and effort to someone.
The library merges the four eeproms of D21G into a single one, which can be addressed normally using two methods:
uint8_t eeRead(uint16_t b) //returns the eeprom value stored in location b
uint8_t eeWrie(uint16_t b, uint8_t v) //updates the eeprom value if different from stored value
These methods take care of everything, so they can be just dropped in your sketch and utilized for both D21G, 1286 and 32u4
Here is an example sketch that performs a write/read test on the whole EEPROM using these two functions.
<code>
/**************************************************************************
*
* Abstract away platform differences in Industruino EEPROM library
* Joins blocks using pointer array
* Updates data in EEPROM only if different (save EEPROM life)
* Take care of write timing
*
* by ariel - ariel@ariel.international
**************************************************************************/#define IS_D21G false
#if IS_D21G
#include <I2C_eeprom.h>
#define DBPORT SerialUSB
#define EEPAGE 4
I2C_eeprom *ePage[EEPAGE]; //array of pointers
#else
#include <EEPROM.h>
#define DBPORT Serial
#endif#define BAUD 115200
#define EESIZE 1024/**************************************
* Read a byte from EEPROM
*************************************/
uint8_t eeRead(uint16_t b)
{
#if IS_D21G
return ePage[b>>8]->readByte(b & 0xFF);
#else
return EEPROM.read(b);
#endif
}/**************************************
* Write a byte to EEPROM
*************************************/void eeWrite(uint16_t b, uint8_t v)
{
#if IS_D21G
if(ePage[b>>8]->readByte(b & 0xFF) != v) //check if value is different
ePage[b>>8]->writeByte(b & 0xFF, v);
delay(5); // NEEDED between successive writings
#else
if(EEPROM.read(b) != v) //check if value is different
EEPROM.write(b, v);
#endif
}/**************************************
* Prepare D21G EEPROM
*************************************/void setEeprom()
{
#if IS_D21G
//create objects
for(byte nE=0; nE<EEPAGE; nE++)
{
ePage[nE] = new I2C_eeprom(0x50+nE, EESIZE/EEPAGE);
ePage[nE]->begin();
}
#endif
}/***********************************/
//Setup
void setup()
{
DBPORT.begin(BAUD);
while (!DBPORT); // wait for Serial Monitor
DBPORT.println();DBPORT.println("CROSS-PLATFORM INDUSTRUINO EEPROM TEST");
DBPORT.println("by ariel - ariel@ariel.international");
DBPORT.println();
DBPORT.println("Byte Write Read");
DBPORT.println("---------------");
//EEPROM test
for (int i=0;i<=EESIZE;i++)
{
//Write a byte in EEPROM cell
byte d = random(256);
eeWrite(i, d);
nicePrint(i);
nicePrint(d);
nicePrint(eeRead(i));
nicePrintln();
}
}void loop()
{
}
/**************************************
* put space before numbers for nice printing
*************************************/void nicePrint(int n)
{
for(int nS=1;nS<5;nS++)
if(n<pow(10,nS)) DBPORT.print(' ');
DBPORT.print(n);
}void nicePrintln()
{
DBPORT.println();
}
The output is:
CROSS-PLATFORM INDUSTRUINO EEPROM TEST
by ariel - ariel@ariel.internationalByte Write Read
---------------
0 167 167
1 241 241
2 217 217
3 42 42
4 130 130
5 200 200
6 216 216
7 254 254
8 67 67
9 77 77
10 152 152
11 85 85
12 140 140
13 226 226
14 179 179
15 71 71
16 23 23
17 17 17
18 152 152
19 84 84
20 47 47
21 17 17
22 45 45
----
Enjoy!
CROSS PLATFORM EEPROM READ/WRITE FOR INT() AND FLOAT()
/*********************************
* EEPROM routines
*///conversion buffer for EEPROM write/read
union
{
byte b[4];
int i[2];
float f;
} convBuf;//This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEWrInt(int p_address, int p_value)
{
convBuf.i[0] = p_value; //first int value
for (byte nb=0; nb<2; nb++) eeWrite(p_address + nb, convBuf.b[nb]);
}//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
unsigned int EERdInt(int p_address)
{
for (byte nb=0; nb<2; nb++) convBuf.b[nb] = eeRead(p_address + nb);
return convBuf.i[0]; //first int value
}//This function will write a 4 byte integer to the eeprom at the specified address to address + 3
void EEWr32(int p_address, float p_value)
{
convBuf.f = p_value;
for (byte nb=0; nb<4; nb++) eeWrite(p_address + nb, convBuf.b[nb]);
}//This function will read a 4 byte integer from the eeprom at the specified address to address + 3
float EERd32(int p_address)
{
for (int nb=0; nb<4; nb++) convBuf.b[nb] = eeRead(p_address + nb); //fill buffer in original byte order
return convBuf.f;
}
Your answer
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
Keep Informed
About This Forum
This community is for professionals and enthusiasts of our products and services.
Read GuidelinesQuestion tools
Stats
Asked: 1/25/18, 2:02 PM |
Seen: 2238 times |
Last updated: 1/25/18, 5:28 PM |
As a nice bonus here are also the int() float() version of the read and write routines! /********************************* * EEPROM routines */ //conversion buffer for EEPROM write/read union { byte b[4]; int i[2]; float f; } convBuf; //This function will write a 2 byte integer to the eeprom at the specified address and address + 1 void EEWrInt(int p_address, int p_value) { convBuf.i[0] = p_value; //first int value for (byte nb=0; nb