From 1445c2eeccb6e8efe718d6358f4826ca683a1af5 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Sat, 28 Jan 2023 18:14:34 +0100 Subject: [PATCH] Backport eeprom functions from 3.13 --- Firmware/eeprom.cpp | 191 ++++++++++++++++++++++++++++---------------- Firmware/eeprom.h | 21 ++++- 2 files changed, 140 insertions(+), 72 deletions(-) diff --git a/Firmware/eeprom.cpp b/Firmware/eeprom.cpp index baa2058fb..347ebbfa3 100644 --- a/Firmware/eeprom.cpp +++ b/Firmware/eeprom.cpp @@ -11,57 +11,22 @@ #include "language.h" -#if 0 -template -static T eeprom_read(T *address); - -template<> -char eeprom_read(char *address) -{ - return eeprom_read_byte(reinterpret_cast(address)); -} -#endif - -template -static void eeprom_write(T *address, T value); - -template<> -void eeprom_write(char *addres, char value) -{ - eeprom_write_byte(reinterpret_cast(addres), static_cast(value)); -} - - -template -static bool eeprom_is_uninitialized(T *address); - -template <> -bool eeprom_is_uninitialized(char *address) -{ - return (0xff == eeprom_read_byte(reinterpret_cast(address))); -} - -bool eeprom_is_sheet_initialized(uint8_t sheet_num) -{ - return (0xffff != eeprom_read_word(reinterpret_cast(&(EEPROM_Sheets_base-> - s[sheet_num].z_offset)))); -} - void eeprom_init() { - if (eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_POWER_COUNT, 0); - if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0); - if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0); - if (eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_FERROR_COUNT, 0); - if (eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0); - if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0); - if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0); - if (eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_POWER_COUNT, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_FERROR_COUNT, 0); + eeprom_init_default_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0); + eeprom_init_default_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0); + eeprom_init_default_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0); + eeprom_init_default_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0); - if (eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0); - if (eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0); - if (eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0); - if (eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0); + eeprom_init_default_word((uint16_t*)EEPROM_MMU_FAIL_TOT, 0); + eeprom_init_default_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_MMU_FAIL, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, 0); + eeprom_init_default_dword((uint32_t*)EEPROM_TOTAL_TOOLCHANGE_COUNT, 0); if (eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)) == EEPROM_EMPTY_VALUE) { eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), 0); @@ -71,23 +36,13 @@ void eeprom_init() eeprom_update_word(reinterpret_cast(&(EEPROM_Sheets_base->s[0].z_offset)), last_babystep); } - for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); ++i) - { - bool is_uninitialized = true; - for (uint_least8_t j = 0; j < (sizeof(Sheet::name)/sizeof(Sheet::name[0])); ++j) - { - if (!eeprom_is_uninitialized(&(EEPROM_Sheets_base->s[i].name[j]))) is_uninitialized = false; - } - if(is_uninitialized) - { - SheetName sheetName; - eeprom_default_sheet_name(i,sheetName); - - for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){ - eeprom_write(&(EEPROM_Sheets_base->s[i].name[a]), sheetName.c[a]); - } - } + // initialize the sheet names in eeprom + for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); i++) { + SheetName sheetName; + eeprom_default_sheet_name(i, sheetName); + eeprom_init_default_block(EEPROM_Sheets_base->s[i].name, (sizeof(Sheet::name)/sizeof(Sheet::name[0])), sheetName.c); } + if(!eeprom_is_sheet_initialized(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)))) { eeprom_switch_to_next_sheet(); @@ -95,16 +50,16 @@ void eeprom_init() check_babystep(); #ifdef PINDA_TEMP_COMP -if (eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_PINDA_TEMP_COMPENSATION, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION, 0); #endif //PINDA_TEMP_COMP - if (eeprom_read_dword((uint32_t*)EEPROM_JOB_ID) == EEPROM_EMPTY_VALUE32) - eeprom_update_dword((uint32_t*)EEPROM_JOB_ID, 0); + eeprom_init_default_dword((uint32_t*)EEPROM_JOB_ID, 0); + eeprom_init_default_dword((uint32_t*)EEPROM_TOTALTIME, 0); + eeprom_init_default_dword((uint32_t*)EEPROM_FILAMENTUSED, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED, 0); + + eeprom_init_default_byte((uint8_t*)EEPROM_HEAT_BED_ON_LOAD_FILAMENT, 1); - if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255) { - eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0); - eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0); - } } //! @brief Get default sheet name for index @@ -178,3 +133,97 @@ void eeprom_switch_to_next_sheet() sheet = eeprom_next_initialized_sheet(sheet); if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet); } + +bool __attribute__((noinline)) eeprom_is_sheet_initialized(uint8_t sheet_num) { + return (eeprom_read_word(reinterpret_cast(&(EEPROM_Sheets_base->s[sheet_num].z_offset))) != EEPROM_EMPTY_VALUE16); +} + + +bool __attribute__((noinline)) eeprom_is_initialized_block(const void *__p, size_t __n) { + const uint8_t *p = (const uint8_t*)__p; + while (__n--) { + if (eeprom_read_byte(p++) != EEPROM_EMPTY_VALUE) + return true; + } + return false; +} + +void eeprom_update_block_P(const void *__src, void *__dst, size_t __n) { + const uint8_t *src = (const uint8_t*)__src; + uint8_t *dst = (uint8_t*)__dst; + while (__n--) { + eeprom_update_byte(dst++, pgm_read_byte(src++)); + } +} + +void eeprom_toggle(uint8_t *__p) { + eeprom_write_byte(__p, !eeprom_read_byte(__p)); +} + +void __attribute__((noinline)) eeprom_increment_byte(uint8_t *__p) { + eeprom_write_byte(__p, eeprom_read_byte(__p) + 1); +} + +void __attribute__((noinline)) eeprom_increment_word(uint16_t *__p) { + eeprom_write_word(__p, eeprom_read_word(__p) + 1); +} + +void __attribute__((noinline)) eeprom_increment_dword(uint32_t *__p) { + eeprom_write_dword(__p, eeprom_read_dword(__p) + 1); +} + + +void __attribute__((noinline)) eeprom_add_byte(uint8_t *__p, uint8_t add) { + eeprom_write_byte(__p, eeprom_read_byte(__p) + add); +} + +void __attribute__((noinline)) eeprom_add_word(uint16_t *__p, uint16_t add) { + eeprom_write_word(__p, eeprom_read_word(__p) + add); +} + +void __attribute__((noinline)) eeprom_add_dword(uint32_t *__p, uint32_t add) { + eeprom_write_dword(__p, eeprom_read_dword(__p) + add); +} + + +uint8_t __attribute__((noinline)) eeprom_init_default_byte(uint8_t *__p, uint8_t def) { + uint8_t val = eeprom_read_byte(__p); + if (val == EEPROM_EMPTY_VALUE) { + eeprom_write_byte(__p, def); + return def; + } + return val; +} + +uint16_t __attribute__((noinline)) eeprom_init_default_word(uint16_t *__p, uint16_t def) { + uint16_t val = eeprom_read_word(__p); + if (val == EEPROM_EMPTY_VALUE16) { + eeprom_write_word(__p, def); + return def; + } + return val; +} + +uint32_t __attribute__((noinline)) eeprom_init_default_dword(uint32_t *__p, uint32_t def) { + uint32_t val = eeprom_read_dword(__p); + if (val == EEPROM_EMPTY_VALUE32) { + eeprom_write_dword(__p, def); + return def; + } + return val; +} + +void __attribute__((noinline)) eeprom_init_default_float(float *__p, float def) { + if (eeprom_read_dword((uint32_t*)__p) == EEPROM_EMPTY_VALUE32) + eeprom_write_float(__p, def); +} + +void __attribute__((noinline)) eeprom_init_default_block(void *__p, size_t __n, const void *def) { + if (!eeprom_is_initialized_block(__p, __n)) + eeprom_update_block(def, __p, __n); +} + +void __attribute__((noinline)) eeprom_init_default_block_P(void *__p, size_t __n, const void *def) { + if (!eeprom_is_initialized_block(__p, __n)) + eeprom_update_block_P(def, __p, __n); +} diff --git a/Firmware/eeprom.h b/Firmware/eeprom.h index 0761ed3b4..5efcf6eeb 100644 --- a/Firmware/eeprom.h +++ b/Firmware/eeprom.h @@ -620,7 +620,6 @@ enum #ifdef __cplusplus void eeprom_init(); -bool eeprom_is_sheet_initialized(uint8_t sheet_num); struct SheetName { char c[sizeof(Sheet::name) + 1]; @@ -628,6 +627,26 @@ struct SheetName void eeprom_default_sheet_name(uint8_t index, SheetName &sheetName); int8_t eeprom_next_initialized_sheet(int8_t sheet); void eeprom_switch_to_next_sheet(); +bool eeprom_is_sheet_initialized(uint8_t sheet_num); + +bool eeprom_is_initialized_block(const void *__p, size_t __n); +void eeprom_update_block_P(const void *__src, void *__dst, size_t __n); +void eeprom_toggle(uint8_t *__p); + +void eeprom_increment_byte(uint8_t *__p); +void eeprom_increment_word(uint16_t *__p); +void eeprom_increment_dword(uint32_t *__p); + +void eeprom_add_byte(uint8_t *__p, uint8_t add); +void eeprom_add_word(uint16_t *__p, uint16_t add); +void eeprom_add_dword(uint32_t *__p, uint32_t add); + +uint8_t eeprom_init_default_byte(uint8_t *__p, uint8_t def); +uint16_t eeprom_init_default_word(uint16_t *__p, uint16_t def); +uint32_t eeprom_init_default_dword(uint32_t *__p, uint32_t def); +void eeprom_init_default_float(float *__p, float def); +void eeprom_init_default_block(void *__p, size_t __n, const void *def); +void eeprom_init_default_block_P(void *__p, size_t __n, const void *def); #endif #endif // EEPROM_H