aboutsummaryrefslogtreecommitdiff
path: root/kernel/uart.c
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2011-03-11 01:28:00 +0000
committerDimitri Sokolyuk <demon@dim13.org>2011-03-11 01:28:00 +0000
commitb8335062ae7d19bd27e6131fadcd7d3a116c4992 (patch)
tree3bd226885fb6f2ca24ee300a44d7b51ffb8d2706 /kernel/uart.c
DimOS RT
Diffstat (limited to 'kernel/uart.c')
-rw-r--r--kernel/uart.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/kernel/uart.c b/kernel/uart.c
new file mode 100644
index 0000000..63f5c11
--- /dev/null
+++ b/kernel/uart.c
@@ -0,0 +1,80 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2011 Dimitri Sokolyuk <demon@dim13.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef BAUD
+#warning "BAUD not set, fallback to default"
+#define BAUD 9600
+#endif
+
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/wdt.h>
+#include <avr/interrupt.h>
+#include <util/setbaud.h>
+#include "kernel.h"
+#include "tasks.h"
+
+void
+uart_putchar(char c)
+{
+ loop_until_bit_is_set(UCSRA, UDRE);
+ UDR = c;
+}
+
+ISR(SIG_UART_RECV)
+{
+ uint8_t c = UDR;
+ uint8_t *p = 0;
+
+ switch (c) {
+ case 'R': /* reboot */
+ wdt_enable(WDTO_15MS);
+ break;
+ case 'D': /* dump */
+ while (p <= (uint8_t *)RAMEND)
+ uart_putchar(*p++);
+ break;
+ case 'T':
+ UCSRB |= _BV(UDRIE);
+ break;
+ case 't':
+ UCSRB &= ~_BV(UDRIE);
+ break;
+ case '\r':
+ case '\n':
+ break;
+ default:
+ uart_putchar('?');
+ break;
+ }
+}
+
+ISR(SIG_UART_DATA)
+{
+ uint8_t r = running();
+
+ UDR = r ? '0' + r : '.';
+}
+
+void
+init_uart(void)
+{
+ UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXEN);
+ UBRRH = UBRRH_VALUE;
+ UBRRL = UBRRL_VALUE;
+ UCSRA &= ~_BV(U2X);
+}