forked from wertarbyte/funkenschlag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
executable file
·62 lines (58 loc) · 1.42 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#if defined(UCSRA) /* e.g. ATMega 8*/
#define UART_CONF_A UCSRA
#define UART_CONF_B UCSRB
#define UART_CONF_C UCSRC
#define UART_CONF_C_VAL (1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0)
#define UART_BRRH UBRRH
#define UART_BRRL UBRRL
#define UART_U2X U2X
#define UART_UDRE UDRE
#define UART_TXEN TXEN
#define UART_RXEN RXEN
#define UART_BUSY (UART_CONF_A & (1<<UDRE))
#define UART_UDR UDR
#elif defined(UCSR0A) /* e.g. ATMega_8 */
#define UART_CONF_A UCSR0A
#define UART_CONF_B UCSR0B
#define UART_CONF_C UCSR0C
#define UART_CONF_C_VAL (1<<UCSZ01 | 1<<UCSZ00)
#define UART_BRRH UBRR0H
#define UART_BRRL UBRR0L
#define UART_U2X U2X0
#define UART_UDRE UDRE0
#define UART_TXEN TXEN0
#define UART_RXEN RXEN0
#define UART_BUSY (UART_CONF_A & (1<<UDRE0))
#define UART_UDR UDR0
#else
#error "Unable to determine UART configuration"
#endif
void serial_init(void) {
#define BAUD 38400
#include <util/setbaud.h>
UART_BRRH = UBRRH_VALUE;
UART_BRRL = UBRRL_VALUE;
#if USE_2X /* output from setbaud.h */
UART_CONF_A |= 1<<UART_U2X;
#else
UART_CONF_A &= ~(1<<UART_U2X);
#endif
#undef BAUD
UART_CONF_B |= (1<<UART_TXEN | 1<<UART_RXEN);
UART_CONF_C = UART_CONF_C_VAL;
}
void serial_write(char c) {
while(!(UART_BUSY));
UART_UDR = c;
}
void serial_write_str(char *s) {
while (*s) {
serial_write(*s++);
}
}