Skip to content

Re-write of EEPROM functions #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

void setup()
{
Serial.begin(9600);
Serial.begin(115200);
Serial.println("EEPROM Examples");

byte myValue1 = 200;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

void setup()
{
Serial.begin(9600);
Serial.begin(115200);
Serial.println("EEPROM Examples");

randomSeed(analogRead(A0));
Expand Down Expand Up @@ -106,8 +106,8 @@ void setup()
uint32_t myValue8 = 241544;
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);

EEPROM.update(randomLocation, myValue7);
EEPROM.update(randomLocation + 4, myValue8);
EEPROM.put(randomLocation, myValue7);
EEPROM.put(randomLocation + 4, myValue8);

int32_t response7;
uint32_t response8;
Expand All @@ -124,8 +124,8 @@ void setup()
float myValue10 = 5.22;
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);

EEPROM.update(randomLocation, myValue9);
EEPROM.update(randomLocation + 4, myValue10);
EEPROM.put(randomLocation, myValue9);
EEPROM.put(randomLocation + 4, myValue10);

float response9;
float response10;
Expand All @@ -145,8 +145,8 @@ void setup()
double myValue12 = 384.95734987;
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);

EEPROM.update(randomLocation, myValue11);
EEPROM.update(randomLocation + 8, myValue12);
EEPROM.put(randomLocation, myValue11);
EEPROM.put(randomLocation + 8, myValue12);

double response11;
double response12;
Expand All @@ -156,7 +156,22 @@ void setup()
Serial.printf("Location %d should be %lf: %lf\n", randomLocation + 8, myValue12, response12);
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Serial.println();
Serial.println("");
Serial.println("String test");

//String write test
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
String myString = "How are you today?";
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
EEPROM.put(randomLocation, myString);

String readMy;
EEPROM.get(randomLocation, readMy);
Serial.printf("Location %d string should read 'How are you today?': ", randomLocation);
Serial.println(readMy);
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Serial.println("");
Serial.print("Flash Contents:");
for (uint16_t x = 0; x < 8 * 4; x += 4)
{
Expand Down
175 changes: 11 additions & 164 deletions libraries/EEPROM/src/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,190 +40,37 @@
#include "EEPROM.h"
#include "Arduino.h"

//Constructor
ap3_EEPROM::ap3_EEPROM()
{
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

//Write a byte to a given "EEPROM" location
//Automatically masks user's byte into flash without
//affecting other bytes in this flash word
void ap3_EEPROM::write(uint16_t eepromLocation, uint8_t dataToWrite)
void write(uint16_t eepromLocation, uint8_t dataToWrite)
{
uint32_t flashLocation = AP3_FLASH_EEPROM_START + eepromLocation;
writeWordToFlash(flashLocation, (uint32_t)dataToWrite | 0xFFFFFF00);
}

//Read a byte from a given location in "EEPROM"
uint8_t ap3_EEPROM::read(uint16_t eepromLocation)
uint8_t read(uint16_t eepromLocation)
{
uint32_t flashLocation = AP3_FLASH_EEPROM_START + eepromLocation;
return (*(uint8_t *)flashLocation);
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

//Get method is overloaded with the following variable types
//char, byte, int, unsigned int, long, unsigned long, float, double?

void ap3_EEPROM::get(uint16_t eepromLocation, uint8_t &dataToGet)
{
dataToGet = *(uint8_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
}
void ap3_EEPROM::get(uint16_t eepromLocation, uint16_t &dataToGet)
{
dataToGet = *(uint16_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
}
void ap3_EEPROM::get(uint16_t eepromLocation, int16_t &dataToGet)
{
dataToGet = *(int16_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
}
void ap3_EEPROM::get(uint16_t eepromLocation, int &dataToGet)
{
dataToGet = *(int *)(AP3_FLASH_EEPROM_START + eepromLocation);
}
void ap3_EEPROM::get(uint16_t eepromLocation, unsigned int &dataToGet)
{
dataToGet = *(unsigned int *)(AP3_FLASH_EEPROM_START + eepromLocation);
}
void ap3_EEPROM::get(uint16_t eepromLocation, int32_t &dataToGet)
{
dataToGet = *(int32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
}
void ap3_EEPROM::get(uint16_t eepromLocation, uint32_t &dataToGet)
{
dataToGet = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
}
void ap3_EEPROM::get(uint16_t eepromLocation, float &dataToGet)
{
union {
float f;
uint32_t b;
} temp;
temp.b = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);

dataToGet = temp.f;
}

void ap3_EEPROM::get(uint16_t eepromLocation, double &dataToGet)
{
union {
double lf;
uint32_t b[2];
} temp;
temp.b[1] = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation); //LSB;
temp.b[0] = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation + 4); //MSB;
dataToGet = temp.lf;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

//Put method is overloaded with the following variable types
//char, byte, int, unsigned int, long, unsigned long, float, double?

void ap3_EEPROM::put(uint16_t eepromLocation, uint8_t dataToWrite)
{
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFFFF00);
}
void ap3_EEPROM::put(uint16_t eepromLocation, uint16_t dataToWrite)
{
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
}
void ap3_EEPROM::put(uint16_t eepromLocation, int16_t dataToWrite)
{
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
}
void ap3_EEPROM::put(uint16_t eepromLocation, int dataToWrite) //ints are 32 bit on M4F
{
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
}
void ap3_EEPROM::put(uint16_t eepromLocation, unsigned int dataToWrite) //ints are 32 bit on M4F
//Write a new byte to a given location in "EEROM" only if new data is there
void update(uint16_t eepromLocation, uint8_t dataToWrite)
{
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
}
void ap3_EEPROM::put(uint16_t eepromLocation, int32_t dataToWrite)
{
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (int32_t)dataToWrite);
}
void ap3_EEPROM::put(uint16_t eepromLocation, uint32_t dataToWrite)
{
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
}
void ap3_EEPROM::put(uint16_t eepromLocation, float dataToWrite)
{
union {
float f;
uint32_t b;
} temp;
temp.f = dataToWrite;

writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)temp.b);
}

void ap3_EEPROM::put(uint16_t eepromLocation, double dataToWrite) //64 bits
{
union {
double lf;
uint32_t b[2];
} temp;
temp.lf = dataToWrite;

writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)temp.b[1]); //LSB
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation + 4), (uint32_t)temp.b[0]); //MSB
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

//The update functions simply call the put functions
//Put automatically checks to see if a spot needs updating
void ap3_EEPROM::update(uint16_t eepromLocation, uint8_t dataToWrite)
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, uint16_t dataToWrite)
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, int16_t dataToWrite)
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, int dataToWrite) //ints are 32 bit on M4F
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, unsigned int dataToWrite) //ints are 32 bit on M4F
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, int32_t dataToWrite)
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, uint32_t dataToWrite)
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, float dataToWrite)
{
put(eepromLocation, dataToWrite);
}
void ap3_EEPROM::update(uint16_t eepromLocation, double dataToWrite) //64 bits
{
put(eepromLocation, dataToWrite);
if (read(eepromLocation) != dataToWrite)
{
write(eepromLocation, dataToWrite);
}
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

uint16_t ap3_EEPROM::length()
{
return (AP3_FLASH_EEPROM_SIZE);
}

//Erase 8k page encapsulating the EEPROM section
void ap3_EEPROM::erase()
void EEPROMClass::erase()
{
am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY,
AM_HAL_FLASH_ADDR2INST(AP3_FLASH_EEPROM_START),
Expand All @@ -240,7 +87,7 @@ void ap3_EEPROM::erase()
//4) Write SRAM back into flash
//5) Write user's data onto the spot with recently created 0xFFs
//Note - this code assumes EEPROM temp space is contained in one page
void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
void writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
{
//Error check
if (flashLocation >= AP3_FLASH_EEPROM_START + AP3_FLASH_EEPROM_SIZE)
Expand Down Expand Up @@ -306,4 +153,4 @@ void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
AP3_FLASH_EEPROM_SIZE);
}

ap3_EEPROM EEPROM;
//ap3_EEPROM EEPROM;
Loading