diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2021-02-10 13:25:03 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-02-11 17:19:42 +0000 |
commit | fdf60424cfa7552e7a29bd09dd10aba137e108a3 (patch) | |
tree | d3dee323ce3a1630722ee686ee7cd7899ec37a83 | |
parent | 7991e4d1f705c84966b382219f194804380b79ab (diff) | |
download | chrome-ec-fdf60424cfa7552e7a29bd09dd10aba137e108a3.tar.gz |
common: uart: split off printf and like functions into separate module
This separates the high-level uart output functions like uart_printf
and uart_putc from the uart buffering module, allowing the buffering
to be implemented separately from the output functions.
The contract between this module and the uart_buffering layer is to
implement uart_tx_char_raw and uart_tx_start.
BUG=b:178033156
BRANCH=none
TEST=buildall
Signed-off-by: Jack Rosenthal <jrosenth@chromium.org>
Change-Id: I5fb62ca2be1fea04654eaadd7a3806ac0c586929
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2685411
Reviewed-by: Simon Glass <sjg@chromium.org>
-rw-r--r-- | common/build.mk | 2 | ||||
-rw-r--r-- | common/uart_buffering.c | 90 | ||||
-rw-r--r-- | common/uart_printf.c | 89 | ||||
-rw-r--r-- | include/uart.h | 16 |
4 files changed, 107 insertions, 90 deletions
diff --git a/common/build.mk b/common/build.mk index e86fc1eb01..3ca7a0fa56 100644 --- a/common/build.mk +++ b/common/build.mk @@ -187,7 +187,7 @@ common-$(CONFIG_WEBUSB_URL)+=webusb_desc.o common-$(CONFIG_WIRELESS)+=wireless.o common-$(HAS_TASK_CHIPSET)+=chipset.o common-$(HAS_TASK_CONSOLE)+=console.o console_output.o -common-$(HAS_TASK_CONSOLE)+=uart_buffering.o uart_hostcmd.o +common-$(HAS_TASK_CONSOLE)+=uart_buffering.o uart_hostcmd.o uart_printf.o common-$(CONFIG_CMD_MEM)+=memory_commands.o common-$(HAS_TASK_HOSTCMD)+=host_command.o ec_features.o common-$(HAS_TASK_PDCMD)+=host_command_pd.o diff --git a/common/uart_buffering.c b/common/uart_buffering.c index 38bbd67d2d..78dc06e5f4 100644 --- a/common/uart_buffering.c +++ b/common/uart_buffering.c @@ -71,16 +71,7 @@ void uart_init_buffer(void) } } -/** - * Put a single character into the transmit buffer. - * - * Does not enable the transmit interrupt; assumes that happens elsewhere. - * - * @param context Context; ignored. - * @param c Character to write. - * @return 0 if the character was transmitted, 1 if it was dropped. - */ -static int __tx_char_raw(void *context, int c) +int uart_tx_char_raw(void *context, int c) { int tx_buf_next, tx_buf_new_tail; @@ -118,14 +109,6 @@ static int __tx_char_raw(void *context, int c) return 0; } -static int __tx_char(void *context, int c) -{ - /* Translate '\n' to '\r\n' */ - if (c == '\n' && __tx_char_raw(NULL, '\r')) - return 1; - return __tx_char_raw(context, c); -} - #ifdef CONFIG_UART_TX_DMA /** @@ -265,77 +248,6 @@ void uart_clear_input(void) #endif /* !CONFIG_UART_RX_DMA */ -int uart_putc(int c) -{ - int rv = __tx_char(NULL, c); - - uart_tx_start(); - - return rv ? EC_ERROR_OVERFLOW : EC_SUCCESS; -} - -int uart_puts(const char *outstr) -{ - /* Put all characters in the output buffer */ - while (*outstr) { - if (__tx_char(NULL, *outstr++) != 0) - break; - } - - uart_tx_start(); - - /* Successful if we consumed all output */ - return *outstr ? EC_ERROR_OVERFLOW : EC_SUCCESS; -} - -int uart_put(const char *out, int len) -{ - /* Put all characters in the output buffer */ - while (len--) { - if (__tx_char(NULL, *out++) != 0) - break; - } - - uart_tx_start(); - - /* Successful if we consumed all output */ - return len ? EC_ERROR_OVERFLOW : EC_SUCCESS; -} - -int uart_put_raw(const char *out, int len) -{ - /* Put all characters in the output buffer */ - while (len--) { - if (__tx_char_raw(NULL, *out++) != 0) - break; - } - - uart_tx_start(); - - /* Successful if we consumed all output */ - return len ? EC_ERROR_OVERFLOW : EC_SUCCESS; -} - -int uart_vprintf(const char *format, va_list args) -{ - int rv = vfnprintf(__tx_char, NULL, format, args); - - uart_tx_start(); - - return rv; -} - -int uart_printf(const char *format, ...) -{ - int rv; - va_list args; - - va_start(args, format); - rv = uart_vprintf(format, args); - va_end(args); - return rv; -} - void uart_flush_output(void) { /* If UART not initialized ignore flush request. */ diff --git a/common/uart_printf.c b/common/uart_printf.c new file mode 100644 index 0000000000..80a72065b3 --- /dev/null +++ b/common/uart_printf.c @@ -0,0 +1,89 @@ +/* Copyright 2021 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stddef.h> + +#include "common.h" +#include "printf.h" +#include "uart.h" + +static int __tx_char(void *context, int c) +{ + /* Translate '\n' to '\r\n' */ + if (c == '\n' && uart_tx_char_raw(context, '\r')) + return 1; + return uart_tx_char_raw(context, c); +} + +int uart_putc(int c) +{ + int rv = __tx_char(NULL, c); + + uart_tx_start(); + + return rv ? EC_ERROR_OVERFLOW : EC_SUCCESS; +} + +int uart_puts(const char *outstr) +{ + /* Put all characters in the output buffer */ + while (*outstr) { + if (__tx_char(NULL, *outstr++) != 0) + break; + } + + uart_tx_start(); + + /* Successful if we consumed all output */ + return *outstr ? EC_ERROR_OVERFLOW : EC_SUCCESS; +} + +int uart_put(const char *out, int len) +{ + /* Put all characters in the output buffer */ + while (len--) { + if (__tx_char(NULL, *out++) != 0) + break; + } + + uart_tx_start(); + + /* Successful if we consumed all output */ + return len ? EC_ERROR_OVERFLOW : EC_SUCCESS; +} + +int uart_put_raw(const char *out, int len) +{ + /* Put all characters in the output buffer */ + while (len--) { + if (uart_tx_char_raw(NULL, *out++) != 0) + break; + } + + uart_tx_start(); + + /* Successful if we consumed all output */ + return len ? EC_ERROR_OVERFLOW : EC_SUCCESS; +} + +int uart_vprintf(const char *format, va_list args) +{ + int rv = vfnprintf(__tx_char, NULL, format, args); + + uart_tx_start(); + + return rv; +} + +int uart_printf(const char *format, ...) +{ + int rv; + va_list args; + + va_start(args, format); + rv = uart_vprintf(format, args); + va_end(args); + return rv; +} diff --git a/include/uart.h b/include/uart.h index b6dce4f21f..95df22d5bd 100644 --- a/include/uart.h +++ b/include/uart.h @@ -85,6 +85,22 @@ int uart_printf(const char *format, ...); int uart_vprintf(const char *format, va_list args); /** + * Put a single character into the transmit buffer. + * + * Does not enable the transmit interrupt; assumes that happens elsewhere. + * + * @param context Context; ignored. + * @param c Character to write. + * @return 0 if the character was transmitted, 1 if it was dropped. + * + * Note: This is intended to be implemented by the UART buffering + * module, and called only by the implementations of the uart_* + * functions. You should stick to the higher level functions, such as + * uart_putc, outside of the UART implementation. + */ +int uart_tx_char_raw(void *context, int c); + +/** * Flush output. Blocks until UART has transmitted all output. */ void uart_flush_output(void); |