eeprom.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 eeprom_is_sheet_initialized(uint8_t sheet_num)
  33. {
  34. return (0xffff != eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base->
  35. s[sheet_num].z_offset))));
  36. }
  37. void eeprom_init()
  38. {
  39. if (eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_POWER_COUNT, 0);
  40. if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0);
  41. if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0);
  42. if (eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_FERROR_COUNT, 0);
  43. if (eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0);
  44. if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0);
  45. if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0);
  46. if (eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0);
  47. if (eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0);
  48. if (eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0);
  49. if (eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
  50. if (eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
  51. if (eeprom_read_dword((uint32_t*)EEPROM_TOTAL_TOOLCHANGE_COUNT) == 0xffffffff) eeprom_update_dword((uint32_t *)EEPROM_TOTAL_TOOLCHANGE_COUNT, 0);
  52. if (eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)) == EEPROM_EMPTY_VALUE)
  53. {
  54. eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), 0);
  55. // When upgrading from version older version (before multiple sheets were implemented in v3.8.0)
  56. // Sheet 1 uses the previous Live adjust Z (@EEPROM_BABYSTEP_Z)
  57. int last_babystep = eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z);
  58. eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[0].z_offset)), last_babystep);
  59. }
  60. for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); ++i)
  61. {
  62. bool is_uninitialized = true;
  63. for (uint_least8_t j = 0; j < (sizeof(Sheet::name)/sizeof(Sheet::name[0])); ++j)
  64. {
  65. if (!eeprom_is_uninitialized(&(EEPROM_Sheets_base->s[i].name[j]))) is_uninitialized = false;
  66. }
  67. if(is_uninitialized)
  68. {
  69. SheetName sheetName;
  70. eeprom_default_sheet_name(i,sheetName);
  71. for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){
  72. eeprom_write(&(EEPROM_Sheets_base->s[i].name[a]), sheetName.c[a]);
  73. }
  74. }
  75. }
  76. if(!eeprom_is_sheet_initialized(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet))))
  77. {
  78. eeprom_switch_to_next_sheet();
  79. }
  80. check_babystep();
  81. #ifdef PINDA_TEMP_COMP
  82. if (eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_PINDA_TEMP_COMPENSATION, 0);
  83. #endif //PINDA_TEMP_COMP
  84. if (eeprom_read_dword((uint32_t*)EEPROM_JOB_ID) == EEPROM_EMPTY_VALUE32)
  85. eeprom_update_dword((uint32_t*)EEPROM_JOB_ID, 0);
  86. 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) {
  87. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  88. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  89. }
  90. //Set Cutter OFF if 0xff
  91. if (eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_CUTTER_ENABLED, 0);
  92. }
  93. //! @brief Get default sheet name for index
  94. //!
  95. //! | index | sheetName |
  96. //! | ----- | --------- |
  97. //! | 0 | Smooth1 |
  98. //! | 1 | Smooth2 |
  99. //! | 2 | Textur1 |
  100. //! | 3 | Textur2 |
  101. //! | 4 | Satin |
  102. //! | 5 | NylonPA |
  103. //! | 6 | Custom1 |
  104. //! | 7 | Custom2 |
  105. //!
  106. //! @param[in] index
  107. //! @param[out] sheetName
  108. void eeprom_default_sheet_name(uint8_t index, SheetName &sheetName)
  109. {
  110. static_assert(8 == sizeof(SheetName),"Default sheet name needs to be adjusted.");
  111. if (index < 2)
  112. {
  113. strcpy_P(sheetName.c, PSTR("Smooth"));
  114. }
  115. else if (index < 4)
  116. {
  117. strcpy_P(sheetName.c, PSTR("Textur"));
  118. }
  119. else if (index < 5)
  120. {
  121. strcpy_P(sheetName.c, PSTR("Satin "));
  122. }
  123. else if (index < 6)
  124. {
  125. strcpy_P(sheetName.c, PSTR("NylonPA"));
  126. }
  127. else
  128. {
  129. strcpy_P(sheetName.c, PSTR("Custom"));
  130. }
  131. if (index <4 || index >5)
  132. {
  133. sheetName.c[6] = '0' + ((index % 2)+1);
  134. sheetName.c[7] = '\0';
  135. }
  136. }
  137. //! @brief Get next initialized sheet
  138. //!
  139. //! If current sheet is the only sheet initialized, current sheet is returned.
  140. //!
  141. //! @param sheet Current sheet
  142. //! @return next initialized sheet
  143. //! @retval -1 no sheet is initialized
  144. int8_t eeprom_next_initialized_sheet(int8_t sheet)
  145. {
  146. for (int8_t i = 0; i < static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet)); ++i)
  147. {
  148. ++sheet;
  149. if (sheet >= static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet))) sheet = 0;
  150. if (eeprom_is_sheet_initialized(sheet)) return sheet;
  151. }
  152. return -1;
  153. }
  154. void eeprom_switch_to_next_sheet()
  155. {
  156. int8_t sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet));
  157. sheet = eeprom_next_initialized_sheet(sheet);
  158. if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet);
  159. }