ConfigurationStore.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size)
  11. {
  12. while (size--) {
  13. uint8_t * const p = (uint8_t * const)pos;
  14. uint8_t v = *value;
  15. // EEPROM has only ~100,000 write cycles,
  16. // so only write bytes that have changed!
  17. if (v != eeprom_read_byte(p)) {
  18. eeprom_write_byte(p, v);
  19. if (eeprom_read_byte(p) != v) {
  20. SERIAL_ECHOLNPGM("EEPROM Error");
  21. return;
  22. }
  23. }
  24. pos++;
  25. value++;
  26. };
  27. }
  28. #define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value))
  29. void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size)
  30. {
  31. do
  32. {
  33. *value = eeprom_read_byte((unsigned char*)pos);
  34. pos++;
  35. value++;
  36. }while(--size);
  37. }
  38. #define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value))
  39. //======================================================================================
  40. #define EEPROM_OFFSET 20
  41. // IMPORTANT: Whenever there are changes made to the variables stored in EEPROM
  42. // in the functions below, also increment the version number. This makes sure that
  43. // the default values are used whenever there is a change to the data, to prevent
  44. // wrong data being written to the variables.
  45. // ALSO: always make sure the variables in the Store and retrieve sections are in the same order.
  46. #define EEPROM_VERSION "V2"
  47. #ifdef EEPROM_SETTINGS
  48. void Config_StoreSettings(uint16_t offset, uint8_t level)
  49. {
  50. char ver[4]= "000";
  51. int i = offset;
  52. EEPROM_WRITE_VAR(i,ver); // invalidate data first
  53. EEPROM_WRITE_VAR(i,axis_steps_per_unit);
  54. EEPROM_WRITE_VAR(i,max_feedrate);
  55. EEPROM_WRITE_VAR(i,max_acceleration_units_per_sq_second);
  56. EEPROM_WRITE_VAR(i,acceleration);
  57. EEPROM_WRITE_VAR(i,retract_acceleration);
  58. EEPROM_WRITE_VAR(i,minimumfeedrate);
  59. EEPROM_WRITE_VAR(i,mintravelfeedrate);
  60. EEPROM_WRITE_VAR(i,minsegmenttime);
  61. EEPROM_WRITE_VAR(i,max_jerk[X_AXIS]);
  62. EEPROM_WRITE_VAR(i,max_jerk[Y_AXIS]);
  63. EEPROM_WRITE_VAR(i,max_jerk[Z_AXIS]);
  64. EEPROM_WRITE_VAR(i,max_jerk[E_AXIS]);
  65. EEPROM_WRITE_VAR(i,add_homing);
  66. /* EEPROM_WRITE_VAR(i,plaPreheatHotendTemp);
  67. EEPROM_WRITE_VAR(i,plaPreheatHPBTemp);
  68. EEPROM_WRITE_VAR(i,plaPreheatFanSpeed);
  69. EEPROM_WRITE_VAR(i,absPreheatHotendTemp);
  70. EEPROM_WRITE_VAR(i,absPreheatHPBTemp);
  71. EEPROM_WRITE_VAR(i,absPreheatFanSpeed);
  72. */
  73. EEPROM_WRITE_VAR(i,zprobe_zoffset);
  74. #ifdef PIDTEMP
  75. EEPROM_WRITE_VAR(i,Kp);
  76. EEPROM_WRITE_VAR(i,Ki);
  77. EEPROM_WRITE_VAR(i,Kd);
  78. #else
  79. float dummy = 3000.0f;
  80. EEPROM_WRITE_VAR(i,dummy);
  81. dummy = 0.0f;
  82. EEPROM_WRITE_VAR(i,dummy);
  83. EEPROM_WRITE_VAR(i,dummy);
  84. #endif
  85. #ifdef PIDTEMPBED
  86. EEPROM_WRITE_VAR(i, bedKp);
  87. EEPROM_WRITE_VAR(i, bedKi);
  88. EEPROM_WRITE_VAR(i, bedKd);
  89. #endif
  90. // EEPROM_WRITE_VAR(i,lcd_contrast);
  91. #ifdef FWRETRACT
  92. EEPROM_WRITE_VAR(i,autoretract_enabled);
  93. EEPROM_WRITE_VAR(i,retract_length);
  94. #if EXTRUDERS > 1
  95. EEPROM_WRITE_VAR(i,retract_length_swap);
  96. #endif
  97. EEPROM_WRITE_VAR(i,retract_feedrate);
  98. EEPROM_WRITE_VAR(i,retract_zlift);
  99. EEPROM_WRITE_VAR(i,retract_recover_length);
  100. #if EXTRUDERS > 1
  101. EEPROM_WRITE_VAR(i,retract_recover_length_swap);
  102. #endif
  103. EEPROM_WRITE_VAR(i,retract_recover_feedrate);
  104. #endif
  105. // Save filament sizes
  106. EEPROM_WRITE_VAR(i, volumetric_enabled);
  107. EEPROM_WRITE_VAR(i, filament_size[0]);
  108. #if EXTRUDERS > 1
  109. EEPROM_WRITE_VAR(i, filament_size[1]);
  110. #if EXTRUDERS > 2
  111. EEPROM_WRITE_VAR(i, filament_size[2]);
  112. #endif
  113. #endif
  114. #ifdef LIN_ADVANCE
  115. if (level >= 10) {
  116. EEPROM_WRITE_VAR(i, extruder_advance_k);
  117. EEPROM_WRITE_VAR(i, advance_ed_ratio);
  118. }
  119. #endif //LIN_ADVANCE
  120. /* MYSERIAL.print("Top address used:\n");
  121. MYSERIAL.print(i);
  122. MYSERIAL.print("; (0x");
  123. MYSERIAL.print(i, HEX);
  124. MYSERIAL.println(")");
  125. */
  126. char ver2[4]=EEPROM_VERSION;
  127. i=offset;
  128. EEPROM_WRITE_VAR(i,ver2); // validate data
  129. SERIAL_ECHO_START;
  130. SERIAL_ECHOLNPGM("Settings Stored");
  131. }
  132. #endif //EEPROM_SETTINGS
  133. #ifndef DISABLE_M503
  134. void Config_PrintSettings(uint8_t level)
  135. { // Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown
  136. printf_P(PSTR(
  137. "%SSteps per unit:\n%S M92 X%.2f Y%.2f Z%.2f E%.2f\n"
  138. "%SMaximum feedrates (mm/s):\n%S M203 X%.2f Y%.2f Z%.2f E%.2f\n"
  139. "%SMaximum Acceleration (mm/s2):\n%S M201 X%.2f Y%.2f Z%.2f E%.2f\n"
  140. "%SAcceleration: S=acceleration, T=retract acceleration\n%S M204 S%.2f T%.2f\n"
  141. "%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"
  142. "%SHome offset (mm):\n%S M206 X%.2f Y%.2f Z%.2f\n"
  143. ),
  144. echomagic, echomagic, axis_steps_per_unit[X_AXIS], axis_steps_per_unit[Y_AXIS], axis_steps_per_unit[Z_AXIS], axis_steps_per_unit[E_AXIS],
  145. echomagic, echomagic, max_feedrate[X_AXIS], max_feedrate[Y_AXIS], max_feedrate[Z_AXIS], max_feedrate[E_AXIS],
  146. 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],
  147. echomagic, echomagic, acceleration, retract_acceleration,
  148. echomagic, echomagic, minimumfeedrate, mintravelfeedrate, minsegmenttime, max_jerk[X_AXIS], max_jerk[Y_AXIS], max_jerk[Z_AXIS], max_jerk[E_AXIS],
  149. echomagic, echomagic, add_homing[X_AXIS], add_homing[Y_AXIS], add_homing[Z_AXIS]
  150. );
  151. #ifdef PIDTEMP
  152. printf_P(PSTR("%SPID settings:\n%S M301 P%.2f I%.2f D%.2f\n"),
  153. echomagic, echomagic, Kp, unscalePID_i(Ki), unscalePID_d(Kd));
  154. #endif
  155. #ifdef PIDTEMPBED
  156. printf_P(PSTR("%SPID heatbed settings:\n%S M304 P%.2f I%.2f D%.2f\n"),
  157. echomagic, echomagic, bedKp, unscalePID_i(bedKi), unscalePID_d(bedKd));
  158. #endif
  159. #ifdef FWRETRACT
  160. printf_P(PSTR(
  161. "%SRetract: S=Length (mm) F:Speed (mm/m) Z: ZLift (mm)\n%S M207 S%.2f F%.2f Z%.2f\n"
  162. "%SRecover: S=Extra length (mm) F:Speed (mm/m)\n%S M208 S%.2f F%.2f\n"
  163. "%SAuto-Retract: S=0 to disable, 1 to interpret extrude-only moves as retracts or recoveries\n%S M209 S%.2f\n"
  164. ),
  165. echomagic, echomagic, retract_length, retract_feedrate*60, retract_zlift,
  166. echomagic, echomagic, retract_recover_length, retract_recover_feedrate*60,
  167. echomagic, echomagic, (unsigned long)(autoretract_enabled ? 1 : 0)
  168. );
  169. #if EXTRUDERS > 1
  170. printf_P(PSTR("%SMulti-extruder settings:\n%S Swap retract length (mm): %.2f\n%S Swap rec. addl. length (mm): %.2f\n"),
  171. echomagic, echomagic, retract_length_swap, echomagic, retract_recover_length_swap);
  172. #endif
  173. if (volumetric_enabled) {
  174. printf_P(PSTR("%SFilament settings:\n%S M200 D%.2f\n"),
  175. echomagic, echomagic, filament_size[0]);
  176. #if EXTRUDERS > 1
  177. printf_P(PSTR("%S M200 T1 D%.2f\n"),
  178. echomagic, echomagic, filament_size[1]);
  179. #if EXTRUDERS > 2
  180. printf_P(PSTR("%S M200 T1 D%.2f\n"),
  181. echomagic, echomagic, filament_size[2]);
  182. #endif
  183. #endif
  184. } else {
  185. printf_P(PSTR("%SFilament settings: Disabled\n"), echomagic);
  186. }
  187. #endif
  188. if (level >= 10) {
  189. #ifdef LIN_ADVANCE
  190. printf_P(PSTR("%SLinear advance settings:\n M900 K%.2f E/D = %.2f\n"),
  191. echomagic, extruder_advance_k, advance_ed_ratio);
  192. #endif //LIN_ADVANCE
  193. }
  194. }
  195. #endif
  196. #ifdef EEPROM_SETTINGS
  197. bool Config_RetrieveSettings(uint16_t offset, uint8_t level)
  198. {
  199. int i=offset;
  200. bool previous_settings_retrieved = true;
  201. char stored_ver[4];
  202. char ver[4]=EEPROM_VERSION;
  203. EEPROM_READ_VAR(i,stored_ver); //read stored version
  204. // SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << stored_ver << "]");
  205. if (strncmp(ver,stored_ver,3) == 0)
  206. {
  207. // version number match
  208. EEPROM_READ_VAR(i,axis_steps_per_unit);
  209. EEPROM_READ_VAR(i,max_feedrate);
  210. EEPROM_READ_VAR(i,max_acceleration_units_per_sq_second);
  211. // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
  212. reset_acceleration_rates();
  213. EEPROM_READ_VAR(i,acceleration);
  214. EEPROM_READ_VAR(i,retract_acceleration);
  215. EEPROM_READ_VAR(i,minimumfeedrate);
  216. EEPROM_READ_VAR(i,mintravelfeedrate);
  217. EEPROM_READ_VAR(i,minsegmenttime);
  218. EEPROM_READ_VAR(i,max_jerk[X_AXIS]);
  219. EEPROM_READ_VAR(i,max_jerk[Y_AXIS]);
  220. EEPROM_READ_VAR(i,max_jerk[Z_AXIS]);
  221. EEPROM_READ_VAR(i,max_jerk[E_AXIS]);
  222. if (max_jerk[X_AXIS] > DEFAULT_XJERK) max_jerk[X_AXIS] = DEFAULT_XJERK;
  223. if (max_jerk[Y_AXIS] > DEFAULT_YJERK) max_jerk[Y_AXIS] = DEFAULT_YJERK;
  224. EEPROM_READ_VAR(i,add_homing);
  225. /*
  226. EEPROM_READ_VAR(i,plaPreheatHotendTemp);
  227. EEPROM_READ_VAR(i,plaPreheatHPBTemp);
  228. EEPROM_READ_VAR(i,plaPreheatFanSpeed);
  229. EEPROM_READ_VAR(i,absPreheatHotendTemp);
  230. EEPROM_READ_VAR(i,absPreheatHPBTemp);
  231. EEPROM_READ_VAR(i,absPreheatFanSpeed);
  232. */
  233. EEPROM_READ_VAR(i,zprobe_zoffset);
  234. #ifndef PIDTEMP
  235. float Kp,Ki,Kd;
  236. #endif
  237. // do not need to scale PID values as the values in EEPROM are already scaled
  238. EEPROM_READ_VAR(i,Kp);
  239. EEPROM_READ_VAR(i,Ki);
  240. EEPROM_READ_VAR(i,Kd);
  241. #ifdef PIDTEMPBED
  242. EEPROM_READ_VAR(i, bedKp);
  243. EEPROM_READ_VAR(i, bedKi);
  244. EEPROM_READ_VAR(i, bedKd);
  245. #endif
  246. // EEPROM_READ_VAR(i,lcd_contrast);
  247. #ifdef FWRETRACT
  248. EEPROM_READ_VAR(i,autoretract_enabled);
  249. EEPROM_READ_VAR(i,retract_length);
  250. #if EXTRUDERS > 1
  251. EEPROM_READ_VAR(i,retract_length_swap);
  252. #endif
  253. EEPROM_READ_VAR(i,retract_feedrate);
  254. EEPROM_READ_VAR(i,retract_zlift);
  255. EEPROM_READ_VAR(i,retract_recover_length);
  256. #if EXTRUDERS > 1
  257. EEPROM_READ_VAR(i,retract_recover_length_swap);
  258. #endif
  259. EEPROM_READ_VAR(i,retract_recover_feedrate);
  260. #endif
  261. EEPROM_READ_VAR(i, volumetric_enabled);
  262. EEPROM_READ_VAR(i, filament_size[0]);
  263. #if EXTRUDERS > 1
  264. EEPROM_READ_VAR(i, filament_size[1]);
  265. #if EXTRUDERS > 2
  266. EEPROM_READ_VAR(i, filament_size[2]);
  267. #endif
  268. #endif
  269. #ifdef LIN_ADVANCE
  270. if (level >= 10) {
  271. EEPROM_READ_VAR(i, extruder_advance_k);
  272. EEPROM_READ_VAR(i, advance_ed_ratio);
  273. }
  274. #endif //LIN_ADVANCE
  275. calculate_extruder_multipliers();
  276. // Call updatePID (similar to when we have processed M301)
  277. updatePID();
  278. SERIAL_ECHO_START;
  279. SERIAL_ECHOLNPGM("Stored settings retrieved");
  280. }
  281. else
  282. {
  283. Config_ResetDefault();
  284. //Return false to inform user that eeprom version was changed and firmware is using default hardcoded settings now.
  285. //In case that storing to eeprom was not used yet, do not inform user that hardcoded settings are used.
  286. if (eeprom_read_byte((uint8_t *)offset) != 0xFF ||
  287. eeprom_read_byte((uint8_t *)offset + 1) != 0xFF ||
  288. eeprom_read_byte((uint8_t *)offset + 2) != 0xFF) {
  289. previous_settings_retrieved = false;
  290. }
  291. }
  292. #ifdef EEPROM_CHITCHAT
  293. Config_PrintSettings();
  294. #endif
  295. return previous_settings_retrieved;
  296. }
  297. #endif
  298. void Config_ResetDefault()
  299. {
  300. float tmp1[]=DEFAULT_AXIS_STEPS_PER_UNIT;
  301. float tmp2[]=DEFAULT_MAX_FEEDRATE;
  302. long tmp3[]=DEFAULT_MAX_ACCELERATION;
  303. for (short i=0;i<4;i++)
  304. {
  305. axis_steps_per_unit[i]=tmp1[i];
  306. max_feedrate[i]=tmp2[i];
  307. max_acceleration_units_per_sq_second[i]=tmp3[i];
  308. }
  309. // steps per sq second need to be updated to agree with the units per sq second
  310. reset_acceleration_rates();
  311. acceleration=DEFAULT_ACCELERATION;
  312. retract_acceleration=DEFAULT_RETRACT_ACCELERATION;
  313. minimumfeedrate=DEFAULT_MINIMUMFEEDRATE;
  314. minsegmenttime=DEFAULT_MINSEGMENTTIME;
  315. mintravelfeedrate=DEFAULT_MINTRAVELFEEDRATE;
  316. max_jerk[X_AXIS] = DEFAULT_XJERK;
  317. max_jerk[Y_AXIS] = DEFAULT_YJERK;
  318. max_jerk[Z_AXIS] = DEFAULT_ZJERK;
  319. max_jerk[E_AXIS] = DEFAULT_EJERK;
  320. add_homing[X_AXIS] = add_homing[Y_AXIS] = add_homing[Z_AXIS] = 0;
  321. #ifdef ENABLE_AUTO_BED_LEVELING
  322. zprobe_zoffset = -Z_PROBE_OFFSET_FROM_EXTRUDER;
  323. #endif
  324. #ifdef PIDTEMP
  325. Kp = DEFAULT_Kp;
  326. Ki = scalePID_i(DEFAULT_Ki);
  327. Kd = scalePID_d(DEFAULT_Kd);
  328. // call updatePID (similar to when we have processed M301)
  329. updatePID();
  330. #ifdef PID_ADD_EXTRUSION_RATE
  331. Kc = DEFAULT_Kc;
  332. #endif//PID_ADD_EXTRUSION_RATE
  333. #endif//PIDTEMP
  334. #ifdef FWRETRACT
  335. autoretract_enabled = false;
  336. retract_length = RETRACT_LENGTH;
  337. #if EXTRUDERS > 1
  338. retract_length_swap = RETRACT_LENGTH_SWAP;
  339. #endif
  340. retract_feedrate = RETRACT_FEEDRATE;
  341. retract_zlift = RETRACT_ZLIFT;
  342. retract_recover_length = RETRACT_RECOVER_LENGTH;
  343. #if EXTRUDERS > 1
  344. retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  345. #endif
  346. retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  347. #endif
  348. volumetric_enabled = false;
  349. filament_size[0] = DEFAULT_NOMINAL_FILAMENT_DIA;
  350. #if EXTRUDERS > 1
  351. filament_size[1] = DEFAULT_NOMINAL_FILAMENT_DIA;
  352. #if EXTRUDERS > 2
  353. filament_size[2] = DEFAULT_NOMINAL_FILAMENT_DIA;
  354. #endif
  355. #endif
  356. calculate_extruder_multipliers();
  357. SERIAL_ECHO_START;
  358. SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded");
  359. }