-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathets_printf.c
312 lines (259 loc) · 8.15 KB
/
ets_printf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_attr.h"
#include "esp8266/eagle_soc.h"
#include "esp8266/uart_register.h"
#include "esp8266/rom_functions.h"
#ifdef CONFIG_ETS_PRINTF_EXIT_WHEN_FLASH_RW
#define ETS_PRINTF_ATTR IRAM_ATTR
#else
#define ETS_PRINTF_ATTR
#endif
#ifndef CONFIG_ESP_CONSOLE_UART_NONE
static void uart_putc(int c)
{
while (1) {
uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(CONFIG_ESP_CONSOLE_UART_NUM)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S);
if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126)
break;
}
WRITE_PERI_REG(UART_FIFO(CONFIG_ESP_CONSOLE_UART_NUM) , c);
}
#else
#define uart_putc(_c) { }
#endif
int __attribute__ ((weak)) ets_putc(int c)
{
#ifdef CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF
if (c == '\n')
uart_putc('\r');
#elif defined(CONFIG_NEWLIB_STDOUT_LINE_ENDING_CR)
if (c == '\n')
c = '\r';
#endif
uart_putc(c);
return c;
}
#if defined(CONFIG_USING_NEW_ETS_VPRINTF) && !defined(BOOTLOADER_BUILD)
#define FILL_0 0x01
#define FILL_LEFT 0x02
#define POINTOR 0x04
#define ALTERNATE 0x08
#define OUPUT_INT 0x10
#define START 0x20
#define END 0x40
#define VINT_STR_MAX 10
typedef union _val_cache {
uint8_t val8;
int32_t val32;
uint32_t val32u;
const char *valcp;
} val_cache_t;
typedef struct _val_attr {
uint8_t type;
uint8_t state;
uint8_t fillbytes;
uint8_t precision;
val_cache_t value;
} val_attr_t;
#define isdigit(_c) ((_c <= '9') && (_c >= '0'))
#define fill_num(_attr) ((attr)->fillbytes)
#define isfill_0(_attr) (fill_num(_attr) && ((_attr)->state & FILL_0))
#define isfill_left(_attr) (fill_num(_attr) && ((_attr)->state & FILL_LEFT))
#define isstart(_attr) ((_attr)->state & START)
static inline void ets_printf_ch_mutlti(uint32_t c, uint32_t len)
{
while (len--)
ets_putc(c);
}
static inline void ets_printf_buf(const char *s, uint32_t len)
{
while (len--)
ets_putc(*s++);
}
static int ets_printf_str(const val_attr_t * const attr)
{
const char *s = attr->value.valcp;
s = s == NULL ? "<null>" : s;
if (fill_num(attr)) {
unsigned char left;
unsigned char len;
while (*s != '\0')
s++;
len = s - attr->value.valcp;
left = fill_num(attr) > len ? fill_num(attr) - len : 0;
if (!isfill_left(attr)) {
ets_printf_ch_mutlti(' ', left);
}
ets_printf_buf(attr->value.valcp, len);
if (isfill_left(attr)) {
ets_printf_ch_mutlti(' ', left);
}
} else {
while (*s != '\0')
ets_putc(*s++);
}
return 0;
}
static int ets_printf_int(val_attr_t * const attr, uint8_t hex)
{
char buf[VINT_STR_MAX];
unsigned char offset = VINT_STR_MAX;
if (attr->value.val32u != 0) {
for (; attr->value.val32u > 0; attr->value.val32u /= hex) {
unsigned char c = attr->value.val32u % hex;
if (c < 10)
buf[--offset] = c + '0';
else
buf[--offset] = c + 'a' - 10;
}
} else
buf[--offset] = '0';
if (fill_num(attr)) {
char fill_data = isfill_0(attr) ? '0' : ' ';
unsigned char len = fill_num(attr) - (VINT_STR_MAX - offset);
unsigned char left = fill_num(attr) > (VINT_STR_MAX - offset) ? len : 0;
if (!isfill_left(attr)) {
ets_printf_ch_mutlti(fill_data, left);
}
ets_printf_buf(&buf[offset], VINT_STR_MAX - offset);
if (isfill_left(attr)) {
fill_data = ' ';
ets_printf_ch_mutlti(fill_data, left);
}
} else {
ets_printf_buf(&buf[offset], VINT_STR_MAX - offset);
}
return 0;
}
int ets_vprintf(const char *fmt, va_list va)
{
for (; ;) {
const char *ps = fmt;
val_attr_t attr;
while (*ps != '\0' && *ps != '%')
ets_putc(*ps++);
if (*ps == '\0')
break;
fmt = ps;
memset(&attr, 0, sizeof(val_attr_t));
for (; ;) {
switch (*++ps) {
case '#':
attr.state |= ALTERNATE;
ps++;
break;
case '0'...'9':
if ((!isstart(&attr) || *(ps - 1) == '-') && *ps == '0') {
attr.state |= FILL_0;
} else {
if (attr.state & POINTOR)
attr.precision = attr.precision * 10 + *ps - '0';
else
attr.fillbytes = attr.fillbytes * 10 + *ps - '0';
}
break;
case '.':
attr.state |= POINTOR;
break;
case '-':
attr.state |= FILL_LEFT;
break;
default:
attr.type = *ps++;
attr.state |= END;
break;
}
if (attr.state & END)
break;
attr.state |= START;
}
switch (attr.type) {
case 'c':
attr.value.val8 = (char)va_arg(va, int);
ets_putc(attr.value.val8);
break;
case 's':
attr.value.valcp = va_arg(va, const char *);
ets_printf_str(&attr);
break;
case 'i':
case 'd':
attr.value.val32 = va_arg(va, int);
if (attr.value.val32 < 0) {
ets_putc('-');
attr.value.val32 = -attr.value.val32;
}
ets_printf_int(&attr, 10);
break;
case 'u':
attr.value.val32u = va_arg(va, unsigned int);
ets_printf_int(&attr, 10);
break;
case 'x':
attr.value.val32u = va_arg(va, unsigned int);
ets_printf_int(&attr, 16);
break;
case 'p':
ets_printf_buf("0x", 2);
attr.value.valcp = va_arg(va, const char *);
ets_printf_int(&attr, 16);
break;
case '%':
ets_putc('%');
break;
default:
ets_putc('%');
ps = fmt + 1;
break;
}
fmt = ps;
}
return 0;
}
#else /* defined(CONFIG_USING_NEW_ETS_VPRINTF) && !defined(BOOTLOADER_BUILD) */
int ets_vprintf(const char *fmt, va_list ap)
{
return ets_io_vprintf(ets_putc, fmt, ap);
}
#endif /* defined(CONFIG_USING_NEW_ETS_VPRINTF) && !defined(BOOTLOADER_BUILD) */
/**
* Re-write ets_printf in SDK side, since ets_printf in ROM will use a global
* variable which address is in heap region of SDK side. If use ets_printf in ROM,
* this variable maybe re-write when heap alloc and modification.
*
* Using new "ets_vprintf" costs stack without alignment and accuracy:
* just "fmt": 136 Bytes
* "%s": 172 Bytes
* "%p", "%d, "%i, "%u", "%x": 215 Bytes
*/
int ETS_PRINTF_ATTR ets_printf(const char *fmt, ...)
{
va_list ap;
int ret;
#ifdef CONFIG_ETS_PRINTF_EXIT_WHEN_FLASH_RW
extern uint8_t FlashIsOnGoing;
if (FlashIsOnGoing) {
return -1;
}
#endif
va_start(ap, fmt);
ret = ets_vprintf(fmt, ap);
va_end(ap);
return ret;
}