|
@ -18,26 +18,12 @@ |
|
|
*/ |
|
|
*/ |
|
|
#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7)) |
|
|
#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" |
|
|
#include "../../inc/MarlinConfig.h" |
|
|
|
|
|
|
|
|
#if ENABLED(FLASH_EEPROM_EMULATION) |
|
|
#if ENABLED(FLASH_EEPROM_EMULATION) |
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
#include "../shared/eeprom_api.h" |
|
|
// Includes
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
#include "HAL.h" |
|
|
|
|
|
#include "eeprom_emul.h" |
|
|
#include "eeprom_emul.h" |
|
|
#include "../shared/eeprom_if.h" |
|
|
|
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
// Local defines
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
|
|
|
// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
|
|
|
// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
|
|
|
// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
|
|
@ -48,18 +34,9 @@ |
|
|
//#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
|
|
|
//#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
|
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
void ee_init() { |
|
|
// Private Variables
|
|
|
static bool ee_initialized = false; |
|
|
// ------------------------
|
|
|
if (!ee_initialized) { |
|
|
|
|
|
|
|
|
static bool eeprom_initialized = false; |
|
|
|
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
// Public functions
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void eeprom_init() { |
|
|
|
|
|
if (!eeprom_initialized) { |
|
|
|
|
|
HAL_FLASH_Unlock(); |
|
|
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); |
|
|
__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
|
|
|
for (;;) HAL_Delay(1); // Spin forever until watchdog reset
|
|
|
|
|
|
|
|
|
HAL_FLASH_Lock(); |
|
|
HAL_FLASH_Lock(); |
|
|
eeprom_initialized = true; |
|
|
ee_initialized = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void eeprom_write_byte(uint8_t *pos, unsigned char value) { |
|
|
void ee_write_byte(uint8_t *pos, unsigned char value) { |
|
|
eeprom_init(); |
|
|
ee_init(); |
|
|
|
|
|
|
|
|
HAL_FLASH_Unlock(); |
|
|
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); |
|
|
__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(); |
|
|
HAL_FLASH_Lock(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint8_t eeprom_read_byte(uint8_t *pos) { |
|
|
uint8_t ee_read_byte(uint8_t *pos) { |
|
|
eeprom_init(); |
|
|
ee_init(); |
|
|
|
|
|
|
|
|
uint16_t data = 0xFF; |
|
|
uint16_t data = 0xFF; |
|
|
const unsigned eeprom_address = (unsigned)pos; |
|
|
const unsigned eeprom_address = (unsigned)pos; |
|
@ -96,19 +73,39 @@ uint8_t eeprom_read_byte(uint8_t *pos) { |
|
|
return uint8_t(data); |
|
|
return uint8_t(data); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void eeprom_read_block(void *__dst, const void *__src, size_t __n) { |
|
|
size_t PersistentStore::capacity() { return E2END + 1; } |
|
|
eeprom_init(); |
|
|
bool PersistentStore::access_start() { return true; } |
|
|
|
|
|
bool PersistentStore::access_finish() { return true; } |
|
|
uint16_t data = 0xFF; |
|
|
|
|
|
const unsigned eeprom_address = (unsigned)__src; |
|
|
bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { |
|
|
LOOP_L_N(c, __n) { |
|
|
while (size--) { |
|
|
EE_ReadVariable(eeprom_address+c, &data); |
|
|
uint8_t * const p = (uint8_t * const)pos; |
|
|
*((uint8_t*)__dst + c) = data; |
|
|
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
|
|
|
#endif // FLASH_EEPROM_EMULATION
|