aboutsummaryrefslogtreecommitdiff
path: root/src/uart
diff options
context:
space:
mode:
Diffstat (limited to 'src/uart')
-rw-r--r--src/uart/simple_uart.c55
-rw-r--r--src/uart/source.mk1
2 files changed, 56 insertions, 0 deletions
diff --git a/src/uart/simple_uart.c b/src/uart/simple_uart.c
new file mode 100644
index 0000000..e640ae1
--- /dev/null
+++ b/src/uart/simple_uart.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <gran/uart/simple_uart.h>
+
+struct simple_uart {
+ struct component component;
+
+ struct component *send;
+ struct packet pkt;
+ bool busy;
+};
+
+static stat simple_uart_clock(struct simple_uart *uart)
+{
+ if (!uart->busy)
+ return OK;
+
+ stat r = SEND(uart, uart->send, uart->pkt);
+ if (r == EBUSY)
+ return OK;
+
+ uart->busy = false;
+ return OK;
+}
+
+static stat simple_uart_receive(struct simple_uart *uart, struct component *from, struct packet pkt)
+{
+ if (uart->busy)
+ return EBUSY;
+
+ uart->busy = true;
+ uart->send = from;
+ uart->pkt = response(pkt);
+
+ size_t size = packet_convsize(&pkt);
+ if (size != 1) {
+ set_flags(&uart->pkt, PACKET_ERROR);
+ return OK;
+ }
+
+ putchar(packet_convu8(&pkt));
+ fflush(stdout);
+ set_flags(&uart->pkt, PACKET_DONE);
+ return OK;
+}
+
+struct component *create_simple_uart()
+{
+ struct simple_uart *uart = calloc(1, sizeof(struct simple_uart));
+ if (!uart)
+ return NULL;
+
+ uart->component.receive = (receive_callback)simple_uart_receive;
+ uart->component.clock = (clock_callback)simple_uart_clock;
+ return (struct component *)uart;
+}
diff --git a/src/uart/source.mk b/src/uart/source.mk
new file mode 100644
index 0000000..cdc0bc9
--- /dev/null
+++ b/src/uart/source.mk
@@ -0,0 +1 @@
+SOURCES += src/uart/simple_uart.c