From 8765f08cc7a150314b93694d63b4d9fa40f50105 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Sat, 3 Aug 2019 12:38:47 +0200
Subject: [PATCH] feat(serial): Add epic_uart_read_str() API-call

Signed-off-by: Rahix <rahix@rahix.de>
---
 epicardium/epicardium.h     | 19 ++++++++++++++++---
 epicardium/modules/serial.c | 16 ++++++++++++++++
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index a9b17b49..a5bde091 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -31,7 +31,7 @@ typedef unsigned int size_t;
 #define API_SYSTEM_EXEC             0x2 /* TODO */
 #define API_UART_WRITE_STR          0x3
 #define API_UART_READ_CHAR          0x4
-#define API_UART_READ_STR           0x5 /* TODO */
+#define API_UART_READ_STR           0x5
 #define API_STREAM_READ             0x6
 #define API_INTERRUPT_ENABLE        0x7
 #define API_INTERRUPT_DISABLE       0x8
@@ -136,14 +136,27 @@ API(API_UART_WRITE_STR, void epic_uart_write_str(
 ));
 
 /**
+ * Try reading a single character from any connected serial device.
  *
- * Try reading a single character from any connected serial device.  If nothing
- * is available, :c:func:`epic_uart_read_char` returns ``(-1)``.
+ * If nothing is available, :c:func:`epic_uart_read_char` returns ``(-1)``.
  *
  * :return:  The byte or ``(-1)`` if no byte was available.
  */
 API(API_UART_READ_CHAR, int epic_uart_read_char(void));
 
+/**
+ * Read as many characters as possible from the UART queue.
+ *
+ * :c:func:`epic_uart_read_str` will not block if no new data is available.  For
+ * an example, see :c:func:`epic_isr_uart_rx`.
+ *
+ * :param char* buf: Buffer to be filled with incoming data.
+ * :param size_t cnt: Size of ``buf``.
+ * :returns: Number of bytes read.  Can be ``0`` if no data was available.
+ *    Might be a negative value if an error occured.
+ */
+API(API_UART_READ_STR, int epic_uart_read_str(char *buf, size_t cnt));
+
 /**
  * **Interrupt Service Routine**
  *
diff --git a/epicardium/modules/serial.c b/epicardium/modules/serial.c
index ac573757..8cffa3ac 100644
--- a/epicardium/modules/serial.c
+++ b/epicardium/modules/serial.c
@@ -43,6 +43,22 @@ int epic_uart_read_char(void)
 	return (-1);
 }
 
+/*
+ * API-call to read data from the queue.
+ */
+int epic_uart_read_str(char *buf, size_t cnt)
+{
+	int i = 0;
+
+	for (i = 0; i < cnt; i++) {
+		if (xQueueReceive(read_queue, &buf[i], 0) != pdTRUE) {
+			break;
+		}
+	}
+
+	return i;
+}
+
 /* Interrupt handler needed for SDK UART implementation */
 void UART0_IRQHandler(void)
 {
-- 
GitLab