From e17b55e33ad5b3dcefcf7b828ac99aeb2daeeae2 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Mon, 4 May 2020 07:19:51 +0100 Subject: [PATCH] Fix conflicting types for 'tfp_printf' (#8269) * Refactor to use mpaland/printf * trim firmware size * remove keymap changes * run clang format * Fixup after rebase * fix up git-submodule command for printf --- .gitmodules | 3 + Makefile | 1 + lib/printf | 1 + tmk_core/common.mk | 9 +- tmk_core/common/chibios/printf.c | 233 --------------------------- tmk_core/common/chibios/printf.h | 110 ------------- tmk_core/common/print.h | 5 +- tmk_core/protocol/chibios/main.c | 3 - tmk_core/protocol/chibios/usb_main.c | 5 +- tmk_core/protocol/chibios/usb_main.h | 2 - 10 files changed, 16 insertions(+), 356 deletions(-) create mode 160000 lib/printf delete mode 100644 tmk_core/common/chibios/printf.c delete mode 100644 tmk_core/common/chibios/printf.h diff --git a/.gitmodules b/.gitmodules index 6d8ab8b94633..b8c66034b35a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "lib/lufa"] path = lib/lufa url = https://github.com/qmk/lufa +[submodule "lib/printf"] + path = lib/printf + url = https://github.com/mpaland/printf.git diff --git a/Makefile b/Makefile index 9478c14f8c2a..cc3bde6b1f0f 100644 --- a/Makefile +++ b/Makefile @@ -567,6 +567,7 @@ ifndef SKIP_GIT if [ ! -e lib/chibios-contrib ]; then git submodule sync lib/chibios-contrib && git submodule update --depth 50 --init lib/chibios-contrib; fi if [ ! -e lib/ugfx ]; then git submodule sync lib/ugfx && git submodule update --depth 50 --init lib/ugfx; fi if [ ! -e lib/lufa ]; then git submodule sync lib/lufa && git submodule update --depth 50 --init lib/lufa; fi + if [ ! -e lib/printf ]; then git submodule sync lib/printf && git submodule update --depth 50 --init lib/printf; fi git submodule status --recursive 2>/dev/null | \ while IFS= read -r x; do \ case "$$x" in \ diff --git a/lib/printf b/lib/printf new file mode 160000 index 000000000000..d3b984684bb8 --- /dev/null +++ b/lib/printf @@ -0,0 +1 @@ +Subproject commit d3b984684bb8a8bdc48cc7a1abecb93ce59bbe3e diff --git a/tmk_core/common.mk b/tmk_core/common.mk index aa8a0eb7ad7e..63de7c7edeeb 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk @@ -1,3 +1,5 @@ +PRINTF_PATH = $(LIB_PATH)/printf + COMMON_DIR = common PLATFORM_COMMON_DIR = $(COMMON_DIR)/$(PLATFORM_KEY) @@ -21,7 +23,12 @@ TMK_COMMON_SRC += $(COMMON_DIR)/host.c \ ifeq ($(PLATFORM),AVR) TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/xprintf.S else ifeq ($(PLATFORM),CHIBIOS) - TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c + TMK_COMMON_SRC += $(PRINTF_PATH)/printf.c + TMK_COMMON_DEFS += -DPRINTF_DISABLE_SUPPORT_FLOAT + TMK_COMMON_DEFS += -DPRINTF_DISABLE_SUPPORT_EXPONENTIAL + TMK_COMMON_DEFS += -DPRINTF_DISABLE_SUPPORT_LONG_LONG + TMK_COMMON_DEFS += -DPRINTF_DISABLE_SUPPORT_PTRDIFF_T + VPATH += $(PRINTF_PATH) else ifeq ($(PLATFORM),ARM_ATSAM) TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c endif diff --git a/tmk_core/common/chibios/printf.c b/tmk_core/common/chibios/printf.c deleted file mode 100644 index a99752bb3d44..000000000000 --- a/tmk_core/common/chibios/printf.c +++ /dev/null @@ -1,233 +0,0 @@ -/* - * found at: http://www.sparetimelabs.com/tinyprintf/tinyprintf.php - * and: http://www.sparetimelabs.com/printfrevisited/printfrevisited.php - */ - -/* -File: printf.c - -Copyright (C) 2004 Kustaa Nyholm - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. - -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -*/ - -#include "printf.h" - -typedef void (*putcf)(void*, char); -static putcf stdout_putf; -static void* stdout_putp; - -// this adds cca 400 bytes -#define PRINTF_LONG_SUPPORT - -#ifdef PRINTF_LONG_SUPPORT - -static void uli2a(unsigned long int num, unsigned int base, int uc, char* bf) { - int n = 0; - unsigned int d = 1; - while (num / d >= base) d *= base; - while (d != 0) { - int dgt = num / d; - num %= d; - d /= base; - if (n || dgt > 0 || d == 0) { - *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); - ++n; - } - } - *bf = 0; -} - -static void li2a(long num, char* bf) { - if (num < 0) { - num = -num; - *bf++ = '-'; - } - uli2a(num, 10, 0, bf); -} - -#endif - -static void ui2a(unsigned int num, unsigned int base, int uc, char* bf) { - int n = 0; - unsigned int d = 1; - while (num / d >= base) d *= base; - while (d != 0) { - int dgt = num / d; - num %= d; - d /= base; - if (n || dgt > 0 || d == 0) { - *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); - ++n; - } - } - *bf = 0; -} - -static void i2a(int num, char* bf) { - if (num < 0) { - num = -num; - *bf++ = '-'; - } - ui2a(num, 10, 0, bf); -} - -static int a2d(char ch) { - if (ch >= '0' && ch <= '9') - return ch - '0'; - else if (ch >= 'a' && ch <= 'f') - return ch - 'a' + 10; - else if (ch >= 'A' && ch <= 'F') - return ch - 'A' + 10; - else - return -1; -} - -static char a2i(char ch, const char** src, int base, int* nump) { - const char* p = *src; - int num = 0; - int digit; - while ((digit = a2d(ch)) >= 0) { - if (digit > base) break; - num = num * base + digit; - ch = *p++; - } - *src = p; - *nump = num; - return ch; -} - -static void putchw(void* putp, putcf putf, int n, char z, char* bf) { - char fc = z ? '0' : ' '; - char ch; - char* p = bf; - while (*p++ && n > 0) n--; - while (n-- > 0) putf(putp, fc); - while ((ch = *bf++)) putf(putp, ch); -} - -void tfp_format(void* putp, putcf putf, const char* fmt, va_list va) { - // This used to handle max of 12, but binary support jumps this to at least 32 - char bf[36]; - - char ch; - - while ((ch = *(fmt++))) { - if (ch != '%') - putf(putp, ch); - else { - char lz = 0; -#ifdef PRINTF_LONG_SUPPORT - char lng = 0; -#endif - int w = 0; - ch = *(fmt++); - if (ch == '0') { - ch = *(fmt++); - lz = 1; - } - if (ch >= '0' && ch <= '9') { - ch = a2i(ch, &fmt, 10, &w); - } -#ifdef PRINTF_LONG_SUPPORT - if (ch == 'l') { - ch = *(fmt++); - lng = 1; - } -#endif - switch (ch) { - case 0: - goto abort; - case 'u': { -#ifdef PRINTF_LONG_SUPPORT - if (lng) - uli2a(va_arg(va, unsigned long int), 10, 0, bf); - else -#endif - ui2a(va_arg(va, unsigned int), 10, 0, bf); - putchw(putp, putf, w, lz, bf); - break; - } - case 'd': { -#ifdef PRINTF_LONG_SUPPORT - if (lng) - li2a(va_arg(va, unsigned long int), bf); - else -#endif - i2a(va_arg(va, int), bf); - putchw(putp, putf, w, lz, bf); - break; - } - case 'x': - case 'X': -#ifdef PRINTF_LONG_SUPPORT - if (lng) - uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf); - else -#endif - ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf); - putchw(putp, putf, w, lz, bf); - break; - case 'c': - putf(putp, (char)(va_arg(va, int))); - break; - case 's': - putchw(putp, putf, w, 0, va_arg(va, char*)); - break; - case 'b': -#ifdef PRINTF_LONG_SUPPORT - if (lng) - uli2a(va_arg(va, unsigned long int), 2, 0, bf); - else -#endif - ui2a(va_arg(va, unsigned int), 2, 0, bf); - putchw(putp, putf, w, lz, bf); - break; - case '%': - putf(putp, ch); - default: - break; - } - } - } -abort:; -} - -void init_printf(void* putp, void (*putf)(void*, char)) { - stdout_putf = putf; - stdout_putp = putp; -} - -int tfp_printf(const char* fmt, ...) { - va_list va; - va_start(va, fmt); - tfp_format(stdout_putp, stdout_putf, fmt, va); - va_end(va); - - return 1; -} - -static void putcp(void* p, char c) { *(*((char**)p))++ = c; } - -int tfp_sprintf(char* s, const char* fmt, ...) { - va_list va; - va_start(va, fmt); - tfp_format(&s, putcp, fmt, va); - putcp(&s, 0); - va_end(va); - - return 1; -} diff --git a/tmk_core/common/chibios/printf.h b/tmk_core/common/chibios/printf.h deleted file mode 100644 index 775459e1e8af..000000000000 --- a/tmk_core/common/chibios/printf.h +++ /dev/null @@ -1,110 +0,0 @@ -/* - * found at: http://www.sparetimelabs.com/tinyprintf/tinyprintf.php - * and: http://www.sparetimelabs.com/printfrevisited/printfrevisited.php - */ - -/* -File: printf.h - -Copyright (C) 2004 Kustaa Nyholm - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. - -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -See the GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -This library is realy just two files: 'printf.h' and 'printf.c'. - -They provide a simple and small (+200 loc) printf functionality to -be used in embedded systems. - -I've found them so usefull in debugging that I do not bother with a -debugger at all. - -They are distributed in source form, so to use them, just compile them -into your project. - -Two printf variants are provided: printf and sprintf. - -The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'. - -Zero padding and field width are also supported. - -If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the -long specifier is also -supported. Note that this will pull in some long math routines (pun intended!) -and thus make your executable noticably longer. - -The memory foot print of course depends on the target cpu, compiler and -compiler options, but a rough guestimate (based on a H8S target) is about -1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space. -Not too bad. Your milage may vary. By hacking the source code you can -get rid of some hunred bytes, I'm sure, but personally I feel the balance of -functionality and flexibility versus code size is close to optimal for -many embedded systems. - -To use the printf you need to supply your own character output function, -something like : - - void putc ( void* p, char c) - { - while (!SERIAL_PORT_EMPTY) ; - SERIAL_PORT_TX_REGISTER = c; - } - -Before you can call printf you need to initialize it to use your -character output function with something like: - - init_printf(NULL,putc); - -Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc', -the NULL (or any pointer) you pass into the 'init_printf' will eventually be -passed to your 'putc' routine. This allows you to pass some storage space (or -anything realy) to the character output function, if necessary. -This is not often needed but it was implemented like that because it made -implementing the sprintf function so neat (look at the source code). - -The code is re-entrant, except for the 'init_printf' function, so it -is safe to call it from interupts too, although this may result in mixed output. -If you rely on re-entrancy, take care that your 'putc' function is re-entrant! - -The printf and sprintf functions are actually macros that translate to -'tfp_printf' and 'tfp_sprintf'. This makes it possible -to use them along with 'stdio.h' printf's in a single source file. -You just need to undef the names before you include the 'stdio.h'. -Note that these are not function like macros, so if you have variables -or struct members with these names, things will explode in your face. -Without variadic macros this is the best we can do to wrap these -fucnction. If it is a problem just give up the macros and use the -functions directly or rename them. - -For further details see source code. - -regs Kusti, 23.10.2004 -*/ - -#ifndef __TFP_PRINTF__ -#define __TFP_PRINTF__ - -#include - -void init_printf(void* putp, void (*putf)(void*, char)); - -int tfp_printf(const char* fmt, ...); -int tfp_sprintf(char* s, const char* fmt, ...); - -void tfp_format(void* putp, void (*putf)(void*, char), const char* fmt, va_list va); - -#define printf tfp_printf -#define sprintf tfp_sprintf - -#endif diff --git a/tmk_core/common/print.h b/tmk_core/common/print.h index 04ca558109dc..1c77236212d2 100644 --- a/tmk_core/common/print.h +++ b/tmk_core/common/print.h @@ -72,9 +72,7 @@ extern "C" # elif defined(PROTOCOL_CHIBIOS) /* PROTOCOL_CHIBIOS */ -# ifndef TERMINAL_ENABLE -# include "chibios/printf.h" -# endif +# include "printf.h" // lib/printf/printf.h # ifdef USER_PRINT /* USER_PRINT */ @@ -89,7 +87,6 @@ extern "C" # define uprintf printf # else /* NORMAL PRINT */ - // Create user & normal print defines # define print(s) printf(s) # define println(s) printf(s "\r\n") diff --git a/tmk_core/protocol/chibios/main.c b/tmk_core/protocol/chibios/main.c index 61665eb6f4ba..7d32c16ed8c5 100644 --- a/tmk_core/protocol/chibios/main.c +++ b/tmk_core/protocol/chibios/main.c @@ -158,9 +158,6 @@ int main(void) { /* Init USB */ init_usb_driver(&USB_DRIVER); - /* init printf */ - init_printf(NULL, sendchar_pf); - #ifdef MIDI_ENABLE setup_midi(); #endif diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index ecc83d9ecc94..65bd291bec59 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -796,9 +796,8 @@ int8_t sendchar(uint8_t c) { } #endif /* CONSOLE_ENABLE */ -void sendchar_pf(void *p, char c) { - (void)p; - sendchar((uint8_t)c); +void _putchar(char character) { + sendchar(character); } #ifdef RAW_ENABLE diff --git a/tmk_core/protocol/chibios/usb_main.h b/tmk_core/protocol/chibios/usb_main.h index 17041b4f2fdf..94baf9b35e06 100644 --- a/tmk_core/protocol/chibios/usb_main.h +++ b/tmk_core/protocol/chibios/usb_main.h @@ -87,6 +87,4 @@ void console_flush_output(void); #endif /* CONSOLE_ENABLE */ -void sendchar_pf(void *p, char c); - #endif /* _USB_MAIN_H_ */