From e8b2043d039f67c7284793b4b589cb8f82b99b7b Mon Sep 17 00:00:00 2001
From: Robert Pelnar <robert.pelnar@seznam.cz>
Date: Sun, 10 Dec 2017 20:38:09 +0100
Subject: [PATCH] Stack monitor in stepper ISR

---
 Firmware/Configuration_prusa.h |  1 +
 Firmware/stepper.cpp           |  8 ++++++++
 Firmware/ultralcd.cpp          | 20 ++++++++++++++++----
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/Firmware/Configuration_prusa.h b/Firmware/Configuration_prusa.h
index fd3aa2a3..911991a4 100644
--- a/Firmware/Configuration_prusa.h
+++ b/Firmware/Configuration_prusa.h
@@ -96,6 +96,7 @@ const bool Z_MIN_ENDSTOP_INVERTING = false; // set to true to invert the logic o
 //#define _NO_ASM
 #define DEBUG_DCODES //D codes
 #if 1
+#define DEBUG_STACK_MONITOR        //Stack monitor in stepper ISR
 //#define DEBUG_FSENSOR_LOG          //Reports fsensor status to serial
 //#define DEBUG_CRASHDET_COUNTERS  //Display crash-detection counters on LCD
 //#define DEBUG_RESUME_PRINT       //Resume/save print debug enable 
diff --git a/Firmware/stepper.cpp b/Firmware/stepper.cpp
index 40720db1..1ca29ad4 100644
--- a/Firmware/stepper.cpp
+++ b/Firmware/stepper.cpp
@@ -41,6 +41,10 @@
 int fsensor_counter = 0; //counter for e-steps
 #endif //PAT9125
 
+#ifdef DEBUG_STACK_MONITOR
+uint16_t SP_min = 0x21FF;
+#endif //DEBUG_STACK_MONITOR
+
 //===========================================================================
 //=============================public variables  ============================
 //===========================================================================
@@ -371,6 +375,10 @@ FORCE_INLINE void trapezoid_generator_reset() {
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
 ISR(TIMER1_COMPA_vect) {
+#ifdef DEBUG_STACK_MONITOR
+	uint16_t sp = SPL + 256 * SPH;
+	if (sp < SP_min) SP_min = sp;
+#endif //DEBUG_STACK_MONITOR
   #ifdef LIN_ADVANCE
     advance_isr_scheduler();
   #else
diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp
index 35497717..432a13e5 100644
--- a/Firmware/ultralcd.cpp
+++ b/Firmware/ultralcd.cpp
@@ -1534,10 +1534,8 @@ static void lcd_menu_fails_stats()
     lcd.print(" Filament fails:    ");
     lcd.setCursor(17, 3);
     lcd.print(itostr3((int)ferror_count));
-    
 
-    
-    if (lcd_clicked())
+	if (lcd_clicked())
     {
         lcd_quick_feedback();
         lcd_return_to_status();
@@ -1545,6 +1543,18 @@ static void lcd_menu_fails_stats()
     
 }
 
+extern uint16_t SP_min;
+static void lcd_menu_debug()
+{
+	fprintf_P(lcdout, PSTR(ESC_H(1,1)"SP_min: 0x%04x"), SP_min);
+
+	if (lcd_clicked())
+    {
+        lcd_quick_feedback();
+        lcd_return_to_status();
+    }
+}
+
 static void lcd_menu_temperatures()
 {
 	fprintf_P(lcdout, PSTR(ESC_H(1,1) "Ambient:  %d%c" ESC_H(1,2) "PINDA:    %d%c"), (int)current_temperature_ambient, '\x01', (int)current_temperature_pinda, '\x01');
@@ -4931,7 +4941,9 @@ static void lcd_main_menu()
   MENU_ITEM(submenu, MSG_SUPPORT, lcd_support_menu);
     
   MENU_ITEM(submenu, PSTR("Fail stats"), lcd_menu_fails_stats);
-    
+
+  MENU_ITEM(submenu, PSTR("Debug"), lcd_menu_debug);
+
   END_MENU();
 
 }