Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

m_printf: stacksize reduced #1097

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions Sming/system/m_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Descr: embedded very simple version of printf with float support
#include <stdarg.h>
#include "osapi.h"

#define MPRINTF_BUF_SIZE 256
#define INITIAL_BUFFSIZE 128

static void defaultPrintChar(uart_t *uart, char c) {
return uart_tx_one_char(c);
Expand Down Expand Up @@ -62,27 +62,25 @@ int m_snprintf(char* buf, int length, const char *fmt, ...)
return n;
}

int m_vprintf ( const char * format, va_list arg )
int m_vprintf(const char *fmt, va_list va)
{
if(!cbc_printchar)
{
return 0;
}

char buf[MPRINTF_BUF_SIZE], *p;

int n = 0;
m_vsnprintf(buf, sizeof(buf), format, arg);

p = buf;
while (p && n < sizeof(buf) && *p)
{
cbc_printchar(cbc_printchar_uart, *p);
n++;
p++;
}
size_t size = INITIAL_BUFFSIZE - 1;

// need to retry if size is not big enough
while (1) {
char buffer[size + 1];
size_t sz = m_vsnprintf(buffer, sizeof(buffer), fmt, va);
if (sz > size) {
size = sz;
continue;
}

return n;
const char *p = buffer;
while (char c = *p++) {
cbc_printchar(cbc_printchar_uart, c);
}
return sz;
}
}

/**
Expand Down