1 /* vim:ts=4 sts=4 et tw=80
5 * for additional information please
6 * see http://lochraster.org/fnordlichtmini
8 * (c) by Alexander Neumann <alexander@bumpern.de>
9 * Lars Noschinski <lars@public.noschinski.de>
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.
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
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/>.
28 #include "../common/io.h"
29 #include <avr/interrupt.h>
32 #include "../common/common.h"
37 /* define uart mode (8N1) */
38 #if defined(__AVR_ATmega8__)
39 /* in atmega8, we need a special switching bit
40 * for addressing UCSRC */
41 #define UART_UCSRC _BV(URSEL) | _BV(UCSZ0) | _BV(UCSZ1)
43 #elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
44 /* in atmega88, this isn't needed any more */
45 #define UART_UCSRC _BV(_UCSZ0_UART0) | _BV(_UCSZ1_UART0)
48 /* global variables */
49 volatile struct global_uart_t global_uart
;
51 /** output one character */
52 void uart_putc(uint8_t data
)
55 fifo_enqueue((fifo_t
*)&global_uart
.tx
, data
);
57 /* enable interrupt */
58 _UCSRB_UART0
|= _BV(_UDRIE_UART0
);
61 /** init the hardware uart */
64 #define BAUD CONFIG_SERIAL_BAUDRATE
65 #include <util/setbaud.h>
68 _UBRRH_UART0
= UBRRH_VALUE
;
69 _UBRRL_UART0
= UBRRL_VALUE
;
72 _UCSRA_UART0
|= (1 << _U2X_UART0
);
76 _UCSRC_UART0
= UART_UCSRC
;
78 /* enable transmitter, receiver and receiver complete interrupt */
79 _UCSRB_UART0
= _BV(_TXEN_UART0
) | _BV(_RXEN_UART0
) | _BV(_RXCIE_UART0
);
82 fifo_init((fifo_t
*)&global_uart
.rx
);
83 fifo_init((fifo_t
*)&global_uart
.tx
);
89 /** uart receive interrupt */
90 ISR(_SIG_UART_RECV_UART0
)
93 /* store received data */
94 fifo_enqueue((fifo_t
*)&global_uart
.rx
, _UDR_UART0
);
98 /** uart data register empty interrupt */
99 ISR(_SIG_UART_DATA_UART0
)
102 /* load next byte to transfer */
103 _UDR_UART0
= fifo_dequeue((fifo_t
*)&global_uart
.tx
);
105 /* check if this interrupt is still needed */
106 if ( fifo_fill((fifo_t
*)&global_uart
.tx
) == 0) {
107 /* disable this interrupt */
108 _UCSRB_UART0
&= ~_BV(_UDRIE_UART0
);