Skip to content

Commit 01094ea

Browse files
mriscocthinkyhead
andauthored
✨🔨 EEPROM exclusion zone (MarlinFirmware#26729)
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
1 parent 6c1fd1f commit 01094ea

31 files changed

+120
-106
lines changed

Marlin/src/HAL/AVR/eeprom.cpp

+3-3
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

+3-3
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

+3-3
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

+7-3
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

+5-11
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

+2-5
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

+4-6
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

+1-1
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

+5-3
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

+1-1
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;

Marlin/src/HAL/LPC1768/eeprom_wired.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#ifndef MARLIN_EEPROM_SIZE
3737
#define MARLIN_EEPROM_SIZE 0x8000 // 32K
3838
#endif
39-
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
39+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
4040

4141
bool PersistentStore::access_start() { eeprom_init(); return true; }
4242
bool PersistentStore::access_finish() { return true; }
@@ -45,7 +45,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
4545
uint16_t written = 0;
4646
while (size--) {
4747
uint8_t v = *value;
48-
uint8_t * const p = (uint8_t * const)pos;
48+
uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos);
4949
if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed!
5050
eeprom_write_byte(p, v);
5151
if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes
@@ -64,7 +64,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
6464
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
6565
do {
6666
// Read from external EEPROM
67-
const uint8_t c = eeprom_read_byte((uint8_t*)pos);
67+
const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
6868
if (writing) *value = c;
6969
crc16(crc, &c, 1);
7070
pos++;

Marlin/src/HAL/SAMD21/eeprom_flash.cpp

+21-17
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@ static const uint8_t flashdata[TOTAL_FLASH_SIZE] __attribute__((__aligned__(256
3737

3838
#include "../shared/eeprom_api.h"
3939

40-
size_t PersistentStore::capacity() {
41-
return MARLIN_EEPROM_SIZE;
42-
/* const uint8_t psz = NVMCTRL->SEESTAT.bit.PSZ,
40+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
41+
42+
/*
43+
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
44+
const uint8_t psz = NVMCTRL->SEESTAT.bit.PSZ,
4345
sblk = NVMCTRL->SEESTAT.bit.SBLK;
4446
45-
return (!psz && !sblk) ? 0
46-
: (psz <= 2) ? (0x200 << psz)
47-
: (sblk == 1 || psz == 3) ? 4096
48-
: (sblk == 2 || psz == 4) ? 8192
49-
: (sblk <= 4 || psz == 5) ? 16384
50-
: (sblk >= 9 && psz == 7) ? 65536
51-
: 32768;*/
47+
return (
48+
(!psz && !sblk) ? 0
49+
: (psz <= 2) ? (0x200 << psz)
50+
: (sblk == 1 || psz == 3) ? 4096
51+
: (sblk == 2 || psz == 4) ? 8192
52+
: (sblk <= 4 || psz == 5) ? 16384
53+
: (sblk >= 9 && psz == 7) ? 65536
54+
: 32768
55+
) - eeprom_exclude_size;
5256
}
57+
*/
5358

5459
uint32_t PAGE_SIZE;
5560
uint32_t ROW_SIZE;
@@ -99,8 +104,7 @@ bool PersistentStore::access_finish() {
99104
volatile uint32_t *dst_addr = (volatile uint32_t *) &flashdata;
100105

101106
uint32_t *pointer = (uint32_t *) buffer;
102-
for (uint32_t i = 0; i < TOTAL_FLASH_SIZE; i+=4) {
103-
107+
for (uint32_t i = 0; i < TOTAL_FLASH_SIZE; i += 4) {
104108
*dst_addr = (uint32_t) *pointer;
105109
pointer++;
106110
dst_addr ++;
@@ -120,19 +124,19 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
120124
if (!hasWritten) {
121125
// init temp buffer
122126
buffer = (uint8_t *) malloc(MARLIN_EEPROM_SIZE);
123-
hasWritten=true;
127+
hasWritten = true;
124128
}
125129

126-
memcpy(buffer+pos,value,size);
130+
memcpy(buffer + REAL_EEPROM_ADDR(pos), value, size);
127131
pos += size;
128132
return false;
129133
}
130134

131135
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
132-
volatile uint8_t *dst_addr = (volatile uint8_t *) &flashdata;
133-
dst_addr += pos;
136+
volatile uint8_t *dst_addr = (volatile uint8_t *) &flashdata;
137+
dst_addr += REAL_EEPROM_ADDR(pos);
134138

135-
memcpy(value,(const void *) dst_addr,size);
139+
memcpy(value, (const void *)dst_addr, size);
136140
pos += size;
137141
return false;
138142
}

Marlin/src/HAL/SAMD21/eeprom_qspi.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
static bool initialized;
4040

41-
size_t PersistentStore::capacity() { return qspi.size(); }
41+
size_t PersistentStore::capacity() { return qspi.size() - eeprom_exclude_size; }
4242

4343
bool PersistentStore::access_start() {
4444
if (!initialized) {
@@ -56,7 +56,7 @@ bool PersistentStore::access_finish() {
5656
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
5757
while (size--) {
5858
const uint8_t v = *value;
59-
qspi.writeByte(pos, v);
59+
qspi.writeByte(REAL_EEPROM_ADDR(pos), v);
6060
crc16(crc, &v, 1);
6161
pos++;
6262
value++;
@@ -66,7 +66,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
6666

6767
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
6868
while (size--) {
69-
uint8_t c = qspi.readByte(pos);
69+
const uint8_t c = qspi.readByte(REAL_EEPROM_ADDR(pos));
7070
if (writing) *value = c;
7171
crc16(crc, &c, 1);
7272
pos++;

0 commit comments

Comments
 (0)