From 0f54d0f416eb60de86218833e87038456383ac13 Mon Sep 17 00:00:00 2001 From: Wenn0101 Date: Thu, 7 Jan 2021 05:58:48 -0700 Subject: [PATCH 1/2] commit changes to eeprom in the arduino core to fix eeprom example --- libraries/EEPROM/src/EEPROM.cpp | 4 ++-- libraries/EEPROM/src/EEPROM.h | 11 ++++++----- platform.txt | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 1b8db272..6dbfd16e 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -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) { } \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 8fe995c8..0cab1588 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -7,6 +7,7 @@ #define _APOLLO3_LIBRARIES_EEPROM_H_ #include "Arduino.h" +#include "FlashIAPBlockDevice.h" #define EEPROM_DEFAULT_SRAM_USAGE (1024) @@ -14,7 +15,7 @@ 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; @@ -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"); @@ -66,7 +67,7 @@ class EEPROMClass : protected FlashIAPBlockDevice { } template 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; } diff --git a/platform.txt b/platform.txt index a2060e35..cbd862d4 100644 --- a/platform.txt +++ b/platform.txt @@ -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 From b9c097e5d7705e565e14af68be350efbc84d54a4 Mon Sep 17 00:00:00 2001 From: Wenn0101 Date: Thu, 7 Jan 2021 06:07:24 -0700 Subject: [PATCH 2/2] Fix EEPROM example 2 --- .../Example2_Structures.ino | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libraries/EEPROM/examples/Example2_Structures/Example2_Structures.ino b/libraries/EEPROM/examples/Example2_Structures/Example2_Structures.ino index d0d7fd19..6bb97078 100644 --- a/libraries/EEPROM/examples/Example2_Structures/Example2_Structures.ino +++ b/libraries/EEPROM/examples/Example2_Structures/Example2_Structures.ino @@ -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) @@ -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); } }