]> git.zerfleddert.de Git - fnordlicht-mini/blob - firmware/fnordlicht-bootloader/uart.h
e305f37ee7cedf2042dfc95fd669e72d5a1538e1
[fnordlicht-mini] / firmware / fnordlicht-bootloader / uart.h
1 /* vim:ts=4 sts=4 et tw=80
2 *
3 * fnordlicht firmware
4 *
5 * for additional information please
6 * see http://lochraster.org/fnordlichtmini
7 *
8 * (c) by Alexander Neumann <alexander@bumpern.de>
9 * Lars Noschinski <lars@public.noschinski.de>
10 *
11 * This program is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as published by
13 * the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #ifndef UART_H
25 #define UART_H
26
27 #include <stdbool.h>
28
29
30 /* define uart mode (8N1) */
31 #if defined(__AVR_ATmega8__)
32 /* in atmega8, we need a special switching bit
33 * for addressing UCSRC */
34 #define UART_UCSRC _BV(URSEL) | _BV(UCSZ0) | _BV(UCSZ1)
35
36 #elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
37 /* in atmega88, this isn't needed any more */
38 #define UART_UCSRC _BV(_UCSZ0_UART0) | _BV(_UCSZ1_UART0)
39 #endif
40
41
42 static inline void uart_init(void)
43 {
44 #define BAUD CONFIG_SERIAL_BAUDRATE
45 #include <util/setbaud.h>
46 /* set baud rate */
47 _UBRRH_UART0 = UBRRH_VALUE;
48 _UBRRL_UART0 = UBRRL_VALUE;
49
50 #if USE_2X
51 _UCSRA_UART0 |= (1 << _U2X_UART0);
52 #endif
53
54 /* set mode */
55 _UCSRC_UART0 = UART_UCSRC;
56
57 /* enable transmitter and receiver */
58 _UCSRB_UART0 = _BV(_TXEN_UART0) | _BV(_RXEN_UART0);
59 }
60
61 static inline bool uart_receive_complete(void)
62 {
63 return _UCSRA_UART0 & _BV(_RXC_UART0);
64 }
65
66 static inline bool uart_send_complete(void)
67 {
68 return _BV(_UDRE_UART0) & _UCSRA_UART0;
69 }
70
71 static inline uint8_t uart_getc(void)
72 {
73 return _UDR_UART0;
74 }
75
76 static void uart_putc(uint8_t data)
77 {
78 while(!(_BV(_UDRE_UART0) & _UCSRA_UART0));
79 UDR0 = data;
80 }
81
82 #endif
Impressum, Datenschutz