forked from schneider42/moodlamp-rf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesusart.h
119 lines (100 loc) · 3.22 KB
/
esusart.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* vim:fdm=marker ts=4 et ai
* {{{
*
* Copyright (c) 2008 by Christian Dietrich <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
}}} */
#include "config.h"
#include <stdint.h>
#ifndef _USART_H
#define _USART_H
/* The baudrate had to be baudrate/100 */
#ifndef TEENSY_SUPPORT
uint16_t usart_baudrate(uint16_t baudrate);
#endif
#ifndef USE_USART
#define USE_USART
#endif
#define _usart_cat_(a,b) a ## b
#define _usart_cat(a,b) _usart_cat_(a,b)
#if !defined(UDR0) || !defined(USE_USART)
#undef USE_USART
#define USE_USART
#endif
#ifndef USART_TX_vect
# if defined(USART0_TX_vect)
# define USART_TX_vect USART0_TX_vect
# define USART_RX_vect USART0_RX_vect
# elif defined(USART0_TXC_vect)
# define USART_TX_vect USART0_TXC_vect
# define USART_RX_vect USART0_RXC_vect
# elif defined(USART_TXC_vect)
# define USART_TX_vect USART_TXC_vect
# define USART_RX_vect USART_RXC_vect
# else
# error "I hate avr-libc"
# endif
#endif
// Sugar for the avr-libc
#if !defined(USART0_TX0_vect) && defined(USART0_TXC_vect)
# define USART0_TX_vect USART0_TXC_vect
# define USART1_TX_vect USART1_TXC_vect
# define USART0_RX_vect USART0_RXC_vect
# define USART1_RX_vect USART1_RXC_vect
#endif
#define usart(a, ...) _usart_cat(a, _usart_cat(USE_USART, __VA_ARGS__))
/* We love the preprocessor */
#if defined(URSEL) || defined(URSEL0)
#define _BV_URSEL _BV(usart(URSEL))
#else
#define _BV_URSEL 0
#endif
/* If the Baudrate isn't set by the module which is using usart.h */
#ifndef BAUD
#define BAUD 19200
#endif
/* We use setbaud.h from the avr-libc */
#include "setbaud.h"
/* This is used in generate_usart_init() */
#if USE_2X
#define USART_2X() usart(UCSR,A) |= (1 << usart(U2X))
#else
#define USART_2X() usart(UCSR,A) &= ~(1 << usart(U2X))
#endif
/* init the usart module */
#define generate_usart_init() \
static void \
usart_init(void) \
{\
/* The ATmega644 datasheet suggests to clear the global\
interrupt flags on initialization ... */\
uint8_t sreg = SREG; cli(); \
usart(UBRR,H) = HI8(UBRRH_VALUE); \
usart(UBRR,L) = LO8(UBRRL_VALUE); ;\
/* set mode: 8 bits, 1 stop, no parity, asynchronous usart */ \
/* and set URSEL, if present, */ \
usart(UCSR,C) = _BV(usart(UCSZ,0)) | _BV(usart(UCSZ,1)) | _BV_URSEL; \
/* Enable the RX interrupt and receiver and transmitter */ \
usart(UCSR,B) |= _BV(usart(TXEN)) | _BV(usart(RXEN)) | _BV(usart(RXCIE));\
/* Set or not set the 2x mode */ \
USART_2X(); \
/* Go! */ \
SREG = sreg;\
}
#endif