aboutsummaryrefslogtreecommitdiffstats
path: root/src/simulator/serial.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-07-11 13:46:07 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-07-11 14:35:24 -0400
commit5294b3cd2de7487dfa4624934f17a1746efd7a20 (patch)
tree21af00d4d5843b25056c877c8e8238c542633bd8 /src/simulator/serial.c
parent00ea4428a302c67c4c460a087402d177185831da (diff)
downloadkutter-5294b3cd2de7487dfa4624934f17a1746efd7a20.tar.gz
kutter-5294b3cd2de7487dfa4624934f17a1746efd7a20.tar.xz
kutter-5294b3cd2de7487dfa4624934f17a1746efd7a20.zip
simulator: Rework dummy simulator code to user timer_irq / serial_irq
Change the simulator to use the generic timer_irq.c and serial_irq.c code for (dummy) timer and io handling. This is just to make the code a better example for other developers (most micro-controllers will use the timer_irq.c and serial_irq.c code). Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/simulator/serial.c')
-rw-r--r--src/simulator/serial.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/simulator/serial.c b/src/simulator/serial.c
new file mode 100644
index 00000000..3c7d008d
--- /dev/null
+++ b/src/simulator/serial.c
@@ -0,0 +1,43 @@
+// Example code for interacting with serial_irq.c
+//
+// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include <fcntl.h> // fcntl
+#include <unistd.h> // STDIN_FILENO
+#include "board/serial_irq.h" // serial_get_tx_byte
+#include "sched.h" // DECL_INIT
+
+void
+serial_init(void)
+{
+ // Make stdin/stdout non-blocking
+ fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
+ fcntl(STDOUT_FILENO, F_SETFL, fcntl(STDOUT_FILENO, F_GETFL, 0) | O_NONBLOCK);
+}
+DECL_INIT(serial_init);
+
+static void
+do_uart(void)
+{
+ for (;;) {
+ uint8_t data;
+ int ret = serial_get_tx_byte(&data);
+ if (ret)
+ break;
+ else
+ write(STDOUT_FILENO, &data, sizeof(data));
+
+ // XXX - Normally the code would check if input data is
+ // available and call serial_rx_byte()
+ }
+}
+
+void
+serial_enable_tx_irq(void)
+{
+ // Normally this would enable the hardware irq, but we just call
+ // do_uart() directly in this demo code.
+ do_uart();
+}