]> git.zerfleddert.de Git - rsbs2/blob - bmc/usart.c
serial support
[rsbs2] / bmc / usart.c
1 #include <avr/io.h>
2 #include <stdio.h>
3
4 #include "usart.h"
5
6 #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)
7 #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))
8 #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD)
9
10 #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
11 #error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!
12 #endif
13
14 int usart_put(char c, FILE *stream)
15 {
16 if (c == '\n')
17 usart_put('\r', stream);
18
19 while(!(UCSRA & (1<<UDRE))) {}
20 UDR = c;
21 return 0;
22 }
23
24 static FILE usart_stdout = FDEV_SETUP_STREAM(usart_put, NULL, _FDEV_SETUP_WRITE);
25
26 void usart_init()
27 {
28 UCSRB |= (1<<TXEN);
29 UCSRC |= (1<<URSEL)|(1 << UCSZ1)|(1 << UCSZ0);
30
31 UBRRH = UBRR_VAL >> 8;
32 UBRRL = UBRR_VAL & 0xFF;
33
34 stdout = &usart_stdout;
35 }
Impressum, Datenschutz