From f9d5ee04b4930fa7b876c278e80072060463f55c Mon Sep 17 00:00:00 2001 From: ellensp <530024+ellensp@users.noreply.github.com> Date: Thu, 1 Feb 2024 07:33:42 +1300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Patch=20STM32=20serial=20UUID=20?= =?UTF-8?q?(#26737)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Followup to #26715 Co-authored-by: Scott Lahteine --- Marlin/src/feature/tmc_util.cpp | 2 +- Marlin/src/gcode/host/M115.cpp | 15 ++++---- Marlin/src/libs/hex_print.cpp | 62 +++++++++++++-------------------- Marlin/src/libs/hex_print.h | 9 ++--- 4 files changed, 38 insertions(+), 50 deletions(-) diff --git a/Marlin/src/feature/tmc_util.cpp b/Marlin/src/feature/tmc_util.cpp index bff6872e4d..421f9b0c22 100644 --- a/Marlin/src/feature/tmc_util.cpp +++ b/Marlin/src/feature/tmc_util.cpp @@ -763,7 +763,7 @@ SERIAL_CHAR('\t'); st.printLabel(); SERIAL_CHAR('\t'); - print_hex_long(drv_status, ':'); + print_hex_long(drv_status, ':', true); if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!"); SERIAL_EOL(); break; diff --git a/Marlin/src/gcode/host/M115.cpp b/Marlin/src/gcode/host/M115.cpp index d99737a261..b2f3496a68 100644 --- a/Marlin/src/gcode/host/M115.cpp +++ b/Marlin/src/gcode/host/M115.cpp @@ -88,14 +88,15 @@ void GcodeSuite::M115() { * This code should work on all STM32-based boards. */ #if ENABLED(STM32_UID_SHORT_FORM) - uint32_t * const UID = (uint32_t*)UID_BASE; - SERIAL_ECHO(hex_long(UID[0]), hex_long(UID[1]), hex_long(UID[2])); + const uint32_t * const UID = (uint32_t*)UID_BASE; + for (uint8_t i = 0; i < 3; i++) print_hex_long(UID[i]); #else - uint16_t * const UID = (uint16_t*)UID_BASE; - SERIAL_ECHO( - F("CEDE2A2F-"), hex_word(UID[0]), C('-'), hex_word(UID[1]), C('-'), hex_word(UID[2]), C('-'), - hex_word(UID[3]), hex_word(UID[4]), hex_word(UID[5]) - ); + const uint16_t * const UID = (uint16_t*)UID_BASE; // Little-endian! + SERIAL_ECHO(F("CEDE2A2F-")); + for (uint8_t i = 1; i <= 6; i++) { + print_hex_word(UID[(i % 2) ? i : i - 2]); // 1111-0000-3333-222255554444 + if (i <= 3) SERIAL_ECHO(C('-')); + } #endif #endif diff --git a/Marlin/src/libs/hex_print.cpp b/Marlin/src/libs/hex_print.cpp index b9edc38c77..9a354011e8 100644 --- a/Marlin/src/libs/hex_print.cpp +++ b/Marlin/src/libs/hex_print.cpp @@ -27,57 +27,43 @@ #include "hex_print.h" #include "../core/serial.h" -#ifdef CPU_32_BIT - constexpr int byte_start = 4; - static char _hex[] = "0x00000000"; -#else - constexpr int byte_start = 0; - static char _hex[] = "0x0000"; -#endif +static char _hex[] = "0x00000000"; // 0:adr32 2:long 4:adr16 6:word 8:byte -char* hex_byte(const uint8_t b) { - _hex[byte_start + 4] = hex_nybble(b >> 4); - _hex[byte_start + 5] = hex_nybble(b); - return &_hex[byte_start + 4]; +inline void __hex_byte(const uint8_t b, const uint8_t o=8) { + _hex[o + 0] = hex_nybble(b >> 4); + _hex[o + 1] = hex_nybble(b); +} +inline void __hex_word(const uint16_t w, const uint8_t o=6) { + __hex_byte(w >> 8, o + 0); + __hex_byte(w , o + 2); +} +inline void __hex_long(const uint32_t w) { + __hex_word(w >> 16, 2); + __hex_word(w , 6); } -inline void __hex_word(const uint16_t w) { - _hex[byte_start + 2] = hex_nybble(w >> 12); - _hex[byte_start + 3] = hex_nybble(w >> 8); - _hex[byte_start + 4] = hex_nybble(w >> 4); - _hex[byte_start + 5] = hex_nybble(w); -} +char* hex_byte(const uint8_t b) { __hex_byte(b); return &_hex[8]; } +char* _hex_word(const uint16_t w) { __hex_word(w); return &_hex[6]; } +char* _hex_long(const uint32_t l) { __hex_long(l); return &_hex[2]; } -char* _hex_word(const uint16_t w) { - __hex_word(w); - return &_hex[byte_start + 2]; -} - -char* _hex_long(const uintptr_t l) { - _hex[2] = hex_nybble(l >> 28); - _hex[3] = hex_nybble(l >> 24); - _hex[4] = hex_nybble(l >> 20); - _hex[5] = hex_nybble(l >> 16); - __hex_word((uint16_t)(l & 0xFFFF)); - return &_hex[2]; -} - -char* hex_address(const void * const w) { +char* hex_address(const void * const a) { #ifdef CPU_32_BIT - (void)hex_long((uintptr_t)w); + (void)_hex_long((uintptr_t)a); + return _hex; #else - (void)hex_word((uintptr_t)w); + _hex[4] = '0'; _hex[5] = 'x'; + (void)_hex_word((uintptr_t)a); + return &_hex[4]; #endif - 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_word(const uint16_t w) { SERIAL_ECHO(_hex_word(w)); } void print_hex_address(const void * const w) { SERIAL_ECHO(hex_address(w)); } -void print_hex_long(const uint32_t w, const char delimiter/*='\0'*/) { - SERIAL_ECHOPGM("0x"); +void print_hex_long(const uint32_t w, const char delimiter/*='\0'*/, const bool prefix/*=false*/) { + if (prefix) SERIAL_ECHOPGM("0x"); for (int B = 24; B >= 8; B -= 8) { print_hex_byte(w >> B); if (delimiter) SERIAL_CHAR(delimiter); diff --git a/Marlin/src/libs/hex_print.h b/Marlin/src/libs/hex_print.h index 4a5cac2b6c..bd1f7ce24d 100644 --- a/Marlin/src/libs/hex_print.h +++ b/Marlin/src/libs/hex_print.h @@ -30,16 +30,17 @@ constexpr char hex_nybble(const uint8_t n) { return (n & 0xF) + ((n & 0xF) < 10 ? '0' : 'A' - 10); } -char* hex_byte(const uint8_t b); char* _hex_word(const uint16_t w); -char* hex_address(const void * const w); -char* _hex_long(const uintptr_t l); +char* _hex_long(const uint32_t l); +char* hex_byte(const uint8_t b); template char* hex_word(T w) { return _hex_word((uint16_t)w); } template char* hex_long(T w) { return _hex_long((uint32_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); -void print_hex_long(const uint32_t w, const char delimiter='\0'); +void print_hex_long(const uint32_t w, const char delimiter='\0', const bool prefix=false);