Skip to content

Commit f9d5ee0

Browse files
ellenspthinkyhead
andauthored
🩹 Patch STM32 serial UUID (MarlinFirmware#26737)
Followup to MarlinFirmware#26715 Co-authored-by: Scott Lahteine <[email protected]>
1 parent ef04680 commit f9d5ee0

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

Marlin/src/feature/tmc_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@
763763
SERIAL_CHAR('\t');
764764
st.printLabel();
765765
SERIAL_CHAR('\t');
766-
print_hex_long(drv_status, ':');
766+
print_hex_long(drv_status, ':', true);
767767
if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!");
768768
SERIAL_EOL();
769769
break;

Marlin/src/gcode/host/M115.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ void GcodeSuite::M115() {
8888
* This code should work on all STM32-based boards.
8989
*/
9090
#if ENABLED(STM32_UID_SHORT_FORM)
91-
uint32_t * const UID = (uint32_t*)UID_BASE;
92-
SERIAL_ECHO(hex_long(UID[0]), hex_long(UID[1]), hex_long(UID[2]));
91+
const uint32_t * const UID = (uint32_t*)UID_BASE;
92+
for (uint8_t i = 0; i < 3; i++) print_hex_long(UID[i]);
9393
#else
94-
uint16_t * const UID = (uint16_t*)UID_BASE;
95-
SERIAL_ECHO(
96-
F("CEDE2A2F-"), hex_word(UID[0]), C('-'), hex_word(UID[1]), C('-'), hex_word(UID[2]), C('-'),
97-
hex_word(UID[3]), hex_word(UID[4]), hex_word(UID[5])
98-
);
94+
const uint16_t * const UID = (uint16_t*)UID_BASE; // Little-endian!
95+
SERIAL_ECHO(F("CEDE2A2F-"));
96+
for (uint8_t i = 1; i <= 6; i++) {
97+
print_hex_word(UID[(i % 2) ? i : i - 2]); // 1111-0000-3333-222255554444
98+
if (i <= 3) SERIAL_ECHO(C('-'));
99+
}
99100
#endif
100101
#endif
101102

Marlin/src/libs/hex_print.cpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,57 +27,43 @@
2727
#include "hex_print.h"
2828
#include "../core/serial.h"
2929

30-
#ifdef CPU_32_BIT
31-
constexpr int byte_start = 4;
32-
static char _hex[] = "0x00000000";
33-
#else
34-
constexpr int byte_start = 0;
35-
static char _hex[] = "0x0000";
36-
#endif
30+
static char _hex[] = "0x00000000"; // 0:adr32 2:long 4:adr16 6:word 8:byte
3731

38-
char* hex_byte(const uint8_t b) {
39-
_hex[byte_start + 4] = hex_nybble(b >> 4);
40-
_hex[byte_start + 5] = hex_nybble(b);
41-
return &_hex[byte_start + 4];
32+
inline void __hex_byte(const uint8_t b, const uint8_t o=8) {
33+
_hex[o + 0] = hex_nybble(b >> 4);
34+
_hex[o + 1] = hex_nybble(b);
4235
}
43-
44-
inline void __hex_word(const uint16_t w) {
45-
_hex[byte_start + 2] = hex_nybble(w >> 12);
46-
_hex[byte_start + 3] = hex_nybble(w >> 8);
47-
_hex[byte_start + 4] = hex_nybble(w >> 4);
48-
_hex[byte_start + 5] = hex_nybble(w);
36+
inline void __hex_word(const uint16_t w, const uint8_t o=6) {
37+
__hex_byte(w >> 8, o + 0);
38+
__hex_byte(w , o + 2);
4939
}
50-
51-
char* _hex_word(const uint16_t w) {
52-
__hex_word(w);
53-
return &_hex[byte_start + 2];
40+
inline void __hex_long(const uint32_t w) {
41+
__hex_word(w >> 16, 2);
42+
__hex_word(w , 6);
5443
}
5544

56-
char* _hex_long(const uintptr_t l) {
57-
_hex[2] = hex_nybble(l >> 28);
58-
_hex[3] = hex_nybble(l >> 24);
59-
_hex[4] = hex_nybble(l >> 20);
60-
_hex[5] = hex_nybble(l >> 16);
61-
__hex_word((uint16_t)(l & 0xFFFF));
62-
return &_hex[2];
63-
}
45+
char* hex_byte(const uint8_t b) { __hex_byte(b); return &_hex[8]; }
46+
char* _hex_word(const uint16_t w) { __hex_word(w); return &_hex[6]; }
47+
char* _hex_long(const uint32_t l) { __hex_long(l); return &_hex[2]; }
6448

65-
char* hex_address(const void * const w) {
49+
char* hex_address(const void * const a) {
6650
#ifdef CPU_32_BIT
67-
(void)hex_long((uintptr_t)w);
51+
(void)_hex_long((uintptr_t)a);
52+
return _hex;
6853
#else
69-
(void)hex_word((uintptr_t)w);
54+
_hex[4] = '0'; _hex[5] = 'x';
55+
(void)_hex_word((uintptr_t)a);
56+
return &_hex[4];
7057
#endif
71-
return _hex;
7258
}
7359

7460
void print_hex_nybble(const uint8_t n) { SERIAL_CHAR(hex_nybble(n)); }
7561
void print_hex_byte(const uint8_t b) { SERIAL_ECHO(hex_byte(b)); }
76-
void print_hex_word(const uint16_t w) { SERIAL_ECHO(hex_word(w)); }
62+
void print_hex_word(const uint16_t w) { SERIAL_ECHO(_hex_word(w)); }
7763
void print_hex_address(const void * const w) { SERIAL_ECHO(hex_address(w)); }
7864

79-
void print_hex_long(const uint32_t w, const char delimiter/*='\0'*/) {
80-
SERIAL_ECHOPGM("0x");
65+
void print_hex_long(const uint32_t w, const char delimiter/*='\0'*/, const bool prefix/*=false*/) {
66+
if (prefix) SERIAL_ECHOPGM("0x");
8167
for (int B = 24; B >= 8; B -= 8) {
8268
print_hex_byte(w >> B);
8369
if (delimiter) SERIAL_CHAR(delimiter);

Marlin/src/libs/hex_print.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@
3030
constexpr char hex_nybble(const uint8_t n) {
3131
return (n & 0xF) + ((n & 0xF) < 10 ? '0' : 'A' - 10);
3232
}
33-
char* hex_byte(const uint8_t b);
3433
char* _hex_word(const uint16_t w);
35-
char* hex_address(const void * const w);
36-
char* _hex_long(const uintptr_t l);
34+
char* _hex_long(const uint32_t l);
3735

36+
char* hex_byte(const uint8_t b);
3837
template<typename T> char* hex_word(T w) { return _hex_word((uint16_t)w); }
3938
template<typename T> char* hex_long(T w) { return _hex_long((uint32_t)w); }
4039

40+
char* hex_address(const void * const w);
41+
4142
void print_hex_nybble(const uint8_t n);
4243
void print_hex_byte(const uint8_t b);
4344
void print_hex_word(const uint16_t w);
4445
void print_hex_address(const void * const w);
45-
void print_hex_long(const uint32_t w, const char delimiter='\0');
46+
void print_hex_long(const uint32_t w, const char delimiter='\0', const bool prefix=false);

0 commit comments

Comments
 (0)