eeprom.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. eeprom_init_default_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. eeprom_init_default_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION, 0);
  45. #endif //PINDA_TEMP_COMP
  46. eeprom_init_default_dword((uint32_t*)EEPROM_JOB_ID, 0);
  47. eeprom_init_default_dword((uint32_t*)EEPROM_TOTALTIME, 0);
  48. eeprom_init_default_dword((uint32_t*)EEPROM_FILAMENTUSED, 0);
  49. eeprom_init_default_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED, 0);
  50. eeprom_init_default_byte((uint8_t*)EEPROM_HEAT_BED_ON_LOAD_FILAMENT, 1);
  51. }
  52. //! @brief Get default sheet name for index
  53. //!
  54. //! | index | sheetName |
  55. //! | ----- | --------- |
  56. //! | 0 | Smooth1 |
  57. //! | 1 | Smooth2 |
  58. //! | 2 | Textur1 |
  59. //! | 3 | Textur2 |
  60. //! | 4 | Satin |
  61. //! | 5 | NylonPA |
  62. //! | 6 | Custom1 |
  63. //! | 7 | Custom2 |
  64. //!
  65. //! @param[in] index
  66. //! @param[out] sheetName
  67. void eeprom_default_sheet_name(uint8_t index, SheetName &sheetName)
  68. {
  69. static_assert(8 == sizeof(SheetName),"Default sheet name needs to be adjusted.");
  70. if (index < 2)
  71. {
  72. strcpy_P(sheetName.c, PSTR("Smooth"));
  73. }
  74. else if (index < 4)
  75. {
  76. strcpy_P(sheetName.c, PSTR("Textur"));
  77. }
  78. else if (index < 5)
  79. {
  80. strcpy_P(sheetName.c, PSTR("Satin "));
  81. }
  82. else if (index < 6)
  83. {
  84. strcpy_P(sheetName.c, PSTR("NylonPA"));
  85. }
  86. else
  87. {
  88. strcpy_P(sheetName.c, PSTR("Custom"));
  89. }
  90. if (index <4 || index >5)
  91. {
  92. sheetName.c[6] = '0' + ((index % 2)+1);
  93. sheetName.c[7] = '\0';
  94. }
  95. }
  96. //! @brief Get next initialized sheet
  97. //!
  98. //! If current sheet is the only sheet initialized, current sheet is returned.
  99. //!
  100. //! @param sheet Current sheet
  101. //! @return next initialized sheet
  102. //! @retval -1 no sheet is initialized
  103. int8_t eeprom_next_initialized_sheet(int8_t sheet)
  104. {
  105. for (int8_t i = 0; i < static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet)); ++i)
  106. {
  107. ++sheet;
  108. if (sheet >= static_cast<int8_t>(sizeof(Sheets::s)/sizeof(Sheet))) sheet = 0;
  109. if (eeprom_is_sheet_initialized(sheet)) return sheet;
  110. }
  111. return -1;
  112. }
  113. void eeprom_switch_to_next_sheet()
  114. {
  115. int8_t sheet = eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet));
  116. sheet = eeprom_next_initialized_sheet(sheet);
  117. if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet);
  118. }
  119. bool __attribute__((noinline)) eeprom_is_sheet_initialized(uint8_t sheet_num) {
  120. return (eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base->s[sheet_num].z_offset))) != EEPROM_EMPTY_VALUE16);
  121. }
  122. bool __attribute__((noinline)) eeprom_is_initialized_block(const void *__p, size_t __n) {
  123. const uint8_t *p = (const uint8_t*)__p;
  124. while (__n--) {
  125. if (eeprom_read_byte(p++) != EEPROM_EMPTY_VALUE)
  126. return true;
  127. }
  128. return false;
  129. }
  130. void eeprom_update_block_P(const void *__src, void *__dst, size_t __n) {
  131. const uint8_t *src = (const uint8_t*)__src;
  132. uint8_t *dst = (uint8_t*)__dst;
  133. while (__n--) {
  134. eeprom_update_byte(dst++, pgm_read_byte(src++));
  135. }
  136. }
  137. void eeprom_toggle(uint8_t *__p) {
  138. eeprom_write_byte(__p, !eeprom_read_byte(__p));
  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. uint8_t __attribute__((noinline)) eeprom_init_default_byte(uint8_t *__p, uint8_t def) {
  159. uint8_t val = eeprom_read_byte(__p);
  160. if (val == EEPROM_EMPTY_VALUE) {
  161. eeprom_write_byte(__p, def);
  162. return def;
  163. }
  164. return val;
  165. }
  166. uint16_t __attribute__((noinline)) eeprom_init_default_word(uint16_t *__p, uint16_t def) {
  167. uint16_t val = eeprom_read_word(__p);
  168. if (val == EEPROM_EMPTY_VALUE16) {
  169. eeprom_write_word(__p, def);
  170. return def;
  171. }
  172. return val;
  173. }
  174. uint32_t __attribute__((noinline)) eeprom_init_default_dword(uint32_t *__p, uint32_t def) {
  175. uint32_t val = eeprom_read_dword(__p);
  176. if (val == EEPROM_EMPTY_VALUE32) {
  177. eeprom_write_dword(__p, def);
  178. return def;
  179. }
  180. return val;
  181. }
  182. void __attribute__((noinline)) eeprom_init_default_float(float *__p, float def) {
  183. if (eeprom_read_dword((uint32_t*)__p) == EEPROM_EMPTY_VALUE32)
  184. eeprom_write_float(__p, def);
  185. }
  186. void __attribute__((noinline)) eeprom_init_default_block(void *__p, size_t __n, const void *def) {
  187. if (!eeprom_is_initialized_block(__p, __n))
  188. eeprom_update_block(def, __p, __n);
  189. }
  190. void __attribute__((noinline)) eeprom_init_default_block_P(void *__p, size_t __n, const void *def) {
  191. if (!eeprom_is_initialized_block(__p, __n))
  192. eeprom_update_block_P(def, __p, __n);
  193. }