From 3ab57086b15975bc4f75fac3dcafdcf598e64772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Thu, 24 Feb 2022 00:56:27 +0000 Subject: [PATCH 1/9] Remove one call to dtostrf I could not see any difference in the functionality when testing this locally Changes save 818 bytes of flash memory (I suspect this is due to dtostrf no longer being called in the firmware for MK3S?) --- Firmware/Marlin_main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index b069a8c48..aff3fa62a 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -11462,8 +11462,7 @@ void restore_print_from_eeprom(bool mbl_was_active) { enquecommand(cmd); // Recover final E axis position and mode float pos_e = eeprom_read_float((float*)(EEPROM_UVLO_CURRENT_POSITION_E)); - sprintf_P(cmd, PSTR("G92 E")); - dtostrf(pos_e, 6, 3, cmd + strlen(cmd)); + sprintf_P(cmd, PSTR("G92 E%6.3f"), pos_e); enquecommand(cmd); if (eeprom_read_byte((uint8_t*)EEPROM_UVLO_E_ABS)) enquecommand_P(PSTR("M82")); //E axis abslute mode From 0e6ff38b611d2b47483234ef3c6ca4f67045bdd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Fri, 25 Feb 2022 21:17:14 +0000 Subject: [PATCH 2/9] Optimise strcpy calls to use strcpy_P Saves 24 bytes of SRAM --- Firmware/ultralcd.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index bd7761ef7..c5b3e63ac 100755 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -1018,7 +1018,7 @@ void lcd_commands() lcd_commands_step = 3; } if (lcd_commands_step == 3 && !blocks_queued()) { //PID calibration - strcpy(cmd1, "M303 E0 S"); + strcpy_P(cmd1, PSTR("M303 E0 S")); strcat(cmd1, ftostr3(pid_temp)); // setting the correct target temperature (for visualization) is done in PID_autotune enquecommand(cmd1); @@ -1031,11 +1031,11 @@ void lcd_commands() lcd_setstatuspgm(_i("PID cal. finished"));////MSG_PID_FINISHED c=20 setAllTargetHotends(0); // reset all hotends temperature including the number displayed on the main screen if (_Kp != 0 || _Ki != 0 || _Kd != 0) { - strcpy(cmd1, "M301 P"); + strcpy_P(cmd1, PSTR("M301 P")); strcat(cmd1, ftostr32(_Kp)); - strcat(cmd1, " I"); + strcat_P(cmd1, PSTR(" I")); strcat(cmd1, ftostr32(_Ki)); - strcat(cmd1, " D"); + strcat_P(cmd1, PSTR(" D")); strcat(cmd1, ftostr32(_Kd)); enquecommand(cmd1); enquecommand_P(PSTR("M500")); From 6d98e99356f997282c57d56f7b92f9a6e1b9a120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 26 Feb 2022 15:25:30 +0000 Subject: [PATCH 3/9] Use sprintf_P for M301 instead of ftostr32 Also fixed indentation of if statement Saves 248 bytes of flash --- Firmware/ultralcd.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index c5b3e63ac..2e8072e39 100755 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -1031,14 +1031,9 @@ void lcd_commands() lcd_setstatuspgm(_i("PID cal. finished"));////MSG_PID_FINISHED c=20 setAllTargetHotends(0); // reset all hotends temperature including the number displayed on the main screen if (_Kp != 0 || _Ki != 0 || _Kd != 0) { - strcpy_P(cmd1, PSTR("M301 P")); - strcat(cmd1, ftostr32(_Kp)); - strcat_P(cmd1, PSTR(" I")); - strcat(cmd1, ftostr32(_Ki)); - strcat_P(cmd1, PSTR(" D")); - strcat(cmd1, ftostr32(_Kd)); - enquecommand(cmd1); - enquecommand_P(PSTR("M500")); + sprintf_P(cmd1, PSTR("M301 P%.2f I%.2f D%.2f"), _Kp, _Ki, _Kd); + enquecommand(cmd1); + enquecommand_P(PSTR("M500")); } else { SERIAL_ECHOPGM("Invalid PID cal. results. Not stored to EEPROM."); From 8e85242c8fc20b43eebd45aabf8655a6be9d69f9 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Sun, 27 Feb 2022 10:19:08 +0100 Subject: [PATCH 4/9] Temperature selection is int16_t. Use sprintf for PID start command. Saves 90B of flash and 2B of ram --- Firmware/ultralcd.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 2e8072e39..26ae4d05c 100755 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -86,7 +86,7 @@ uint8_t farm_timer = 8; bool printer_connected = true; static ShortTimer display_time; //just timer for showing pid finished message on lcd; -float pid_temp = DEFAULT_PID_TEMP; +static uint16_t pid_temp = DEFAULT_PID_TEMP; static bool forceMenuExpire = false; static bool lcd_autoDeplete; @@ -1018,8 +1018,7 @@ void lcd_commands() lcd_commands_step = 3; } if (lcd_commands_step == 3 && !blocks_queued()) { //PID calibration - strcpy_P(cmd1, PSTR("M303 E0 S")); - strcat(cmd1, ftostr3(pid_temp)); + sprintf_P(cmd1, PSTR("M303 E0 S%3u"), pid_temp); // setting the correct target temperature (for visualization) is done in PID_autotune enquecommand(cmd1); lcd_setstatuspgm(_i("PID cal."));////MSG_PID_RUNNING c=20 @@ -2786,7 +2785,7 @@ void pid_extruder() if (pid_temp < HEATER_0_MINTEMP) pid_temp = HEATER_0_MINTEMP; lcd_encoder = 0; lcd_set_cursor(1, 2); - lcd_print(ftostr3(pid_temp)); + lcd_printf_P(PSTR("%3u"), pid_temp); if (lcd_clicked()) { lcd_commands_type = LcdCommands::PidExtruder; lcd_return_to_status(); From efde923e57e1fdcc057264c1aab4c84b8addf714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 27 Feb 2022 10:42:41 +0000 Subject: [PATCH 5/9] Purge ftostr3 Saves 54 bytes of Flash and 2 bytes of SRAM --- Firmware/Marlin_main.cpp | 2 +- Firmware/ultralcd.cpp | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index aff3fa62a..ceeadb5cb 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -11956,7 +11956,7 @@ void M600_wait_for_user(float HotendTempBckp) { else { counterBeep = 20; //beeper will be inactive during waiting for nozzle preheat lcd_set_cursor(1, 4); - lcd_print(ftostr3(degHotend(active_extruder))); + lcd_printf_P(PSTR("%3d"), (int16_t)degHotend(active_extruder)); } break; diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 26ae4d05c..eb75daecb 100755 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -2891,9 +2891,7 @@ bool lcd_wait_for_pinda(float temp) { lcd_set_cursor(0, 4); lcd_print(LCD_STR_THERMOMETER[0]); - lcd_print(ftostr3(current_temperature_pinda)); - lcd_print('/'); - lcd_print(ftostr3(temp)); + lcd_printf_P(PSTR("%3d/%3d"), (int16_t)current_temperature_pinda, (int16_t) temp); lcd_print(LCD_STR_DEGREE[0]); delay_keep_alive(1000); serialecho_temperatures(); @@ -2911,9 +2909,7 @@ void lcd_wait_for_heater() { lcd_display_message_fullscreen_P(_T(MSG_WIZARD_HEATING)); lcd_set_cursor(0, 4); lcd_print(LCD_STR_THERMOMETER[0]); - lcd_print(ftostr3(degHotend(active_extruder))); - lcd_print('/'); - lcd_print(ftostr3(degTargetHotend(active_extruder))); + lcd_printf_P(PSTR("%3d/%3d"), (int16_t)degHotend(active_extruder), (int16_t) degTargetHotend(active_extruder)); lcd_print(LCD_STR_DEGREE[0]); } @@ -2927,14 +2923,12 @@ void lcd_wait_for_cool_down() { lcd_set_cursor(0, 4); lcd_print(LCD_STR_THERMOMETER[0]); - lcd_print(ftostr3(degHotend(0))); - lcd_print("/0"); + lcd_printf_P(PSTR("%3d/0"), (int16_t)degHotend(0)); lcd_print(LCD_STR_DEGREE[0]); lcd_set_cursor(9, 4); lcd_print(LCD_STR_BEDTEMP[0]); - lcd_print(ftostr3(degBed())); - lcd_print("/0"); + lcd_printf_P(PSTR("%3d/0"), (int16_t)degBed()); lcd_print(LCD_STR_DEGREE[0]); delay_keep_alive(1000); serialecho_temperatures(); From bb56c35b87834a52b9f81edc633881be6dfdd26c Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Sun, 27 Feb 2022 20:35:27 +0100 Subject: [PATCH 6/9] Optimise PINDA cal status Serial print "PINDA cal status:" is always printed no matter what cal_status is so we can pull that out of the conditional statment. cal_status is also a boolean, lets just print it directly. Its a simpler code. Saves 32 bytes of flash and 22 bytes of SRAM Change serial messages to PGM --- Firmware/Marlin_main.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index ceeadb5cb..0ed2f5b09 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -8315,12 +8315,13 @@ Sigma_Exit: - `S` - Microsteps - `I` - Table index */ - case 861: + case 861: { + const char * const _header = PSTR("index, temp, ustep, um"); if (code_seen('?')) { // ? - Print out current EEPROM offset values - uint8_t cal_status = calibration_status_pinda(); int16_t usteps = 0; - cal_status ? SERIAL_PROTOCOLLN("PINDA cal status: 1") : SERIAL_PROTOCOLLN("PINDA cal status: 0"); - SERIAL_PROTOCOLLN("index, temp, ustep, um"); + SERIAL_PROTOCOLPGM("PINDA cal status: "); + SERIAL_PROTOCOLLN(calibration_status_pinda()); + SERIAL_PROTOCOLLNRPGM(_header); for (uint8_t i = 0; i < 6; i++) { if(i > 0) { @@ -8349,7 +8350,7 @@ Sigma_Exit: eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 3, z_shift); z_shift = 120; //60C - 300um - 120usteps eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + 4, z_shift); - SERIAL_PROTOCOLLN("factory restored"); + SERIAL_PROTOCOLLNPGM("factory restored"); } else if (code_seen('Z')) { // Z - Set all values to 0 (effectively disabling PINDA temperature compensation) eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 1); @@ -8357,7 +8358,7 @@ Sigma_Exit: for (uint8_t i = 0; i < 5; i++) { eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + i, z_shift); } - SERIAL_PROTOCOLLN("zerorized"); + SERIAL_PROTOCOLLNPGM("zerorized"); } else if (code_seen('S')) { // Sxxx Iyyy - Set compensation ustep value S for compensation table index I int16_t usteps = code_value_short(); @@ -8365,8 +8366,8 @@ Sigma_Exit: uint8_t index = code_value_uint8(); if (index < 5) { eeprom_update_word((uint16_t*)EEPROM_PROBE_TEMP_SHIFT + index, usteps); - SERIAL_PROTOCOLLN("OK"); - SERIAL_PROTOCOLLN("index, temp, ustep, um"); + SERIAL_PROTOCOLLNRPGM(MSG_OK); + SERIAL_PROTOCOLLNRPGM(_header); for (uint8_t i = 0; i < 6; i++) { usteps = 0; @@ -8387,9 +8388,9 @@ Sigma_Exit: } } else { - SERIAL_PROTOCOLPGM("no valid command"); + SERIAL_PROTOCOLLNPGM("no valid command"); } - break; + } break; #endif //PINDA_THERMISTOR From af36f654d1bc33c6b8b0e04aa1b2af9c99aff859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 27 Feb 2022 16:03:24 +0000 Subject: [PATCH 7/9] Optimise a few uses of SERIAL_PROTOCOLLN Saves 34 bytes of flash memory and reduces code by 8 lines. --- Firmware/Marlin_main.cpp | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 0ed2f5b09..c7480c4cf 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -3676,9 +3676,7 @@ void gcode_M114() SERIAL_PROTOCOLPGM(" Z:"); SERIAL_PROTOCOL(float(st_get_position(Z_AXIS)) / cs.axis_steps_per_unit[Z_AXIS]); SERIAL_PROTOCOLPGM(" E:"); - SERIAL_PROTOCOL(float(st_get_position(E_AXIS)) / cs.axis_steps_per_unit[E_AXIS]); - - SERIAL_PROTOCOLLN(); + SERIAL_PROTOCOLLN(float(st_get_position(E_AXIS)) / cs.axis_steps_per_unit[E_AXIS]); } #if (defined(FANCHECK) && (((defined(TACH_0) && (TACH_0 >-1)) || (defined(TACH_1) && (TACH_1 > -1))))) @@ -6158,8 +6156,7 @@ if(eSoundMode!=e_SOUND_MODE_SILENT) SERIAL_PROTOCOL('.'); SERIAL_PROTOCOL(uint8_t(ip[2])); SERIAL_PROTOCOL('.'); - SERIAL_PROTOCOL(uint8_t(ip[3])); - SERIAL_PROTOCOLLN(); + SERIAL_PROTOCOLLN(uint8_t(ip[3])); } else { SERIAL_PROTOCOLPGM("?Toshiba FlashAir GetIP failed\n"); } @@ -7730,8 +7727,7 @@ Sigma_Exit: SERIAL_PROTOCOL(" Servo "); SERIAL_PROTOCOL(servo_index); SERIAL_PROTOCOL(": "); - SERIAL_PROTOCOL(servos[servo_index].read()); - SERIAL_PROTOCOLLN(); + SERIAL_PROTOCOLLN(servos[servo_index].read()); } } break; @@ -7839,8 +7835,7 @@ Sigma_Exit: SERIAL_PROTOCOL(" i:"); SERIAL_PROTOCOL(unscalePID_i(cs.bedKi)); SERIAL_PROTOCOL(" d:"); - SERIAL_PROTOCOL(unscalePID_d(cs.bedKd)); - SERIAL_PROTOCOLLN(); + SERIAL_PROTOCOLLN(unscalePID_d(cs.bedKd)); } break; #endif //PIDTEMP @@ -8272,8 +8267,7 @@ Sigma_Exit: LCD_MESSAGERPGM(_T(MSG_PLEASE_WAIT)); SERIAL_PROTOCOLPGM("Wait for PINDA target temperature:"); - SERIAL_PROTOCOL(set_target_pinda); - SERIAL_PROTOCOLLN(); + SERIAL_PROTOCOLLN(set_target_pinda); codenum = _millis(); cancel_heatup = false; @@ -8334,8 +8328,7 @@ Sigma_Exit: SERIAL_PROTOCOLPGM(", "); SERIAL_PROTOCOL(usteps); SERIAL_PROTOCOLPGM(", "); - SERIAL_PROTOCOL(mm * 1000); - SERIAL_PROTOCOLLN(); + SERIAL_PROTOCOLLN(mm * 1000); } } else if (code_seen('!')) { // ! - Set factory default values @@ -8381,8 +8374,7 @@ Sigma_Exit: SERIAL_PROTOCOLPGM(", "); SERIAL_PROTOCOL(usteps); SERIAL_PROTOCOLPGM(", "); - SERIAL_PROTOCOL(mm * 1000); - SERIAL_PROTOCOLLN(); + SERIAL_PROTOCOLLN(mm * 1000); } } } From 97c371e5e86b6c844252422db26741deef64fbf1 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Sun, 27 Feb 2022 20:55:40 +0100 Subject: [PATCH 8/9] Make the "ln" functions no-inline. Save 348B of flash --- Firmware/Marlin.h | 9 ++++++--- Firmware/Marlin_main.cpp | 26 ++++++++++---------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/Firmware/Marlin.h b/Firmware/Marlin.h index 5afaad56c..57ef18d8b 100755 --- a/Firmware/Marlin.h +++ b/Firmware/Marlin.h @@ -79,9 +79,9 @@ extern FILE _uartout; #define SERIAL_PROTOCOL_F(x,y) (MYSERIAL.print(x,y)) #define SERIAL_PROTOCOLPGM(x) (serialprintPGM(PSTR(x))) #define SERIAL_PROTOCOLRPGM(x) (serialprintPGM((x))) -#define SERIAL_PROTOCOLLN(x) (MYSERIAL.println(x)/*,MYSERIAL.write('\n')*/) -#define SERIAL_PROTOCOLLNPGM(x) (serialprintPGM(PSTR(x)),MYSERIAL.println()/*write('\n')*/) -#define SERIAL_PROTOCOLLNRPGM(x) (serialprintPGM((x)),MYSERIAL.println()/*write('\n')*/) +#define SERIAL_PROTOCOLLN(x) (MYSERIAL.println(x)) +#define SERIAL_PROTOCOLLNPGM(x) (serialprintlnPGM(PSTR(x))) +#define SERIAL_PROTOCOLLNRPGM(x) (serialprintlnPGM((x))) extern const char errormagic[] PROGMEM; @@ -115,6 +115,9 @@ void serial_echopair_P(const char *s_P, unsigned long v); // I'd rather skip a few CPU ticks than 5.5KB (!!) of FLASH void serialprintPGM(const char *str); +//The "ln" variant of the function above. +void serialprintlnPGM(const char *str); + bool is_buffer_empty(); void process_commands(); void ramming(); diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index c7480c4cf..17aa5481c 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -467,22 +467,16 @@ void serial_echopair_P(const char *s_P, double v) void serial_echopair_P(const char *s_P, unsigned long v) { serialprintPGM(s_P); SERIAL_ECHO(v); } -/*FORCE_INLINE*/ void serialprintPGM(const char *str) -{ -#if 0 - char ch=pgm_read_byte(str); - while(ch) - { - MYSERIAL.write(ch); - ch=pgm_read_byte(++str); - } -#else - // hmm, same size as the above version, the compiler did a good job optimizing the above - while( uint8_t ch = pgm_read_byte(str) ){ - MYSERIAL.write((char)ch); - ++str; - } -#endif +void serialprintPGM(const char *str) { + while(uint8_t ch = pgm_read_byte(str)) { + MYSERIAL.write((char)ch); + ++str; + } +} + +void serialprintlnPGM(const char *str) { + serialprintPGM(str); + MYSERIAL.println(); } #ifdef SDSUPPORT From 4a02ff3de833c46017401ed4e131284efcea7bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Mon, 28 Feb 2022 07:43:01 +0000 Subject: [PATCH 9/9] Change SERIAL_PROTOCOL to SERIAL_PROTOCOLPGM in a few lines Saves 12 bytes of SRAM but increases flash use by 12 bytes. --- Firmware/Marlin_main.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 17aa5481c..c2623a3e0 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -7786,14 +7786,14 @@ Sigma_Exit: updatePID(); SERIAL_PROTOCOLRPGM(MSG_OK); - SERIAL_PROTOCOL(" p:"); + SERIAL_PROTOCOLPGM(" p:"); SERIAL_PROTOCOL(cs.Kp); - SERIAL_PROTOCOL(" i:"); + SERIAL_PROTOCOLPGM(" i:"); SERIAL_PROTOCOL(unscalePID_i(cs.Ki)); - SERIAL_PROTOCOL(" d:"); + SERIAL_PROTOCOLPGM(" d:"); SERIAL_PROTOCOL(unscalePID_d(cs.Kd)); #ifdef PID_ADD_EXTRUSION_RATE - SERIAL_PROTOCOL(" c:"); + SERIAL_PROTOCOLPGM(" c:"); //Kc does not have scaling applied above, or in resetting defaults SERIAL_PROTOCOL(Kc); #endif @@ -7824,11 +7824,11 @@ Sigma_Exit: updatePID(); SERIAL_PROTOCOLRPGM(MSG_OK); - SERIAL_PROTOCOL(" p:"); + SERIAL_PROTOCOLPGM(" p:"); SERIAL_PROTOCOL(cs.bedKp); - SERIAL_PROTOCOL(" i:"); + SERIAL_PROTOCOLPGM(" i:"); SERIAL_PROTOCOL(unscalePID_i(cs.bedKi)); - SERIAL_PROTOCOL(" d:"); + SERIAL_PROTOCOLPGM(" d:"); SERIAL_PROTOCOLLN(unscalePID_d(cs.bedKd)); } break;