diff --git a/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.cpp b/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.cpp index cb4811d2a..3e74203ae 100644 --- a/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.cpp +++ b/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.cpp @@ -49,7 +49,7 @@ */ #if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7)) -#include "../../inc/MarlinConfigPre.h" +#include "../../inc/MarlinConfig.h" #if ENABLED(FLASH_EEPROM_EMULATION) @@ -65,7 +65,7 @@ uint16_t DataVar = 0; uint16_t VirtAddVarTab[NB_OF_VAR]; /* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ + static HAL_StatusTypeDef EE_Format(); static uint16_t EE_FindValidPage(uint8_t Operation); static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data); @@ -79,6 +79,9 @@ static uint16_t EE_VerifyPageFullyErased(uint32_t Address); * @retval - Flash error code: on write Flash error * - FLASH_COMPLETE: on success */ + +/* Private functions ---------------------------------------------------------*/ + uint16_t EE_Initialize() { /* Get Page0 and Page1 status */ uint16_t PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS), diff --git a/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.h b/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.h index e4094f8e1..84c4c6e3d 100644 --- a/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.h +++ b/Marlin/src/HAL/STM32_F4_F7/eeprom_emul.h @@ -50,7 +50,6 @@ // ------------------------ #include "../../inc/MarlinConfig.h" -#include "HAL.h" /* Exported constants --------------------------------------------------------*/ /* EEPROM emulation firmware error codes */ diff --git a/Marlin/src/HAL/STM32_F4_F7/eeprom_if_flash.cpp b/Marlin/src/HAL/STM32_F4_F7/eeprom_flash.cpp similarity index 63% rename from Marlin/src/HAL/STM32_F4_F7/eeprom_if_flash.cpp rename to Marlin/src/HAL/STM32_F4_F7/eeprom_flash.cpp index 86b80f2b8..cd5648c73 100644 --- a/Marlin/src/HAL/STM32_F4_F7/eeprom_if_flash.cpp +++ b/Marlin/src/HAL/STM32_F4_F7/eeprom_flash.cpp @@ -18,26 +18,12 @@ */ #if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7)) -/** - * Arduino-style interface for Flash emulated EEPROM - */ - -// Include configs and pins to get all EEPROM flags #include "../../inc/MarlinConfig.h" #if ENABLED(FLASH_EEPROM_EMULATION) -// ------------------------ -// Includes -// ------------------------ - -#include "HAL.h" +#include "../shared/eeprom_api.h" #include "eeprom_emul.h" -#include "../shared/eeprom_if.h" - -// ------------------------ -// Local defines -// ------------------------ // FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to // FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7 @@ -48,18 +34,9 @@ //#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR #endif -// ------------------------ -// Private Variables -// ------------------------ - -static bool eeprom_initialized = false; - -// ------------------------ -// Public functions -// ------------------------ - -void eeprom_init() { - if (!eeprom_initialized) { +void ee_init() { + static bool ee_initialized = false; + if (!ee_initialized) { 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); @@ -69,12 +46,12 @@ void eeprom_init() { for (;;) HAL_Delay(1); // Spin forever until watchdog reset HAL_FLASH_Lock(); - eeprom_initialized = true; + ee_initialized = true; } } -void eeprom_write_byte(uint8_t *pos, unsigned char value) { - eeprom_init(); +void ee_write_byte(uint8_t *pos, unsigned char value) { + ee_init(); 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); @@ -86,8 +63,8 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) { HAL_FLASH_Lock(); } -uint8_t eeprom_read_byte(uint8_t *pos) { - eeprom_init(); +uint8_t ee_read_byte(uint8_t *pos) { + ee_init(); uint16_t data = 0xFF; const unsigned eeprom_address = (unsigned)pos; @@ -96,19 +73,39 @@ uint8_t eeprom_read_byte(uint8_t *pos) { return uint8_t(data); } -void eeprom_read_block(void *__dst, const void *__src, size_t __n) { - eeprom_init(); +size_t PersistentStore::capacity() { return E2END + 1; } +bool PersistentStore::access_start() { return true; } +bool PersistentStore::access_finish() { return true; } - uint16_t data = 0xFF; - const unsigned eeprom_address = (unsigned)__src; - LOOP_L_N(c, __n) { - EE_ReadVariable(eeprom_address+c, &data); - *((uint8_t*)__dst + c) = data; - } +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 v = *value; + // EEPROM has only ~100,000 write cycles, + // so only write bytes that have changed! + if (v != ee_read_byte(p)) { + ee_write_byte(p, v); + if (ee_read_byte(p) != v) { + SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE); + return true; + } + } + crc16(crc, &v, 1); + pos++; + value++; + }; + return false; } -void eeprom_update_block(const void *__src, void *__dst, size_t __n) { - +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_byte((uint8_t*)pos); + if (writing) *value = c; + crc16(crc, &c, 1); + pos++; + value++; + } while (--size); + return false; } #endif // FLASH_EEPROM_EMULATION