From a086bcfc5e0a1525af44f8ca0b90ee87b38d3917 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Mon, 31 Aug 2015 17:41:04 +0200 Subject: [PATCH] Add a uprintf (UART printf) that outputs data on selected UART --- drivers/serial.c | 1 - include/drivers/serial.h | 9 ++++++++ include/lib/stdio.h | 4 ++++ lib/uprintf.c | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lib/uprintf.c diff --git a/drivers/serial.c b/drivers/serial.c index 7dc5d83..d636d2a 100644 --- a/drivers/serial.c +++ b/drivers/serial.c @@ -34,7 +34,6 @@ #include "lib/utils.h" #include "drivers/serial.h" -#define SERIAL_OUT_BUFF_SIZE 64 struct uart_device { uint32_t num; diff --git a/include/drivers/serial.h b/include/drivers/serial.h index c31e855..8fce4f0 100644 --- a/include/drivers/serial.h +++ b/include/drivers/serial.h @@ -33,6 +33,15 @@ #define SERIAL_MODE_RS485 SERIAL_CAP_RS485 #define SERIAL_MODE_IRDA SERIAL_CAP_IRDA + +/* This buffer size is the maximum write size for a serial_printf or a uprintf call. + * Previously this value was 64, but it is often too short, so I set it to 96, which + * should be OK for most cases. In need of a bigger write buffer, change this value + * or perform multiple write calls (better). + */ +#define SERIAL_OUT_BUFF_SIZE 96 + + /***************************************************************************** */ /* Serial Write * diff --git a/include/lib/stdio.h b/include/lib/stdio.h index 19dd837..e6ee86d 100644 --- a/include/lib/stdio.h +++ b/include/lib/stdio.h @@ -31,4 +31,8 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); int snprintf(char* buf, size_t size, const char *format, ...); + +int uprintf(int uart_num, const char *format, ...); + + #endif /* LIB_STDIO_H */ diff --git a/lib/uprintf.c b/lib/uprintf.c new file mode 100644 index 0000000..57269cd --- /dev/null +++ b/lib/uprintf.c @@ -0,0 +1,45 @@ +/**************************************************************************** + * lib/uprintf.c + * + * UART printf + * + * Copyright 2012 Nathael Pajani + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +#include +#include +#include "drivers/serial.h" +#include "lib/string.h" +#include "lib/stdio.h" + + +int uprintf(int uart_num, const char* format, ...) +{ + char printf_buf[SERIAL_OUT_BUFF_SIZE]; + va_list args; + int r; + + va_start(args, format); + r = vsnprintf(printf_buf, SERIAL_OUT_BUFF_SIZE, format, args); + va_end(args); + + serial_write(uart_num, printf_buf, r); + + return r; +} + + -- 2.43.0