From 73ff0687f2f5331e962a4af0126097e9921db631 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 10 Nov 2015 18:57:45 +0200 Subject: lib/utils/printf: Move from stmhal/ . This file contains various MicroPython-specific helper functions, so isn't good fit for lib/libc/. --- cc3200/application.mk | 2 +- cc3200/bootmgr/bootloader.mk | 7 +-- esp8266/Makefile | 2 +- lib/utils/printf.c | 114 +++++++++++++++++++++++++++++++++++++++++++ minimal/Makefile | 2 +- stmhal/Makefile | 2 +- stmhal/printf.c | 114 ------------------------------------------- teensy/Makefile | 2 +- 8 files changed, 121 insertions(+), 124 deletions(-) create mode 100644 lib/utils/printf.c delete mode 100644 stmhal/printf.c diff --git a/cc3200/application.mk b/cc3200/application.mk index 36d8e4090..5b81f7228 100644 --- a/cc3200/application.mk +++ b/cc3200/application.mk @@ -152,6 +152,7 @@ APP_LIB_SRC_C = $(addprefix lib/,\ netutils/netutils.c \ timeutils/timeutils.c \ utils/pyexec.c \ + utils/printf.c \ ) APP_STM_SRC_C = $(addprefix stmhal/,\ @@ -162,7 +163,6 @@ APP_STM_SRC_C = $(addprefix stmhal/,\ irq.c \ lexerfatfs.c \ moduselect.c \ - printf.c \ pybstdio.c \ ) diff --git a/cc3200/bootmgr/bootloader.mk b/cc3200/bootmgr/bootloader.mk index a97f888ed..ee5236913 100644 --- a/cc3200/bootmgr/bootloader.mk +++ b/cc3200/bootmgr/bootloader.mk @@ -65,16 +65,13 @@ BOOT_PY_SRC_C = $(addprefix py/,\ mpprint.c \ ) -BOOT_STM_SRC_C = $(addprefix stmhal/,\ - printf.c \ - ) - BOOT_LIB_SRC_C = $(addprefix lib/,\ libc/string0.c \ + utils/printf.c \ ) OBJ = $(addprefix $(BUILD)/, $(BOOT_HAL_SRC_C:.c=.o) $(BOOT_SL_SRC_C:.c=.o) $(BOOT_CC3100_SRC_C:.c=.o) $(BOOT_UTIL_SRC_C:.c=.o) $(BOOT_MISC_SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(BOOT_MAIN_SRC_C:.c=.o) $(BOOT_MAIN_SRC_S:.s=.o) $(BOOT_PY_SRC_C:.c=.o) $(BOOT_STM_SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(BOOT_MAIN_SRC_C:.c=.o) $(BOOT_MAIN_SRC_S:.s=.o) $(BOOT_PY_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(BOOT_LIB_SRC_C:.c=.o)) # Add the linker script diff --git a/esp8266/Makefile b/esp8266/Makefile index d930a2555..016212315 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -67,7 +67,6 @@ SRC_C = \ $(BUILD)/frozen.c \ STM_SRC_C = $(addprefix stmhal/,\ - printf.c \ pybstdio.c \ ) @@ -77,6 +76,7 @@ LIB_SRC_C = $(addprefix lib/,\ netutils/netutils.c \ timeutils/timeutils.c \ utils/pyexec.c \ + utils/printf.c \ ) SRC_S = \ diff --git a/lib/utils/printf.c b/lib/utils/printf.c new file mode 100644 index 000000000..611ad2df4 --- /dev/null +++ b/lib/utils/printf.c @@ -0,0 +1,114 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +#include "py/obj.h" +#include "py/mphal.h" + +#if MICROPY_PY_BUILTINS_FLOAT +#include "py/formatfloat.h" +#endif + +int printf(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int ret = mp_vprintf(&mp_plat_print, fmt, ap); + va_end(ap); + return ret; +} + +int vprintf(const char *fmt, va_list ap) { + return mp_vprintf(&mp_plat_print, fmt, ap); +} + +#if MICROPY_DEBUG_PRINTERS +mp_uint_t mp_verbose_flag = 1; + +int DEBUG_printf(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int ret = mp_vprintf(&mp_plat_print, fmt, ap); + va_end(ap); + return ret; +} +#endif + +// need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a') +#undef putchar // Some stdlibs have a #define for putchar +int putchar(int c) { + char chr = c; + mp_hal_stdout_tx_strn_cooked(&chr, 1); + return chr; +} + +// need this because gcc optimises printf("string\n") -> puts("string") +int puts(const char *s) { + mp_hal_stdout_tx_strn_cooked(s, strlen(s)); + char chr = '\n'; + mp_hal_stdout_tx_strn_cooked(&chr, 1); + return 1; +} + +typedef struct _strn_print_env_t { + char *cur; + size_t remain; +} strn_print_env_t; + +STATIC void strn_print_strn(void *data, const char *str, mp_uint_t len) { + strn_print_env_t *strn_print_env = data; + if (len > strn_print_env->remain) { + len = strn_print_env->remain; + } + memcpy(strn_print_env->cur, str, len); + strn_print_env->cur += len; + strn_print_env->remain -= len; +} + +int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) { + strn_print_env_t strn_print_env = {str, size}; + mp_print_t print = {&strn_print_env, strn_print_strn}; + int len = mp_vprintf(&print, fmt, ap); + // add terminating null byte + if (size > 0) { + if (strn_print_env.remain == 0) { + strn_print_env.cur[-1] = 0; + } else { + strn_print_env.cur[0] = 0; + } + } + return len; +} + +int snprintf(char *str, size_t size, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int ret = vsnprintf(str, size, fmt, ap); + va_end(ap); + return ret; +} diff --git a/minimal/Makefile b/minimal/Makefile index 57dc84ec8..f6e0ef14c 100644 --- a/minimal/Makefile +++ b/minimal/Makefile @@ -44,7 +44,7 @@ SRC_C = \ main.c \ uart_core.c \ uart_extra.c \ - stmhal/printf.c \ + lib/utils/printf.c \ lib/utils/pyexec.c \ lib/libc/string0.c \ lib/mp-readline/readline.c \ diff --git a/stmhal/Makefile b/stmhal/Makefile index ab1c345af..de41d9261 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -104,6 +104,7 @@ SRC_LIB = $(addprefix lib/,\ netutils/netutils.c \ timeutils/timeutils.c \ utils/pyexec.c \ + utils/printf.c \ ) SRC_C = \ @@ -130,7 +131,6 @@ SRC_C = \ uart.c \ can.c \ usb.c \ - printf.c \ gccollect.c \ pybstdio.c \ help.c \ diff --git a/stmhal/printf.c b/stmhal/printf.c deleted file mode 100644 index 611ad2df4..000000000 --- a/stmhal/printf.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include -#include - -#include "py/obj.h" -#include "py/mphal.h" - -#if MICROPY_PY_BUILTINS_FLOAT -#include "py/formatfloat.h" -#endif - -int printf(const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - int ret = mp_vprintf(&mp_plat_print, fmt, ap); - va_end(ap); - return ret; -} - -int vprintf(const char *fmt, va_list ap) { - return mp_vprintf(&mp_plat_print, fmt, ap); -} - -#if MICROPY_DEBUG_PRINTERS -mp_uint_t mp_verbose_flag = 1; - -int DEBUG_printf(const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - int ret = mp_vprintf(&mp_plat_print, fmt, ap); - va_end(ap); - return ret; -} -#endif - -// need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a') -#undef putchar // Some stdlibs have a #define for putchar -int putchar(int c) { - char chr = c; - mp_hal_stdout_tx_strn_cooked(&chr, 1); - return chr; -} - -// need this because gcc optimises printf("string\n") -> puts("string") -int puts(const char *s) { - mp_hal_stdout_tx_strn_cooked(s, strlen(s)); - char chr = '\n'; - mp_hal_stdout_tx_strn_cooked(&chr, 1); - return 1; -} - -typedef struct _strn_print_env_t { - char *cur; - size_t remain; -} strn_print_env_t; - -STATIC void strn_print_strn(void *data, const char *str, mp_uint_t len) { - strn_print_env_t *strn_print_env = data; - if (len > strn_print_env->remain) { - len = strn_print_env->remain; - } - memcpy(strn_print_env->cur, str, len); - strn_print_env->cur += len; - strn_print_env->remain -= len; -} - -int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) { - strn_print_env_t strn_print_env = {str, size}; - mp_print_t print = {&strn_print_env, strn_print_strn}; - int len = mp_vprintf(&print, fmt, ap); - // add terminating null byte - if (size > 0) { - if (strn_print_env.remain == 0) { - strn_print_env.cur[-1] = 0; - } else { - strn_print_env.cur[0] = 0; - } - } - return len; -} - -int snprintf(char *str, size_t size, const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - int ret = vsnprintf(str, size, fmt, ap); - va_end(ap); - return ret; -} diff --git a/teensy/Makefile b/teensy/Makefile index b458485ce..c18263180 100644 --- a/teensy/Makefile +++ b/teensy/Makefile @@ -98,7 +98,6 @@ STM_SRC_C = $(addprefix stmhal/,\ irq.c \ pin.c \ pin_named_pins.c \ - printf.c \ pybstdio.c \ ) @@ -110,6 +109,7 @@ LIB_SRC_C = $(addprefix lib/,\ libc/string0.c \ mp-readline/readline.c \ utils/pyexec.c \ + utils/printf.c \ ) SRC_TEENSY = $(addprefix core/,\ -- cgit v1.2.3