From 21529d6a79330b61a5ffcebb2557a754b26bf6d1 Mon Sep 17 00:00:00 2001 From: jzlv Date: Wed, 23 Nov 2022 18:48:03 +0800 Subject: [PATCH] [update][util] update vlibc --- utils/CMakeLists.txt | 1 + utils/libc/printf.c | 5 +-- utils/vlibc/printf.c | 74 +++++++++++++++++++++++++++-------- utils/vlibc/syscalls.c | 69 ++++---------------------------- utils/vlibc/vlibc_vsnprintf.c | 11 ++++-- 5 files changed, 74 insertions(+), 86 deletions(-) diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 8ef3c884..6a1ebf71 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -105,4 +105,5 @@ endif() sdk_library_add_sources(bflb_timestamp/bflb_timestamp.c) sdk_add_include_directories(bflb_timestamp) +sdk_add_compile_options(-fno-builtin-printf) sdk_add_link_options(-u_malloc_r -u_vsnprintf) \ No newline at end of file diff --git a/utils/libc/printf.c b/utils/libc/printf.c index f1a0d180..969b60d0 100644 --- a/utils/libc/printf.c +++ b/utils/libc/printf.c @@ -3,10 +3,7 @@ struct bflb_device_s *console = NULL; -int puts(const char *fmt) __attribute__((alias("bflb_printf"))); -int printf(const char *fmt, ...) __attribute__((alias("bflb_printf"))); - -int bflb_printf(const char *fmt, ...) +int printf(const char *fmt, ...) { char print_buf[128]; uint32_t len; diff --git a/utils/vlibc/printf.c b/utils/vlibc/printf.c index 8ad0da18..4cecf28b 100644 --- a/utils/vlibc/printf.c +++ b/utils/vlibc/printf.c @@ -8,11 +8,6 @@ struct bflb_device_s *console = NULL; -void bflb_reg_dump(uint32_t addr) -{ - printf("%08lx[31:0]=%08lx\r\n", addr, *(volatile uint32_t *)(uintptr_t)(addr)); -} - uint16_t __console_output(void *ptr, uint16_t size) { for (size_t i = 0; i < size; i++) { @@ -21,18 +16,6 @@ uint16_t __console_output(void *ptr, uint16_t size) return size; } -void bflb_uart_set_console(struct bflb_device_s *dev) -{ - console = dev; - bflb_uart_putchar(console, '\r'); - vlibc_stdout = vlibc_fopen("<" IOCONSOLE_NAME, "w"); - vlibc_setvbuf(vlibc_stdout, NULL, _IONBF, 0); - vlibc_stderr = vlibc_stdout; - - extern void log_init(void); - log_init(); -} - __WEAK uint32_t __vlibc_io_init(const char *name, uint8_t mode) { (void)mode; @@ -65,4 +48,61 @@ __WEAK size_t __vlibc_io_mem2dev(struct __vlibc_io *io, const void *ptr, size_t } return 0; +} + +#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ') +void bflb_dump_hex(const void *ptr, uint32_t buflen) +{ + unsigned char *buf = (unsigned char *)ptr; + int i, j; + + for (i = 0; i < buflen; i += 16) { + printf("%08X:", i); + + for (j = 0; j < 16; j++) + if (i + j < buflen) { + if ((j % 8) == 0) { + printf(" "); + } + + printf("%02X ", buf[i + j]); + } else + printf(" "); + printf(" "); + + for (j = 0; j < 16; j++) + if (i + j < buflen) + printf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.'); + printf("\n"); + } +} + +void bflb_reg_dump(uint32_t addr) +{ + printf("%08lx[31:0]=%08lx\r\n", addr, *(volatile uint32_t *)(uintptr_t)(addr)); +} + +int bflb_data_compare(const uint8_t *expected, uint8_t *input, uint32_t len) +{ + int i = 0; + for (i = 0; i < len; i++) { + if (input[i] != expected[i]) { + printf("Compare fail at %d,input %02x, but expect %02x\r\n", i, input[i], expected[i]); + return -1; + } + } + + return 0; +} + +void bflb_uart_set_console(struct bflb_device_s *dev) +{ + console = dev; + bflb_uart_putchar(console, '\r'); + vlibc_stdout = vlibc_fopen("<" IOCONSOLE_NAME, "w"); + vlibc_setvbuf(vlibc_stdout, NULL, _IONBF, 0); + vlibc_stderr = vlibc_stdout; + + extern void log_init(void); + log_init(); } \ No newline at end of file diff --git a/utils/vlibc/syscalls.c b/utils/vlibc/syscalls.c index 92e97ae9..036c25e3 100644 --- a/utils/vlibc/syscalls.c +++ b/utils/vlibc/syscalls.c @@ -2,28 +2,20 @@ #include #include #include "mmheap.h" +#include "bflb_uart.h" extern struct heap_info mmheap_root; -#ifdef CONF_VFS_ENABLE -#include -#endif +extern struct bflb_device_s *console; /* Reentrant versions of system calls. */ -/* global errno in RT-Thread */ +/* global errno */ static volatile int _sys_errno = 0; #ifndef _REENT_ONLY int *__errno() { - // #if (configUSE_POSIX_ERRNO == 1) - // { - // extern int FreeRTOS_errno; - - // return &FreeRTOS_errno; - // } - // #endif return (int *)&_sys_errno; } #endif @@ -84,108 +76,58 @@ int _link_r(struct _reent *ptr, const char *old, const char *new) _off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - _off_t rc; - - rc = aos_lseek(fd, pos, whence); - return rc; -#endif } int _mkdir_r(struct _reent *ptr, const char *name, int mode) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - int rc; - - rc = aos_mkdir(name); - return rc; -#endif } int _open_r(struct _reent *ptr, const char *file, int flags, int mode) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - int rc; - - rc = aos_open(file, flags); - return rc; -#endif } int _close_r(struct _reent *ptr, int fd) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - return aos_close(fd); -#endif } _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - _ssize_t rc; - - rc = aos_read(fd, buf, nbytes); - return rc; -#endif } int _rename_r(struct _reent *ptr, const char *old, const char *new) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - int rc; - - rc = aos_rename(old, new); - return rc; -#endif } int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - int rc; - - rc = aos_stat(file, pstat); - return rc; -#endif } int _unlink_r(struct _reent *ptr, const char *file) { -#ifndef CONF_VFS_ENABLE /* return "not supported" */ ptr->_errno = -ENOSYS; return -1; -#else - return aos_unlink(file); -#endif } int _wait_r(struct _reent *ptr, int *status) @@ -197,7 +139,10 @@ int _wait_r(struct _reent *ptr, int *status) _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) { - return -1; + if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd)) { + bflb_uart_put(console, (uint8_t *)buf, nbytes); + } + return 0; } void *_malloc_r(struct _reent *ptr, size_t size) diff --git a/utils/vlibc/vlibc_vsnprintf.c b/utils/vlibc/vlibc_vsnprintf.c index 68e53dda..c3457dcf 100644 --- a/utils/vlibc/vlibc_vsnprintf.c +++ b/utils/vlibc/vlibc_vsnprintf.c @@ -1314,6 +1314,7 @@ static int _vsnprintf(output_gadget_t *output, void (*putc_function)(output_gadg * @param arg * @return int */ +// int vfprintf(FILE *__restrict, const char *__restrict, __VALIST) __attribute__((alias("vlibc_vfprintf"))); int vlibc_vfprintf(VLIBC_FILE *stream, const char *format, va_list arg) { CHECK_FILE(stream, EOF); @@ -1389,6 +1390,7 @@ int vlibc_vfprintf(VLIBC_FILE *stream, const char *format, va_list arg) * @param arg * @return int */ +// int vprintf(const char *, __VALIST) __attribute__((alias("vlibc_vprintf"))); int vlibc_vprintf(const char *format, va_list arg) { return vlibc_vfprintf(vlibc_stdout, format, arg); @@ -1402,7 +1404,7 @@ int vlibc_vprintf(const char *format, va_list arg) * @param arg * @return int */ -int vsnprintf (char *, size_t, const char *, va_list) __attribute__ ((alias ("vlibc_vsnprintf"))); +int vsnprintf(char *__restrict, size_t, const char *__restrict, __VALIST) __attribute__((alias("vlibc_vsnprintf"))); int vlibc_vsnprintf(char *str, size_t size, const char *format, va_list arg) { CHECK_FILE(str, EOF); @@ -1423,6 +1425,7 @@ int vlibc_vsnprintf(char *str, size_t size, const char *format, va_list arg) * @param arg * @return int */ +// int vsprintf(char *__restrict, const char *__restrict, __VALIST) __attribute__((alias("vlibc_vsprintf"))); int vlibc_vsprintf(char *str, const char *format, va_list arg) { return vlibc_vsnprintf(str, PRINTF_MAX_POSSIBLE_BUFFER_SIZE, format, arg); @@ -1435,6 +1438,7 @@ int vlibc_vsprintf(char *str, const char *format, va_list arg) * @param ... * @return int */ +// int fprintf(FILE *__restrict, const char *__restrict, ...) __attribute__((alias("vlibc_fprintf"))); int vlibc_fprintf(VLIBC_FILE *stream, const char *format, ...) { va_list args; @@ -1450,8 +1454,7 @@ int vlibc_fprintf(VLIBC_FILE *stream, const char *format, ...) * @param ... * @return int */ -// int puts(const char *__restrict fmt) __attribute__((alias("vlibc_printf"))); -// int printf(const char *__restrict fmt, ...) __attribute__((alias("vlibc_printf"))); +int printf(const char *__restrict, ...) __attribute__((alias("vlibc_printf"))); int vlibc_printf(const char *format, ...) { va_list args; @@ -1468,6 +1471,7 @@ int vlibc_printf(const char *format, ...) * @param ... * @return int */ +// int sprintf(char *__restrict, const char *__restrict, ...) __attribute__((alias("vlibc_sprintf"))); int vlibc_sprintf(char *str, const char *format, ...) { va_list args; @@ -1485,6 +1489,7 @@ int vlibc_sprintf(char *str, const char *format, ...) * @param ... * @return int */ +// int snprintf(char *__restrict, size_t, const char *__restrict, ...) __attribute__((alias("vlibc_snprintf"))); int vlibc_snprintf(char *str, size_t size, const char *format, ...) { va_list args;