ConfigurationStore.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "Marlin.h"
  2. #include "planner.h"
  3. #include "temperature.h"
  4. #include "ultralcd.h"
  5. #include "ConfigurationStore.h"
  6. #include "Configuration_prusa.h"
  7. #ifdef MESH_BED_LEVELING
  8. #include "mesh_bed_leveling.h"
  9. #endif
  10. M500_conf cs;
  11. #ifdef DEBUG_EEPROM_WRITE
  12. #define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value), #value)
  13. #else //DEBUG_EEPROM_WRITE
  14. #define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value), 0)
  15. #endif //DEBUG_EEPROM_WRITE
  16. void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size, char* name)
  17. {
  18. #ifdef DEBUG_EEPROM_WRITE
  19. printf_P(PSTR("EEPROM_WRITE_VAR addr=0x%04x size=0x%02hhx name=%s\n"), pos, size, name);
  20. #endif //DEBUG_EEPROM_WRITE
  21. while (size--) {
  22. uint8_t * const p = (uint8_t * const)pos;
  23. uint8_t v = *value;
  24. // EEPROM has only ~100,000 write cycles,
  25. // so only write bytes that have changed!
  26. if (v != eeprom_read_byte(p)) {
  27. eeprom_write_byte(p, v);
  28. if (eeprom_read_byte(p) != v) {
  29. SERIAL_ECHOLNPGM("EEPROM Error");
  30. return;
  31. }
  32. }
  33. pos++;
  34. value++;
  35. };
  36. }
  37. #ifdef DEBUG_EEPROM_READ
  38. #define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value), #value)
  39. #else //DEBUG_EEPROM_READ
  40. #define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value), 0)
  41. #endif //DEBUG_EEPROM_READ
  42. void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size, char* name)
  43. {
  44. #ifdef DEBUG_EEPROM_READ
  45. printf_P(PSTR("EEPROM_READ_VAR addr=0x%04x size=0x%02hhx name=%s\n"), pos, size, name);
  46. #endif //DEBUG_EEPROM_READ
  47. do
  48. {
  49. *value = eeprom_read_byte((unsigned char*)pos);
  50. pos++;
  51. value++;
  52. }while(--size);
  53. }
  54. //======================================================================================
  55. #define EEPROM_OFFSET 20
  56. // IMPORTANT: Whenever there are changes made to the variables stored in EEPROM
  57. // in the functions below, also increment the version number. This makes sure that
  58. // the default values are used whenever there is a change to the data, to prevent
  59. // wrong data being written to the variables.
  60. // ALSO: always make sure the variables in the Store and retrieve sections are in the same order.
  61. #define EEPROM_VERSION "V2"
  62. #ifdef EEPROM_SETTINGS
  63. void Config_StoreSettings(uint16_t offset)
  64. {
  65. int i = offset;
  66. strcpy(cs.version,"000"); //!< invalidate data first @TODO use erase to save one erase cycle
  67. _EEPROM_writeData(i,reinterpret_cast<uint8_t*>(&cs),sizeof(cs),0);
  68. strcpy(cs.version,EEPROM_VERSION); // // validate data
  69. i = offset;
  70. EEPROM_WRITE_VAR(i,cs.version); // validate data
  71. SERIAL_ECHO_START;
  72. SERIAL_ECHOLNPGM("Settings Stored");
  73. }
  74. #endif //EEPROM_SETTINGS
  75. #ifndef DISABLE_M503
  76. void Config_PrintSettings(uint8_t level)
  77. { // Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown
  78. #ifdef TMC2130
  79. printf_P(PSTR(
  80. "%SSteps per unit:\n%S M92 X%.2f Y%.2f Z%.2f E%.2f\n"
  81. "%SMaximum feedrates - normal (mm/s):\n%S M203 X%.2f Y%.2f Z%.2f E%.2f\n"
  82. "%SMaximum feedrates - stealth (mm/s):\n%S M203 X%.2f Y%.2f Z%.2f E%.2f\n"
  83. "%SMaximum acceleration - normal (mm/s2):\n%S M201 X%lu Y%lu Z%lu E%lu\n"
  84. "%SMaximum acceleration - stealth (mm/s2):\n%S M201 X%lu Y%lu Z%lu E%lu\n"
  85. "%SAcceleration: S=acceleration, T=retract acceleration\n%S M204 S%.2f T%.2f\n"
  86. "%SAdvanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum XY jerk (mm/s), Z=maximum Z jerk (mm/s), E=maximum E jerk (mm/s)\n%S M205 S%.2f T%.2f B%.2f X%.2f Y%.2f Z%.2f E%.2f\n"
  87. "%SHome offset (mm):\n%S M206 X%.2f Y%.2f Z%.2f\n"
  88. ),
  89. echomagic, echomagic, cs.axis_steps_per_unit[X_AXIS], cs.axis_steps_per_unit[Y_AXIS], cs.axis_steps_per_unit[Z_AXIS], cs.axis_steps_per_unit[E_AXIS],
  90. echomagic, echomagic, cs.max_feedrate_normal[X_AXIS], cs.max_feedrate_normal[Y_AXIS], cs.max_feedrate_normal[Z_AXIS], cs.max_feedrate_normal[E_AXIS],
  91. echomagic, echomagic, max_feedrate_silent[X_AXIS], max_feedrate_silent[Y_AXIS], max_feedrate_silent[Z_AXIS], max_feedrate_silent[E_AXIS],
  92. echomagic, echomagic, max_acceleration_units_per_sq_second_normal[X_AXIS], max_acceleration_units_per_sq_second_normal[Y_AXIS], max_acceleration_units_per_sq_second_normal[Z_AXIS], max_acceleration_units_per_sq_second_normal[E_AXIS],
  93. echomagic, echomagic, max_acceleration_units_per_sq_second_silent[X_AXIS], max_acceleration_units_per_sq_second_silent[Y_AXIS], max_acceleration_units_per_sq_second_silent[Z_AXIS], max_acceleration_units_per_sq_second_silent[E_AXIS],
  94. echomagic, echomagic, acceleration, retract_acceleration,
  95. echomagic, echomagic, minimumfeedrate, mintravelfeedrate, minsegmenttime, max_jerk[X_AXIS], max_jerk[Y_AXIS], max_jerk[Z_AXIS], max_jerk[E_AXIS],
  96. echomagic, echomagic, add_homing[X_AXIS], add_homing[Y_AXIS], add_homing[Z_AXIS]
  97. #else //TMC2130
  98. printf_P(PSTR(
  99. "%SSteps per unit:\n%S M92 X%.2f Y%.2f Z%.2f E%.2f\n"
  100. "%SMaximum feedrates (mm/s):\n%S M203 X%.2f Y%.2f Z%.2f E%.2f\n"
  101. "%SMaximum acceleration (mm/s2):\n%S M201 X%lu Y%lu Z%lu E%lu\n"
  102. "%SAcceleration: S=acceleration, T=retract acceleration\n%S M204 S%.2f T%.2f\n"
  103. "%SAdvanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum XY jerk (mm/s), Z=maximum Z jerk (mm/s), E=maximum E jerk (mm/s)\n%S M205 S%.2f T%.2f B%.2f X%.2f Y%.2f Z%.2f E%.2f\n"
  104. "%SHome offset (mm):\n%S M206 X%.2f Y%.2f Z%.2f\n"
  105. ),
  106. echomagic, echomagic, cs.axis_steps_per_unit[X_AXIS], cs.axis_steps_per_unit[Y_AXIS], cs.axis_steps_per_unit[Z_AXIS], cs.axis_steps_per_unit[E_AXIS],
  107. echomagic, echomagic, max_feedrate[X_AXIS], max_feedrate[Y_AXIS], max_feedrate[Z_AXIS], max_feedrate[E_AXIS],
  108. echomagic, echomagic, max_acceleration_units_per_sq_second[X_AXIS], max_acceleration_units_per_sq_second[Y_AXIS], max_acceleration_units_per_sq_second[Z_AXIS], max_acceleration_units_per_sq_second[E_AXIS],
  109. echomagic, echomagic, acceleration, retract_acceleration,
  110. echomagic, echomagic, minimumfeedrate, mintravelfeedrate, minsegmenttime, max_jerk[X_AXIS], max_jerk[Y_AXIS], max_jerk[Z_AXIS], max_jerk[E_AXIS],
  111. echomagic, echomagic, add_homing[X_AXIS], add_homing[Y_AXIS], add_homing[Z_AXIS]
  112. #endif //TMC2130
  113. );
  114. #ifdef PIDTEMP
  115. printf_P(PSTR("%SPID settings:\n%S M301 P%.2f I%.2f D%.2f\n"),
  116. echomagic, echomagic, Kp, unscalePID_i(Ki), unscalePID_d(Kd));
  117. #endif
  118. #ifdef PIDTEMPBED
  119. printf_P(PSTR("%SPID heatbed settings:\n%S M304 P%.2f I%.2f D%.2f\n"),
  120. echomagic, echomagic, bedKp, unscalePID_i(bedKi), unscalePID_d(bedKd));
  121. #endif
  122. #ifdef FWRETRACT
  123. printf_P(PSTR(
  124. "%SRetract: S=Length (mm) F:Speed (mm/m) Z: ZLift (mm)\n%S M207 S%.2f F%.2f Z%.2f\n"
  125. "%SRecover: S=Extra length (mm) F:Speed (mm/m)\n%S M208 S%.2f F%.2f\n"
  126. "%SAuto-Retract: S=0 to disable, 1 to interpret extrude-only moves as retracts or recoveries\n%S M209 S%d\n"
  127. ),
  128. echomagic, echomagic, retract_length, retract_feedrate*60, retract_zlift,
  129. echomagic, echomagic, retract_recover_length, retract_recover_feedrate*60,
  130. echomagic, echomagic, (autoretract_enabled ? 1 : 0)
  131. );
  132. #if EXTRUDERS > 1
  133. printf_P(PSTR("%SMulti-extruder settings:\n%S Swap retract length (mm): %.2f\n%S Swap rec. addl. length (mm): %.2f\n"),
  134. echomagic, echomagic, retract_length_swap, echomagic, retract_recover_length_swap);
  135. #endif
  136. if (volumetric_enabled) {
  137. printf_P(PSTR("%SFilament settings:\n%S M200 D%.2f\n"),
  138. echomagic, echomagic, filament_size[0]);
  139. #if EXTRUDERS > 1
  140. printf_P(PSTR("%S M200 T1 D%.2f\n"),
  141. echomagic, echomagic, filament_size[1]);
  142. #if EXTRUDERS > 2
  143. printf_P(PSTR("%S M200 T1 D%.2f\n"),
  144. echomagic, echomagic, filament_size[2]);
  145. #endif
  146. #endif
  147. } else {
  148. printf_P(PSTR("%SFilament settings: Disabled\n"), echomagic);
  149. }
  150. #endif
  151. if (level >= 10) {
  152. #ifdef LIN_ADVANCE
  153. printf_P(PSTR("%SLinear advance settings:\n M900 K%.2f E/D = %.2f\n"),
  154. echomagic, extruder_advance_k, advance_ed_ratio);
  155. #endif //LIN_ADVANCE
  156. }
  157. }
  158. #endif
  159. #ifdef EEPROM_SETTINGS
  160. static_assert (EXTRUDERS == 1, "ConfigurationStore M500_conf not implemented for more extruders.");
  161. static_assert (NUM_AXIS == 4, "ConfigurationStore M500_conf not implemented for more axis.");
  162. #ifdef ENABLE_AUTO_BED_LEVELING
  163. static_assert (false, "zprobe_zoffset was not initialized in printers in field to -(Z_PROBE_OFFSET_FROM_EXTRUDER), so it contains"
  164. "0.0, if this is not acceptable, increment EEPROM_VERSION to force use default_conf");
  165. #endif
  166. static const M500_conf default_conf PROGMEM =
  167. {
  168. EEPROM_VERSION,
  169. DEFAULT_AXIS_STEPS_PER_UNIT,
  170. DEFAULT_MAX_FEEDRATE,
  171. DEFAULT_MAX_ACCELERATION,
  172. DEFAULT_ACCELERATION,
  173. DEFAULT_RETRACT_ACCELERATION,
  174. DEFAULT_MINIMUMFEEDRATE,
  175. DEFAULT_MINTRAVELFEEDRATE,
  176. DEFAULT_MINSEGMENTTIME,
  177. {DEFAULT_XJERK, DEFAULT_YJERK, DEFAULT_ZJERK, DEFAULT_EJERK},
  178. {0,0,0},
  179. -(Z_PROBE_OFFSET_FROM_EXTRUDER),
  180. DEFAULT_Kp,
  181. DEFAULT_Ki*PID_dT,
  182. DEFAULT_Kd/PID_dT,
  183. DEFAULT_bedKp,
  184. DEFAULT_bedKi*PID_dT,
  185. DEFAULT_bedKd/PID_dT,
  186. 0,
  187. false,
  188. RETRACT_LENGTH,
  189. RETRACT_FEEDRATE,
  190. RETRACT_ZLIFT,
  191. RETRACT_RECOVER_LENGTH,
  192. RETRACT_RECOVER_FEEDRATE,
  193. false,
  194. {DEFAULT_NOMINAL_FILAMENT_DIA},
  195. DEFAULT_MAX_FEEDRATE_SILENT,
  196. DEFAULT_MAX_ACCELERATION_SILENT,
  197. };
  198. static_assert (sizeof(M500_conf) == 188, "sizeof(M500_conf) has changed, ensure that version has been incremented, "
  199. "or if you added members in the end of struct, ensure that historically uninitialized values will be initialized");
  200. //!
  201. //! @retval true Stored or default settings retrieved
  202. //! @retval false default settings retrieved, eeprom was erased.
  203. bool Config_RetrieveSettings(uint16_t offset)
  204. {
  205. int i=offset;
  206. bool previous_settings_retrieved = true;
  207. char ver[4]=EEPROM_VERSION;
  208. EEPROM_READ_VAR(i,cs.version); //read stored version
  209. // SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << cs.version << "]");
  210. if (strncmp(ver,cs.version,3) == 0) // version number match
  211. {
  212. i=offset;
  213. EEPROM_READ_VAR(i,cs);
  214. if (max_jerk[X_AXIS] > DEFAULT_XJERK) max_jerk[X_AXIS] = DEFAULT_XJERK;
  215. if (max_jerk[Y_AXIS] > DEFAULT_YJERK) max_jerk[Y_AXIS] = DEFAULT_YJERK;
  216. calculate_extruder_multipliers();
  217. #ifdef TMC2130
  218. for (uint8_t j = X_AXIS; j <= Y_AXIS; j++)
  219. {
  220. if (cs.max_feedrate_normal[j] > NORMAL_MAX_FEEDRATE_XY)
  221. cs.max_feedrate_normal[j] = NORMAL_MAX_FEEDRATE_XY;
  222. if (max_feedrate_silent[j] > SILENT_MAX_FEEDRATE_XY)
  223. max_feedrate_silent[j] = SILENT_MAX_FEEDRATE_XY;
  224. if (max_acceleration_units_per_sq_second_normal[j] > NORMAL_MAX_ACCEL_XY)
  225. max_acceleration_units_per_sq_second_normal[j] = NORMAL_MAX_ACCEL_XY;
  226. if (max_acceleration_units_per_sq_second_silent[j] > SILENT_MAX_ACCEL_XY)
  227. max_acceleration_units_per_sq_second_silent[j] = SILENT_MAX_ACCEL_XY;
  228. }
  229. #endif //TMC2130
  230. reset_acceleration_rates();
  231. // Call updatePID (similar to when we have processed M301)
  232. updatePID();
  233. SERIAL_ECHO_START;
  234. SERIAL_ECHOLNPGM("Stored settings retrieved");
  235. }
  236. else
  237. {
  238. Config_ResetDefault();
  239. //Return false to inform user that eeprom version was changed and firmware is using default hardcoded settings now.
  240. //In case that storing to eeprom was not used yet, do not inform user that hardcoded settings are used.
  241. if (eeprom_read_byte((uint8_t *)offset) != 0xFF ||
  242. eeprom_read_byte((uint8_t *)offset + 1) != 0xFF ||
  243. eeprom_read_byte((uint8_t *)offset + 2) != 0xFF) {
  244. previous_settings_retrieved = false;
  245. }
  246. }
  247. #ifdef EEPROM_CHITCHAT
  248. Config_PrintSettings();
  249. #endif
  250. return previous_settings_retrieved;
  251. }
  252. #endif
  253. void Config_ResetDefault()
  254. {
  255. memcpy_P(&cs,&default_conf, sizeof(cs));
  256. // steps per sq second need to be updated to agree with the units per sq second
  257. reset_acceleration_rates();
  258. #ifdef PIDTEMP
  259. updatePID();
  260. #ifdef PID_ADD_EXTRUSION_RATE
  261. Kc = DEFAULT_Kc; //this is not stored by Config_StoreSettings
  262. #endif//PID_ADD_EXTRUSION_RATE
  263. #endif//PIDTEMP
  264. calculate_extruder_multipliers();
  265. SERIAL_ECHO_START;
  266. SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded");
  267. }