[feat] add printf function

This commit is contained in:
zhji 2025-03-19 16:50:09 +08:00
parent 87e54ba209
commit e1920665b2
5 changed files with 1177 additions and 7 deletions

View File

@ -6,6 +6,8 @@ add_library(${TARGET} STATIC ${FILELIST})
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/pio_instance pio_instance)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/eth eth)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/printf printf)
target_link_libraries(${TARGET} pio_instance)
target_link_libraries(${TARGET} eth)
target_link_libraries(${TARGET} printf)

View File

@ -0,0 +1,10 @@
file(GLOB FILELIST
printf.c
vsnprintf.c
)
set(TARGET printf)
add_library(${TARGET} STATIC ${FILELIST})
target_include_directories(${TARGET} PUBLIC ${BASE_DIR}/driver/inc)
target_include_directories(${TARGET} PUBLIC ${BASE_DIR}/driver/inc/reg)

72
component/printf/printf.c Normal file
View File

@ -0,0 +1,72 @@
#include "stdint.h"
#include "stdarg.h"
#include "string.h"
#include "uart.h"
uint8_t console = UART_ID_0;
extern void uart_write_block(uint8_t uart_id, uint8_t *data, uint32_t length);
int putchar(int c)
{
uart_write_block(console, (uint8_t *)c, 1);
return c;
}
int puts(const char *c)
{
int len;
len = strlen(c);
uart_write_block(console, (uint8_t *)c, len);
return len;
}
int putstring(const char *c)
{
int len;
if (c == NULL) {
return 0;
}
len = strlen(c);
uart_write_block(console, (uint8_t *)c, len);
return len;
}
#if defined(CONFIG_VSNPRINTF_NANO) && CONFIG_VSNPRINTF_NANO
int printf(const char *fmt, ...)
{
char print_buf[512];
int len;
va_list ap;
va_start(ap, fmt);
len = vsnprintf(print_buf, sizeof(print_buf), fmt, ap);
va_end(ap);
len = (len > sizeof(print_buf)) ? sizeof(print_buf) : len;
uart_write_block(console, (uint8_t *)print_buf, len);
return len;
}
#else
extern int console_vsnprintf(const char *fmt, va_list args);
int printf(const char *fmt, ...)
{
int len;
va_list ap;
va_start(ap, fmt);
len = console_vsnprintf(fmt, ap);
va_end(ap);
return len;
}
#endif

1090
component/printf/vsnprintf.c Normal file

File diff suppressed because it is too large Load Diff

10
main.c
View File

@ -10,6 +10,7 @@
#include "main_core0.h"
#include "main_core1.h"
#include "udp.h"
#include "stdio.h"
#define STR_MAC_SRC "11:22:33:44:55:66"
#define STR_MAC_DST "1C:1B:0D:2E:A9:99"
@ -136,7 +137,6 @@ struct uart_cfg_s uart_cfg = {
.tx_fifo_level = UART_FIFO_LEVEL_1_2,
.rx_fifo_level = UART_FIFO_LEVEL_1_2,
};
uint8_t uart_data[] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0};
int main(void)
{
@ -173,12 +173,11 @@ int main(void)
reset_disable(RESET_UART0);
uart_init(UART_ID_0, &uart_cfg);
uart_set_baudrate(UART_ID_0, 200 * 1000 * 1000, 2 * 1000 * 1000);
uart_set_baudrate(UART_ID_0, 200 * 1000 * 1000, 6 * 1000 * 1000);
while (1) {
static uint32_t tick_led = 0;
static uint32_t tick_rmii = 0;
static uint32_t tick_uart = 0;
if (tick_1ms > (tick_rmii + 1)) {
tick_rmii = tick_1ms;
pio_rmii_write(&rmii, (uint32_t *)buffer, length);
@ -186,10 +185,7 @@ int main(void)
if (tick_1ms > (tick_led + 500)) {
tick_led = tick_1ms;
gpio_toggle(gpio_led_cfg.pin);
}
if (tick_1ms > (tick_uart + 10)) {
tick_uart = tick_1ms;
uart_write_block(UART_ID_0, uart_data, sizeof(uart_data));
printf("tick_led = %ld\r\n", tick_led);
}
}