From 0ddb4d88a98bec8d449d186c9149e2df31017d2e Mon Sep 17 00:00:00 2001
From: Scott Lahteine <thinkyhead@users.noreply.github.com>
Date: Mon, 28 Sep 2020 16:37:00 -0500
Subject: [PATCH] Add extra CardReader debugging

---
 Marlin/src/sd/cardreader.cpp | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp
index 49ee7747a2..ec834564ec 100644
--- a/Marlin/src/sd/cardreader.cpp
+++ b/Marlin/src/sd/cardreader.cpp
@@ -55,6 +55,7 @@
 
 #define DEBUG_OUT EITHER(DEBUG_CARDREADER, MARLIN_DEV_MODE)
 #include "../core/debug_out.h"
+#include "../libs/hex_print.h"
 
 // public:
 
@@ -796,9 +797,12 @@ const char* CardReader::diveToFile(const bool update_cwd, SdFile*& diveDir, cons
   // Parsing the path string
   const char *item_name_adr = path;
 
+  DEBUG_ECHOLNPAIR("diveToFile: path = '", path, "'");
+
   if (path[0] == '/') {               // Starting at the root directory?
     diveDir = &root;
     item_name_adr++;
+    DEBUG_ECHOLNPAIR("diveToFile: CWD to root: ", hex_address((void*)diveDir));
     if (update_cwd) workDirDepth = 0; // The cwd can be updated for the benefit of sub-programs
   }
   else
@@ -806,6 +810,8 @@ const char* CardReader::diveToFile(const bool update_cwd, SdFile*& diveDir, cons
 
   startDir = diveDir;
 
+  DEBUG_ECHOLNPAIR("diveToFile: startDir = ", hex_address((void*)startDir));
+
   while (item_name_adr) {
     // Find next subdirectory delimiter
     char * const name_end = strchr(item_name_adr, '/');
@@ -821,6 +827,8 @@ const char* CardReader::diveToFile(const bool update_cwd, SdFile*& diveDir, cons
 
     if (echo) SERIAL_ECHOLN(dosSubdirname);
 
+    DEBUG_ECHOLNPAIR("diveToFile: sub = ", hex_address((void*)sub));
+
     // Open diveDir (closing first)
     sub->close();
     if (!sub->open(diveDir, dosSubdirname, O_READ)) {
@@ -830,19 +838,25 @@ const char* CardReader::diveToFile(const bool update_cwd, SdFile*& diveDir, cons
     }
 
     // Close diveDir if not at starting-point
-    if (diveDir != startDir) diveDir->close();
+    if (diveDir != startDir) {
+      DEBUG_ECHOLNPAIR("diveToFile: closing diveDir: ", hex_address((void*)diveDir));
+      diveDir->close();
+    }
 
     // diveDir now subDir
     diveDir = sub;
+    DEBUG_ECHOLNPAIR("diveToFile: diveDir = sub: ", hex_address((void*)diveDir));
 
     // Update workDirParents and workDirDepth
     if (update_cwd) {
+      DEBUG_ECHOLNPAIR("diveToFile: update_cwd");
       if (workDirDepth < MAX_DIR_DEPTH)
         workDirParents[workDirDepth++] = *diveDir;
     }
 
     // Point sub at the other scratch object
     sub = (diveDir != &newDir1) ? &newDir1 : &newDir2;
+    DEBUG_ECHOLNPAIR("diveToFile: swapping sub = ", hex_address((void*)sub));
 
     // Next path atom address
     item_name_adr = name_end + 1;
@@ -850,6 +864,7 @@ const char* CardReader::diveToFile(const bool update_cwd, SdFile*& diveDir, cons
 
   if (update_cwd) {
     workDir = *diveDir;
+    DEBUG_ECHOLNPAIR("diveToFile: final workDir = ", hex_address((void*)diveDir));
     flag.workDirIsRoot = (workDirDepth == 0);
     TERN_(SDCARD_SORT_ALPHA, presort());
   }