55#include "SEGGER_RTT.h"
56#include "SEGGER_RTT_Conf.h"
65#ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE
66 #define SEGGER_RTT_PRINTF_BUFFER_SIZE (64)
73#define FORMAT_FLAG_LEFT_JUSTIFY (1u << 0)
74#define FORMAT_FLAG_PAD_ZERO (1u << 1)
75#define FORMAT_FLAG_PRINT_SIGN (1u << 2)
76#define FORMAT_FLAG_ALTERNATE (1u << 3)
92 unsigned RTTBufferIndex;
116 if ((Cnt + 1u) <= p->BufferSize) {
117 *(p->pBuffer + Cnt) = c;
124 if (p->Cnt == p->BufferSize) {
125 if (SEGGER_RTT_Write(p->RTTBufferIndex, p->pBuffer, p->Cnt) != p->Cnt) {
137static void _PrintUnsigned(
SEGGER_RTT_PRINTF_DESC * pBufferDesc,
unsigned v,
unsigned Base,
unsigned NumDigits,
unsigned FieldWidth,
unsigned FormatFlags) {
138 static const char _aV2C[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' };
151 while (Number >= Base) {
152 Number = (Number / Base);
155 if (NumDigits > Width) {
161 if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) {
162 if (FieldWidth != 0u) {
163 if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0u)) {
168 while ((FieldWidth != 0u) && (Width < FieldWidth)) {
170 _StoreChar(pBufferDesc, c);
171 if (pBufferDesc->ReturnValue < 0) {
177 if (pBufferDesc->ReturnValue >= 0) {
184 if (NumDigits > 1u) {
200 _StoreChar(pBufferDesc, _aV2C[Div]);
201 if (pBufferDesc->ReturnValue < 0) {
209 if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) {
210 if (FieldWidth != 0u) {
211 while ((FieldWidth != 0u) && (Width < FieldWidth)) {
213 _StoreChar(pBufferDesc,
' ');
214 if (pBufferDesc->ReturnValue < 0) {
227static void _PrintInt(
SEGGER_RTT_PRINTF_DESC * pBufferDesc,
int v,
unsigned Base,
unsigned NumDigits,
unsigned FieldWidth,
unsigned FormatFlags) {
231 Number = (v < 0) ? -v : v;
237 while (Number >= (
int)Base) {
238 Number = (Number / (int)Base);
241 if (NumDigits > Width) {
244 if ((FieldWidth > 0u) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) {
251 if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u)) {
252 if (FieldWidth != 0u) {
253 while ((FieldWidth != 0u) && (Width < FieldWidth)) {
255 _StoreChar(pBufferDesc,
' ');
256 if (pBufferDesc->ReturnValue < 0) {
265 if (pBufferDesc->ReturnValue >= 0) {
268 _StoreChar(pBufferDesc,
'-');
269 }
else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) {
270 _StoreChar(pBufferDesc,
'+');
274 if (pBufferDesc->ReturnValue >= 0) {
278 if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u)) {
279 if (FieldWidth != 0u) {
280 while ((FieldWidth != 0u) && (Width < FieldWidth)) {
282 _StoreChar(pBufferDesc,
'0');
283 if (pBufferDesc->ReturnValue < 0) {
289 if (pBufferDesc->ReturnValue >= 0) {
293 _PrintUnsigned(pBufferDesc, (
unsigned)v, Base, NumDigits, FieldWidth, FormatFlags);
322int SEGGER_RTT_vprintf(
unsigned BufferIndex,
const char * sFormat, va_list * pParamList) {
327 unsigned FormatFlags;
329 char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
331 BufferDesc.pBuffer = acBuffer;
332 BufferDesc.BufferSize = SEGGER_RTT_PRINTF_BUFFER_SIZE;
334 BufferDesc.RTTBufferIndex = BufferIndex;
335 BufferDesc.ReturnValue = 0;
352 case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++;
break;
353 case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++;
break;
354 case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++;
break;
355 case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++;
break;
356 default: v = 0;
break;
365 if ((c <
'0') || (c >
'9')) {
369 FieldWidth = (FieldWidth * 10u) + ((
unsigned)c -
'0');
381 if ((c <
'0') || (c >
'9')) {
385 NumDigits = NumDigits * 10u + ((unsigned)c -
'0');
393 if ((c ==
'l') || (c ==
'h')) {
406 v = va_arg(*pParamList,
int);
408 _StoreChar(&BufferDesc, c0);
412 v = va_arg(*pParamList,
int);
413 _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);
416 v = va_arg(*pParamList,
int);
417 _PrintUnsigned(&BufferDesc, (
unsigned)v, 10u, NumDigits, FieldWidth, FormatFlags);
421 v = va_arg(*pParamList,
int);
422 _PrintUnsigned(&BufferDesc, (
unsigned)v, 16u, NumDigits, FieldWidth, FormatFlags);
426 const char * s = va_arg(*pParamList,
const char *);
436 _StoreChar(&BufferDesc, c);
437 }
while (BufferDesc.ReturnValue >= 0);
441 v = va_arg(*pParamList,
int);
442 _PrintUnsigned(&BufferDesc, (
unsigned)v, 16u, 8u, 8u, 0u);
445 _StoreChar(&BufferDesc,
'%');
452 _StoreChar(&BufferDesc, c);
454 }
while (BufferDesc.ReturnValue >= 0);
456 if (BufferDesc.ReturnValue > 0) {
460 if (BufferDesc.Cnt != 0u) {
461 SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt);
463 BufferDesc.ReturnValue += (int)BufferDesc.Cnt;
465 return BufferDesc.ReturnValue;
499int SEGGER_RTT_printf(
unsigned BufferIndex,
const char * sFormat, ...) {
503 va_start(ParamList, sFormat);
504 r = SEGGER_RTT_vprintf(BufferIndex, sFormat, &ParamList);