[realtek-ambz] Standardize all stdio output with printf library

This commit is contained in:
Kuba Szczodrzyński
2022-06-19 23:30:40 +02:00
parent 34c0df2761
commit b4005d8bad
10 changed files with 151 additions and 4 deletions

View File

@@ -0,0 +1,9 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
// make printf.c define wrapper functions
#define printf_ __wrap_printf
#define sprintf_ __wrap_sprintf
#define vsprintf_ __wrap_vsprintf
#define snprintf_ __wrap_snprintf
#define vsnprintf_ __wrap_vsnprintf
#define vprintf_ __wrap_vprintf

View File

@@ -0,0 +1,9 @@
// https://github.com/embeddedartistry/libc/blob/master/src/stdio/putchar.c
#include <printf/printf.h>
#include <stdio.h>
int __wrap_putchar(int c) {
putchar_((char)c);
return c;
}

View File

@@ -0,0 +1,21 @@
// https://github.com/embeddedartistry/libc/blob/master/src/stdio/puts.c
#include <printf/printf.h>
#include <stdio.h>
int __wrap_puts(const char *str) {
int r = 0;
for (const char *c = str; *c != 0; c++) {
putchar_((int)*c);
r++;
}
// puts adds a newline
if (r) {
putchar_('\n');
r++;
}
return r ? r : EOF;
}

View File

@@ -28,9 +28,14 @@
// remove previously defined workaround
#undef boolean
// stdio.h
#define printf rtl_printf
#define sprintf rtl_sprintf
// undefine ROM stdio in favor of printf() library (wrappers)
#undef printf
#undef sprintf
#undef vsprintf
#undef snprintf
#undef vsnprintf
#undef vprintf
#include <stdio.h>
// moved from syscalls.h
#define _close __rtl_close

View File

@@ -0,0 +1,30 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <stdarg.h>
#include <stdio.h>
// include LOGUART_PutChar()
#include <rtl8710b.h>
// make sure to call the "real" vprintf instead of rtl_vprintf
#undef vprintf
void putchar_(char c) {
LOGUART_PutChar(c);
}
// stdio wrappers for Realtek SDK
int __wrap_rtl_printf(const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = vprintf(format, va);
va_end(va);
return ret;
}
int __wrap_DiagPrintf(const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = vprintf(format, va);
va_end(va);
return ret;
}

View File

@@ -26,6 +26,9 @@ env.Append(
"-Wl,--no-enum-size-warning",
"-Wl,--no-undefined",
"-Wl,--warn-common",
# wrappers from port/printf/
"-Wl,-wrap,putchar",
"-Wl,-wrap,puts",
],
)
# Arduino core uses __libc_init_array
@@ -129,6 +132,7 @@ for code, base_dir in env["ARDUINO_DIRS"].items():
# Sources - external library ports
env.AddLibraryFlashDB(version="03500fa")
env.AddLibraryPrintf(version="6.0.0")
# Libs & linker config
env.Append(

View File

@@ -27,9 +27,27 @@ env.Append(
# implemented features
("LT_ARD_HAS_WIFI", "1"),
("LT_ARD_HAS_MD5", "1"),
# not broken anymore with printf() library
("LT_PRINTF_BROKEN", "0"),
],
LINKFLAGS=[
"-Wl,--undefined=InfraStart",
# stdio wrappers (port/printf/printf.c)
"-Wl,-wrap,rtl_printf",
"-Wl,-wrap,rtl_sprintf",
"-Wl,-wrap,rtl_snprintf",
"-Wl,-wrap,rtl_vsnprintf",
"-Wl,-wrap,rtl_vsnprintf_r",
"-Wl,-wrap,rtl_vprintf",
"-Wl,-wrap,rtl_vfprintf",
"-Wl,-wrap,DiagPrintf",
"-Wl,-wrap,DiagSPrintf",
"-Wl,-wrap,DiagSnPrintf",
"-Wl,-wrap,prvDiagPrintf",
"-Wl,-wrap,prvDiagSPrintf",
"-Wl,-wrap,VSprintf",
"-Wl,-wrap,LOG_PRINTF",
"-Wl,-wrap,__rtl_vfprintf_r_v1_00",
],
)

39
builder/libs/printf.py Normal file
View File

@@ -0,0 +1,39 @@
# Copyright (c) Kuba Szczodrzyński 2022-06-19.
from SCons.Script import DefaultEnvironment
env = DefaultEnvironment()
platform = env.PioPlatform()
def env_add_printf(
env,
version: str,
):
package_dir = platform.get_package_dir(f"library-printf@{version}")
env.AddLibrary(
name=f"printf{version}",
base_dir=package_dir,
srcs=[
"+<src/printf/printf.c>",
],
includes=[
"+<src>",
],
options=dict(
CFLAGS=["-Wno-maybe-uninitialized"],
CPPDEFINES=[("PRINTF_INCLUDE_CONFIG_H", "1")],
LINKFLAGS=[
"-Wl,-wrap,printf",
"-Wl,-wrap,sprintf",
"-Wl,-wrap,vsprintf",
"-Wl,-wrap,snprintf",
"-Wl,-wrap,vsnprintf",
"-Wl,-wrap,vprintf",
],
),
)
env.AddMethod(env_add_printf, "AddLibraryPrintf")

View File

@@ -18,8 +18,9 @@ env.SConscript("utils/flash.py", exports="env")
env.SConscript("utils/libs.py", exports="env")
env.SConscript("utils/uf2.py", exports="env")
# Vendor-specific library ports
env.SConscript("libs/lwip.py", exports="env")
env.SConscript("libs/flashdb.py", exports="env")
env.SConscript("libs/lwip.py", exports="env")
env.SConscript("libs/printf.py", exports="env")
# Firmware name
if env.get("PROGNAME", "program") == "program":

View File

@@ -62,6 +62,9 @@
"libraries": {
"flashdb": [
"03500fa"
],
"printf": [
"v6.0.0"
]
}
},
@@ -81,6 +84,14 @@
"description": "An ultra-lightweight database that supports key-value and time series data"
}
},
"library-printf": {
"type": "framework",
"optional": true,
"base_url": "https://github.com/eyalroz/printf",
"manifest": {
"description": "Tiny, fast(ish), self-contained and fully loaded printf, sprinf etc. implementation, mainly for embedded systems."
}
},
"toolchain-gccarmnoneeabi": {
"type": "toolchain",
"optionalVersions": [