Skip to content

Commit

Permalink
Added an exclusion zone in the EEPROM.
Browse files Browse the repository at this point in the history
That zone is used by some boot loaders to save flags that control the flashing at boot process.

The exclusion zone should be defined in the **.INI** board environments that require it by:
```log
 -DEEPROM_EXCL_ZONE=100,200
```
or in the board pin file by:
```c++
#define EEPROM_EXCL_ZONE 100,200  // Test a range
or
#define EEPROM_EXCL_ZONE 333      // Test a single byte
```
For example, in the Ender-3S1 F4: Marlin\src\pins\stm32f4\pins_CREALITY_V24S1_301F4.h:
```c++
#define EEPROM_EXCL_ZONE 919,926  // Test a range
```

### Related Issues

In the Ender-3 S1 with the STM32F401 SoC the range of exclusion is from 919 to 926. Even if the zone is filled with 0xFF values, the boot loader changes it when the printer is rebooted:

```log
0370: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
0380: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
0390: FF FF FF FF 00 FF FF FF FF FF FF FF FF 66 FF FF
03A0: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
03B0: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
```
If the zone is not excluded, the boot loader may not allow flashing new firmware and may also cause CRC EEPROM corruption.

Perhaps related to:
mriscoc#125
mriscoc#1180
MarlinFirmware/Marlin#26144
  • Loading branch information
mriscoc authored and Apie56 committed Apr 6, 2024
1 parent 341fabd commit 462decb
Show file tree
Hide file tree
Showing 44 changed files with 123 additions and 110 deletions.
Binary file modified Marlin/lib/proui/stm32f1/libproui_abl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f1/libproui_abl_ultipanel.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f1/libproui_mbl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f1/libproui_mbl_ultipanel.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f1/libproui_ubl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f1/libproui_ubl_ultipanel.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f4/libproui_abl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f4/libproui_mbl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32f4/libproui_ubl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32g0/libproui_abl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32g0/libproui_mbl_dwin.a
Binary file not shown.
Binary file modified Marlin/lib/proui/stm32g0/libproui_ubl_dwin.a
Binary file not shown.
6 changes: 3 additions & 3 deletions Marlin/src/HAL/AVR/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE size_t(E2END + 1)
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }

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

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
uint8_t c = eeprom_read_byte((uint8_t*)pos);
const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/HAL/DUE/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,14 +958,14 @@ static void ee_Init() {
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
bool PersistentStore::access_start() { ee_Init(); return true; }
bool PersistentStore::access_finish() { ee_Flush(); return true; }

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

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
uint8_t c = ee_Read(uint32_t(pos));
uint8_t c = ee_Read(uint32_t(REAL_EEPROM_ADDR(pos)));
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/HAL/DUE/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
#ifndef MARLIN_EEPROM_SIZE
#error "MARLIN_EEPROM_SIZE is required for I2C / SPI EEPROM."
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
bool PersistentStore::access_start() { eeprom_init(); return true; }
bool PersistentStore::access_finish() { return true; }

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

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
uint8_t c = eeprom_read_byte((uint8_t*)pos);
const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
Expand Down
10 changes: 7 additions & 3 deletions Marlin/src/HAL/ESP32/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,28 @@
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }

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

bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
for (size_t i = 0; i < size; i++) {
EEPROM.write(pos++, value[i]);
const int p = REAL_EEPROM_ADDR(pos);
EEPROM.write(p, value[i]);
crc16(crc, &value[i], 1);
++pos;
}
return false;
}

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
for (size_t i = 0; i < size; i++) {
uint8_t c = EEPROM.read(pos++);
const int p = REAL_EEPROM_ADDR(pos);
uint8_t c = EEPROM.read(p);
if (writing) value[i] = c;
crc16(crc, &c, 1);
++pos;
}
return false;
}
Expand Down
16 changes: 5 additions & 11 deletions Marlin/src/HAL/HC32/eeprom_bl24cxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#error "MARLIN_EEPROM_SIZE is required for IIC_BL24CXX_EEPROM."
#endif

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

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

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

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size,
uint16_t *crc, const bool writing /*=true*/) {
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing /*=true*/) {
do {
uint8_t *const p = (uint8_t *const)pos;
uint8_t c = eeprom_read_byte(p);
if (writing)
{
*value = c;
}

const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
value++;
Expand Down
7 changes: 2 additions & 5 deletions Marlin/src/HAL/HC32/eeprom_sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
#endif

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

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

bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uint16_t *crc, const bool writing /*=true*/) {
for (size_t i = 0; i < size; i++) {
uint8_t c = HAL_eeprom_data[pos + i];
const uint8_t c = HAL_eeprom_data[pos + i];
if (writing) value[i] = c;
crc16(crc, &c, 1);
}

pos += size;
return false;
}
Expand Down
10 changes: 4 additions & 6 deletions Marlin/src/HAL/HC32/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#ifndef MARLIN_EEPROM_SIZE
#error "MARLIN_EEPROM_SIZE is required for I2C / SPI EEPROM."
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }

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

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

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

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing /*=true*/) {
do {
uint8_t c = eeprom_read_byte((uint8_t *)pos);
if (writing && value) {
*value = c;
}
const uint8_t c = eeprom_read_byte((uint8_t *)REAL_EEPROM_ADDR(pos));
if (writing && value) *value = c;

crc16(crc, &c, 1);
pos++;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/LINUX/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
uint8_t buffer[MARLIN_EEPROM_SIZE];
char filename[] = "eeprom.dat";

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

bool PersistentStore::access_start() {
const char eeprom_erase_value = 0xFF;
Expand Down
8 changes: 5 additions & 3 deletions Marlin/src/HAL/LPC1768/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0};
static bool eeprom_dirty = false;
static int current_slot = 0;

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

bool PersistentStore::access_start() {
uint32_t first_nblank_loc, first_nblank_val;
Expand Down Expand Up @@ -112,16 +112,18 @@ bool PersistentStore::access_finish() {
}

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

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
const int p = REAL_EEPROM_ADDR(pos);
const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos];
if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[pos + i];
if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[p + i];
crc16(crc, buff, size);
pos += size;
return false; // return true for any error
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/LPC1768/eeprom_sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool eeprom_file_open = false;
#define MARLIN_EEPROM_SIZE size_t(0x1000) // 4KiB of Emulated EEPROM
#endif

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

bool PersistentStore::access_start() {
const char eeprom_erase_value = 0xFF;
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/HAL/LPC1768/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x8000 // 32K
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }

bool PersistentStore::access_start() { eeprom_init(); return true; }
bool PersistentStore::access_finish() { return true; }
Expand All @@ -45,7 +45,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
uint16_t written = 0;
while (size--) {
uint8_t v = *value;
uint8_t * const p = (uint8_t * const)pos;
uint8_t * const p = (uint8_t * const)REAL_EEPROM_ADDR(pos);
if (v != eeprom_read_byte(p)) { // EEPROM has only ~100,000 write cycles, so only write bytes that have changed!
eeprom_write_byte(p, v);
if (++written & 0x7F) delay(2); else safe_delay(2); // Avoid triggering watchdog during long EEPROM writes
Expand All @@ -64,7 +64,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
// Read from external EEPROM
const uint8_t c = eeprom_read_byte((uint8_t*)pos);
const uint8_t c = eeprom_read_byte((uint8_t*)REAL_EEPROM_ADDR(pos));
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
Expand Down
38 changes: 21 additions & 17 deletions Marlin/src/HAL/SAMD21/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,24 @@ static const uint8_t flashdata[TOTAL_FLASH_SIZE] __attribute__((__aligned__(256

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

size_t PersistentStore::capacity() {
return MARLIN_EEPROM_SIZE;
/* const uint8_t psz = NVMCTRL->SEESTAT.bit.PSZ,
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }

/*
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE - eeprom_exclude_size; }
const uint8_t psz = NVMCTRL->SEESTAT.bit.PSZ,
sblk = NVMCTRL->SEESTAT.bit.SBLK;
return (!psz && !sblk) ? 0
: (psz <= 2) ? (0x200 << psz)
: (sblk == 1 || psz == 3) ? 4096
: (sblk == 2 || psz == 4) ? 8192
: (sblk <= 4 || psz == 5) ? 16384
: (sblk >= 9 && psz == 7) ? 65536
: 32768;*/
return (
(!psz && !sblk) ? 0
: (psz <= 2) ? (0x200 << psz)
: (sblk == 1 || psz == 3) ? 4096
: (sblk == 2 || psz == 4) ? 8192
: (sblk <= 4 || psz == 5) ? 16384
: (sblk >= 9 && psz == 7) ? 65536
: 32768
) - eeprom_exclude_size;
}
*/

uint32_t PAGE_SIZE;
uint32_t ROW_SIZE;
Expand Down Expand Up @@ -99,8 +104,7 @@ bool PersistentStore::access_finish() {
volatile uint32_t *dst_addr = (volatile uint32_t *) &flashdata;

uint32_t *pointer = (uint32_t *) buffer;
for (uint32_t i = 0; i < TOTAL_FLASH_SIZE; i+=4) {

for (uint32_t i = 0; i < TOTAL_FLASH_SIZE; i += 4) {
*dst_addr = (uint32_t) *pointer;
pointer++;
dst_addr ++;
Expand All @@ -120,19 +124,19 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
if (!hasWritten) {
// init temp buffer
buffer = (uint8_t *) malloc(MARLIN_EEPROM_SIZE);
hasWritten=true;
hasWritten = true;
}

memcpy(buffer+pos,value,size);
memcpy(buffer + REAL_EEPROM_ADDR(pos), value, size);
pos += size;
return false;
}

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

memcpy(value,(const void *) dst_addr,size);
memcpy(value, (const void *)dst_addr, size);
pos += size;
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/HAL/SAMD21/eeprom_qspi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

static bool initialized;

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

bool PersistentStore::access_start() {
if (!initialized) {
Expand All @@ -56,7 +56,7 @@ bool PersistentStore::access_finish() {
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
const uint8_t v = *value;
qspi.writeByte(pos, v);
qspi.writeByte(REAL_EEPROM_ADDR(pos), v);
crc16(crc, &v, 1);
pos++;
value++;
Expand All @@ -66,7 +66,7 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui

bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
while (size--) {
uint8_t c = qspi.readByte(pos);
const uint8_t c = qspi.readByte(REAL_EEPROM_ADDR(pos));
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
Expand Down
Loading

0 comments on commit 462decb

Please sign in to comment.