Skip to content

CurieEEPROM - ensure 32-bit allignment #121

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion libraries/CurieEEPROM/examples/eeprom_crc/eeprom_crc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libraries/CurieEEPROM/examples/eeprom_get/eeprom_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions libraries/CurieEEPROM/examples/eeprom_put/eeprom_put.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand Down
6 changes: 3 additions & 3 deletions libraries/CurieEEPROM/examples/eeprom_read/eeprom_read.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ 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);
Serial.print("\t");
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;
}
Expand Down
21 changes: 3 additions & 18 deletions libraries/CurieEEPROM/examples/eeprom_write/eeprom_write.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,19 @@ 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");
Serial.print(addr);
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");
}

Expand Down
17 changes: 15 additions & 2 deletions libraries/CurieEEPROM/src/CurieEEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ 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)
{
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))
Expand Down Expand Up @@ -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);
}
Expand Down
28 changes: 17 additions & 11 deletions libraries/CurieEEPROM/src/CurieEEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <inttypes.h>
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -106,7 +112,7 @@ class CurieEEPROM
{
for(int i = 0; i<size; i++)
{
write(addr+i*sizeof(uint32_t), dwords[i]);
write(addr+i, dwords[i]);
}
}
else
Expand Down