|
27 | 27 | #include "hex_print.h"
|
28 | 28 | #include "../core/serial.h"
|
29 | 29 |
|
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 |
37 | 31 |
|
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); |
42 | 35 | }
|
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); |
49 | 39 | }
|
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); |
54 | 43 | }
|
55 | 44 |
|
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]; } |
64 | 48 |
|
65 |
| -char* hex_address(const void * const w) { |
| 49 | +char* hex_address(const void * const a) { |
66 | 50 | #ifdef CPU_32_BIT
|
67 |
| - (void)hex_long((uintptr_t)w); |
| 51 | + (void)_hex_long((uintptr_t)a); |
| 52 | + return _hex; |
68 | 53 | #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]; |
70 | 57 | #endif
|
71 |
| - return _hex; |
72 | 58 | }
|
73 | 59 |
|
74 | 60 | void print_hex_nybble(const uint8_t n) { SERIAL_CHAR(hex_nybble(n)); }
|
75 | 61 | 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)); } |
77 | 63 | void print_hex_address(const void * const w) { SERIAL_ECHO(hex_address(w)); }
|
78 | 64 |
|
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"); |
81 | 67 | for (int B = 24; B >= 8; B -= 8) {
|
82 | 68 | print_hex_byte(w >> B);
|
83 | 69 | if (delimiter) SERIAL_CHAR(delimiter);
|
|
0 commit comments