From 549aed1a4b896d4e42186b027d3db2b3efa21703 Mon Sep 17 00:00:00 2001 From: Dino Tinitigan Date: Mon, 22 Feb 2016 14:08:34 -0800 Subject: [PATCH] CurieEEPROM - ensure 32-bit allignment -ensure 32-bit address allignment for put(), get(), read(), and write() --- .../examples/eeprom_crc/eeprom_crc.ino | 2 +- .../examples/eeprom_get/eeprom_get.ino | 2 +- .../examples/eeprom_put/eeprom_put.ino | 3 +- .../examples/eeprom_read/eeprom_read.ino | 6 ++-- .../examples/eeprom_write/eeprom_write.ino | 21 ++------------ libraries/CurieEEPROM/src/CurieEEPROM.cpp | 17 +++++++++-- libraries/CurieEEPROM/src/CurieEEPROM.h | 28 +++++++++++-------- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/libraries/CurieEEPROM/examples/eeprom_crc/eeprom_crc.ino b/libraries/CurieEEPROM/examples/eeprom_crc/eeprom_crc.ino index df860428..84294810 100644 --- a/libraries/CurieEEPROM/examples/eeprom_crc/eeprom_crc.ino +++ b/libraries/CurieEEPROM/examples/eeprom_crc/eeprom_crc.ino @@ -44,7 +44,7 @@ unsigned long eeprom_crc(void) { unsigned long crc = ~0L; - for (int index = 0 ; index < EEPROM.length()/4 ; ++index) { + for (int index = 0 ; index < EEPROM.length(); ++index) { crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4); crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4); crc = ~crc; diff --git a/libraries/CurieEEPROM/examples/eeprom_get/eeprom_get.ino b/libraries/CurieEEPROM/examples/eeprom_get/eeprom_get.ino index bd30e6b5..2fd3e072 100644 --- a/libraries/CurieEEPROM/examples/eeprom_get/eeprom_get.ino +++ b/libraries/CurieEEPROM/examples/eeprom_get/eeprom_get.ino @@ -52,7 +52,7 @@ struct MyObject { }; void secondTest() { - int eeAddress = sizeof(float); //Move address to the next byte after float 'f'. + int eeAddress = 1; //Move address to the next byte after float 'f'. MyObject customVar; //Variable to store custom object read from EEPROM. EEPROM.get(eeAddress, customVar); diff --git a/libraries/CurieEEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/CurieEEPROM/examples/eeprom_put/eeprom_put.ino index ead52aba..4435e7c4 100644 --- a/libraries/CurieEEPROM/examples/eeprom_put/eeprom_put.ino +++ b/libraries/CurieEEPROM/examples/eeprom_put/eeprom_put.ino @@ -47,8 +47,7 @@ void setup() { "Working!" }; - eeAddress += sizeof(float); //Move address to the next byte after float 'f'. - + eeAddress++; EEPROM.put(eeAddress, customVar); Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); } diff --git a/libraries/CurieEEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/CurieEEPROM/examples/eeprom_read/eeprom_read.ino index 6bef6937..2682d2dc 100644 --- a/libraries/CurieEEPROM/examples/eeprom_read/eeprom_read.ino +++ b/libraries/CurieEEPROM/examples/eeprom_read/eeprom_read.ino @@ -22,7 +22,7 @@ void setup() { } void loop() { - // read a byte from the current address of the EEPROM + // read a dword from the current address of the EEPROM value = EEPROM.read(address); Serial.print(address); @@ -30,8 +30,8 @@ void loop() { Serial.print(value, DEC); Serial.println(); - //increment address by 4 since we are using DWORDs and we have a granularity of a DWORD or 4 bytes - address = address + 4; + //increment address + address++; if (address == EEPROM.length()) { address = 0; } diff --git a/libraries/CurieEEPROM/examples/eeprom_write/eeprom_write.ino b/libraries/CurieEEPROM/examples/eeprom_write/eeprom_write.ino index 250c7848..eb55d180 100644 --- a/libraries/CurieEEPROM/examples/eeprom_write/eeprom_write.ino +++ b/libraries/CurieEEPROM/examples/eeprom_write/eeprom_write.ino @@ -17,9 +17,8 @@ void setup() { while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } - //use write for the first half of the EEPROM area - Serial.println("Writing with write()"); - for(int i = 0; i < EEPROM.length()/8; i++) + + for(int i = 0; i < 512; i++) { unsigned long val = analogRead(0); Serial.print("Addr:\t"); @@ -27,24 +26,10 @@ void setup() { Serial.print("\tWriting: "); Serial.println(val); EEPROM.write(addr, val); - addr +=4; //increment address by 4 since we are using DWORDs - delay(100); - } - - //use write8 for the second half of the EEPROM area - Serial.println("Writing with write8()"); - for(int i = EEPROM.length()/2; i < EEPROM.length(); i++) - { - byte val8 = analogRead(0)/4; - Serial.print("Addr:\t"); - Serial.print(addr); - Serial.print("\tWriting: "); - Serial.println(val8); - EEPROM.write(addr, val8); addr++; delay(100); } - + Serial.println("done writing"); } diff --git a/libraries/CurieEEPROM/src/CurieEEPROM.cpp b/libraries/CurieEEPROM/src/CurieEEPROM.cpp index 6e7d4d5b..bb0c8060 100644 --- a/libraries/CurieEEPROM/src/CurieEEPROM.cpp +++ b/libraries/CurieEEPROM/src/CurieEEPROM.cpp @@ -31,6 +31,12 @@ void CurieEEPROM::clear() void CurieEEPROM::write(uint32_t address, uint32_t data) { + //make sure address is within valid range + if(address > 0x1FF) + { + return; + } + uint32_t currentValue = read(address); //only do something if value is different from what is currently stored if(currentValue==data) @@ -38,6 +44,9 @@ void CurieEEPROM::write(uint32_t address, uint32_t data) return; } + //allign address to 32-bit addressing + address*=sizeof(uint32_t); + uint32_t rom_wr_ctrl = 0; //make sure address is valid if((address > 0x7FC) || (address%4)) @@ -168,11 +177,15 @@ void CurieEEPROM::update8(uint32_t addr, uint8_t value) uint32_t CurieEEPROM::read(uint32_t address) { - //make sure address is valid - if((address > 0x7FC) || (address%4)) + //make sure address is within valid range + if(address > 0x1FF) { return 0; } + + //allign address to 32-bit addressing + address*=sizeof(uint32_t); + address += EEPROM_ADDR; return *(uint32_t*)(address); } diff --git a/libraries/CurieEEPROM/src/CurieEEPROM.h b/libraries/CurieEEPROM/src/CurieEEPROM.h index 20624cc9..cfbce2cb 100644 --- a/libraries/CurieEEPROM/src/CurieEEPROM.h +++ b/libraries/CurieEEPROM/src/CurieEEPROM.h @@ -24,7 +24,7 @@ #define EEPROM_OFFSET 0x00001000 #define CTRL_REG 0xb0000018 -#define EEPROM_SIZE 2048 //EEPROM size in bytes +#define EEPROM_SIZE 512 //EEPROM size in dwords #include @@ -54,17 +54,22 @@ class CurieEEPROM //Functionality to 'get' and 'put' objects to and from EEPROM. template< typename T > T &get(uint32_t addr, T &t) { - //make sure address is valid - if((addr > 0x7FC) || (addr%4)) + //make sure address is within valid range + if(addr > 0x1FF) { return t; } + int byteCount = sizeof(T); - //return if size of object is greater than size of EEPROM - if(byteCount > EEPROM_SIZE) + //return if object size is too big + if(addr + byteCount > 0x7FC) { return t; } + + //allign address to 32-bit addressing + addr*=sizeof(uint32_t); + byte *bytes = to_bytes(t); for(int i = 0; i < byteCount; i++) { @@ -76,15 +81,16 @@ class CurieEEPROM } template< typename T > T put(uint32_t addr, T t) { - //make sure address is valid - if((addr > 0x7FC) || (addr%4)) + //make sure address is within valid range + if(addr > 0x1FF) { return t; } uint32_t rom_wr_ctrl = 0; int byteCount = sizeof(T); - //return if size of object is greater than size of EEPROM - if(byteCount > EEPROM_SIZE) + + //return if object size is too big + if(addr + byteCount > 0x7FC) { return t; } @@ -96,7 +102,7 @@ class CurieEEPROM bool blockAvailable = true; for(int i =0; i < size; i++) { - uint32_t data32 = read(addr+i*sizeof(uint32_t)); + uint32_t data32 = read(addr+i); if(data32 != 0xFFFFFFFF) { blockAvailable = false; @@ -106,7 +112,7 @@ class CurieEEPROM { for(int i = 0; i