Skip to content

Fix eeprom #321

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

Merged
merged 2 commits into from
Jan 15, 2021
Merged
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 @@ -5,8 +5,6 @@

#include "EEPROM.h"

#define SERIAL Serial

#define EEPROM_PREFS_IDX (0)
#define EEPROM_VALID_CODE (0xAB)
#define DEFAULT_VER_MAJOR (0)
Expand All @@ -23,44 +21,46 @@ typedef struct {
preferences_t prefs;

void setup() {
SERIAL.begin(115200);
SERIAL.println("EEPROM Example2_Structures");
Serial.begin(115200);
Serial.println("EEPROM Example2_Structures");
EEPROM.init();

// use EEPROM.get(int index, T type) to retrieve
// an arbitrary type from flash memory
prefs.valid = 0x00;
EEPROM.get(EEPROM_PREFS_IDX, prefs);

if(prefs.valid != EEPROM_VALID_CODE){
SERIAL.println("EEPROM was invalid");
Serial.println("EEPROM was invalid");

// use EEPROM to store the default structure
preferences_t default_prefs;
EEPROM.put(EEPROM_VALID_IDX, default_prefs);
EEPROM.put(EEPROM_PREFS_IDX, default_prefs);

SERIAL.println("EEPROM initialized");
Serial.println("EEPROM initialized");
}

// verify that the prefs are valid
EEPROM.get(EEPROM_PREFS_IDX, prefs);
if(prefs.valid != EEPROM_VALID_CODE){
SERIAL.println("ERROR");
Serial.println("ERROR");
while(1){};
}

SERIAL.println("EEPROM is valid");
Serial.println("EEPROM is valid");

SERIAL.printf("version: %d.%d.%d\n", refs.major, prefs.minor, prefs.patch);
Serial.printf("version: %d.%d.%d\n", prefs.ver_major, prefs.ver_minor, prefs.ver_patch);

SERIAL.printf("\nany characters received over SERIAL will increment the patch version and be stored after power-down\n");
Serial.printf("\nany characters received over SERIAL will increment the patch version and be stored after power-down\n");
}

void loop() {
if(SERRIAL.available()){
while(SERIAL.available()){
SERIAL.read();
prefs.patch++;
if(Serial.available()){
while(Serial.available()){
Serial.read();
prefs.ver_patch++;
}
EEPROM.put(EEPROM_PREFS_IDX, prefs);
SERIAL.printf("version: %d.%d.%d\n", refs.major, prefs.minor, prefs.patch);
Serial.printf("version: %d.%d.%d\n", prefs.ver_major, prefs.ver_minor, prefs.ver_patch);
}
}
4 changes: 2 additions & 2 deletions libraries/EEPROM/src/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ EEPROMClass EEPROM;
#define EEPROM_ADDRESS(A) ((A/AM_HAL_FLASH_PAGE_SIZE) * AM_HAL_FLASH_PAGE_SIZE)
#define EEPROM_SIZE(S) (((S+(AM_HAL_FLASH_PAGE_SIZE-1))/AM_HAL_FLASH_PAGE_SIZE) * AM_HAL_FLASH_PAGE_SIZE)

EEPROMClass::EEPROMClass(uint32_t address, uint32_t size)
EEPROMClass::EEPROMClass(uint32_t address, uint32_t size) :
FlashIAPBlockDevice(EEPROM_ADDRESS(address), EEPROM_SIZE(size))
{

}

EEPROMClass::EEPROMClass(void) :
EEPROM(DEFAULT_ADDRESS, DEFAULT_SIZE)
FlashIAPBlockDevice(DEFAULT_ADDRESS, DEFAULT_SIZE)
{

}
11 changes: 6 additions & 5 deletions libraries/EEPROM/src/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#define _APOLLO3_LIBRARIES_EEPROM_H_

#include "Arduino.h"
#include "FlashIAPBlockDevice.h"

#define EEPROM_DEFAULT_SRAM_USAGE (1024)

typedef struct _eeprom_config_t {
mbed::bd_size_t sram_bytes = EEPROM_DEFAULT_SRAM_USAGE;
} eeprom_config_t;

class EEPROMClass : protected FlashIAPBlockDevice {
class EEPROMClass : public FlashIAPBlockDevice {
private:
eeprom_config_t _cfg;

Expand All @@ -35,17 +36,17 @@ class EEPROMClass : protected FlashIAPBlockDevice {
}
uint8_t read(int idx){
uint8_t val = 0x00;
read(&val, idx, 1);
read(idx, &val, 1);
return val;
}
void write(int idx, uint8_t* data, uint32_t size){
mbed::bd_size_t scratch_size = (_cfg.sram_bytes+3)/4;
uint32_t scratch[scratch_size];
FlashIAPBlockDevice::read((uint8_t*)scratch, 0, _cfg.sram_bytes); // keep all of flash in sram in case we need to erase
if(memcmp((void*)(((uint8_t*)scratch) + idx), data, size)){ // compare desired data (data) to existing information in flash (scratch)
if(memcmp((void*)(((uint8_t*)scratch) + idx), data, size)){ // compare desired data (data) to existing information in flash (scratch)
erase();
memcpy(scratch, data, size);
int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size);
printf("updating flash. result: %d\n", result);
return;
}
printf("contents already match\n");
Expand All @@ -66,7 +67,7 @@ class EEPROMClass : protected FlashIAPBlockDevice {
}

template <typename T> T &get(int idx, T &t){
read((uint8_t*)&t, idx, sizeof(T)/sizeof(uint8_t));
read(idx,(uint8_t*)&t, sizeof(T)/sizeof(uint8_t));
return t;
}

Expand Down
4 changes: 2 additions & 2 deletions platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ defines.cxx={defines.all} @{build.variant.path}/mbed/.cxx-symbols
defines.ld={defines.all} @{build.variant.path}/mbed/.ld-symbols

# includes
includes.core={includes} "-I{cores.path}/arduino" "-I{cores.path}/arduino/mbed-bridge" "-I{cores.path}/arduino/mbed-bridge/core-api"
includes.core={includes} "-I{cores.path}/arduino" "-I{cores.path}/arduino/mbed-bridge" "-I{cores.path}/arduino/mbed-bridge/core-api"
includes.mbed=@{build.variant.path}/mbed/.includes
includes.variant={build.includes}
includes.extra=
includes.extra="-I{cores.path}/mbed-os/drivers/"
includes.all={includes.core} {includes.mbed} {includes.variant} {includes.extra}

# libraries
Expand Down