Skip to content

Commit 01094ea

Browse files
mriscocthinkyhead
andauthored
✨🔨 EEPROM exclusion zone (MarlinFirmware#26729)
Co-authored-by: Scott Lahteine <[email protected]>
1 parent 6c1fd1f commit 01094ea

31 files changed

+120
-106
lines changed

Marlin/src/HAL/AVR/eeprom.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
#ifndef MARLIN_EEPROM_SIZE
3636
#define MARLIN_EEPROM_SIZE size_t(E2END + 1)
3737
#endif
38-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
38+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
3939
bool PersistentStore::access_start() { return true; }
4040
bool PersistentStore::access_finish() { return true; }
4141

4242
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
4343
uint16_t written = 0;
4444
while (size--) {
45-
uint8_t * const p = (uint8_t * const)pos;
45+
uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos);
4646
uint8_t v = *value;
4747
if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed!
4848
eeprom_write_byte(p, v);
@@ -61,7 +61,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
6161

6262
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
6363
do {
64-
uint8_t c = eeprom_read_byte((uint8_t*)pos);
64+
const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
6565
if (writing) *value = c;
6666
crc16(crc, &c, 1);
6767
pos++;

Marlin/src/HAL/DUE/eeprom_flash.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,14 +958,14 @@ static void ee_Init() {
958958
#ifndef MARLIN_EEPROM_SIZE
959959
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
960960
#endif
961-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
961+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
962962
bool PersistentStore::access_start() { ee_Init(); return true; }
963963
bool PersistentStore::access_finish() { ee_Flush(); return true; }
964964

965965
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
966966
uint16_t written = 0;
967967
while (size--) {
968-
uint8_t * const p = (uint8_t * const)pos;
968+
uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos);
969969
uint8_t v = *value;
970970
if (v != ee_Read(uint32_t(p))) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed!
971971
ee_Write(uint32_t(p), v);
@@ -984,7 +984,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
984984

985985
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
986986
do {
987-
uint8_t c = ee_Read(uint32_t(pos));
987+
uint8_t c = ee_Read(uint32_t(REAL_EEPROM_ADDR(pos)));
988988
if (writing) *value = c;
989989
crc16(crc, &c, 1);
990990
pos++;

Marlin/src/HAL/DUE/eeprom_wired.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
#ifndef MARLIN_EEPROM_SIZE
3737
#error "MARLIN_EEPROM_SIZE is required for I2C / SPI EEPROM."
3838
#endif
39-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
39+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
4040
bool PersistentStore::access_start() { eeprom_init(); return true; }
4141
bool PersistentStore::access_finish() { return true; }
4242

4343
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
4444
uint16_t written = 0;
4545
while (size--) {
46-
uint8_t * const p = (uint8_t * const)pos;
46+
uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos);
4747
uint8_t v = *value;
4848
if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed!
4949
eeprom_write_byte(p, v);
@@ -62,7 +62,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
6262

6363
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
6464
do {
65-
uint8_t c = eeprom_read_byte((uint8_t*)pos);
65+
const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
6666
if (writing) *value = c;
6767
crc16(crc, &c, 1);
6868
pos++;

Marlin/src/HAL/ESP32/eeprom.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,28 @@
3131
#ifndef MARLIN_EEPROM_SIZE
3232
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
3333
#endif
34-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
34+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
3535

3636
bool PersistentStore::access_start() { return EEPROM.begin(MARLIN_EEPROM_SIZE); }
3737
bool PersistentStore::access_finish() { EEPROM.end(); return true; }
3838

3939
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
4040
for (size_t i = 0; i < size; i++) {
41-
EEPROM.write(pos++, value[i]);
41+
const int p = REAL_EEPROM_ADDR(pos);
42+
EEPROM.write(p, value[i]);
4243
crc16(crc, &value[i], 1);
44+
++pos;
4345
}
4446
return false;
4547
}
4648

4749
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
4850
for (size_t i = 0; i < size; i++) {
49-
uint8_t c = EEPROM.read(pos++);
51+
const int p = REAL_EEPROM_ADDR(pos);
52+
uint8_t c = EEPROM.read(p);
5053
if (writing) value[i] = c;
5154
crc16(crc, &c, 1);
55+
++pos;
5256
}
5357
return false;
5458
}

Marlin/src/HAL/HC32/eeprom_bl24cxx.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#error "MARLIN_EEPROM_SIZE is required for IIC_BL24CXX_EEPROM."
3838
#endif
3939

40-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
40+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
4141

4242
bool PersistentStore::access_start() {
4343
eeprom_init();
@@ -49,7 +49,7 @@ bool PersistentStore::access_finish() { return true; }
4949
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
5050
while (size--) {
5151
uint8_t v = *value;
52-
uint8_t *const p = (uint8_t *const)pos;
52+
uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos);
5353

5454
// EEPROM has only ~100,000 write cycles,
5555
// so only write bytes that have changed!
@@ -70,16 +70,10 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
7070
return false;
7171
}
7272

73-
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size,
74-
uint16_t *crc, const bool writing /*=true*/) {
73+
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing /*=true*/) {
7574
do {
76-
uint8_t *const p = (uint8_t *const)pos;
77-
uint8_t c = eeprom_read_byte(p);
78-
if (writing)
79-
{
80-
*value = c;
81-
}
82-
75+
const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
76+
if (writing) *value = c;
8377
crc16(crc, &c, 1);
8478
pos++;
8579
value++;

Marlin/src/HAL/HC32/eeprom_sdcard.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
3939
#endif
4040

41-
size_t PersistentStore::capacity() {
42-
return MARLIN_EEPROM_SIZE;
43-
}
41+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
4442

4543
#define _ALIGN(x) __attribute__((aligned(x)))
4644
static char _ALIGN(4) HAL_eeprom_data[MARLIN_EEPROM_SIZE];
@@ -85,11 +83,10 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
8583

8684
bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uint16_t *crc, const bool writing /*=true*/) {
8785
for (size_t i = 0; i < size; i++) {
88-
uint8_t c = HAL_eeprom_data[pos + i];
86+
const uint8_t c = HAL_eeprom_data[pos + i];
8987
if (writing) value[i] = c;
9088
crc16(crc, &c, 1);
9189
}
92-
9390
pos += size;
9491
return false;
9592
}

Marlin/src/HAL/HC32/eeprom_wired.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#ifndef MARLIN_EEPROM_SIZE
3636
#error "MARLIN_EEPROM_SIZE is required for I2C / SPI EEPROM."
3737
#endif
38-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
38+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
3939

4040
bool PersistentStore::access_finish() { return true; }
4141

@@ -56,7 +56,7 @@ bool PersistentStore::access_start() {
5656

5757
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
5858
while (size--) {
59-
uint8_t *const p = (uint8_t *const)pos;
59+
uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos);
6060
uint8_t v = *value;
6161
// EEPROM has only ~100,000 write cycles,
6262
// so only write bytes that have changed!
@@ -77,10 +77,8 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
7777

7878
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing /*=true*/) {
7979
do {
80-
uint8_t c = eeprom_read_byte((uint8_t *)pos);
81-
if (writing && value) {
82-
*value = c;
83-
}
80+
const uint8_t c = eeprom_read_byte((uint8_t *)REAL_EEPROM_ADDR(pos));
81+
if (writing && value) *value = c;
8482

8583
crc16(crc, &c, 1);
8684
pos++;

Marlin/src/HAL/LINUX/eeprom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
uint8_t buffer[MARLIN_EEPROM_SIZE];
3636
char filename[] = "eeprom.dat";
3737

38-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
38+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
3939

4040
bool PersistentStore::access_start() {
4141
const char eeprom_erase_value = 0xFF;

Marlin/src/HAL/LPC1768/eeprom_flash.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0};
6161
static bool eeprom_dirty = false;
6262
static int current_slot = 0;
6363

64-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
64+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
6565

6666
bool PersistentStore::access_start() {
6767
uint32_t first_nblank_loc, first_nblank_val;
@@ -112,16 +112,18 @@ bool PersistentStore::access_finish() {
112112
}
113113

114114
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
115-
for (size_t i = 0; i < size; i++) ram_eeprom[pos + i] = value[i];
115+
const int p = REAL_EEPROM_ADDR(pos);
116+
for (size_t i = 0; i < size; i++) ram_eeprom[p + i] = value[i];
116117
eeprom_dirty = true;
117118
crc16(crc, value, size);
118119
pos += size;
119120
return false; // return true for any error
120121
}
121122

122123
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
124+
const int p = REAL_EEPROM_ADDR(pos);
123125
const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos];
124-
if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[pos + i];
126+
if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[p + i];
125127
crc16(crc, buff, size);
126128
pos += size;
127129
return false; // return true for any error

Marlin/src/HAL/LPC1768/eeprom_sdcard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool eeprom_file_open = false;
4949
#define MARLIN_EEPROM_SIZE size_t(0x1000) // 4KiB of Emulated EEPROM
5050
#endif
5151

52-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
52+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
5353

5454
bool PersistentStore::access_start() {
5555
const char eeprom_erase_value = 0xFF;

0 commit comments

Comments
 (0)