From 9a82a041f4d691b0510b35dfda68ef2bc47329b5 Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Mon, 10 Dec 2018 19:08:29 +0100 Subject: [PATCH 1/2] Allow compilation of the printf code when no serial ports are used. --- src/main/common/printf.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/common/printf.c b/src/main/common/printf.c index 2a00c4a9e3..82de878418 100644 --- a/src/main/common/printf.c +++ b/src/main/common/printf.c @@ -40,16 +40,19 @@ #include "common/utils.h" +#ifdef SERIAL_PORT_COUNT #include "drivers/serial.h" #include "io/serial.h" +static serialPort_t *printfSerialPort; +#endif + #include "printf.h" #ifdef REQUIRE_PRINTF_LONG_SUPPORT #include "typeconversion.h" #endif -static serialPort_t *printfSerialPort; #ifdef REQUIRE_CC_ARM_PRINTF_SUPPORT @@ -166,6 +169,7 @@ void init_printf(void *putp, void (*putf) (void *, char)) stdout_putp = putp; } +#ifdef SERIAL_PORT_COUNT int tfp_printf(const char *fmt, ...) { va_list va; @@ -175,6 +179,7 @@ int tfp_printf(const char *fmt, ...) while (!isSerialTransmitBufferEmpty(printfSerialPort)); return written; } +#endif static void putcp(void *p, char c) { @@ -193,19 +198,24 @@ int tfp_sprintf(char *s, const char *fmt, ...) } +#ifdef SERIAL_PORT_COUNT static void _putc(void *p, char c) { UNUSED(p); serialWrite(printfSerialPort, c); } +#endif void printfSupportInit(void) { +#ifdef SERIAL_PORT_COUNT init_printf(NULL, _putc); +#endif } #else +#ifdef SERIAL_PORT_COUNT // keil/armcc version int fputc(int c, FILE *f) { @@ -214,14 +224,20 @@ int fputc(int c, FILE *f) serialWrite(printfSerialPort, c); return c; } +#endif void printfSupportInit(void) { +#ifdef SERIAL_PORT_COUNT // Nothing to do +#endif } #endif +#ifdef SERIAL_PORT_COUNT void setPrintfSerialPort(serialPort_t *serialPort) { printfSerialPort = serialPort; } +#endif + From 672c9060675f6c58236227f151663087c113235a Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Wed, 6 Mar 2019 11:12:19 +0100 Subject: [PATCH 2/2] Extract serial-related code from printf.c --- src/main/cli/cli.c | 1 + src/main/common/printf.c | 70 ++------------------------- src/main/common/printf.h | 11 ++--- src/main/common/printf_serial.c | 86 +++++++++++++++++++++++++++++++++ src/main/common/printf_serial.h | 27 +++++++++++ src/main/fc/init.c | 6 ++- 6 files changed, 125 insertions(+), 76 deletions(-) create mode 100644 src/main/common/printf_serial.c create mode 100644 src/main/common/printf_serial.h diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index aa50ff788d..415bec5b08 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -52,6 +52,7 @@ extern uint8_t __config_end; #include "common/color.h" #include "common/maths.h" #include "common/printf.h" +#include "common/printf_serial.h" #include "common/strtol.h" #include "common/time.h" #include "common/typeconversion.h" diff --git a/src/main/common/printf.c b/src/main/common/printf.c index 82de878418..4c65e293ed 100644 --- a/src/main/common/printf.c +++ b/src/main/common/printf.c @@ -38,15 +38,6 @@ #include "build/build_config.h" -#include "common/utils.h" - -#ifdef SERIAL_PORT_COUNT -#include "drivers/serial.h" -#include "io/serial.h" - -static serialPort_t *printfSerialPort; -#endif - #include "printf.h" #ifdef REQUIRE_PRINTF_LONG_SUPPORT @@ -56,9 +47,8 @@ static serialPort_t *printfSerialPort; #ifdef REQUIRE_CC_ARM_PRINTF_SUPPORT -typedef void (*putcf) (void *, char); -static putcf stdout_putf; -static void *stdout_putp; +putcf stdout_putf; +void *stdout_putp; // print bf, padded from left to at least n characters. // padding is zero ('0') if z!=0, space (' ') otherwise @@ -169,18 +159,6 @@ void init_printf(void *putp, void (*putf) (void *, char)) stdout_putp = putp; } -#ifdef SERIAL_PORT_COUNT -int tfp_printf(const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - int written = tfp_format(stdout_putp, stdout_putf, fmt, va); - va_end(va); - while (!isSerialTransmitBufferEmpty(printfSerialPort)); - return written; -} -#endif - static void putcp(void *p, char c) { *(*((char **) p))++ = c; @@ -197,47 +175,5 @@ int tfp_sprintf(char *s, const char *fmt, ...) return written; } - -#ifdef SERIAL_PORT_COUNT -static void _putc(void *p, char c) -{ - UNUSED(p); - serialWrite(printfSerialPort, c); -} -#endif - -void printfSupportInit(void) -{ -#ifdef SERIAL_PORT_COUNT - init_printf(NULL, _putc); -#endif -} - -#else - -#ifdef SERIAL_PORT_COUNT -// keil/armcc version -int fputc(int c, FILE *f) -{ - // let DMA catch up a bit when using set or dump, we're too fast. - while (!isSerialTransmitBufferEmpty(printfSerialPort)); - serialWrite(printfSerialPort, c); - return c; -} -#endif - -void printfSupportInit(void) -{ -#ifdef SERIAL_PORT_COUNT - // Nothing to do -#endif -} -#endif - -#ifdef SERIAL_PORT_COUNT -void setPrintfSerialPort(serialPort_t *serialPort) -{ - printfSerialPort = serialPort; -} -#endif +#endif // REQUIRE_CC_ARM_PRINTF_SUPPORT diff --git a/src/main/common/printf.h b/src/main/common/printf.h index f20cddef94..615744a4a2 100644 --- a/src/main/common/printf.h +++ b/src/main/common/printf.h @@ -106,14 +106,11 @@ regs Kusti, 23.10.2004 #include +typedef void (*putcf) (void *, char); +extern putcf stdout_putf; +extern void *stdout_putp; + void init_printf(void *putp, void (*putf) (void *, char)); -// Disabling this, in favour of tfp_format to be used in cli.c -//int tfp_printf(const char *fmt, ...); int tfp_sprintf(char *s, const char *fmt, ...); - int tfp_format(void *putp, void (*putf) (void *, char), const char *fmt, va_list va); - -struct serialPort_s; -void setPrintfSerialPort(struct serialPort_s *serialPort); -void printfSupportInit(void); diff --git a/src/main/common/printf_serial.c b/src/main/common/printf_serial.c new file mode 100644 index 0000000000..4e29b993ce --- /dev/null +++ b/src/main/common/printf_serial.c @@ -0,0 +1,86 @@ +/* + * This file is part of Cleanflight and Betaflight. + * + * Cleanflight and Betaflight are free software. You can redistribute + * this software and/or modify this software under the terms of the + * GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Cleanflight and Betaflight are distributed in the hope that they + * will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software. + * + * If not, see . + */ + + +#include +#include +#include +#include + +#include "platform.h" + +#include "common/printf.h" + +#include "drivers/serial.h" +#include "io/serial.h" + +#include "printf_serial.h" + +#ifdef SERIAL_PORT_COUNT + +static serialPort_t *printfSerialPort; + +void setPrintfSerialPort(serialPort_t *serialPort) +{ + printfSerialPort = serialPort; +} + +#ifdef REQUIRE_CC_ARM_PRINTF_SUPPORT + +int tfp_printf(const char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + int written = tfp_format(stdout_putp, stdout_putf, fmt, va); + va_end(va); + while (!isSerialTransmitBufferEmpty(printfSerialPort)); + return written; +} + +static void _putc(void *p, char c) +{ + UNUSED(p); + serialWrite(printfSerialPort, c); +} + + +void printfSerialInit(void) +{ + init_printf(NULL, _putc); +} + +#else // REQUIRE_CC_ARM_PRINTF_SUPPORT + +// keil/armcc version +int fputc(int c, FILE *f) +{ + // let DMA catch up a bit when using set or dump, we're too fast. + while (!isSerialTransmitBufferEmpty(printfSerialPort)); + serialWrite(printfSerialPort, c); + return c; +} + +void printfSerialInit(void) +{ + // Nothing to do +} +#endif + +#endif // SERIAL_PORT_COUNT diff --git a/src/main/common/printf_serial.h b/src/main/common/printf_serial.h new file mode 100644 index 0000000000..db47a5be62 --- /dev/null +++ b/src/main/common/printf_serial.h @@ -0,0 +1,27 @@ +/* + * This file is part of Cleanflight and Betaflight. + * + * Cleanflight and Betaflight are free software. You can redistribute + * this software and/or modify this software under the terms of the + * GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Cleanflight and Betaflight are distributed in the hope that they + * will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software. + * + * If not, see . + */ + + +struct serialPort_s; + +void printfSerialInit(void); +void setPrintfSerialPort(struct serialPort_s *serialPort); + +int tfp_printf(const char *fmt, ...); diff --git a/src/main/fc/init.c b/src/main/fc/init.c index 1b78b59bf2..ffcdbffe08 100644 --- a/src/main/fc/init.c +++ b/src/main/fc/init.c @@ -32,7 +32,7 @@ #include "common/axis.h" #include "common/color.h" #include "common/maths.h" -#include "common/printf.h" +#include "common/printf_serial.h" #include "config/config_eeprom.h" #include "config/feature.h" @@ -249,7 +249,9 @@ void init(void) HAL_Init(); #endif - printfSupportInit(); +#ifdef SERIAL_PORT_COUNT + printfSerialInit(); +#endif systemInit();