]>
Commit | Line | Data |
---|---|---|
ec1bef8e | 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 "pwm.h" | |
34 | #include "fifo.h" | |
35 | #include "uart.h" | |
36 | ||
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) | |
42 | ||
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) | |
46 | #endif | |
47 | ||
48 | /* global variables */ | |
49 | volatile struct global_uart_t global_uart; | |
50 | ||
51 | /** output one character */ | |
52 | void uart_putc(uint8_t data) | |
53 | { | |
e06282dc | 54 | #if UART_TX_ENABLED |
ec1bef8e | 55 | /* store data */ |
56 | fifo_enqueue((fifo_t *)&global_uart.tx, data); | |
57 | ||
58 | /* enable interrupt */ | |
59 | _UCSRB_UART0 |= _BV(_UDRIE_UART0); | |
e06282dc | 60 | #endif |
ec1bef8e | 61 | } |
62 | ||
63 | /** init the hardware uart */ | |
64 | void uart_init(void) | |
65 | { | |
66 | #define BAUD CONFIG_SERIAL_BAUDRATE | |
67 | #include <util/setbaud.h> | |
68 | ||
69 | /* set baud rate */ | |
70 | _UBRRH_UART0 = UBRRH_VALUE; | |
71 | _UBRRL_UART0 = UBRRL_VALUE; | |
72 | ||
73 | #if USE_2X | |
74 | _UCSRA_UART0 |= (1 << _U2X_UART0); | |
75 | #endif | |
76 | ||
77 | /* set mode */ | |
78 | _UCSRC_UART0 = UART_UCSRC; | |
79 | ||
e06282dc | 80 | #if UART_TX_ENABLED |
ec1bef8e | 81 | /* enable transmitter, receiver and receiver complete interrupt */ |
82 | _UCSRB_UART0 = _BV(_TXEN_UART0) | _BV(_RXEN_UART0) | _BV(_RXCIE_UART0); | |
e06282dc | 83 | /* init tx fifo */ |
84 | fifo_init((fifo_t *)&global_uart.tx); | |
85 | #else | |
86 | /* enable receiver and receiver complete interrupt */ | |
87 | _UCSRB_UART0 = _BV(_RXEN_UART0) | _BV(_RXCIE_UART0); | |
88 | #endif | |
ec1bef8e | 89 | |
e06282dc | 90 | /* init rx fifo */ |
ec1bef8e | 91 | fifo_init((fifo_t *)&global_uart.rx); |
ec1bef8e | 92 | } |
93 | ||
94 | ||
95 | /** interrupts*/ | |
96 | ||
97 | /** uart receive interrupt */ | |
98 | ISR(_SIG_UART_RECV_UART0) | |
99 | { | |
100 | ||
101 | /* store received data */ | |
102 | fifo_enqueue((fifo_t *)&global_uart.rx, _UDR_UART0); | |
103 | ||
104 | } | |
105 | ||
e06282dc | 106 | #if UART_TX_ENABLED |
ec1bef8e | 107 | /** uart data register empty interrupt */ |
108 | ISR(_SIG_UART_DATA_UART0) | |
109 | { | |
ec1bef8e | 110 | /* load next byte to transfer */ |
111 | _UDR_UART0 = fifo_dequeue((fifo_t *)&global_uart.tx); | |
112 | ||
113 | /* check if this interrupt is still needed */ | |
114 | if ( fifo_fill((fifo_t *)&global_uart.tx) == 0) { | |
115 | /* disable this interrupt */ | |
116 | _UCSRB_UART0 &= ~_BV(_UDRIE_UART0); | |
117 | } | |
ec1bef8e | 118 | } |
e06282dc | 119 | #endif |
ec1bef8e | 120 | |
121 | ||
122 | #endif |