Skip to content

Commit 21f7309

Browse files
KonstantinKondrashovespressif-bot
authored andcommitted
feat(esp_rom): Adds esp_rom_cvt func for logging and rom_vprintf
1 parent b445e38 commit 21f7309

File tree

3 files changed

+71
-60
lines changed

3 files changed

+71
-60
lines changed

components/esp_rom/include/esp_rom_sys.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ int esp_rom_printf(const char *fmt, ...);
5353
*/
5454
int esp_rom_vprintf(const char *fmt, va_list ap);
5555

56+
/**
57+
* @brief Convert an unsigned integer value to a string representation in the specified radix.
58+
*
59+
* This function converts the given unsigned integer value to a string representation in the specified radix.
60+
* The resulting string is stored in the provided character buffer `buf`.
61+
*
62+
* @param[in] val The unsigned integer value to be converted.
63+
* @param[in] radix The base of the numeral system to be used for the conversion.
64+
* It determines the number of unique digits in the numeral system
65+
* (e.g., 2 for binary, 10 for decimal, 16 for hexadecimal).
66+
* @param[in] pad The optional padding width (0 - unused) for the resulting string. It adds zero-padding.
67+
* (val=123, pad=6 -> result=000123).
68+
* @param[in] digits Pointer to a character array representing the digits of the
69+
* numeral system. The array must contain characters in the order of increasing
70+
* values, corresponding to the digits of the radix. For example, "0123456789ABCDEF"
71+
* or hexadecimal.
72+
* @param[out] buf Pointer to the character buffer where the resulting string will
73+
* be stored. The buffer must have enough space to accommodate the entire converted
74+
* string, including the null-terminator.
75+
*
76+
* @return The length of the resulting string (excluding the null-terminator).
77+
*
78+
* @note The buffer `buf` must have sufficient space to hold the entire converted string, including the null-terminator.
79+
* The caller is responsible for ensuring the buffer's size is large enough to prevent buffer overflow.
80+
* @note The provided `digits` array must have enough elements to cover the entire radix used for conversion. Otherwise, undefined behavior may occur.
81+
*/
82+
int esp_rom_cvt(unsigned long long val, long radix, int pad, const char *digits, char *buf);
83+
5684
/**
5785
* @brief Pauses execution for us microseconds
5886
*

components/esp_rom/patches/esp_rom_print.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,52 @@
1414
#include "rom/ets_sys.h"
1515
#include "sdkconfig.h"
1616

17-
#if !ESP_ROM_HAS_VPRINTF_FUNC
18-
static int _cvt(unsigned long long val, char *buf, long radix, const char *digits)
17+
int esp_rom_cvt(unsigned long long val, long radix, int pad, const char *digits, char *buf)
1918
{
20-
#ifdef SUPPORT_LITTLE_RADIX
21-
char temp[64];
22-
#else
23-
char temp[32];
24-
#endif
25-
char *cp = temp;
19+
char *orig_buf = buf;
2620
int length = 0;
2721

28-
if (val == 0) {
29-
/* Special case */
30-
*cp++ = '0';
31-
} else {
32-
while (val) {
33-
*cp++ = digits[val % radix];
34-
val /= radix;
35-
}
22+
if (radix <= 0 || digits == NULL || buf == NULL) {
23+
return 0;
3624
}
37-
while (cp != temp) {
38-
*buf++ = *--cp;
25+
26+
// The comments below show an example of the conversion process for val = 123 and pad = 6
27+
do {
28+
*buf++ = digits[val % radix];
29+
val /= radix;
30+
length++;
31+
} while (val);
32+
// 3 2 1
33+
// buf = [0] [1] [2] [3]
34+
35+
// length = 3, pad = 6
36+
while (pad > 0 && pad > length) {
37+
*buf++ = '0';
3938
length++;
4039
}
4140
*buf = '\0';
41+
// length = 6
42+
// 3 2 1 0 0 0 \0
43+
// buf = [0] [1] [2] [3] [4] [5] [6]
44+
45+
--buf;
46+
// reverse the order of characters
47+
// 3 2 1 0 0 0 \0
48+
// [0] [1] [2] [3] [4] [5] [6]
49+
// orig_buf -- ^ ^ ----- buf
50+
while (orig_buf < buf) {
51+
char first_char = *orig_buf;
52+
char last_char = *buf;
53+
*buf-- = first_char;
54+
*orig_buf++ = last_char;
55+
}
56+
// 0 0 0 1 2 3 \0
57+
// buf = [0] [1] [2] [3] [4] [5] [6]
58+
4259
return (length);
4360
}
4461

62+
#if !ESP_ROM_HAS_VPRINTF_FUNC
4563
#define is_digit(c) ((c >= '0') && (c <= '9'))
4664
static int ets_vprintf(void (*putc)(char c), const char *fmt, va_list ap)
4765
{
@@ -151,14 +169,14 @@ static int ets_vprintf(void (*putc)(char c), const char *fmt, va_list ap)
151169
case 'D':
152170
case 'u':
153171
case 'U':
154-
length = _cvt(val, buf, 10, "0123456789");
172+
length = esp_rom_cvt(val, 10, 0, "0123456789", buf);
155173
break;
156174
case 'p':
157175
case 'x':
158-
length = _cvt(val, buf, 16, "0123456789abcdef");
176+
length = esp_rom_cvt(val, 16, 0, "0123456789abcdef", buf);
159177
break;
160178
case 'X':
161-
length = _cvt(val, buf, 16, "0123456789ABCDEF");
179+
length = esp_rom_cvt(val, 16, 0, "0123456789ABCDEF", buf);
162180
break;
163181
}
164182
cp = buf;

components/log/src/util.c

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,19 @@
55
*/
66

77
#include <stdint.h>
8+
#include "esp_rom_sys.h"
89

910
int esp_log_util_cvt(unsigned long long val, long radix, int pad, const char *digits, char *buf)
1011
{
11-
char *orig_buf = buf;
12-
int length = 0;
13-
14-
// val = 123
15-
do {
16-
*buf++ = digits[val % radix];
17-
val /= radix;
18-
length++;
19-
} while (val);
20-
// 3 2 1
21-
// buf = [0] [1] [2] [3]
22-
23-
// length = 3, pad = 6
24-
while (pad > 0 && pad > length) {
25-
*buf++ = '0';
26-
length++;
27-
}
28-
*buf = '\0';
29-
// length = 6
30-
// 3 2 1 0 0 0 \0
31-
// buf = [0] [1] [2] [3] [4] [5] [6]
32-
33-
--buf;
34-
// reverse the order of characters
35-
// 3 2 1 0 0 0 \0
36-
// [0] [1] [2] [3] [4] [5] [6]
37-
// orig_buf -- ^ ^ ----- buf
38-
while (orig_buf < buf) {
39-
char first_char = *orig_buf;
40-
char last_char = *buf;
41-
*buf-- = first_char;
42-
*orig_buf++ = last_char;
43-
}
44-
// 0 0 0 1 2 3 \0
45-
// buf = [0] [1] [2] [3] [4] [5] [6]
46-
47-
return (length);
12+
return esp_rom_cvt(val, radix, pad, digits, buf);
4813
}
4914

5015
int esp_log_util_cvt_hex(unsigned long long val, int pad, char *buf)
5116
{
52-
return esp_log_util_cvt(val, 16, pad, "0123456789abcdef", buf);
17+
return esp_rom_cvt(val, 16, pad, "0123456789abcdef", buf);
5318
}
5419

5520
int esp_log_util_cvt_dec(unsigned long long val, int pad, char *buf)
5621
{
57-
return esp_log_util_cvt(val, 10, pad, "0123456789", buf);
22+
return esp_rom_cvt(val, 10, pad, "0123456789", buf);
5823
}

0 commit comments

Comments
 (0)