From 7577717b5915b58927b27c4a2bca5e3bf22a787b Mon Sep 17 00:00:00 2001
From: daid303 <daid303@gmail.com>
Date: Wed, 12 Dec 2012 13:08:26 +0100
Subject: [PATCH 1/7] Fixed #336

---
 Marlin/thermistortables.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/Marlin/thermistortables.h b/Marlin/thermistortables.h
index d963afff1a..cdf591b2c4 100644
--- a/Marlin/thermistortables.h
+++ b/Marlin/thermistortables.h
@@ -474,7 +474,7 @@ const short temptable_55[][2] PROGMEM = {
 
 //Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
 #ifndef HEATER_0_RAW_HI_TEMP
-# if HEATER_0_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
+# ifdef HEATER_0_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
 #  define HEATER_0_RAW_HI_TEMP 0
 #  define HEATER_0_RAW_LO_TEMP 16383
 # else                          //In case of an thermocouple the highest temperature results in the highest ADC value
@@ -497,7 +497,7 @@ const short temptable_55[][2] PROGMEM = {
 
 //Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
 #ifndef HEATER_1_RAW_HI_TEMP
-# if HEATER_1_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
+# ifdef HEATER_1_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
 #  define HEATER_1_RAW_HI_TEMP 0
 #  define HEATER_1_RAW_LO_TEMP 16383
 # else                          //In case of an thermocouple the highest temperature results in the highest ADC value
@@ -520,7 +520,7 @@ const short temptable_55[][2] PROGMEM = {
 
 //Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
 #ifndef HEATER_2_RAW_HI_TEMP
-# if HEATER_2_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
+# ifdef HEATER_2_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
 #  define HEATER_2_RAW_HI_TEMP 0
 #  define HEATER_2_RAW_LO_TEMP 16383
 # else                          //In case of an thermocouple the highest temperature results in the highest ADC value
@@ -540,7 +540,7 @@ const short temptable_55[][2] PROGMEM = {
 
 //Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
 #ifndef HEATER_BED_RAW_HI_TEMP
-# if BED_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
+# ifdef BED_USES_THERMISTOR   //In case of a thermistor the highest temperature results in the lowest ADC value
 #  define HEATER_BED_RAW_HI_TEMP 0
 #  define HEATER_BED_RAW_LO_TEMP 16383
 # else                          //In case of an thermocouple the highest temperature results in the highest ADC value

From 65c3f1ffcd4067203f87940718cea88a98531550 Mon Sep 17 00:00:00 2001
From: daid303 <daid303@gmail.com>
Date: Wed, 12 Dec 2012 21:56:17 +0100
Subject: [PATCH 2/7] Most likely fixing #338

---
 Marlin/temperature.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 5a0762d9f1..1082bffc9f 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -1042,6 +1042,7 @@ ISR(TIMER0_COMPB_vect)
 #if EXTRUDERS > 2
       current_temperature_raw[2] = raw_temp_2_value;
 #endif
+      current_temperature_bed_raw = raw_temp_bed_value;
     }
     
     temp_meas_ready = true;

From f30b46b2a8e5e4d293598c47fbea31bc26c1e492 Mon Sep 17 00:00:00 2001
From: daid303 <daid303@gmail.com>
Date: Wed, 12 Dec 2012 23:06:17 +0100
Subject: [PATCH 3/7] One more fix for #338, I really should get a heated bed
 to test with.

---
 Marlin/temperature.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 1082bffc9f..34f4cb4ddd 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -1102,9 +1102,9 @@ ISR(TIMER0_COMPB_vect)
   /* No bed MINTEMP error? */
 #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
 # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
-    if(current_temperature_bed <= bed_maxttemp_raw) {
+    if(current_temperature_bed_raw <= bed_maxttemp_raw) {
 #else
-    if(current_temperature_bed >= bed_maxttemp_raw) {
+    if(current_temperature_bed_raw >= bed_maxttemp_raw) {
 #endif
        target_temperature_bed = 0;
        bed_max_temp_error();

From 16fbb455effa317735add5c994e7a91f5dc272fd Mon Sep 17 00:00:00 2001
From: Kevin O'Connor <kevin@koconnor.net>
Date: Wed, 12 Dec 2012 23:15:28 -0500
Subject: [PATCH 4/7] Use linker "--relax" option.

The "relax" option enables the linker to convert certain "call"
instructions to the smaller "rcall" instruction.  This reduces the
size of the resulting binary.
---
 Marlin/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Marlin/Makefile b/Marlin/Makefile
index 930a200719..a004d36d56 100644
--- a/Marlin/Makefile
+++ b/Marlin/Makefile
@@ -254,7 +254,7 @@ endif
 CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CEXTRA) $(CTUNING)
 CXXFLAGS =         $(CDEFS) $(CINCS) -O$(OPT) -Wall    $(CEXTRA) $(CTUNING)
 #ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 
-LDFLAGS = -lm
+LDFLAGS = -lm -Wl,--relax
 
 
 # Programming support using avrdude. Settings and variables.

From 0908458381ba8a84616fa445ffe62e4c6f040b42 Mon Sep 17 00:00:00 2001
From: Kevin O'Connor <kevin@koconnor.net>
Date: Wed, 12 Dec 2012 23:18:31 -0500
Subject: [PATCH 5/7] Use gcc "-fwhole-program" optimization.

Use "whole program" and "link time optimization" features of gcc.  The
whole-program optimization enables the compiler to evaluate the entire
firmware for optimization instead of just one code file at a time.
This leads to better overall optimizations.
---
 Marlin/Makefile | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/Marlin/Makefile b/Marlin/Makefile
index a004d36d56..9eed5cdf3a 100644
--- a/Marlin/Makefile
+++ b/Marlin/Makefile
@@ -227,6 +227,22 @@ OPT = s
 
 DEFINES ?=
 
+# Program settings
+CC = $(AVR_TOOLS_PATH)avr-gcc
+CXX = $(AVR_TOOLS_PATH)avr-g++
+OBJCOPY = $(AVR_TOOLS_PATH)avr-objcopy
+OBJDUMP = $(AVR_TOOLS_PATH)avr-objdump
+AR  = $(AVR_TOOLS_PATH)avr-ar
+SIZE = $(AVR_TOOLS_PATH)avr-size
+NM = $(AVR_TOOLS_PATH)avr-nm
+AVRDUDE = avrdude
+REMOVE = rm -f
+MV = mv -f
+
+# Tool for testing compiler flags
+cc-option=$(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`" \
+    ; then echo "$(2)"; else echo "$(3)"; fi ;)
+
 # Place -D or -U options here
 CDEFS = -DF_CPU=$(F_CPU) ${addprefix -D , $(DEFINES)}
 CXXDEFS = -DF_CPU=$(F_CPU) ${addprefix -D , $(DEFINES)}
@@ -251,8 +267,10 @@ CTUNING += -DMOTHERBOARD=${HARDWARE_MOTHERBOARD}
 endif
 #CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
 
-CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CEXTRA) $(CTUNING)
-CXXFLAGS =         $(CDEFS) $(CINCS) -O$(OPT) -Wall    $(CEXTRA) $(CTUNING)
+CFLAGS := $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CEXTRA) $(CTUNING) \
+    $(call cc-option,$(CC),-flto -fwhole-program,)
+CXXFLAGS :=         $(CDEFS) $(CINCS) -O$(OPT) -Wall    $(CEXTRA) $(CTUNING) \
+    $(call cc-option,$(CC),-flto -fwhole-program,)
 #ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs 
 LDFLAGS = -lm -Wl,--relax
 
@@ -264,18 +282,6 @@ AVRDUDE_FLAGS = -D -C $(ARDUINO_INSTALL_DIR)/hardware/tools/avrdude.conf \
 	-p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \
 	-b $(UPLOAD_RATE)
 
-# Program settings
-CC = $(AVR_TOOLS_PATH)avr-gcc
-CXX = $(AVR_TOOLS_PATH)avr-g++
-OBJCOPY = $(AVR_TOOLS_PATH)avr-objcopy
-OBJDUMP = $(AVR_TOOLS_PATH)avr-objdump
-AR  = $(AVR_TOOLS_PATH)avr-ar
-SIZE = $(AVR_TOOLS_PATH)avr-size
-NM = $(AVR_TOOLS_PATH)avr-nm
-AVRDUDE = avrdude
-REMOVE = rm -f
-MV = mv -f
-
 # Define all object files.
 OBJ = ${patsubst %.c, $(BUILD_DIR)/%.o, ${SRC}}
 OBJ += ${patsubst %.cpp, $(BUILD_DIR)/%.o, ${CXXSRC}}
@@ -372,9 +378,13 @@ extcoff: $(TARGET).elf
 	$(NM) -n $< > $@
 
 	# Link: create ELF output file from library.
-$(BUILD_DIR)/$(TARGET).elf: $(OBJ) Configuration.h
+$(BUILD_DIR)/$(TARGET).elf: $(BUILD_DIR)/$(TARGET).o
 	$(Pecho) "  CXX   $@"
-	$P $(CC) $(ALL_CXXFLAGS) -Wl,--gc-sections -o $@ -L. $(OBJ) $(LDFLAGS)
+	$P $(CC) $(ALL_CXXFLAGS) -Wl,--gc-sections -o $@ -L. $^ $(LDFLAGS)
+
+$(BUILD_DIR)/$(TARGET).o: $(OBJ) Configuration.h
+	$(Pecho) "  CXX   $@"
+	$P $(CC) $(ALL_CXXFLAGS) -nostdlib -Wl,-r -o $@ $(OBJ)
 
 $(BUILD_DIR)/%.o: %.c Configuration.h Configuration_adv.h $(MAKEFILE)
 	$(Pecho) "  CC    $<"

From 047d037f8cc67b81ff239e633914045fba00f027 Mon Sep 17 00:00:00 2001
From: daid303 <daid303@gmail.com>
Date: Thu, 13 Dec 2012 09:04:41 +0100
Subject: [PATCH 6/7] #ifdef fix for #326

---
 Marlin/ultralcd.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp
index 49cbe996da..beb79beeeb 100644
--- a/Marlin/ultralcd.cpp
+++ b/Marlin/ultralcd.cpp
@@ -476,7 +476,7 @@ static void lcd_control_temperature_preheat_pla_settings_menu()
 #if TEMP_SENSOR_BED != 0
     MENU_ITEM_EDIT(int3, MSG_BED, &plaPreheatHPBTemp, 0, BED_MAXTEMP - 15);
 #endif
-#if EEPROM_SETTINGS
+#ifdef EEPROM_SETTINGS
     MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
 #endif
     END_MENU();
@@ -491,7 +491,7 @@ static void lcd_control_temperature_preheat_abs_settings_menu()
 #if TEMP_SENSOR_BED != 0
     MENU_ITEM_EDIT(int3, MSG_BED, &absPreheatHPBTemp, 0, BED_MAXTEMP - 15);
 #endif
-#if EEPROM_SETTINGS
+#ifdef EEPROM_SETTINGS
     MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
 #endif
     END_MENU();

From 08356361ac4ab977077a4b341d6c15062a09f380 Mon Sep 17 00:00:00 2001
From: daid303 <daid303@gmail.com>
Date: Thu, 13 Dec 2012 10:25:29 +0100
Subject: [PATCH 7/7] Fixing a problem with heated beds not working without PID
 and BED_LIMIT_SWITCHING

---
 Marlin/temperature.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 34f4cb4ddd..6187672162 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -432,9 +432,9 @@ void manage_heater()
 	    soft_pwm_bed = 0;
 	  }
 
-    #elif not defined BED_LIMIT_SWITCHING
+    #elif !defined(BED_LIMIT_SWITCHING)
       // Check if temperature is within the correct range
-      if((current_temperature_bed > BED_MAXTEMP) && (current_temperature_bed < BED_MINTEMP))
+      if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
       {
         if(current_temperature_bed >= target_temperature_bed)
         {