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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
79
80
89
90
96
97
103
107
108
109
110
111
116
117
118
119
126
127
128
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
150
153
154
155
156
172
173
174
175
176
177
178
189
190
191
192
201
202
203
204
215
216
217
218
222
223
224
225
226
227
228
229
230
231
232
236
239
242
243
244
245
246
257
258
259
260
292
293
294
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
338
339
340
341
342
343
344
354
355
356
357
358
366
367
368
369
370
371
372
383
384
385
386
387
395
396
397
398
399
406
410
414
415
419
432
436
439
441
442
443
446
447
448
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* ... */
#include "SEGGER_RTT.h"
#include "SEGGER_RTT_Conf.h"
/* ... */
#ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE
#define SEGGER_RTT_PRINTF_BUFFER_SIZE (64)
#endif
#include <stdlib.h>
#include <stdarg.h>
#define FORMAT_FLAG_LEFT_JUSTIFY (1u << 0)
#define FORMAT_FLAG_PAD_ZERO (1u << 1)
#define FORMAT_FLAG_PRINT_SIGN (1u << 2)
#define FORMAT_FLAG_ALTERNATE (1u << 3)
/* ... */
typedef struct {
char* pBuffer;
unsigned BufferSize;
unsigned Cnt;
int ReturnValue;
unsigned RTTBufferIndex;
...} SEGGER_RTT_PRINTF_DESC;
/* ... */
/* ... */
/* ... */
static void _StoreChar(SEGGER_RTT_PRINTF_DESC * p, char c) {
unsigned Cnt;
Cnt = p->Cnt;
if ((Cnt + 1u) <= p->BufferSize) {
*(p->pBuffer + Cnt) = c;
p->Cnt = Cnt + 1u;
p->ReturnValue++;
}if ((Cnt + 1u) <= p->BufferSize) { ... }
if (p->Cnt == p->BufferSize) {
if (SEGGER_RTT_Write(p->RTTBufferIndex, p->pBuffer, p->Cnt) != p->Cnt) {
p->ReturnValue = -1;
}if (SEGGER_RTT_Write(p->RTTBufferIndex, p->pBuffer, p->Cnt) != p->Cnt) { ... } else {
p->Cnt = 0u;
}else { ... }
}if (p->Cnt == p->BufferSize) { ... }
}{ ... }
/* ... */
static void _PrintUnsigned(SEGGER_RTT_PRINTF_DESC * pBufferDesc, unsigned v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
static const char _aV2C[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
unsigned Div;
unsigned Digit;
unsigned Number;
unsigned Width;
char c;
Number = v;
Digit = 1u;
Width = 1u;
while (Number >= Base) {
Number = (Number / Base);
Width++;
}while (Number >= Base) { ... }
if (NumDigits > Width) {
Width = NumDigits;
}if (NumDigits > Width) { ... }
if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) {
if (FieldWidth != 0u) {
if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0u)) {
c = '0';
}if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0u)) { ... } else {
c = ' ';
}else { ... }
while ((FieldWidth != 0u) && (Width < FieldWidth)) {
FieldWidth--;
_StoreChar(pBufferDesc, c);
if (pBufferDesc->ReturnValue < 0) {
break;
}if (pBufferDesc->ReturnValue < 0) { ... }
}while ((FieldWidth != 0u) && (Width < FieldWidth)) { ... }
}if (FieldWidth != 0u) { ... }
}if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) { ... }
if (pBufferDesc->ReturnValue >= 0) {
while (1) {
if (NumDigits > 1u) {
NumDigits--;
}if (NumDigits > 1u) { ... } else {
Div = v / Digit;
if (Div < Base) {
break;
}if (Div < Base) { ... }
}else { ... }
Digit *= Base;
}while (1) { ... }
do {
Div = v / Digit;
v -= Div * Digit;
_StoreChar(pBufferDesc, _aV2C[Div]);
if (pBufferDesc->ReturnValue < 0) {
break;
}if (pBufferDesc->ReturnValue < 0) { ... }
Digit /= Base;
...} while (Digit);
if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) {
if (FieldWidth != 0u) {
while ((FieldWidth != 0u) && (Width < FieldWidth)) {
FieldWidth--;
_StoreChar(pBufferDesc, ' ');
if (pBufferDesc->ReturnValue < 0) {
break;
}if (pBufferDesc->ReturnValue < 0) { ... }
}while ((FieldWidth != 0u) && (Width < FieldWidth)) { ... }
}if (FieldWidth != 0u) { ... }
}if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) { ... }
}if (pBufferDesc->ReturnValue >= 0) { ... }
}{ ... }
/* ... */
static void _PrintInt(SEGGER_RTT_PRINTF_DESC * pBufferDesc, int v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
unsigned Width;
int Number;
Number = (v < 0) ? -v : v;
Width = 1u;
while (Number >= (int)Base) {
Number = (Number / (int)Base);
Width++;
}while (Number >= (int)Base) { ... }
if (NumDigits > Width) {
Width = NumDigits;
}if (NumDigits > Width) { ... }
if ((FieldWidth > 0u) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) {
FieldWidth--;
}if ((FieldWidth > 0u) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) { ... }
if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u)) {
if (FieldWidth != 0u) {
while ((FieldWidth != 0u) && (Width < FieldWidth)) {
FieldWidth--;
_StoreChar(pBufferDesc, ' ');
if (pBufferDesc->ReturnValue < 0) {
break;
}if (pBufferDesc->ReturnValue < 0) { ... }
}while ((FieldWidth != 0u) && (Width < FieldWidth)) { ... }
}if (FieldWidth != 0u) { ... }
}if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u)) { ... }
if (pBufferDesc->ReturnValue >= 0) {
if (v < 0) {
v = -v;
_StoreChar(pBufferDesc, '-');
}if (v < 0) { ... } else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) {
_StoreChar(pBufferDesc, '+');
}else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) { ... } else {
}else { ... }
if (pBufferDesc->ReturnValue >= 0) {
if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u)) {
if (FieldWidth != 0u) {
while ((FieldWidth != 0u) && (Width < FieldWidth)) {
FieldWidth--;
_StoreChar(pBufferDesc, '0');
if (pBufferDesc->ReturnValue < 0) {
break;
}if (pBufferDesc->ReturnValue < 0) { ... }
}while ((FieldWidth != 0u) && (Width < FieldWidth)) { ... }
}if (FieldWidth != 0u) { ... }
}if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u)) { ... }
if (pBufferDesc->ReturnValue >= 0) {
_PrintUnsigned(pBufferDesc, (unsigned)v, Base, NumDigits, FieldWidth, FormatFlags);
}if (pBufferDesc->ReturnValue >= 0) { ... }
}if (pBufferDesc->ReturnValue >= 0) { ... }
}if (pBufferDesc->ReturnValue >= 0) { ... }
}{ ... }
/* ... */
/* ... */
int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList) {
char c;
SEGGER_RTT_PRINTF_DESC BufferDesc;
int v;
unsigned NumDigits;
unsigned FormatFlags;
unsigned FieldWidth;
char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
BufferDesc.pBuffer = acBuffer;
BufferDesc.BufferSize = SEGGER_RTT_PRINTF_BUFFER_SIZE;
BufferDesc.Cnt = 0u;
BufferDesc.RTTBufferIndex = BufferIndex;
BufferDesc.ReturnValue = 0;
do {
c = *sFormat;
sFormat++;
if (c == 0u) {
break;
}if (c == 0u) { ... }
if (c == '%') {
FormatFlags = 0u;
v = 1;
do {
c = *sFormat;
switch (c) {
case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break;
case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++; break;
case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++; break;
case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++; break;
default: v = 0; break;
}switch (c) { ... }
...} while (v);
FieldWidth = 0u;
do {
c = *sFormat;
if ((c < '0') || (c > '9')) {
break;
}if ((c < '0') || (c > '9')) { ... }
sFormat++;
FieldWidth = (FieldWidth * 10u) + ((unsigned)c - '0');
...} while (1);
NumDigits = 0u;
c = *sFormat;
if (c == '.') {
sFormat++;
do {
c = *sFormat;
if ((c < '0') || (c > '9')) {
break;
}if ((c < '0') || (c > '9')) { ... }
sFormat++;
NumDigits = NumDigits * 10u + ((unsigned)c - '0');
...} while (1);
}if (c == '.') { ... }
c = *sFormat;
do {
if ((c == 'l') || (c == 'h')) {
sFormat++;
c = *sFormat;
}if ((c == 'l') || (c == 'h')) { ... } else {
break;
}else { ... }
...} while (1);
switch (c) {
case 'c': {
char c0;
v = va_arg(*pParamList, int);
c0 = (char)v;
_StoreChar(&BufferDesc, c0);
break;
...}case 'c':
case 'd':
v = va_arg(*pParamList, int);
_PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);
break;case 'd':
case 'u':
v = va_arg(*pParamList, int);
_PrintUnsigned(&BufferDesc, (unsigned)v, 10u, NumDigits, FieldWidth, FormatFlags);
break;case 'u':
case 'x':
case 'X':
v = va_arg(*pParamList, int);
_PrintUnsigned(&BufferDesc, (unsigned)v, 16u, NumDigits, FieldWidth, FormatFlags);
break;case 'X':
case 's':
{
const char * s = va_arg(*pParamList, const char *);
do {
c = *s;
s++;
if (c == '\0') {
break;
}if (c == '\0') { ... }
_StoreChar(&BufferDesc, c);
...} while (BufferDesc.ReturnValue >= 0);
...}
break;case 's':
case 'p':
v = va_arg(*pParamList, int);
_PrintUnsigned(&BufferDesc, (unsigned)v, 16u, 8u, 8u, 0u);
break;case 'p':
case '%':
_StoreChar(&BufferDesc, '%');
break;case '%':
default:
break;default
}switch (c) { ... }
sFormat++;
}if (c == '%') { ... } else {
_StoreChar(&BufferDesc, c);
}else { ... }
...} while (BufferDesc.ReturnValue >= 0);
if (BufferDesc.ReturnValue > 0) {
if (BufferDesc.Cnt != 0u) {
SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt);
}if (BufferDesc.Cnt != 0u) { ... }
BufferDesc.ReturnValue += (int)BufferDesc.Cnt;
}if (BufferDesc.ReturnValue > 0) { ... }
return BufferDesc.ReturnValue;
}{ ... }
/* ... */
int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...) {
int r;
va_list ParamList;
va_start(ParamList, sFormat);
r = SEGGER_RTT_vprintf(BufferIndex, sFormat, &ParamList);
va_end(ParamList);
return r;
}{ ... }