eeprom.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. void eeprom_init()
  10. {
  11. eeprom_init_default_byte((uint8_t*)EEPROM_POWER_COUNT, 0);
  12. eeprom_init_default_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0);
  13. eeprom_init_default_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0);
  14. eeprom_init_default_byte((uint8_t*)EEPROM_FERROR_COUNT, 0);
  15. eeprom_init_default_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0);
  16. eeprom_init_default_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0);
  17. eeprom_init_default_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0);
  18. eeprom_init_default_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0);
  19. eeprom_init_default_word((uint16_t*)EEPROM_MMU_FAIL_TOT, 0);
  20. eeprom_init_default_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, 0);
  21. eeprom_init_default_byte((uint8_t*)EEPROM_MMU_FAIL, 0);
  22. eeprom_init_default_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, 0);
  23. if (eeprom_read_dword((uint32_t*)EEPROM_TOTAL_TOOLCHANGE_COUNT) == 0xffffffff) eeprom_update_dword((uint32_t *)EEPROM_TOTAL_TOOLCHANGE_COUNT, 0);
  24. if (eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)) == EEPROM_EMPTY_VALUE)
  25. {
  26. eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), 0);
  27. // When upgrading from version older version (before multiple sheets were implemented in v3.8.0)
  28. // Sheet 1 uses the previous Live adjust Z (@EEPROM_BABYSTEP_Z)
  29. int last_babystep = eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z);
  30. eeprom_update_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[0].z_offset)), last_babystep);
  31. }
  32. // initialize the sheet names in eeprom
  33. for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); i++) {
  34. SheetName sheetName;
  35. eeprom_default_sheet_name(i, sheetName);
  36. eeprom_init_default_block(EEPROM_Sheets_base->s[i].name, (sizeof(Sheet::name)/sizeof(Sheet::name[0])), sheetName.c);
  37. }
  38. if(!eeprom_is_sheet_initialized(eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet))))
  39. {
  40. eeprom_switch_to_next_sheet();
  41. }
  42. check_babystep();
  43. #ifdef PINDA_TEMP_COMP
  44. if (eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_PINDA_TEMP_COMPENSATION, 0);
  45. #endif //PINDA_TEMP_COMP
  46. if (eeprom_read_dword((uint32_t*)EEPROM_JOB_ID) == EEPROM_EMPTY_VALUE32)
  47. eeprom_update_dword((uint32_t*)EEPROM_JOB_ID, 0);
  48. if (eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME) == 0xffffffff) {
  49. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  50. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  51. }
  52. //Set Cutter OFF if 0xff
  53. eeprom_init_default_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED, 0);
  54. }
  55. //! @brief Get default sheet name for index
  56. //!
  57. //! | index | sheetName |
  58. //! | ----- | --------- |
  59. //! | 0 | Smooth1 |
  60. //! | 1 | Smooth2 |
  61. //! | 2 | Textur1 |
  62. //! | 3 | Textur2 |
  63. //! | 4 | Satin |
  64. //! | 5 | NylonPA |
  65. //! | 6 | Custom1 |
  66. //! | 7 | Custom2 |
  67. //!
  68. //! @param[in] index
  69. //! @param[out] sheetName
  70. void eeprom_default_sheet_name(uint8_t index, SheetName &sheetName)
  71. {
  72. static_assert(8 == sizeof(SheetName),"Default sheet name needs to be adjusted.");
  73. if (index < 2)
  74. {
  75. strcpy_P(sheetName.c, PSTR("Smooth"));
  76. }
  77. else if (index < 4)
  78. {
  79. strcpy_P(sheetName.c, PSTR("Textur"));
  80. }
  81. else if (index < 5)
  82. {
  83. strcpy_P(sheetName.c, PSTR("Satin "));
  84. }
  85. else if (index < 6)
  86. {
  87. strcpy_P(sheetName.c, PSTR("NylonPA"));
  88. }
  89. else
  90. {
  91. strcpy_P(sheetName.c, PSTR("Custom"));
  92. }
  93. if (index <4 || index >5)
  94. {
  95. sheetName.c[6] = '0' + ((index % 2)+1);
  96. sheetName.c[7] = '\0';
  97. }
  98. }
  99. //! @brief Get next initialized sheet
  100. //!
  101. //! If current sheet is the only sheet initialized, current sheet is returned.
  102. //!
  103. //! @param sheet Current sheet
  104. //! @return next initialized sheet
  105. //! @retval -1 no sheet is initialized
  106. int8_t eeprom_next_initialized_sheet(int8_t sheet)
  107. {
  108. for (int8_t i = 0; i < static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet)); ++i)
  109. {
  110. ++sheet;
  111. if (sheet >= static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet))) sheet = 0;
  112. if (eeprom_is_sheet_initialized(sheet)) return sheet;
  113. }
  114. return -1;
  115. }
  116. void eeprom_switch_to_next_sheet()
  117. {
  118. int8_t sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet));
  119. sheet = eeprom_next_initialized_sheet(sheet);
  120. if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet);
  121. }
  122. bool __attribute__((noinline)) eeprom_is_sheet_initialized(uint8_t sheet_num) {
  123. return (eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base->s[sheet_num].z_offset))) != EEPROM_EMPTY_VALUE16);
  124. }
  125. bool __attribute__((noinline)) eeprom_is_initialized_block(const void *__p, size_t __n) {
  126. const uint8_t *p = (const uint8_t*)__p;
  127. while (__n--) {
  128. if (eeprom_read_byte(p++) != EEPROM_EMPTY_VALUE)
  129. return true;
  130. }
  131. return false;
  132. }
  133. void eeprom_update_block_P(const void *__src, void *__dst, size_t __n) {
  134. const uint8_t *src = (const uint8_t*)__src;
  135. uint8_t *dst = (uint8_t*)__dst;
  136. while (__n--) {
  137. eeprom_update_byte(dst++, pgm_read_byte(src++));
  138. }
  139. }
  140. void __attribute__((noinline)) eeprom_increment_byte(uint8_t *__p) {
  141. eeprom_write_byte(__p, eeprom_read_byte(__p) + 1);
  142. }
  143. void __attribute__((noinline)) eeprom_increment_word(uint16_t *__p) {
  144. eeprom_write_word(__p, eeprom_read_word(__p) + 1);
  145. }
  146. void __attribute__((noinline)) eeprom_increment_dword(uint32_t *__p) {
  147. eeprom_write_dword(__p, eeprom_read_dword(__p) + 1);
  148. }
  149. void __attribute__((noinline)) eeprom_add_byte(uint8_t *__p, uint8_t add) {
  150. eeprom_write_byte(__p, eeprom_read_byte(__p) + add);
  151. }
  152. void __attribute__((noinline)) eeprom_add_word(uint16_t *__p, uint16_t add) {
  153. eeprom_write_word(__p, eeprom_read_word(__p) + add);
  154. }
  155. void __attribute__((noinline)) eeprom_add_dword(uint32_t *__p, uint32_t add) {
  156. eeprom_write_dword(__p, eeprom_read_dword(__p) + add);
  157. }
  158. void __attribute__((noinline)) eeprom_init_default_byte(uint8_t *__p, uint8_t def) {
  159. if (eeprom_read_byte(__p) == EEPROM_EMPTY_VALUE)
  160. eeprom_write_byte(__p, def);
  161. }
  162. void __attribute__((noinline)) eeprom_init_default_word(uint16_t *__p, uint16_t def) {
  163. if (eeprom_read_word(__p) == EEPROM_EMPTY_VALUE16)
  164. eeprom_write_word(__p, def);
  165. }
  166. void __attribute__((noinline)) eeprom_init_default_dword(uint32_t *__p, uint32_t def) {
  167. if (eeprom_read_dword(__p) == EEPROM_EMPTY_VALUE32)
  168. eeprom_write_dword(__p, def);
  169. }
  170. void __attribute__((noinline)) eeprom_init_default_float(float *__p, float def) {
  171. if (eeprom_read_dword((uint32_t*)__p) == EEPROM_EMPTY_VALUE32)
  172. eeprom_write_float(__p, def);
  173. }
  174. void __attribute__((noinline)) eeprom_init_default_block(void *__p, size_t __n, const void *def) {
  175. if (!eeprom_is_initialized_block(__p, __n))
  176. eeprom_update_block(def, __p, __n);
  177. }
  178. void __attribute__((noinline)) eeprom_init_default_block_P(void *__p, size_t __n, const void *def) {
  179. if (!eeprom_is_initialized_block(__p, __n))
  180. eeprom_update_block_P(def, __p, __n);
  181. }