Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix STM32_UID string corruption #26737

Merged
merged 9 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Marlin/src/feature/tmc_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions Marlin/src/gcode/host/M115.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
58 changes: 22 additions & 36 deletions Marlin/src/libs/hex_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
_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);
inline void __hex_word(const uint16_t w, const uint8_t o=6) {
__hex_byte(w >> 8, o + 0);
__hex_byte(w , o + 2);
}

char* _hex_word(const uint16_t w) {
__hex_word(w);
return &_hex[byte_start + 2];
inline void __hex_long(const uint32_t w) {
__hex_word(w >> 16, 2);
__hex_word(w , 6);
}

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_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_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);
Expand Down
9 changes: 5 additions & 4 deletions Marlin/src/libs/hex_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T> char* hex_word(T w) { return _hex_word((uint16_t)w); }
template<typename T> 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);
Loading