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