summaryrefslogtreecommitdiff
path: root/common/uart_printf.c
diff options
context:
space:
mode:
authorJack Rosenthal <jrosenth@chromium.org>2021-02-10 13:25:03 -0700
committerCommit Bot <commit-bot@chromium.org>2021-02-11 17:19:42 +0000
commitfdf60424cfa7552e7a29bd09dd10aba137e108a3 (patch)
treed3dee323ce3a1630722ee686ee7cd7899ec37a83 /common/uart_printf.c
parent7991e4d1f705c84966b382219f194804380b79ab (diff)
downloadchrome-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>
Diffstat (limited to 'common/uart_printf.c')
-rw-r--r--common/uart_printf.c89
1 files changed, 89 insertions, 0 deletions
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;
+}