Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@ if(NOT ESP_TARGET_LIB)
endif()

set(srcs
src/log.c
src/flash.c
)

if(STUB_LOG_ENABLED IN_LIST STUB_COMPILE_DEFS)
list(APPEND srcs src/log_common.c)

if(STUB_LIB_LOG_UART IN_LIST STUB_COMPILE_DEFS)
list(APPEND srcs src/log_uart.c)
else()
# STUB_LIB_LOG_BUF is by default
if(NOT STUB_LIB_LOG_BUF IN_LIST STUB_COMPILE_DEFS)
list(APPEND STUB_COMPILE_DEFS STUB_LIB_LOG_BUF)
endif()
list(APPEND srcs src/log_buf.c)
endif()
endif()

add_library(${ESP_STUB_LIB} STATIC ${srcs})

target_include_directories(${ESP_STUB_LIB}
PUBLIC include
INTERFACE include
PRIVATE include/esp-stub-lib
)
include_directories(include)

# STUB_COMPILE_DEFS is optional definitions coming from the parent CMakeLists.txt
target_compile_definitions(${ESP_STUB_LIB} PRIVATE ${STUB_COMPILE_DEFS})
Expand Down
3 changes: 2 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ if(${ESP_TARGET} STREQUAL "esp8266")
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--entry=stub_main_esp8266)
endif()

target_compile_definitions(${PROJECT_NAME} PRIVATE asm=__asm__ STUB_LOG_ENABLED)
set(STUB_COMPILE_DEFS "STUB_LOG_ENABLED")
target_compile_definitions(${PROJECT_NAME} PRIVATE asm=__asm__ ${STUB_COMPILE_DEFS})

set(MAP_FILE ${CMAKE_CURRENT_BINARY_DIR}/${APP_NAME}.map)
target_link_options(${PROJECT_NAME} PRIVATE -Wl,-Map=${MAP_FILE})
Expand Down
18 changes: 9 additions & 9 deletions example/stub_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ struct stub_cmd_handler {
int (*handler)(va_list ap);
};

#ifdef STUB_LOG_ENABLED
#define STUB_LOG_INIT(uart_num, baudrate) stub_lib_log_init(uart_num, baudrate)
#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__)
#else
#define STUB_LOG_INIT(uart_num, baudrate)
#define STUB_LOG(fmt, ...)
#endif

static __attribute__((unused)) int handle_test1(va_list ap)
{
(void)ap;
Expand All @@ -41,6 +33,14 @@ static __attribute__((unused)) int handle_test1(va_list ap)
STUB_LOG("stub command test:%c\n", 'A');
STUB_LOG("stub command test:%l\n", 10); // not supported

STUB_LOGE("stub command test\n");
STUB_LOGW("stub command test\n");
STUB_LOGI("stub command test\n");
STUB_LOGD("stub command test\n");
STUB_LOGV("stub command test\n");
STUB_LOG_TRACE();
STUB_LOG_TRACEF("foo:%u\n", 0x2A);

return 0;
}

Expand Down Expand Up @@ -84,7 +84,7 @@ int stub_main(int cmd, ...)

va_start(ap, cmd);

STUB_LOG_INIT(0, 115200);
STUB_LOG_INIT();

stub_lib_flash_init(&flash_state);

Expand Down
24 changes: 22 additions & 2 deletions include/esp-stub-lib/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@

#pragma once

#include <stdint.h>
#if defined(STUB_LOG_ENABLED)

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void stub_lib_log_init(uint8_t uart_num, uint32_t baudrate);
void stub_lib_log_init();
void stub_lib_log_printf(const char *fmt, ...);

#ifdef __cplusplus
}
#endif // __cplusplus

#define STUB_LOG_INIT() stub_lib_log_init()
#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__)

#else // defined(STUB_LOG_ENABLED)

#define STUB_LOG_INIT() do {} while(0)
#define STUB_LOG(fmt, ...) do {} while(0)

#endif // defined(STUB_LOG_ENABLED)

#define STUB_LOGE(fmt, ...) STUB_LOG("STUB_E: " fmt, ##__VA_ARGS__)
#define STUB_LOGW(fmt, ...) STUB_LOG("STUB_W: " fmt, ##__VA_ARGS__)
#define STUB_LOGI(fmt, ...) STUB_LOG("STUB_I: " fmt, ##__VA_ARGS__)
#define STUB_LOGD(fmt, ...) STUB_LOG("STUB_D: " fmt, ##__VA_ARGS__)
#define STUB_LOGV(fmt, ...) STUB_LOG("STUB_V: " fmt, ##__VA_ARGS__)
// trace only function name
#define STUB_LOG_TRACE() STUB_LOG("STUB_TRACE %s()\n", __func__)
// trace with format
#define STUB_LOG_TRACEF(fmt, ...) STUB_LOG("STUB_TRACE %s(): " fmt, __func__, ##__VA_ARGS__)
35 changes: 35 additions & 0 deletions src/log_buf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
#include <log.h>

#include <stdint.h>
#include <stddef.h>

extern void ets_printf(const char *fmt, ...);
extern void ets_install_putc1(void (*p)(char c));
extern void ets_install_putc2(void (*p)(char c));

#if !defined(STUB_LIB_LOG_BUF_SIZE)
#define STUB_LIB_LOG_BUF_SIZE 4096
#endif

struct stub_lib_log_buf {
uint32_t count;
char buf[STUB_LIB_LOG_BUF_SIZE];
};

static struct stub_lib_log_buf g_stub_lib_log_buf;
static void stub_lib_log_buf_write_char(char c)
{
g_stub_lib_log_buf.buf[g_stub_lib_log_buf.count] = c;
g_stub_lib_log_buf.count = (g_stub_lib_log_buf.count + 1) % STUB_LIB_LOG_BUF_SIZE;
}

void stub_lib_log_init()
{
ets_install_putc1(stub_lib_log_buf_write_char);
ets_install_putc2(NULL);
}
21 changes: 3 additions & 18 deletions src/log.c → src/log_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,11 @@
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <log.h>

#include <target/esp_rom_caps.h>
#include <target/uart.h>
#include <stdarg.h>

// These functions are defined in the ROM
extern void ets_install_uart_printf(void);
extern void ets_printf(const char *fmt, ...);
extern void ets_install_putc1(void (*p)(char c));
extern void ets_install_putc2(void (*p)(char c));

void stub_lib_log_init(uint8_t uart_num, uint32_t baudrate)
{
stub_target_uart_init(uart_num, baudrate);
ets_install_putc1(NULL);
ets_install_putc2(NULL);
ets_install_uart_printf();
}

// This function is designed to avoid implementing vprintf() to reduce code size.
// It only supports a subset of format specifiers: %s, %d, %u, %x, %X, %c.
Expand Down Expand Up @@ -70,7 +55,7 @@ void stub_lib_log_printf(const char *fmt, ...)
break;
default:
buf[0] = '%';
buf[1] = *fmt ? *fmt++ : '\0';
buf[1] = *fmt;
buf[2] = '\0';
ets_printf("%s", buf);
break;
Expand Down
21 changes: 21 additions & 0 deletions src/log_uart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
#include <log.h>

#include <target/uart.h>
#include <stddef.h>

extern void ets_install_putc1(void (*p)(char c));
extern void ets_install_putc2(void (*p)(char c));
extern void ets_install_uart_printf(void);

void stub_lib_log_init()
{
stub_target_uart_init(0, 115200);
ets_install_putc1(NULL);
ets_install_putc2(NULL);
ets_install_uart_printf();
}