]> git.zerfleddert.de Git - fnordlicht-mini/blame_incremental - firmware/fnordlicht-controller/uart.c
more tx disabling.
[fnordlicht-mini] / firmware / fnordlicht-controller / uart.c
... / ...
CommitLineData
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#include "globals.h"
25
26#if CONFIG_SERIAL
27
28#include "../common/io.h"
29#include <avr/interrupt.h>
30
31#include "globals.h"
32#include "../common/common.h"
33#include "fifo.h"
34#include "uart.h"
35
36/* define uart mode (8N1) */
37#if defined(__AVR_ATmega8__)
38/* in atmega8, we need a special switching bit
39 * for addressing UCSRC */
40#define UART_UCSRC _BV(URSEL) | _BV(UCSZ0) | _BV(UCSZ1)
41
42#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
43/* in atmega88, this isn't needed any more */
44#define UART_UCSRC _BV(_UCSZ0_UART0) | _BV(_UCSZ1_UART0)
45#endif
46
47/* global variables */
48volatile struct global_uart_t global_uart;
49
50/** output one character */
51void uart_putc(uint8_t data)
52{
53 /* store data */
54 fifo_enqueue((fifo_t *)&global_uart.tx, data);
55
56 /* enable interrupt */
57 _UCSRB_UART0 |= _BV(_UDRIE_UART0);
58}
59
60/** init the hardware uart */
61void uart_init(void)
62{
63 #define BAUD CONFIG_SERIAL_BAUDRATE
64 #include <util/setbaud.h>
65
66 /* set baud rate */
67 _UBRRH_UART0 = UBRRH_VALUE;
68 _UBRRL_UART0 = UBRRL_VALUE;
69
70 #if USE_2X
71 _UCSRA_UART0 |= (1 << _U2X_UART0);
72 #endif
73
74 /* set mode */
75 _UCSRC_UART0 = UART_UCSRC;
76
77 /* enable transmitter, receiver and receiver complete interrupt */
78 _UCSRB_UART0 = _BV(_TXEN_UART0) | _BV(_RXEN_UART0) | _BV(_RXCIE_UART0);
79
80 /* init fifos */
81 fifo_init((fifo_t *)&global_uart.rx);
82 fifo_init((fifo_t *)&global_uart.tx);
83}
84
85
86/** interrupts*/
87
88/** uart receive interrupt */
89ISR(_SIG_UART_RECV_UART0)
90{
91
92 /* store received data */
93 fifo_enqueue((fifo_t *)&global_uart.rx, _UDR_UART0);
94
95}
96
97/** uart data register empty interrupt */
98ISR(_SIG_UART_DATA_UART0)
99{
100
101 /* load next byte to transfer */
102 _UDR_UART0 = fifo_dequeue((fifo_t *)&global_uart.tx);
103
104 /* check if this interrupt is still needed */
105 if ( fifo_fill((fifo_t *)&global_uart.tx) == 0) {
106 /* disable this interrupt */
107 _UCSRB_UART0 &= ~_BV(_UDRIE_UART0);
108 }
109
110}
111
112
113#endif
Impressum, Datenschutz