From e05d050a1e6d30ae98f6e08e1be3e38fea26bd17 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Wed, 12 Apr 2017 19:39:26 -0500
Subject: [PATCH] Add hex routine to print an address

---
 Marlin/M100_Free_Mem_Chk.cpp  | 26 +++++++++++++-------------
 Marlin/hex_print_routines.cpp | 31 +++++++++++++++++--------------
 Marlin/hex_print_routines.h   |  2 ++
 Marlin/ubl.cpp                |  4 ++--
 Marlin/ubl_G29.cpp            |  8 ++++----
 5 files changed, 38 insertions(+), 33 deletions(-)

diff --git a/Marlin/M100_Free_Mem_Chk.cpp b/Marlin/M100_Free_Mem_Chk.cpp
index 5387a71ede..0851ff69db 100644
--- a/Marlin/M100_Free_Mem_Chk.cpp
+++ b/Marlin/M100_Free_Mem_Chk.cpp
@@ -86,7 +86,6 @@ int16_t count_test_bytes(const uint8_t * const ptr) {
   return -1;
 }
 
-
 //
 // M100 sub-commands
 //
@@ -172,7 +171,7 @@ void free_memory_pool_report(const char * const ptr, const uint16_t size) {
       const uint16_t j = count_test_bytes(addr);
       if (j > 8) {
         SERIAL_ECHOPAIR("Found ", j);
-        SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)addr));
+        SERIAL_ECHOLNPAIR(" bytes free at ", hex_address(addr));
         if (j > max_cnt) {
           max_cnt  = j;
           max_addr = addr;
@@ -185,7 +184,7 @@ void free_memory_pool_report(const char * const ptr, const uint16_t size) {
   if (block_cnt > 1) {
     SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
     SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt);
-    SERIAL_ECHOLNPAIR(" bytes at 0x", hex_word((uint16_t)max_addr));
+    SERIAL_ECHOLNPAIR(" bytes at ", hex_address(max_addr));
   }
   SERIAL_ECHOLNPAIR("check_for_free_memory_corruption() = ", check_for_free_memory_corruption("M100 F "));
 }
@@ -206,7 +205,7 @@ void free_memory_pool_report(const char * const ptr, const uint16_t size) {
       for (uint16_t i = 1; i <= size; i++) {
         char * const addr = ptr + i * j;
         *addr = i;
-        SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)addr));
+        SERIAL_ECHOPAIR("\nCorrupting address: ", hex_address(addr));
       }
       SERIAL_EOL;
     }
@@ -235,8 +234,9 @@ void init_free_memory(uint8_t *ptr, int16_t size) {
 
   for (uint16_t i = 0; i < size; i++) {
     if (((char) ptr[i]) != TEST_BYTE) {
-      SERIAL_ECHOPAIR("? address : 0x", hex_word((uint16_t)ptr + i));
-      SERIAL_ECHOLNPAIR("=", hex_byte(ptr[i]));
+      SERIAL_ECHOPAIR("? address : ", hex_address(ptr + i));
+      SERIAL_ECHOPAIR("=", hex_byte(ptr[i]));
+      SERIAL_EOL; SERIAL_EOL;
     }
   }
 }
@@ -245,13 +245,13 @@ void init_free_memory(uint8_t *ptr, int16_t size) {
  * M100: Free Memory Check
  */
 void gcode_M100() {
-  SERIAL_ECHOPAIR("\n__brkval : 0x", hex_word((uint16_t)__brkval));
-  SERIAL_ECHOPAIR("\n__bss_end: 0x", hex_word((uint16_t)&__bss_end));
+  SERIAL_ECHOPAIR("\n__brkval : ", hex_address(__brkval));
+  SERIAL_ECHOPAIR("\n__bss_end : ", hex_address(&__bss_end));
 
   uint8_t *ptr = END_OF_HEAP(), *sp = top_of_stack();
 
-  SERIAL_ECHOPAIR("\nstart of free space : 0x", hex_word((uint16_t)ptr));
-  SERIAL_ECHOLNPAIR("\nStack Pointer : 0x", hex_word((uint16_t)sp));
+  SERIAL_ECHOPAIR("\nstart of free space : ", hex_address(ptr));
+  SERIAL_ECHOLNPAIR("\nStack Pointer : ", hex_address(sp));
 
   // Always init on the first invocation of M100
   static bool m100_not_initialized = true;
@@ -287,9 +287,9 @@ int check_for_free_memory_corruption(char *title) {
 
     n = sp - ptr;
     SERIAL_ECHOPAIR("\nfmc() n=", n);
-    SERIAL_ECHOPAIR("\n&__brkval: 0x", hex_word((uint16_t)&__brkval));
-    SERIAL_ECHOPAIR("=0x",             hex_word((uint16_t)__brkval));
-    SERIAL_ECHOPAIR("\n__bss_end: 0x", hex_word((uint16_t)&__bss_end));
+    SERIAL_ECHOPAIR("\n&__brkval: ", hex_address(&__brkval));
+    SERIAL_ECHOPAIR("=",             hex_address(__brkval));
+    SERIAL_ECHOPAIR("\n__bss_end: ", hex_address(&__bss_end));
     SERIAL_ECHOPAIR(" sp=", hex_word(sp));
 
     if (sp < ptr)  {
diff --git a/Marlin/hex_print_routines.cpp b/Marlin/hex_print_routines.cpp
index 02b07754d1..39e5b4cd73 100644
--- a/Marlin/hex_print_routines.cpp
+++ b/Marlin/hex_print_routines.cpp
@@ -19,32 +19,35 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */
-
-
 #include "Marlin.h"
 #if ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(M100_FREE_MEMORY_WATCHER)
 
 #include "hex_print_routines.h"
 
-static char _hex[5] = { 0 };
+static char _hex[7] = "0x0000";
 
 char* hex_byte(const uint8_t b) {
-  _hex[0] = hex_nybble(b >> 4);
-  _hex[1] = hex_nybble(b);
-  _hex[2] = '\0';
-  return _hex;
+  _hex[4] = hex_nybble(b >> 4);
+  _hex[5] = hex_nybble(b);
+  return &_hex[4];
 }
 
 char* hex_word(const uint16_t w) {
-  _hex[0] = hex_nybble(w >> 12);
-  _hex[1] = hex_nybble(w >> 8);
-  _hex[2] = hex_nybble(w >> 4);
-  _hex[3] = hex_nybble(w);
+  _hex[2] = hex_nybble(w >> 12);
+  _hex[3] = hex_nybble(w >> 8);
+  _hex[4] = hex_nybble(w >> 4);
+  _hex[5] = hex_nybble(w);
+  return &_hex[2];
+}
+
+char* hex_address(const void * const w) {
+  (void)hex_word((uint16_t)w);
   return _hex;
 }
 
-void print_hex_nybble(const uint8_t n) { SERIAL_CHAR(hex_nybble(n)); }
-void print_hex_byte(const uint8_t b)   { SERIAL_ECHO(hex_byte(b)); }
-void print_hex_word(const uint16_t w)  { SERIAL_ECHO(hex_word(w)); }
+void print_hex_nybble(const uint8_t n)       { SERIAL_CHAR(hex_nybble(n));  }
+void print_hex_byte(const uint8_t b)         { SERIAL_ECHO(hex_byte(b));    }
+void print_hex_word(const uint16_t w)        { SERIAL_ECHO(hex_word(w));    }
+void print_hex_address(const void * const w) { SERIAL_ECHO(hex_address(w)); }
 
 #endif // AUTO_BED_LEVELING_UBL || M100_FREE_MEMORY_WATCHER
diff --git a/Marlin/hex_print_routines.h b/Marlin/hex_print_routines.h
index c5c4f759f0..ea40733318 100644
--- a/Marlin/hex_print_routines.h
+++ b/Marlin/hex_print_routines.h
@@ -36,10 +36,12 @@ inline char hex_nybble(const uint8_t n) {
 }
 char* hex_byte(const uint8_t b);
 char* hex_word(const uint16_t w);
+char* hex_address(const void * const w);
 
 void print_hex_nybble(const uint8_t n);
 void print_hex_byte(const uint8_t b);
 void print_hex_word(const uint16_t w);
+void print_hex_address(const void * const w);
 
 #endif // AUTO_BED_LEVELING_UBL || M100_FREE_MEMORY_WATCHER
 #endif // HEX_PRINT_ROUTINES_H
diff --git a/Marlin/ubl.cpp b/Marlin/ubl.cpp
index 1dce269840..056632d24d 100644
--- a/Marlin/ubl.cpp
+++ b/Marlin/ubl.cpp
@@ -118,7 +118,7 @@
     eeprom_read_block((void *)&z_values, (void *)j, sizeof(z_values));
 
     SERIAL_PROTOCOLPAIR("Mesh loaded from slot ", m);
-    SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
+    SERIAL_PROTOCOLLNPAIR(" at offset ", hex_address((void*)j));
   }
 
   void unified_bed_leveling::store_mesh(const int16_t m) {
@@ -140,7 +140,7 @@
     eeprom_write_block((const void *)&z_values, (void *)j, sizeof(z_values));
 
     SERIAL_PROTOCOLPAIR("Mesh saved in slot ", m);
-    SERIAL_PROTOCOLLNPAIR(" at offset 0x", hex_word(j));
+    SERIAL_PROTOCOLLNPAIR(" at offset ", hex_address((void*)j));
   }
 
   void unified_bed_leveling::reset() {
diff --git a/Marlin/ubl_G29.cpp b/Marlin/ubl_G29.cpp
index f6e2b13ab6..222215c777 100644
--- a/Marlin/ubl_G29.cpp
+++ b/Marlin/ubl_G29.cpp
@@ -1206,9 +1206,9 @@
     SERIAL_PROTOCOLLNPAIR("ubl_state_recursion_chk :", ubl_state_recursion_chk);
     SERIAL_EOL;
     safe_delay(50);
-    SERIAL_PROTOCOLLNPAIR("Free EEPROM space starts at: 0x", hex_word(ubl.eeprom_start));
+    SERIAL_PROTOCOLLNPAIR("Free EEPROM space starts at: ", hex_address((void*)ubl.eeprom_start));
 
-    SERIAL_PROTOCOLLNPAIR("end of EEPROM              : 0x", hex_word(E2END));
+    SERIAL_PROTOCOLLNPAIR("end of EEPROM              : ", hex_address((void*)E2END));
     safe_delay(50);
 
     SERIAL_PROTOCOLLNPAIR("sizeof(ubl) :  ", (int)sizeof(ubl));
@@ -1217,7 +1217,7 @@
     SERIAL_EOL;
     safe_delay(50);
 
-    SERIAL_PROTOCOLLNPAIR("EEPROM free for UBL: 0x", hex_word(k));
+    SERIAL_PROTOCOLLNPAIR("EEPROM free for UBL: ", hex_address((void*)k));
     safe_delay(50);
 
     SERIAL_PROTOCOLPAIR("EEPROM can hold ", k / sizeof(ubl.z_values));
@@ -1295,7 +1295,7 @@
     eeprom_read_block((void *)&tmp_z_values, (void *)j, sizeof(tmp_z_values));
 
     SERIAL_ECHOPAIR("Subtracting Mesh ", storage_slot);
-    SERIAL_PROTOCOLLNPAIR(" loaded from EEPROM address 0x", hex_word(j)); // Soon, we can remove the extra clutter of printing
+    SERIAL_PROTOCOLLNPAIR(" loaded from EEPROM address ", hex_address((void*)j)); // Soon, we can remove the extra clutter of printing
                                                                         // the address in the EEPROM where the Mesh is stored.
 
     for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
-- 
GitLab