eeprom.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //! @file
  2. //! @date Jun 20, 2019
  3. //! @author Marek Běl
  4. #include "eeprom.h"
  5. #include "Marlin.h"
  6. #include <avr/eeprom.h>
  7. #include <stdint.h>
  8. #include "language.h"
  9. #if 0
  10. template <typename T>
  11. static T eeprom_read(T *address);
  12. template<>
  13. char eeprom_read<char>(char *address)
  14. {
  15. return eeprom_read_byte(reinterpret_cast<uint8_t*>(address));
  16. }
  17. #endif
  18. template <typename T>
  19. static void eeprom_write(T *address, T value);
  20. template<>
  21. void eeprom_write<char>(char *addres, char value)
  22. {
  23. eeprom_write_byte(reinterpret_cast<uint8_t*>(addres), static_cast<uint8_t>(value));
  24. }
  25. template <typename T>
  26. static bool eeprom_is_uninitialized(T *address);
  27. template <>
  28. bool eeprom_is_uninitialized<char>(char *address)
  29. {
  30. return (0xff == eeprom_read_byte(reinterpret_cast<uint8_t*>(address)));
  31. }
  32. bool is_sheet_initialized(uint8_t sheet_num){
  33. return (0xffff != eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base->
  34. s[sheet_num].z_offset))));
  35. }
  36. void eeprom_init()
  37. {
  38. if (eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_POWER_COUNT, 0);
  39. if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0);
  40. if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0);
  41. if (eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_FERROR_COUNT, 0);
  42. if (eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0);
  43. if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0);
  44. if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0);
  45. if (eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0);
  46. if (eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0);
  47. if (eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0);
  48. if (eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
  49. if (eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
  50. if (eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)) == 0xff) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), 0);
  51. for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); ++i)
  52. {
  53. bool is_uninitialized = true;
  54. for (uint_least8_t j = 0; j < (sizeof(Sheet::name)/sizeof(Sheet::name[0])); ++j)
  55. {
  56. if (!eeprom_is_uninitialized(&(EEPROM_Sheets_base->s[i].name[j]))) is_uninitialized = false;
  57. }
  58. if(is_uninitialized)
  59. {
  60. char sheet_PROGMEM_buffer[8];
  61. strcpy_P(sheet_PROGMEM_buffer, (char *)pgm_read_word(&(defaultSheetNames[i])));
  62. for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){
  63. eeprom_write(&(EEPROM_Sheets_base->s[i].name[a]), sheet_PROGMEM_buffer[a]);
  64. }
  65. // When upgrading from version older version (before multiple sheets were implemented in v3.8.0)
  66. // Sheet 1 uses the previous Live adjust Z (@EEPROM_BABYSTEP_Z)
  67. if(i == 0){
  68. int last_babystep = eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z);
  69. eeprom_write_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[i].z_offset)), last_babystep);
  70. }
  71. }
  72. }
  73. check_babystep();
  74. }