Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 12, 2025
1 parent 734b75c commit 050cd18
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
55 changes: 29 additions & 26 deletions Marlin/src/HAL/STM32/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,13 @@
#define EEPROM_SLOTS ((FLASH_UNIT_SIZE) / (MARLIN_EEPROM_SIZE))
#define SLOT_ADDRESS(slot) (FLASH_ADDRESS_START + (slot * (MARLIN_EEPROM_SIZE)))

#ifdef STM32H7xx
#define FLASHWORD_SIZE 32U // STM32H7xx a FLASHWORD is 32 bytes (256 bits)
#define UNLOCK_FLASH() if (!flash_unlocked) { \
HAL_FLASH_Unlock(); \
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | \
FLASH_FLAG_PGSERR); \
flash_unlocked = true; \
}
#else
#define FLASHWORD_SIZE 4U // STM32F4xx a FLASHWORD is 4 bytes sizeof(uint32_t)
#define UNLOCK_FLASH() if (!flash_unlocked) { \
HAL_FLASH_Unlock(); \
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | \
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); \
flash_unlocked = true; \
}
#endif

#define LOCK_FLASH() if (flash_unlocked) { HAL_FLASH_Lock(); flash_unlocked = false; }
#ifdef STM32H7xx
#define FLASHWORD_SIZE 32U // STM32H7xx a FLASHWORD is 32 bytes (256 bits)
#define FLASH_FLAGS_TO_CLEAR (FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGSERR)
#else
#define FLASHWORD_SIZE 4U // STM32F4xx a FLASHWORD is 4 bytes sizeof(uint32_t)
#define FLASH_FLAGS_TO_CLEAR (FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)
#endif

#define EMPTY_UINT32 ((uint32_t)-1)
#define EMPTY_UINT8 ((uint8_t)-1)
Expand Down Expand Up @@ -186,7 +174,12 @@ bool PersistentStore::access_finish() {
EraseInitStruct.NbSectors = 1;

current_slot = EEPROM_SLOTS - 1;
UNLOCK_FLASH();

if (!flash_unlocked) {
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAGS_TO_CLEAR);
flash_unlocked = true;
}

TERN_(HAS_PAUSE_SERVO_OUTPUT, PAUSE_SERVO_OUTPUT());
hal.isr_off();
Expand All @@ -197,26 +190,33 @@ bool PersistentStore::access_finish() {
DEBUG_ECHOLNPGM("HAL_FLASHEx_Erase=", status);
DEBUG_ECHOLNPGM("GetError=", HAL_FLASH_GetError());
DEBUG_ECHOLNPGM("SectorError=", SectorError);
LOCK_FLASH();
if (flash_unlocked) {
HAL_FLASH_Lock();
flash_unlocked = false;
}
return false;
}
}

UNLOCK_FLASH();
if (!flash_unlocked) {
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAGS_TO_CLEAR);
flash_unlocked = true;
}

uint32_t offset = 0,
address = SLOT_ADDRESS(current_slot),
address_end = address + MARLIN_EEPROM_SIZE;
// data = 0;
//data = 0;

bool success = true;

while (address < address_end) {
#ifdef STM32H7xx
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, address, uint32_t(ram_eeprom + offset));
#else
// memcpy(&data, ram_eeprom + offset, sizeof(data)); // IRON, IMPROVED
// status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data);
//memcpy(&data, ram_eeprom + offset, sizeof(data)); // IRON, IMPROVED
//status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data);
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *(uint32_t*)(ram_eeprom + offset)); // IRON, OPTIMIZED
#endif
if (status == HAL_OK) {
Expand All @@ -232,7 +232,10 @@ bool PersistentStore::access_finish() {
}
}

LOCK_FLASH();
if (flash_unlocked) {
HAL_FLASH_Lock();
flash_unlocked = false;
}

if (success) {
eeprom_data_written = false;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#error "SDCARD_EEPROM_EMULATION requires SDSUPPORT. Enable SDSUPPORT or choose another EEPROM emulation."
#endif

#if !ANY(STM32F4xx, STM32H7xx) && ENABLED(FLASH_EEPROM_LEVELING)
#if NONE(STM32F4xx, STM32H7xx) && ENABLED(FLASH_EEPROM_LEVELING)
#error "FLASH_EEPROM_LEVELING is currently only supported on STM32F4/H7 hardware." // IRON
#endif

Expand Down

0 comments on commit 050cd18

Please sign in to comment.