[core] Add macros for wrapping and disabling SDK printf()
This commit is contained in:
@@ -16,4 +16,15 @@
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
extern unsigned char __disable_bk_printf;
|
||||
// include printf() wrapper disable methods
|
||||
#include <printf_port.h>
|
||||
|
||||
// make non-SDK code call the proper printf()
|
||||
#undef bk_printf
|
||||
#undef os_printf
|
||||
#undef warning_prf
|
||||
#undef fatal_prf
|
||||
#define bk_printf printf
|
||||
#define os_printf printf
|
||||
#define warning_prf printf
|
||||
#define fatal_prf printf
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
extern uint32_t flash_ctrl(uint32_t cmd, void *param);
|
||||
|
||||
static int init() {
|
||||
__disable_bk_printf = 1;
|
||||
__wrap_bk_printf_disable();
|
||||
flash_init();
|
||||
__disable_bk_printf = 0;
|
||||
__wrap_bk_printf_enable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <printf_config.h>
|
||||
|
||||
#include <printf/printf.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// include bk_send_byte()
|
||||
#include <uart_pub.h>
|
||||
extern void bk_send_byte(uint8_t uport, uint8_t data);
|
||||
extern int uart_print_port;
|
||||
|
||||
void putchar_(char c) {
|
||||
uart_write_byte(uart_print_port, c);
|
||||
bk_send_byte(uart_print_port, c);
|
||||
}
|
||||
|
||||
WRAP_PRINTF(bk_printf);
|
||||
|
||||
7
arduino/beken-72xx/port/printf/printf_port.h
Normal file
7
arduino/beken-72xx/port/printf/printf_port.h
Normal file
@@ -0,0 +1,7 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-20. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <printf_config.h>
|
||||
|
||||
WRAP_DISABLE_DEF(bk_printf);
|
||||
@@ -1,5 +1,9 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#define PRINTF_HAS_DISABLE 1
|
||||
|
||||
// make printf.c define wrapper functions
|
||||
#define printf_ __wrap_printf
|
||||
#define sprintf_ __wrap_sprintf
|
||||
@@ -7,3 +11,59 @@
|
||||
#define snprintf_ __wrap_snprintf
|
||||
#define vsnprintf_ __wrap_vsnprintf
|
||||
#define vprintf_ __wrap_vprintf
|
||||
|
||||
#define WRAP_DISABLE_DEF(name) \
|
||||
extern void __wrap_##name##_disable(); \
|
||||
extern void __wrap_##name##_enable(); \
|
||||
extern void __wrap_##name##_set(unsigned char disabled); \
|
||||
extern unsigned char __wrap_##name##_get();
|
||||
|
||||
#define WRAP_DISABLE_DECL(name) \
|
||||
static unsigned char __wrap_##name##_disabled = 0; \
|
||||
void __wrap_##name##_disable() { \
|
||||
__wrap_##name##_disabled = 1; \
|
||||
} \
|
||||
void __wrap_##name##_enable() { \
|
||||
__wrap_##name##_disabled = 0; \
|
||||
} \
|
||||
void __wrap_##name##_set(unsigned char disabled) { \
|
||||
__wrap_##name##_disabled = disabled; \
|
||||
} \
|
||||
unsigned char __wrap_##name##_get() { \
|
||||
return __wrap_##name##_disabled; \
|
||||
}
|
||||
|
||||
#define WRAP_DISABLE_CHECK(name) \
|
||||
{ \
|
||||
if (__wrap_##name##_disabled) \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
#define WRAP_PRINTF(name) \
|
||||
WRAP_DISABLE_DECL(name) \
|
||||
int __wrap_##name(const char *format, ...) { \
|
||||
WRAP_DISABLE_CHECK(name); \
|
||||
va_list va; \
|
||||
va_start(va, format); \
|
||||
const int ret = vprintf(format, va); \
|
||||
va_end(va); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define WRAP_SPRINTF(name) \
|
||||
int __wrap_##name(char *s, const char *format, ...) { \
|
||||
va_list va; \
|
||||
va_start(va, format); \
|
||||
const int ret = vsprintf(s, format, va); \
|
||||
va_end(va); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define WRAP_SNPRINTF(name) \
|
||||
int __wrap_##name(char *s, size_t count, const char *format, ...) { \
|
||||
va_list va; \
|
||||
va_start(va, format); \
|
||||
const int ret = vsnprintf(s, count, format, va); \
|
||||
va_end(va); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
@@ -55,3 +55,6 @@ extern void DumpForOneBytes(void *addr, int cnt); // cnt max 0x70!
|
||||
extern void SystemCoreClockUpdate(void);
|
||||
|
||||
extern int _sscanf_patch(const char *buf, const char *fmt, ...);
|
||||
|
||||
// include printf() wrapper disable methods
|
||||
#include <printf_port.h>
|
||||
|
||||
@@ -1,30 +1,17 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <printf_config.h>
|
||||
|
||||
#include <printf/printf.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
// include LOGUART_PutChar()
|
||||
#include <rtl8710b.h>
|
||||
// make sure to call the "real" vprintf instead of rtl_vprintf
|
||||
#undef vprintf
|
||||
|
||||
extern void LOGUART_PutChar(char c);
|
||||
|
||||
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;
|
||||
}
|
||||
WRAP_PRINTF(rtl_printf);
|
||||
WRAP_PRINTF(DiagPrintf);
|
||||
|
||||
8
arduino/realtek-ambz/port/printf/printf_port.h
Normal file
8
arduino/realtek-ambz/port/printf/printf_port.h
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-20. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <printf_config.h>
|
||||
|
||||
WRAP_DISABLE_DEF(rtl_printf);
|
||||
WRAP_DISABLE_DEF(DiagPrintf);
|
||||
@@ -22,6 +22,10 @@ env.Append(
|
||||
"-Wno-char-subscripts",
|
||||
"-Wno-missing-braces",
|
||||
],
|
||||
LINKFLAGS=[
|
||||
# stdio wrappers (port/printf/printf.c)
|
||||
"-Wl,-wrap,bk_printf",
|
||||
],
|
||||
)
|
||||
|
||||
# Build all libraries
|
||||
|
||||
@@ -103,8 +103,6 @@ env.Append(
|
||||
"-Wl,-wrap,snprintf",
|
||||
"-Wl,-wrap,sprintf",
|
||||
"-Wl,-wrap,vsnprintf",
|
||||
# for disabling os_printf() output
|
||||
"-Wl,-wrap,bk_printf",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -148,7 +146,6 @@ env.AddLibrary(
|
||||
"+<arch_main.c>",
|
||||
"+<ate_app.c>",
|
||||
"+<intc.c>",
|
||||
"+<wrap.c>",
|
||||
*srcs_fixups,
|
||||
],
|
||||
)
|
||||
|
||||
@@ -56,7 +56,6 @@ env.Append(
|
||||
("in_addr_t", "u32_t"),
|
||||
("INT_MAX", "2147483647"), # for RECV_BUFSIZE_DEFAULT
|
||||
("ERRNO", "1"), # for LwIP
|
||||
("vprintf", "rtl_vprintf"),
|
||||
"MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED", # enable PSK in mbedTLS
|
||||
# "MBEDTLS_DEBUG_C",
|
||||
],
|
||||
|
||||
@@ -25,7 +25,12 @@ void entry_set_world_flag(void) {
|
||||
#endif // CFG_SUPPORT_BOOTLOADER
|
||||
|
||||
extern void main(void);
|
||||
extern unsigned char __disable_bk_printf;
|
||||
|
||||
#ifdef LIBRETUYA
|
||||
// beken-72xx has printf_port.h
|
||||
extern void __wrap_bk_printf_disable();
|
||||
extern void __wrap_bk_printf_enable();
|
||||
#endif
|
||||
|
||||
void entry_main(void) {
|
||||
// compatibility with BK7231S_1.0.5
|
||||
@@ -33,7 +38,9 @@ void entry_main(void) {
|
||||
entry_set_world_flag();
|
||||
#endif
|
||||
// suppress all output during initialization
|
||||
__disable_bk_printf = 1;
|
||||
#if LIBRETUYA
|
||||
__wrap_bk_printf_disable();
|
||||
#endif
|
||||
// read reboot cause into bk_misc_get_start_type()
|
||||
bk_misc_init_start_type();
|
||||
// clear driver registration arrays
|
||||
@@ -50,7 +57,9 @@ void entry_main(void) {
|
||||
bk_wdg_reload();
|
||||
#endif
|
||||
// enable bk_printf output again
|
||||
__disable_bk_printf = 0;
|
||||
#if LIBRETUYA
|
||||
__wrap_bk_printf_enable();
|
||||
#endif
|
||||
// run the app
|
||||
main();
|
||||
}
|
||||
|
||||
18
platform/beken-72xx/fixups/inc/uart_pub.h
Normal file
18
platform/beken-72xx/fixups/inc/uart_pub.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-20. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include_next "uart_pub.h"
|
||||
|
||||
#ifdef LIBRETUYA
|
||||
// make uart.c call __wrap_bk_printf() instead of bk_printf()
|
||||
// standard wrapping does not work in this case, as bk_printf()
|
||||
// is implemented in the same translation unit
|
||||
extern void __wrap_bk_printf(const char *fmt, ...);
|
||||
#undef bk_printf
|
||||
#undef os_printf
|
||||
#undef as_printf
|
||||
// not defining bk_printf() again, as this would just change the impl name
|
||||
#define os_printf __wrap_bk_printf
|
||||
#define as_printf (__wrap_bk_printf("%s:%d\r\n", __FUNCTION__, __LINE__))
|
||||
#endif
|
||||
@@ -1,22 +0,0 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-14. */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
extern int __wrap_vsnprintf(char *str, size_t size, const char *format, va_list args);
|
||||
extern void bk_send_string(unsigned char uport, const char *string);
|
||||
extern int uart_print_port;
|
||||
|
||||
unsigned char __disable_bk_printf = 0;
|
||||
|
||||
void __wrap_bk_printf(const char *fmt, ...) {
|
||||
if (__disable_bk_printf)
|
||||
return;
|
||||
va_list ap;
|
||||
char string[128];
|
||||
va_start(ap, fmt);
|
||||
__wrap_vsnprintf(string, sizeof(string) - 1, fmt, ap);
|
||||
string[127] = 0;
|
||||
bk_send_string(uart_print_port, string);
|
||||
va_end(ap);
|
||||
}
|
||||
Reference in New Issue
Block a user