Skip to content

Commit f6d8aa8

Browse files
bigdinotechcalvinatintel
authored andcommitted
CurieEEPROM - ensure 32-bit allignment
-ensure 32-bit address allignment for put(), get(), read(), and write()
1 parent 64ee879 commit f6d8aa8

File tree

7 files changed

+41
-38
lines changed

7 files changed

+41
-38
lines changed

libraries/CurieEEPROM/examples/eeprom_crc/eeprom_crc.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ unsigned long eeprom_crc(void) {
4444

4545
unsigned long crc = ~0L;
4646

47-
for (int index = 0 ; index < EEPROM.length()/4 ; ++index) {
47+
for (int index = 0 ; index < EEPROM.length(); ++index) {
4848
crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
4949
crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
5050
crc = ~crc;

libraries/CurieEEPROM/examples/eeprom_get/eeprom_get.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct MyObject {
5252
};
5353

5454
void secondTest() {
55-
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
55+
int eeAddress = 1; //Move address to the next byte after float 'f'.
5656

5757
MyObject customVar; //Variable to store custom object read from EEPROM.
5858
EEPROM.get(eeAddress, customVar);

libraries/CurieEEPROM/examples/eeprom_put/eeprom_put.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ void setup() {
4747
"Working!"
4848
};
4949

50-
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
51-
50+
eeAddress++;
5251
EEPROM.put(eeAddress, customVar);
5352
Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
5453
}

libraries/CurieEEPROM/examples/eeprom_read/eeprom_read.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ void setup() {
2222
}
2323

2424
void loop() {
25-
// read a byte from the current address of the EEPROM
25+
// read a dword from the current address of the EEPROM
2626
value = EEPROM.read(address);
2727

2828
Serial.print(address);
2929
Serial.print("\t");
3030
Serial.print(value, DEC);
3131
Serial.println();
3232

33-
//increment address by 4 since we are using DWORDs and we have a granularity of a DWORD or 4 bytes
34-
address = address + 4;
33+
//increment address
34+
address++;
3535
if (address == EEPROM.length()) {
3636
address = 0;
3737
}

libraries/CurieEEPROM/examples/eeprom_write/eeprom_write.ino

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,19 @@ void setup() {
1717
while (!Serial) {
1818
; // wait for serial port to connect. Needed for native USB port only
1919
}
20-
//use write for the first half of the EEPROM area
21-
Serial.println("Writing with write()");
22-
for(int i = 0; i < EEPROM.length()/8; i++)
20+
21+
for(int i = 0; i < 512; i++)
2322
{
2423
unsigned long val = analogRead(0);
2524
Serial.print("Addr:\t");
2625
Serial.print(addr);
2726
Serial.print("\tWriting: ");
2827
Serial.println(val);
2928
EEPROM.write(addr, val);
30-
addr +=4; //increment address by 4 since we are using DWORDs
31-
delay(100);
32-
}
33-
34-
//use write8 for the second half of the EEPROM area
35-
Serial.println("Writing with write8()");
36-
for(int i = EEPROM.length()/2; i < EEPROM.length(); i++)
37-
{
38-
byte val8 = analogRead(0)/4;
39-
Serial.print("Addr:\t");
40-
Serial.print(addr);
41-
Serial.print("\tWriting: ");
42-
Serial.println(val8);
43-
EEPROM.write(addr, val8);
4429
addr++;
4530
delay(100);
4631
}
47-
32+
4833
Serial.println("done writing");
4934
}
5035

libraries/CurieEEPROM/src/CurieEEPROM.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@ void CurieEEPROM::clear()
3131

3232
void CurieEEPROM::write(uint32_t address, uint32_t data)
3333
{
34+
//make sure address is within valid range
35+
if(address > 0x1FF)
36+
{
37+
return;
38+
}
39+
3440
uint32_t currentValue = read(address);
3541
//only do something if value is different from what is currently stored
3642
if(currentValue==data)
3743
{
3844
return;
3945
}
4046

47+
//allign address to 32-bit addressing
48+
address*=sizeof(uint32_t);
49+
4150
uint32_t rom_wr_ctrl = 0;
4251
//make sure address is valid
4352
if((address > 0x7FC) || (address%4))
@@ -168,11 +177,15 @@ void CurieEEPROM::update8(uint32_t addr, uint8_t value)
168177

169178
uint32_t CurieEEPROM::read(uint32_t address)
170179
{
171-
//make sure address is valid
172-
if((address > 0x7FC) || (address%4))
180+
//make sure address is within valid range
181+
if(address > 0x1FF)
173182
{
174183
return 0;
175184
}
185+
186+
//allign address to 32-bit addressing
187+
address*=sizeof(uint32_t);
188+
176189
address += EEPROM_ADDR;
177190
return *(uint32_t*)(address);
178191
}

libraries/CurieEEPROM/src/CurieEEPROM.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define EEPROM_OFFSET 0x00001000
2525
#define CTRL_REG 0xb0000018
2626

27-
#define EEPROM_SIZE 2048 //EEPROM size in bytes
27+
#define EEPROM_SIZE 512 //EEPROM size in dwords
2828

2929

3030
#include <inttypes.h>
@@ -54,17 +54,22 @@ class CurieEEPROM
5454
//Functionality to 'get' and 'put' objects to and from EEPROM.
5555
template< typename T > T &get(uint32_t addr, T &t)
5656
{
57-
//make sure address is valid
58-
if((addr > 0x7FC) || (addr%4))
57+
//make sure address is within valid range
58+
if(addr > 0x1FF)
5959
{
6060
return t;
6161
}
62+
6263
int byteCount = sizeof(T);
63-
//return if size of object is greater than size of EEPROM
64-
if(byteCount > EEPROM_SIZE)
64+
//return if object size is too big
65+
if(addr + byteCount > 0x7FC)
6566
{
6667
return t;
6768
}
69+
70+
//allign address to 32-bit addressing
71+
addr*=sizeof(uint32_t);
72+
6873
byte *bytes = to_bytes(t);
6974
for(int i = 0; i < byteCount; i++)
7075
{
@@ -76,15 +81,16 @@ class CurieEEPROM
7681
}
7782
template< typename T > T put(uint32_t addr, T t)
7883
{
79-
//make sure address is valid
80-
if((addr > 0x7FC) || (addr%4))
84+
//make sure address is within valid range
85+
if(addr > 0x1FF)
8186
{
8287
return t;
8388
}
8489
uint32_t rom_wr_ctrl = 0;
8590
int byteCount = sizeof(T);
86-
//return if size of object is greater than size of EEPROM
87-
if(byteCount > EEPROM_SIZE)
91+
92+
//return if object size is too big
93+
if(addr + byteCount > 0x7FC)
8894
{
8995
return t;
9096
}
@@ -96,7 +102,7 @@ class CurieEEPROM
96102
bool blockAvailable = true;
97103
for(int i =0; i < size; i++)
98104
{
99-
uint32_t data32 = read(addr+i*sizeof(uint32_t));
105+
uint32_t data32 = read(addr+i);
100106
if(data32 != 0xFFFFFFFF)
101107
{
102108
blockAvailable = false;
@@ -106,7 +112,7 @@ class CurieEEPROM
106112
{
107113
for(int i = 0; i<size; i++)
108114
{
109-
write(addr+i*sizeof(uint32_t), dwords[i]);
115+
write(addr+i, dwords[i]);
110116
}
111117
}
112118
else

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy