]> git.zerfleddert.de Git - rsbs2/blobdiff - bmc/usart.c
serial support
[rsbs2] / bmc / usart.c
diff --git a/bmc/usart.c b/bmc/usart.c
new file mode 100644 (file)
index 0000000..36ba83c
--- /dev/null
@@ -0,0 +1,35 @@
+#include <avr/io.h>
+#include <stdio.h>
+
+#include "usart.h"
+
+#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)
+#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))
+#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD)
+
+#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
+#error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch! 
+#endif 
+
+int usart_put(char c, FILE *stream)
+{
+       if (c == '\n')
+               usart_put('\r', stream);
+
+       while(!(UCSRA & (1<<UDRE))) {}
+       UDR = c;
+       return 0;
+}
+
+static FILE usart_stdout = FDEV_SETUP_STREAM(usart_put, NULL, _FDEV_SETUP_WRITE);
+
+void usart_init()
+{
+       UCSRB |= (1<<TXEN);
+       UCSRC |= (1<<URSEL)|(1 << UCSZ1)|(1 << UCSZ0);
+
+       UBRRH = UBRR_VAL >> 8;
+       UBRRL = UBRR_VAL & 0xFF;
+
+       stdout = &usart_stdout;
+}
Impressum, Datenschutz