Skip to content

Commit b07e03c

Browse files
authored
Merge pull request #321 from sparkfun/fix_EEPROM
Fix eeprom
2 parents 0ff3135 + b9c097e commit b07e03c

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

Diff for: libraries/EEPROM/examples/Example2_Structures/Example2_Structures.ino

+16-16
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include "EEPROM.h"
77

8-
#define SERIAL Serial
9-
108
#define EEPROM_PREFS_IDX (0)
119
#define EEPROM_VALID_CODE (0xAB)
1210
#define DEFAULT_VER_MAJOR (0)
@@ -23,44 +21,46 @@ typedef struct {
2321
preferences_t prefs;
2422

2523
void setup() {
26-
SERIAL.begin(115200);
27-
SERIAL.println("EEPROM Example2_Structures");
24+
Serial.begin(115200);
25+
Serial.println("EEPROM Example2_Structures");
26+
EEPROM.init();
2827

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

3333
if(prefs.valid != EEPROM_VALID_CODE){
34-
SERIAL.println("EEPROM was invalid");
34+
Serial.println("EEPROM was invalid");
3535

3636
// use EEPROM to store the default structure
3737
preferences_t default_prefs;
38-
EEPROM.put(EEPROM_VALID_IDX, default_prefs);
38+
EEPROM.put(EEPROM_PREFS_IDX, default_prefs);
3939

40-
SERIAL.println("EEPROM initialized");
40+
Serial.println("EEPROM initialized");
4141
}
4242

4343
// verify that the prefs are valid
4444
EEPROM.get(EEPROM_PREFS_IDX, prefs);
4545
if(prefs.valid != EEPROM_VALID_CODE){
46-
SERIAL.println("ERROR");
46+
Serial.println("ERROR");
4747
while(1){};
4848
}
4949

50-
SERIAL.println("EEPROM is valid");
50+
Serial.println("EEPROM is valid");
5151

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

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

5757
void loop() {
58-
if(SERRIAL.available()){
59-
while(SERIAL.available()){
60-
SERIAL.read();
61-
prefs.patch++;
58+
if(Serial.available()){
59+
while(Serial.available()){
60+
Serial.read();
61+
prefs.ver_patch++;
6262
}
6363
EEPROM.put(EEPROM_PREFS_IDX, prefs);
64-
SERIAL.printf("version: %d.%d.%d\n", refs.major, prefs.minor, prefs.patch);
64+
Serial.printf("version: %d.%d.%d\n", prefs.ver_major, prefs.ver_minor, prefs.ver_patch);
6565
}
6666
}

Diff for: libraries/EEPROM/src/EEPROM.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ EEPROMClass EEPROM;
1414
#define EEPROM_ADDRESS(A) ((A/AM_HAL_FLASH_PAGE_SIZE) * AM_HAL_FLASH_PAGE_SIZE)
1515
#define EEPROM_SIZE(S) (((S+(AM_HAL_FLASH_PAGE_SIZE-1))/AM_HAL_FLASH_PAGE_SIZE) * AM_HAL_FLASH_PAGE_SIZE)
1616

17-
EEPROMClass::EEPROMClass(uint32_t address, uint32_t size)
17+
EEPROMClass::EEPROMClass(uint32_t address, uint32_t size) :
1818
FlashIAPBlockDevice(EEPROM_ADDRESS(address), EEPROM_SIZE(size))
1919
{
2020

2121
}
2222

2323
EEPROMClass::EEPROMClass(void) :
24-
EEPROM(DEFAULT_ADDRESS, DEFAULT_SIZE)
24+
FlashIAPBlockDevice(DEFAULT_ADDRESS, DEFAULT_SIZE)
2525
{
2626

2727
}

Diff for: libraries/EEPROM/src/EEPROM.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
#define _APOLLO3_LIBRARIES_EEPROM_H_
88

99
#include "Arduino.h"
10+
#include "FlashIAPBlockDevice.h"
1011

1112
#define EEPROM_DEFAULT_SRAM_USAGE (1024)
1213

1314
typedef struct _eeprom_config_t {
1415
mbed::bd_size_t sram_bytes = EEPROM_DEFAULT_SRAM_USAGE;
1516
} eeprom_config_t;
1617

17-
class EEPROMClass : protected FlashIAPBlockDevice {
18+
class EEPROMClass : public FlashIAPBlockDevice {
1819
private:
1920
eeprom_config_t _cfg;
2021

@@ -35,17 +36,17 @@ class EEPROMClass : protected FlashIAPBlockDevice {
3536
}
3637
uint8_t read(int idx){
3738
uint8_t val = 0x00;
38-
read(&val, idx, 1);
39+
read(idx, &val, 1);
3940
return val;
4041
}
4142
void write(int idx, uint8_t* data, uint32_t size){
4243
mbed::bd_size_t scratch_size = (_cfg.sram_bytes+3)/4;
4344
uint32_t scratch[scratch_size];
4445
FlashIAPBlockDevice::read((uint8_t*)scratch, 0, _cfg.sram_bytes); // keep all of flash in sram in case we need to erase
45-
if(memcmp((void*)(((uint8_t*)scratch) + idx), data, size)){ // compare desired data (data) to existing information in flash (scratch)
46+
if(memcmp((void*)(((uint8_t*)scratch) + idx), data, size)){ // compare desired data (data) to existing information in flash (scratch)
4647
erase();
48+
memcpy(scratch, data, size);
4749
int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size);
48-
printf("updating flash. result: %d\n", result);
4950
return;
5051
}
5152
printf("contents already match\n");
@@ -66,7 +67,7 @@ class EEPROMClass : protected FlashIAPBlockDevice {
6667
}
6768

6869
template <typename T> T &get(int idx, T &t){
69-
read((uint8_t*)&t, idx, sizeof(T)/sizeof(uint8_t));
70+
read(idx,(uint8_t*)&t, sizeof(T)/sizeof(uint8_t));
7071
return t;
7172
}
7273

Diff for: platform.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ defines.cxx={defines.all} @{build.variant.path}/mbed/.cxx-symbols
2121
defines.ld={defines.all} @{build.variant.path}/mbed/.ld-symbols
2222

2323
# includes
24-
includes.core={includes} "-I{cores.path}/arduino" "-I{cores.path}/arduino/mbed-bridge" "-I{cores.path}/arduino/mbed-bridge/core-api"
24+
includes.core={includes} "-I{cores.path}/arduino" "-I{cores.path}/arduino/mbed-bridge" "-I{cores.path}/arduino/mbed-bridge/core-api"
2525
includes.mbed=@{build.variant.path}/mbed/.includes
2626
includes.variant={build.includes}
27-
includes.extra=
27+
includes.extra="-I{cores.path}/mbed-os/drivers/"
2828
includes.all={includes.core} {includes.mbed} {includes.variant} {includes.extra}
2929

3030
# libraries

0 commit comments

Comments
 (0)