Sat, 07 Nov 2015 13:23:07 +0100
Initial code from reprappro Marlin repository
0 | 1 | #ifndef EEPROM_H |
2 | #define EEPROM_H | |
3 | ||
4 | #include "Marlin.h" | |
5 | #include "planner.h" | |
6 | #include "temperature.h" | |
7 | #include "FPUTransform.h" | |
8 | //#include <EEPROM.h> | |
9 | ||
10 | ||
11 | ||
12 | template <class T> int EEPROM_writeAnything(int &ee, const T& value) | |
13 | { | |
14 | const byte* p = (const byte*)(const void*)&value; | |
15 | int i; | |
16 | for (i = 0; i < (int)sizeof(value); i++) | |
17 | eeprom_write_byte((unsigned char *)ee++, *p++); | |
18 | return i; | |
19 | } | |
20 | ||
21 | template <class T> int EEPROM_readAnything(int &ee, T& value) | |
22 | { | |
23 | byte* p = (byte*)(void*)&value; | |
24 | int i; | |
25 | for (i = 0; i < (int)sizeof(value); i++) | |
26 | *p++ = eeprom_read_byte((unsigned char *)ee++); | |
27 | return i; | |
28 | } | |
29 | //====================================================================================== | |
30 | ||
31 | ||
32 | ||
33 | ||
34 | #define EEPROM_OFFSET 100 | |
35 | ||
36 | ||
37 | // IMPORTANT: Whenever there are changes made to the variables stored in EEPROM | |
38 | // in the functions below, also increment the version number. This makes sure that | |
39 | // the default values are used whenever there is a change to the data, to prevent | |
40 | // wrong data being written to the variables. | |
41 | // ALSO: always make sure the variables in the Store and retrieve sections are in the same order. | |
42 | #define EEPROM_VERSION "V05" | |
43 | ||
44 | inline void EEPROM_StoreSettings() | |
45 | { | |
46 | #ifdef EEPROM_SETTINGS | |
47 | char ver[4]= "000"; | |
48 | int i=EEPROM_OFFSET; | |
49 | EEPROM_writeAnything(i,ver); // invalidate data first | |
50 | EEPROM_writeAnything(i,axis_steps_per_unit); | |
51 | EEPROM_writeAnything(i,max_feedrate); | |
52 | EEPROM_writeAnything(i,max_acceleration_units_per_sq_second); | |
53 | EEPROM_writeAnything(i,acceleration); | |
54 | EEPROM_writeAnything(i,retract_acceleration); | |
55 | EEPROM_writeAnything(i,minimumfeedrate); | |
56 | EEPROM_writeAnything(i,mintravelfeedrate); | |
57 | EEPROM_writeAnything(i,minsegmenttime); | |
58 | EEPROM_writeAnything(i,max_xy_jerk); | |
59 | EEPROM_writeAnything(i,max_z_jerk); | |
60 | EEPROM_writeAnything(i,max_e_jerk); | |
61 | EEPROM_writeAnything(i,add_homeing); | |
62 | #ifdef PIDTEMP | |
63 | EEPROM_writeAnything(i,Kp); | |
64 | EEPROM_writeAnything(i,Ki); | |
65 | EEPROM_writeAnything(i,Kd); | |
66 | EEPROM_writeAnything(i,Ki_Max); | |
67 | #else | |
68 | EEPROM_writeAnything(i,3000); | |
69 | EEPROM_writeAnything(i,0); | |
70 | EEPROM_writeAnything(i,0); | |
71 | #endif | |
72 | #if defined(UMFPUSUPPORT) && (UMFPUSUPPORT > -1) | |
73 | EEPROM_writeAnything(i,FPUEnabled); | |
74 | #endif | |
75 | EEPROM_writeAnything(i,max_length); | |
76 | #ifdef ADVANCE | |
77 | EEPROM_writeAnything(i,advance_k); | |
78 | #endif | |
79 | EEPROM_writeAnything(i,b_beta); | |
80 | EEPROM_writeAnything(i,b_resistor); | |
81 | EEPROM_writeAnything(i,b_thermistor); | |
82 | EEPROM_writeAnything(i,b_inf); | |
83 | EEPROM_writeAnything(i,n_beta); | |
84 | EEPROM_writeAnything(i,n_resistor); | |
85 | EEPROM_writeAnything(i,n_thermistor); | |
86 | EEPROM_writeAnything(i,n_inf); | |
87 | char ver2[4]=EEPROM_VERSION; | |
88 | i=EEPROM_OFFSET; | |
89 | EEPROM_writeAnything(i,ver2); // validate data | |
90 | SERIAL_ECHO_START; | |
91 | SERIAL_ECHOLNPGM("Settings Stored"); | |
92 | #endif //EEPROM_SETTINGS | |
93 | } | |
94 | ||
95 | ||
96 | inline void EEPROM_printSettings() | |
97 | { // if def=true, the default values will be used | |
98 | #ifdef EEPROM_SETTINGS | |
99 | SERIAL_ECHO_START; | |
100 | SERIAL_ECHOLNPGM("Steps per unit:"); | |
101 | SERIAL_ECHO_START; | |
102 | SERIAL_ECHOPAIR(" M92 X",axis_steps_per_unit[0]); | |
103 | SERIAL_ECHOPAIR(" Y",axis_steps_per_unit[1]); | |
104 | SERIAL_ECHOPAIR(" Z",axis_steps_per_unit[2]); | |
105 | SERIAL_ECHOPAIR(" E",axis_steps_per_unit[3]); | |
106 | SERIAL_ECHOLN(""); | |
107 | ||
108 | SERIAL_ECHO_START; | |
109 | SERIAL_ECHOLNPGM("Maximum feedrates (mm/s):"); | |
110 | SERIAL_ECHO_START; | |
111 | SERIAL_ECHOPAIR(" M203 X",max_feedrate[0]); | |
112 | SERIAL_ECHOPAIR(" Y",max_feedrate[1] ); | |
113 | SERIAL_ECHOPAIR(" Z", max_feedrate[2] ); | |
114 | SERIAL_ECHOPAIR(" E", max_feedrate[3]); | |
115 | SERIAL_ECHOLN(""); | |
116 | SERIAL_ECHO_START; | |
117 | SERIAL_ECHOLNPGM("Maximum Acceleration (mm/s2):"); | |
118 | SERIAL_ECHO_START; | |
119 | SERIAL_ECHOPAIR(" M201 X" ,max_acceleration_units_per_sq_second[0] ); | |
120 | SERIAL_ECHOPAIR(" Y" , max_acceleration_units_per_sq_second[1] ); | |
121 | SERIAL_ECHOPAIR(" Z" ,max_acceleration_units_per_sq_second[2] ); | |
122 | SERIAL_ECHOPAIR(" E" ,max_acceleration_units_per_sq_second[3]); | |
123 | SERIAL_ECHOLN(""); | |
124 | SERIAL_ECHO_START; | |
125 | SERIAL_ECHOLNPGM("Acceleration: S=acceleration, T=retract acceleration"); | |
126 | SERIAL_ECHO_START; | |
127 | SERIAL_ECHOPAIR(" M204 S",acceleration ); | |
128 | SERIAL_ECHOPAIR(" T" ,retract_acceleration); | |
129 | SERIAL_ECHOLN(""); | |
130 | SERIAL_ECHO_START; | |
131 | SERIAL_ECHOLNPGM("Advanced 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), K=advance_k"); | |
132 | SERIAL_ECHO_START; | |
133 | SERIAL_ECHOPAIR(" M205 S",minimumfeedrate ); | |
134 | SERIAL_ECHOPAIR(" T" ,mintravelfeedrate ); | |
135 | SERIAL_ECHOPAIR(" B" ,minsegmenttime ); | |
136 | SERIAL_ECHOPAIR(" X" ,max_xy_jerk ); | |
137 | SERIAL_ECHOPAIR(" Z" ,max_z_jerk); | |
138 | SERIAL_ECHOPAIR(" E" ,max_e_jerk); | |
139 | #ifdef ADVANCE | |
140 | SERIAL_ECHOPAIR(" K" ,advance_k); | |
141 | #endif | |
142 | SERIAL_ECHOLN(""); | |
143 | SERIAL_ECHO_START; | |
144 | SERIAL_ECHOPAIR(" M206 X",add_homeing[0]); | |
145 | SERIAL_ECHOPAIR(" Y",add_homeing[1] ); | |
146 | SERIAL_ECHOPAIR(" Z", add_homeing[2] ); | |
147 | SERIAL_ECHOLN(""); | |
148 | SERIAL_ECHO_START; | |
149 | SERIAL_ECHOPAIR(" M208 X",max_length[0]); | |
150 | SERIAL_ECHOPAIR(" Y",max_length[1] ); | |
151 | SERIAL_ECHOPAIR(" Z", max_length[2] ); | |
152 | SERIAL_ECHOLN(""); | |
153 | #ifdef PIDTEMP | |
154 | SERIAL_ECHO_START; | |
155 | SERIAL_ECHOLNPGM("PID settings:"); | |
156 | SERIAL_ECHO_START; | |
157 | SERIAL_ECHOPAIR(" M301 P",Kp); | |
158 | SERIAL_ECHOPAIR(" I" ,Ki/PID_dT); | |
159 | SERIAL_ECHOPAIR(" D" ,Kd*PID_dT); | |
160 | SERIAL_ECHOPAIR(" W" ,Ki_Max); | |
161 | SERIAL_ECHOLN(""); | |
162 | #endif | |
163 | SERIAL_ECHO_START; | |
164 | SERIAL_ECHOLNPGM("Thermistor settings: M304 Hh Bb Rr Tt, H0=Bed, H1..n=nozzle, b=thermistor beta value, r=series resistor, t=thermistor resistance as 25C"); | |
165 | SERIAL_ECHO_START; | |
166 | SERIAL_ECHOLN(" M304 H0"); | |
167 | SERIAL_ECHOPAIR(" B",b_beta); | |
168 | SERIAL_ECHOPAIR(" R",b_resistor); | |
169 | SERIAL_ECHOPAIR(" T",b_thermistor); | |
170 | SERIAL_ECHOLN(" M304 H1"); | |
171 | SERIAL_ECHOPAIR(" B",n_beta); | |
172 | SERIAL_ECHOPAIR(" R",n_resistor); | |
173 | SERIAL_ECHOPAIR(" T",n_thermistor); | |
174 | SERIAL_ECHOLN(""); | |
175 | #if defined(UMFPUSUPPORT) && (UMFPUSUPPORT > -1) | |
176 | SERIAL_ECHOPAIR(" FPU Enabled" , FPUEnabled?" yes":" no"); | |
177 | SERIAL_ECHOLN(""); | |
178 | #endif | |
179 | #endif | |
180 | } | |
181 | ||
182 | ||
183 | inline void EEPROM_RetrieveSettings(bool def=false) | |
184 | { // if def=true, the default values will be used | |
185 | #ifdef EEPROM_SETTINGS | |
186 | int i=EEPROM_OFFSET; | |
187 | char stored_ver[4]; | |
188 | char ver[4]=EEPROM_VERSION; | |
189 | EEPROM_readAnything(i,stored_ver); //read stored version | |
190 | // SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << stored_ver << "]"); | |
191 | if ((!def)&&(strncmp(ver,stored_ver,3)==0)) | |
192 | { // version number match | |
193 | EEPROM_readAnything(i,axis_steps_per_unit); | |
194 | EEPROM_readAnything(i,max_feedrate); | |
195 | EEPROM_readAnything(i,max_acceleration_units_per_sq_second); | |
196 | EEPROM_readAnything(i,acceleration); | |
197 | EEPROM_readAnything(i,retract_acceleration); | |
198 | EEPROM_readAnything(i,minimumfeedrate); | |
199 | EEPROM_readAnything(i,mintravelfeedrate); | |
200 | EEPROM_readAnything(i,minsegmenttime); | |
201 | EEPROM_readAnything(i,max_xy_jerk); | |
202 | EEPROM_readAnything(i,max_z_jerk); | |
203 | EEPROM_readAnything(i,max_e_jerk); | |
204 | EEPROM_readAnything(i,add_homeing); | |
205 | #ifndef PIDTEMP | |
206 | float Kp,Ki,Kd; | |
207 | int Ki_Max; | |
208 | #endif | |
209 | EEPROM_readAnything(i,Kp); | |
210 | EEPROM_readAnything(i,Ki); | |
211 | EEPROM_readAnything(i,Kd); | |
212 | EEPROM_readAnything(i,Ki_Max); | |
213 | #if defined(UMFPUSUPPORT) && (UMFPUSUPPORT > -1) | |
214 | EEPROM_readAnything(i,FPUEnabled); | |
215 | #endif | |
216 | EEPROM_readAnything(i,max_length); | |
217 | #ifdef ADVANCE | |
218 | EEPROM_readAnything(i,advance_k); | |
219 | #endif | |
220 | EEPROM_readAnything(i,b_beta); | |
221 | EEPROM_readAnything(i,b_resistor); | |
222 | EEPROM_readAnything(i,b_thermistor); | |
223 | EEPROM_readAnything(i,b_inf); | |
224 | EEPROM_readAnything(i,n_beta); | |
225 | EEPROM_readAnything(i,n_resistor); | |
226 | EEPROM_readAnything(i,n_thermistor); | |
227 | EEPROM_readAnything(i,n_inf); | |
228 | ||
229 | SERIAL_ECHO_START; | |
230 | SERIAL_ECHOLNPGM("Stored settings retreived:"); | |
231 | } | |
232 | else | |
233 | #endif | |
234 | { | |
235 | float tmp1[]=DEFAULT_AXIS_STEPS_PER_UNIT; | |
236 | float tmp2[]=DEFAULT_MAX_FEEDRATE; | |
237 | long tmp3[]=DEFAULT_MAX_ACCELERATION; | |
238 | float tmp4[]=AXES_MAX_LENGTHS; | |
239 | for (short i=0;i<4;i++) | |
240 | { | |
241 | axis_steps_per_unit[i]=tmp1[i]; | |
242 | max_feedrate[i]=tmp2[i]; | |
243 | max_acceleration_units_per_sq_second[i]=tmp3[i]; | |
244 | max_length[i]=tmp4[i]; | |
245 | } | |
246 | acceleration=DEFAULT_ACCELERATION; | |
247 | retract_acceleration=DEFAULT_RETRACT_ACCELERATION; | |
248 | minimumfeedrate=DEFAULT_MINIMUMFEEDRATE; | |
249 | minsegmenttime=DEFAULT_MINSEGMENTTIME; | |
250 | mintravelfeedrate=DEFAULT_MINTRAVELFEEDRATE; | |
251 | max_xy_jerk=DEFAULT_XYJERK; | |
252 | max_z_jerk=DEFAULT_ZJERK; | |
253 | max_e_jerk=DEFAULT_EJERK; | |
254 | #ifdef ADVANCE | |
255 | advance_k=EXTRUDER_ADVANCE_K; | |
256 | #endif | |
257 | b_beta = BED_BETA; | |
258 | b_resistor = BED_RS; | |
259 | b_thermistor = BED_NTC; | |
260 | b_inf = BED_R_INF; | |
261 | n_beta = E_BETA; | |
262 | n_resistor = E_RS; | |
263 | n_thermistor = E_NTC; | |
264 | n_inf = E_R_INF; | |
265 | SERIAL_ECHO_START; | |
266 | SERIAL_ECHOLN("Using Default settings:"); | |
267 | } | |
268 | #ifdef EEPROM_CHITCHAT | |
269 | EEPROM_printSettings(); | |
270 | #endif | |
271 | } | |
272 | ||
273 | #endif | |
274 | ||
275 |