Skip to content

Commit 56cb8cb

Browse files
authored
Merge pull request prusa3d#3432 from leptun/MK3_3.12_Optimizations_PR3
memory and flash optimizations for 3.12 PR3
2 parents 6e15df6 + 4a02ff3 commit 56cb8cb

File tree

3 files changed

+53
-76
lines changed

3 files changed

+53
-76
lines changed

Firmware/Marlin.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ extern FILE _uartout;
7979
#define SERIAL_PROTOCOL_F(x,y) (MYSERIAL.print(x,y))
8080
#define SERIAL_PROTOCOLPGM(x) (serialprintPGM(PSTR(x)))
8181
#define SERIAL_PROTOCOLRPGM(x) (serialprintPGM((x)))
82-
#define SERIAL_PROTOCOLLN(x) (MYSERIAL.println(x)/*,MYSERIAL.write('\n')*/)
83-
#define SERIAL_PROTOCOLLNPGM(x) (serialprintPGM(PSTR(x)),MYSERIAL.println()/*write('\n')*/)
84-
#define SERIAL_PROTOCOLLNRPGM(x) (serialprintPGM((x)),MYSERIAL.println()/*write('\n')*/)
82+
#define SERIAL_PROTOCOLLN(x) (MYSERIAL.println(x))
83+
#define SERIAL_PROTOCOLLNPGM(x) (serialprintlnPGM(PSTR(x)))
84+
#define SERIAL_PROTOCOLLNRPGM(x) (serialprintlnPGM((x)))
8585

8686

8787
extern const char errormagic[] PROGMEM;
@@ -115,6 +115,9 @@ void serial_echopair_P(const char *s_P, unsigned long v);
115115
// I'd rather skip a few CPU ticks than 5.5KB (!!) of FLASH
116116
void serialprintPGM(const char *str);
117117

118+
//The "ln" variant of the function above.
119+
void serialprintlnPGM(const char *str);
120+
118121
bool is_buffer_empty();
119122
void process_commands();
120123
void ramming();

Firmware/Marlin_main.cpp

+37-51
Original file line numberDiff line numberDiff line change
@@ -467,22 +467,16 @@ void serial_echopair_P(const char *s_P, double v)
467467
void serial_echopair_P(const char *s_P, unsigned long v)
468468
{ serialprintPGM(s_P); SERIAL_ECHO(v); }
469469

470-
/*FORCE_INLINE*/ void serialprintPGM(const char *str)
471-
{
472-
#if 0
473-
char ch=pgm_read_byte(str);
474-
while(ch)
475-
{
476-
MYSERIAL.write(ch);
477-
ch=pgm_read_byte(++str);
478-
}
479-
#else
480-
// hmm, same size as the above version, the compiler did a good job optimizing the above
481-
while( uint8_t ch = pgm_read_byte(str) ){
482-
MYSERIAL.write((char)ch);
483-
++str;
484-
}
485-
#endif
470+
void serialprintPGM(const char *str) {
471+
while(uint8_t ch = pgm_read_byte(str)) {
472+
MYSERIAL.write((char)ch);
473+
++str;
474+
}
475+
}
476+
477+
void serialprintlnPGM(const char *str) {
478+
serialprintPGM(str);
479+
MYSERIAL.println();
486480
}
487481

488482
#ifdef SDSUPPORT
@@ -3676,9 +3670,7 @@ void gcode_M114()
36763670
SERIAL_PROTOCOLPGM(" Z:");
36773671
SERIAL_PROTOCOL(float(st_get_position(Z_AXIS)) / cs.axis_steps_per_unit[Z_AXIS]);
36783672
SERIAL_PROTOCOLPGM(" E:");
3679-
SERIAL_PROTOCOL(float(st_get_position(E_AXIS)) / cs.axis_steps_per_unit[E_AXIS]);
3680-
3681-
SERIAL_PROTOCOLLN();
3673+
SERIAL_PROTOCOLLN(float(st_get_position(E_AXIS)) / cs.axis_steps_per_unit[E_AXIS]);
36823674
}
36833675

36843676
#if (defined(FANCHECK) && (((defined(TACH_0) && (TACH_0 >-1)) || (defined(TACH_1) && (TACH_1 > -1)))))
@@ -6158,8 +6150,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT)
61586150
SERIAL_PROTOCOL('.');
61596151
SERIAL_PROTOCOL(uint8_t(ip[2]));
61606152
SERIAL_PROTOCOL('.');
6161-
SERIAL_PROTOCOL(uint8_t(ip[3]));
6162-
SERIAL_PROTOCOLLN();
6153+
SERIAL_PROTOCOLLN(uint8_t(ip[3]));
61636154
} else {
61646155
SERIAL_PROTOCOLPGM("?Toshiba FlashAir GetIP failed\n");
61656156
}
@@ -7730,8 +7721,7 @@ SERIAL_PROTOCOLPGM("\n\n");
77307721
SERIAL_PROTOCOL(" Servo ");
77317722
SERIAL_PROTOCOL(servo_index);
77327723
SERIAL_PROTOCOL(": ");
7733-
SERIAL_PROTOCOL(servos[servo_index].read());
7734-
SERIAL_PROTOCOLLN();
7724+
SERIAL_PROTOCOLLN(servos[servo_index].read());
77357725
}
77367726
}
77377727
break;
@@ -7796,14 +7786,14 @@ SERIAL_PROTOCOLPGM("\n\n");
77967786

77977787
updatePID();
77987788
SERIAL_PROTOCOLRPGM(MSG_OK);
7799-
SERIAL_PROTOCOL(" p:");
7789+
SERIAL_PROTOCOLPGM(" p:");
78007790
SERIAL_PROTOCOL(cs.Kp);
7801-
SERIAL_PROTOCOL(" i:");
7791+
SERIAL_PROTOCOLPGM(" i:");
78027792
SERIAL_PROTOCOL(unscalePID_i(cs.Ki));
7803-
SERIAL_PROTOCOL(" d:");
7793+
SERIAL_PROTOCOLPGM(" d:");
78047794
SERIAL_PROTOCOL(unscalePID_d(cs.Kd));
78057795
#ifdef PID_ADD_EXTRUSION_RATE
7806-
SERIAL_PROTOCOL(" c:");
7796+
SERIAL_PROTOCOLPGM(" c:");
78077797
//Kc does not have scaling applied above, or in resetting defaults
78087798
SERIAL_PROTOCOL(Kc);
78097799
#endif
@@ -7834,13 +7824,12 @@ SERIAL_PROTOCOLPGM("\n\n");
78347824

78357825
updatePID();
78367826
SERIAL_PROTOCOLRPGM(MSG_OK);
7837-
SERIAL_PROTOCOL(" p:");
7827+
SERIAL_PROTOCOLPGM(" p:");
78387828
SERIAL_PROTOCOL(cs.bedKp);
7839-
SERIAL_PROTOCOL(" i:");
7829+
SERIAL_PROTOCOLPGM(" i:");
78407830
SERIAL_PROTOCOL(unscalePID_i(cs.bedKi));
7841-
SERIAL_PROTOCOL(" d:");
7842-
SERIAL_PROTOCOL(unscalePID_d(cs.bedKd));
7843-
SERIAL_PROTOCOLLN();
7831+
SERIAL_PROTOCOLPGM(" d:");
7832+
SERIAL_PROTOCOLLN(unscalePID_d(cs.bedKd));
78447833
}
78457834
break;
78467835
#endif //PIDTEMP
@@ -8272,8 +8261,7 @@ SERIAL_PROTOCOLPGM("\n\n");
82728261
LCD_MESSAGERPGM(_T(MSG_PLEASE_WAIT));
82738262

82748263
SERIAL_PROTOCOLPGM("Wait for PINDA target temperature:");
8275-
SERIAL_PROTOCOL(set_target_pinda);
8276-
SERIAL_PROTOCOLLN();
8264+
SERIAL_PROTOCOLLN(set_target_pinda);
82778265

82788266
codenum = _millis();
82798267
cancel_heatup = false;
@@ -8315,12 +8303,13 @@ SERIAL_PROTOCOLPGM("\n\n");
83158303
- `S` - Microsteps
83168304
- `I` - Table index
83178305
*/
8318-
case 861:
8306+
case 861: {
8307+
const char * const _header = PSTR("index, temp, ustep, um");
83198308
if (code_seen('?')) { // ? - Print out current EEPROM offset values
8320-
uint8_t cal_status = calibration_status_pinda();
83218309
int16_t usteps = 0;
8322-
cal_status ? SERIAL_PROTOCOLLN("PINDA cal status: 1") : SERIAL_PROTOCOLLN("PINDA cal status: 0");
8323-
SERIAL_PROTOCOLLN("index, temp, ustep, um");
8310+
SERIAL_PROTOCOLPGM("PINDA cal status: ");
8311+
SERIAL_PROTOCOLLN(calibration_status_pinda());
8312+
SERIAL_PROTOCOLLNRPGM(_header);
83248313
for (uint8_t i = 0; i < 6; i++)
83258314
{
83268315
if(i > 0) {
@@ -8333,8 +8322,7 @@ SERIAL_PROTOCOLPGM("\n\n");
83338322
SERIAL_PROTOCOLPGM(", ");
83348323
SERIAL_PROTOCOL(usteps);
83358324
SERIAL_PROTOCOLPGM(", ");
8336-
SERIAL_PROTOCOL(mm * 1000);
8337-
SERIAL_PROTOCOLLN();
8325+
SERIAL_PROTOCOLLN(mm * 1000);
83388326
}
83398327
}
83408328
else if (code_seen('!')) { // ! - Set factory default values
@@ -8349,24 +8337,24 @@ SERIAL_PROTOCOLPGM("\n\n");
83498337
eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 3, z_shift);
83508338
z_shift = 120; //60C - 300um - 120usteps
83518339
eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 4, z_shift);
8352-
SERIAL_PROTOCOLLN("factory restored");
8340+
SERIAL_PROTOCOLLNPGM("factory restored");
83538341
}
83548342
else if (code_seen('Z')) { // Z - Set all values to 0 (effectively disabling PINDA temperature compensation)
83558343
eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1);
83568344
int16_t z_shift = 0;
83578345
for (uint8_t i = 0; i < 5; i++) {
83588346
eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i, z_shift);
83598347
}
8360-
SERIAL_PROTOCOLLN("zerorized");
8348+
SERIAL_PROTOCOLLNPGM("zerorized");
83618349
}
83628350
else if (code_seen('S')) { // Sxxx Iyyy - Set compensation ustep value S for compensation table index I
83638351
int16_t usteps = code_value_short();
83648352
if (code_seen('I')) {
83658353
uint8_t index = code_value_uint8();
83668354
if (index < 5) {
83678355
eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + index, usteps);
8368-
SERIAL_PROTOCOLLN("OK");
8369-
SERIAL_PROTOCOLLN("index, temp, ustep, um");
8356+
SERIAL_PROTOCOLLNRPGM(MSG_OK);
8357+
SERIAL_PROTOCOLLNRPGM(_header);
83708358
for (uint8_t i = 0; i < 6; i++)
83718359
{
83728360
usteps = 0;
@@ -8380,16 +8368,15 @@ SERIAL_PROTOCOLPGM("\n\n");
83808368
SERIAL_PROTOCOLPGM(", ");
83818369
SERIAL_PROTOCOL(usteps);
83828370
SERIAL_PROTOCOLPGM(", ");
8383-
SERIAL_PROTOCOL(mm * 1000);
8384-
SERIAL_PROTOCOLLN();
8371+
SERIAL_PROTOCOLLN(mm * 1000);
83858372
}
83868373
}
83878374
}
83888375
}
83898376
else {
8390-
SERIAL_PROTOCOLPGM("no valid command");
8377+
SERIAL_PROTOCOLLNPGM("no valid command");
83918378
}
8392-
break;
8379+
} break;
83938380

83948381
#endif //PINDA_THERMISTOR
83958382

@@ -11462,8 +11449,7 @@ void restore_print_from_eeprom(bool mbl_was_active) {
1146211449
enquecommand(cmd);
1146311450
// Recover final E axis position and mode
1146411451
float pos_e = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E));
11465-
sprintf_P(cmd, PSTR("G92 E"));
11466-
dtostrf(pos_e, 6, 3, cmd + strlen(cmd));
11452+
sprintf_P(cmd, PSTR("G92 E%6.3f"), pos_e);
1146711453
enquecommand(cmd);
1146811454
if (eeprom_read_byte((uint8_t*)EEPROM_UVLO_E_ABS))
1146911455
enquecommand_P(PSTR("M82")); //E axis abslute mode
@@ -11957,7 +11943,7 @@ void M600_wait_for_user(float HotendTempBckp) {
1195711943
else {
1195811944
counterBeep = 20; //beeper will be inactive during waiting for nozzle preheat
1195911945
lcd_set_cursor(1, 4);
11960-
lcd_print(ftostr3(degHotend(active_extruder)));
11946+
lcd_printf_P(PSTR("%3d"), (int16_t)degHotend(active_extruder));
1196111947
}
1196211948
break;
1196311949

Firmware/ultralcd.cpp

+10-22
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ uint8_t farm_timer = 8;
8686
bool printer_connected = true;
8787

8888
static ShortTimer display_time; //just timer for showing pid finished message on lcd;
89-
float pid_temp = DEFAULT_PID_TEMP;
89+
static uint16_t pid_temp = DEFAULT_PID_TEMP;
9090

9191
static bool forceMenuExpire = false;
9292
static bool lcd_autoDeplete;
@@ -1018,8 +1018,7 @@ void lcd_commands()
10181018
lcd_commands_step = 3;
10191019
}
10201020
if (lcd_commands_step == 3 && !blocks_queued()) { //PID calibration
1021-
strcpy(cmd1, "M303 E0 S");
1022-
strcat(cmd1, ftostr3(pid_temp));
1021+
sprintf_P(cmd1, PSTR("M303 E0 S%3u"), pid_temp);
10231022
// setting the correct target temperature (for visualization) is done in PID_autotune
10241023
enquecommand(cmd1);
10251024
lcd_setstatuspgm(_i("PID cal."));////MSG_PID_RUNNING c=20
@@ -1031,14 +1030,9 @@ void lcd_commands()
10311030
lcd_setstatuspgm(_i("PID cal. finished"));////MSG_PID_FINISHED c=20
10321031
setAllTargetHotends(0); // reset all hotends temperature including the number displayed on the main screen
10331032
if (_Kp != 0 || _Ki != 0 || _Kd != 0) {
1034-
strcpy(cmd1, "M301 P");
1035-
strcat(cmd1, ftostr32(_Kp));
1036-
strcat(cmd1, " I");
1037-
strcat(cmd1, ftostr32(_Ki));
1038-
strcat(cmd1, " D");
1039-
strcat(cmd1, ftostr32(_Kd));
1040-
enquecommand(cmd1);
1041-
enquecommand_P(PSTR("M500"));
1033+
sprintf_P(cmd1, PSTR("M301 P%.2f I%.2f D%.2f"), _Kp, _Ki, _Kd);
1034+
enquecommand(cmd1);
1035+
enquecommand_P(PSTR("M500"));
10421036
}
10431037
else {
10441038
SERIAL_ECHOPGM("Invalid PID cal. results. Not stored to EEPROM.");
@@ -2791,7 +2785,7 @@ void pid_extruder()
27912785
if (pid_temp < HEATER_0_MINTEMP) pid_temp = HEATER_0_MINTEMP;
27922786
lcd_encoder = 0;
27932787
lcd_set_cursor(1, 2);
2794-
lcd_print(ftostr3(pid_temp));
2788+
lcd_printf_P(PSTR("%3u"), pid_temp);
27952789
if (lcd_clicked()) {
27962790
lcd_commands_type = LcdCommands::PidExtruder;
27972791
lcd_return_to_status();
@@ -2897,9 +2891,7 @@ bool lcd_wait_for_pinda(float temp) {
28972891

28982892
lcd_set_cursor(0, 4);
28992893
lcd_print(LCD_STR_THERMOMETER[0]);
2900-
lcd_print(ftostr3(current_temperature_pinda));
2901-
lcd_print('/');
2902-
lcd_print(ftostr3(temp));
2894+
lcd_printf_P(PSTR("%3d/%3d"), (int16_t)current_temperature_pinda, (int16_t) temp);
29032895
lcd_print(LCD_STR_DEGREE[0]);
29042896
delay_keep_alive(1000);
29052897
serialecho_temperatures();
@@ -2917,9 +2909,7 @@ void lcd_wait_for_heater() {
29172909
lcd_display_message_fullscreen_P(_T(MSG_WIZARD_HEATING));
29182910
lcd_set_cursor(0, 4);
29192911
lcd_print(LCD_STR_THERMOMETER[0]);
2920-
lcd_print(ftostr3(degHotend(active_extruder)));
2921-
lcd_print('/');
2922-
lcd_print(ftostr3(degTargetHotend(active_extruder)));
2912+
lcd_printf_P(PSTR("%3d/%3d"), (int16_t)degHotend(active_extruder), (int16_t) degTargetHotend(active_extruder));
29232913
lcd_print(LCD_STR_DEGREE[0]);
29242914
}
29252915

@@ -2933,14 +2923,12 @@ void lcd_wait_for_cool_down() {
29332923

29342924
lcd_set_cursor(0, 4);
29352925
lcd_print(LCD_STR_THERMOMETER[0]);
2936-
lcd_print(ftostr3(degHotend(0)));
2937-
lcd_print("/0");
2926+
lcd_printf_P(PSTR("%3d/0"), (int16_t)degHotend(0));
29382927
lcd_print(LCD_STR_DEGREE[0]);
29392928

29402929
lcd_set_cursor(9, 4);
29412930
lcd_print(LCD_STR_BEDTEMP[0]);
2942-
lcd_print(ftostr3(degBed()));
2943-
lcd_print("/0");
2931+
lcd_printf_P(PSTR("%3d/0"), (int16_t)degBed());
29442932
lcd_print(LCD_STR_DEGREE[0]);
29452933
delay_keep_alive(1000);
29462934
serialecho_temperatures();

0 commit comments

Comments
 (0)