From 577568f146654d8331f6191e5d25aef5087bcb98 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sat, 6 Feb 2021 20:53:25 -0700 Subject: [PATCH 1/7] Fix compile error "FlashIAPBlockDevice.h" not found Bring back includes from previous commit: https://github.com/sparkfun/Arduino_Apollo3/commit/0f54d0f416eb60de86218833e87038456383ac13 --- platform.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform.txt b/platform.txt index 005dba2c..0f658439 100644 --- a/platform.txt +++ b/platform.txt @@ -24,7 +24,7 @@ defines.ld={defines.all} "@{build.variant.path}/mbed/.ld-symbols" 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 e185bda8ff886363ce3aa61d8517353c7c879ce1 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sat, 6 Feb 2021 20:54:07 -0700 Subject: [PATCH 2/7] Remove printf --- libraries/EEPROM/src/EEPROM.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 0cab1588..83a809fd 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -49,7 +49,7 @@ class EEPROMClass : public FlashIAPBlockDevice { int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size); return; } - printf("contents already match\n"); + //printf("contents already match\n"); } void write(int idx, uint8_t val){ write(idx, &val, 1); From 1dc5e51440a93f8a361d49c972475a8d7a489e11 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sat, 6 Feb 2021 21:35:36 -0700 Subject: [PATCH 3/7] Fix memcpy index Index is 32bit --- libraries/EEPROM/src/EEPROM.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 83a809fd..49bc0921 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -42,14 +42,17 @@ class EEPROMClass : public FlashIAPBlockDevice { 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) + memcpy(scratch + idx/4, data, size); + erase(); - memcpy(scratch, data, size); int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size); + return; } - //printf("contents already match\n"); } void write(int idx, uint8_t val){ write(idx, &val, 1); From 21ccb0fbac7ddfef491965f866ba6516d416238c Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sat, 6 Feb 2021 21:54:00 -0700 Subject: [PATCH 4/7] Write to EEPROM with any byte size/alignment --- libraries/EEPROM/src/EEPROM.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 49bc0921..40804250 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -46,7 +46,19 @@ class EEPROMClass : public FlashIAPBlockDevice { 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) - memcpy(scratch + idx/4, data, size); + + if(idx % 4 == 0) + { + memcpy(scratch + idx/4, data, size); //We have byte alignment + } + else + { + //Move data to byte alignment + uint8_t alignedData[size + (idx % 4)]; + memcpy(alignedData + (idx % 4), data, size + (idx % 4)); //Shift data + + memcpy(scratch + (idx / 4), alignedData, size + (idx % 4)); //Paint aligned data back onto scratch + } erase(); int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size); From 33fcc6821c20b55cf4e3576a1e599687aa412844 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 7 Feb 2021 10:25:18 -0700 Subject: [PATCH 5/7] Fix example name typo Was causing the example not to appear --- .../Example1_PutGet/{Example1_GetPut.ino => Example1_PutGet.ino} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libraries/EEPROM/examples/Example1_PutGet/{Example1_GetPut.ino => Example1_PutGet.ino} (100%) diff --git a/libraries/EEPROM/examples/Example1_PutGet/Example1_GetPut.ino b/libraries/EEPROM/examples/Example1_PutGet/Example1_PutGet.ino similarity index 100% rename from libraries/EEPROM/examples/Example1_PutGet/Example1_GetPut.ino rename to libraries/EEPROM/examples/Example1_PutGet/Example1_PutGet.ino From 369b8856ad7f57b627283db8de0c9f5806af9cfb Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 7 Feb 2021 12:44:06 -0700 Subject: [PATCH 6/7] Change scratch to 8bit to avoid alignment corruption --- .../Example3_AllFunctions.ino | 231 ++++++++++++++++++ libraries/EEPROM/src/EEPROM.h | 28 +-- 2 files changed, 241 insertions(+), 18 deletions(-) create mode 100644 libraries/EEPROM/examples/Example3_AllFunctions/Example3_AllFunctions.ino diff --git a/libraries/EEPROM/examples/Example3_AllFunctions/Example3_AllFunctions.ino b/libraries/EEPROM/examples/Example3_AllFunctions/Example3_AllFunctions.ino new file mode 100644 index 00000000..c77b172e --- /dev/null +++ b/libraries/EEPROM/examples/Example3_AllFunctions/Example3_AllFunctions.ino @@ -0,0 +1,231 @@ +/* + Reading and writing test of the EEPROM functions on the Artemis + By: Nathan Seidle + SparkFun Electronics + Date: June 24th, 2019 + This example code is in the public domain. + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/15376 + + Page erase takes 15ms + Write byte takes 30ms - This is much longer than Arduino that takes 3.3ms + Float write across two words takes 30ms + Update (no write) takes 1ms +*/ + +#include + +// Give a default if the variant does not define one. +#ifndef A0 +#define A0 0 +#endif + +void setup() +{ + Serial.begin(115200); + Serial.println("EEPROM Examples"); + + randomSeed(analogRead(A0)); + + EEPROM.init(); + + // You may choose to enable more or less EEPROM - + // Default length is 1024 bytes (if setLength is not called) + EEPROM.setLength(1024 * 1); // this would make the length 1080 bytes. Max is 4096. + // Note: larger sizes will increase RAM usage and execution time + + long startTime; + long endTime; + int randomLocation; + + //Test erase time + startTime = millis(); + EEPROM.erase(); + endTime = millis(); + Serial.printf("Time to erase all EEPROM: %dms\n", endTime - startTime); + + //Byte sequential test + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + Serial.println(); + Serial.println("8 bit tests"); + uint8_t myValue1 = 200; + byte myValue2 = 23; + randomLocation = random(0, EEPROM.length() - sizeof(myValue1)); + + startTime = millis(); + EEPROM.write(randomLocation, myValue1); //(location, data) + endTime = millis(); + EEPROM.put(randomLocation + sizeof(myValue1), myValue2); + + Serial.printf("Write byte time: %dms\n", endTime - startTime); + + startTime = millis(); + EEPROM.write(randomLocation, myValue1); //(location, data) + endTime = millis(); + + Serial.printf("Write identical byte to same location: %dms\n", endTime - startTime); + + byte response1 = EEPROM.read(randomLocation); + byte response2 = EEPROM.read(randomLocation + sizeof(myValue1)); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue1, response1); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue1), myValue2, response2); + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + + Serial.println(); + Serial.println("16 bit tests"); + + //int16_t and uint16_t sequential test + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + uint16_t myValue3 = 3411; + int16_t myValue4 = -366; + randomLocation = random(0, EEPROM.length() - sizeof(myValue3)); + + EEPROM.put(randomLocation, myValue3); + EEPROM.put(randomLocation + sizeof(myValue3), myValue4); + + uint16_t response3; + int16_t response4; + EEPROM.get(randomLocation, response3); + EEPROM.get(randomLocation + sizeof(myValue3), response4); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue3, response3); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue3), myValue4, response4); + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + + Serial.println(); + Serial.println("32 bit tests"); + + //int and unsigned int (32) sequential test + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + Serial.printf("Size of int: %d\n", sizeof(int)); + int myValue5 = -245000; + unsigned int myValue6 = 400123; + randomLocation = random(0, EEPROM.length() - sizeof(myValue5)); + + EEPROM.put(randomLocation, myValue5); + EEPROM.put(randomLocation + sizeof(myValue5), myValue6); + + int response5; + unsigned int response6; + EEPROM.get(randomLocation, response5); + EEPROM.get(randomLocation + sizeof(myValue5), response6); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue5, response5); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue5), myValue6, response6); + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + + //int32_t and uint32_t sequential test + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + int32_t myValue7 = -341002; + uint32_t myValue8 = 241544; + randomLocation = random(0, EEPROM.length() - sizeof(myValue7)); + + EEPROM.put(randomLocation, myValue7); + EEPROM.put(randomLocation + sizeof(myValue7), myValue8); + + int32_t response7; + uint32_t response8; + EEPROM.get(randomLocation, response7); + EEPROM.get(randomLocation + sizeof(myValue7), response8); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue7, response7); + Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue7), myValue8, response8); + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + + //float (32) sequential test + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + Serial.printf("Size of float: %d\n", sizeof(float)); + float myValue9 = -7.35; + float myValue10 = 5.22; + randomLocation = random(0, EEPROM.length() - sizeof(myValue9)); + + EEPROM.put(randomLocation, myValue9); + EEPROM.put(randomLocation + sizeof(myValue9), myValue10); + + float response9; + float response10; + EEPROM.get(randomLocation, response9); + EEPROM.get(randomLocation + sizeof(myValue9), response10); + Serial.printf("Location %d should be ", randomLocation); + Serial.print(myValue9); + Serial.print(": "); + Serial.print(response9); + Serial.println(); + Serial.printf("Location %d should be ", randomLocation + sizeof(myValue9)); + Serial.print(myValue10); + Serial.print(": "); + Serial.print(response10); + Serial.println(); + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + + Serial.println(); + Serial.println("64 bit tests"); + + //double (64) sequential test + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + Serial.printf("Size of double: %d\n", sizeof(double)); + double myValue11 = -290.3485723409857; + double myValue12 = 384.95734987; + double myValue13 = 917.14159; + double myValue14 = 254.8877; + randomLocation = random(0, EEPROM.length() - sizeof(myValue11)); + + startTime = millis(); + EEPROM.put(randomLocation, myValue11); + endTime = millis(); + Serial.printf("Time to record 64-bits: %dms\n", endTime - startTime); + + EEPROM.put(randomLocation + sizeof(myValue11), myValue12); + EEPROM.put(EEPROM.length() - sizeof(myValue13), myValue13); //Test end of EEPROM space + + double response11; + double response12; + double response13; + EEPROM.get(randomLocation, response11); + EEPROM.get(randomLocation + sizeof(myValue11), response12); + EEPROM.get(EEPROM.length() - sizeof(myValue13), response13); + + Serial.printf("Location %d should be ", randomLocation); + Serial.print(myValue11, 13); + Serial.print(": "); + Serial.print(response11, 13); + Serial.println(); + Serial.printf("Location %d should be ", randomLocation + sizeof(myValue11)); + Serial.print(myValue12, 8); + Serial.print(": "); + Serial.print(response12, 8); + Serial.println(); + Serial.printf("Edge of EEPROM %d should be ", EEPROM.length() - sizeof(myValue13)); + Serial.print(myValue13, 5); + Serial.print(": "); + Serial.print(response13, 5); + Serial.println(); + + double response14; + EEPROM.put(EEPROM.length() - sizeof(myValue14), myValue14); //Test the re-write of a spot + EEPROM.get(EEPROM.length() - sizeof(myValue14), response14); + Serial.printf("Rewrite of %d should be ", EEPROM.length() - sizeof(myValue14)); + Serial.print(myValue14, 4); + Serial.print(": "); + Serial.print(response14, 4); + Serial.println(); + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + + Serial.println(); + Serial.println("String test"); + + //String write test + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + char myString[19] = "How are you today?"; + randomLocation = random(0, EEPROM.length() - sizeof(myString)); + EEPROM.put(randomLocation, myString); + + char readMy[19]; + EEPROM.get(randomLocation, readMy); + Serial.printf("Location %d string should read 'How are you today?': ", randomLocation); + Serial.println(readMy); + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +} + +void loop() +{ +} \ No newline at end of file diff --git a/libraries/EEPROM/src/EEPROM.h b/libraries/EEPROM/src/EEPROM.h index 40804250..c526f06d 100644 --- a/libraries/EEPROM/src/EEPROM.h +++ b/libraries/EEPROM/src/EEPROM.h @@ -27,9 +27,15 @@ class EEPROMClass : public FlashIAPBlockDevice { void config(eeprom_config_t cfg){ _cfg = cfg; } + mbed::bd_size_t length(void){ + return(_cfg.sram_bytes); + } void configSramUsage(mbed::bd_size_t bytes){ _cfg.sram_bytes = bytes; } + void setLength(mbed::bd_size_t bytes){ //Old function to maintain compatibility + configSramUsage(bytes); + } void read(int idx, uint8_t* data, uint32_t size){ FlashIAPBlockDevice::read(data, idx, size); @@ -40,29 +46,15 @@ class EEPROMClass : public FlashIAPBlockDevice { 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]; + mbed::bd_size_t scratch_size = _cfg.sram_bytes; + uint8_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(idx % 4 == 0) - { - memcpy(scratch + idx/4, data, size); //We have byte alignment - } - else - { - //Move data to byte alignment - uint8_t alignedData[size + (idx % 4)]; - memcpy(alignedData + (idx % 4), data, size + (idx % 4)); //Shift data - - memcpy(scratch + (idx / 4), alignedData, size + (idx % 4)); //Paint aligned data back onto scratch - } - + memcpy(scratch + idx, data, size); erase(); - int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size); - + int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, scratch_size); return; } } From d675e41bbcbc2b052b417308e58c6a297cdb6fa3 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Sun, 7 Feb 2021 13:16:59 -0700 Subject: [PATCH 7/7] Remove driver link to avoid SPI conflict. --- platform.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform.txt b/platform.txt index 0f658439..005dba2c 100644 --- a/platform.txt +++ b/platform.txt @@ -24,7 +24,7 @@ defines.ld={defines.all} "@{build.variant.path}/mbed/.ld-symbols" 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="-I{cores.path}/mbed-os/drivers/" +includes.extra= includes.all={includes.core} {includes.mbed} {includes.variant} {includes.extra} # libraries