2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: uart.c,v 1.33.4.7 2009-01-23 23:08:42 zer0 Exp $
22 /* Olivier MATZ, Droids-corp 2004 - 2009 */
25 #include <aversive/list.h>
28 #include <uart_defs.h>
29 #include <uart_private.h>
31 struct cirbuf g_tx_fifo[UART_HW_NUM];
32 struct cirbuf g_rx_fifo[UART_HW_NUM];
34 /* global vars are initialized to 0 (NULL) */
35 event *rx_event[UART_HW_NUM];
36 event *tx_event[UART_HW_NUM];
38 const struct regs uart_regs[UART_HW_NUM] = {
82 * This is the interruption function which occurs when the entire
83 * frame in the transmit shift register has been shifted out and
84 * there is no new data in the transmit buffer.
87 #ifndef SIG_UART0_DATA
88 #define SIG_UART0_DATA SIG_USART0_DATA
90 SIGNAL(SIG_UART0_DATA)
92 uart_send_next_char(0);
96 #ifndef SIG_UART1_DATA
97 #define SIG_UART1_DATA SIG_USART1_DATA
99 SIGNAL(SIG_UART1_DATA)
101 uart_send_next_char(1);
105 #ifndef SIG_UART2_DATA
106 #define SIG_UART2_DATA SIG_USART2_DATA
108 SIGNAL(SIG_UART2_DATA)
110 uart_send_next_char(2);
114 #ifndef SIG_UART3_DATA
115 #define SIG_UART3_DATA SIG_USART3_DATA
117 SIGNAL(SIG_UART3_DATA)
119 uart_send_next_char(3);
123 static void uart_recv_next_char(uint8_t num);
126 * This is the interruption function which occurs when there is
127 * a new unread data in the reception buffer.
130 #ifndef SIG_UART0_RECV
131 #define SIG_UART0_RECV SIG_USART0_RECV
133 SIGNAL(SIG_UART0_RECV)
135 uart_recv_next_char(0);
139 #ifndef SIG_UART1_RECV
140 #define SIG_UART1_RECV SIG_USART1_RECV
142 SIGNAL(SIG_UART1_RECV)
144 uart_recv_next_char(1);
148 #ifndef SIG_UART2_RECV
149 #define SIG_UART2_RECV SIG_USART2_RECV
151 SIGNAL(SIG_UART2_RECV)
153 uart_recv_next_char(2);
157 #ifndef SIG_UART3_RECV
158 #define SIG_UART3_RECV SIG_USART3_RECV
160 SIGNAL(SIG_UART3_RECV)
162 uart_recv_next_char(3);
168 * transmit next character of fifo if any, and call the event function.
169 * This function is executed with intr locked.
171 void uart_send_next_char(uint8_t num)
173 #ifdef CONFIG_MODULE_UART_9BITS
174 if (uart_getconf_nbits(num) == 9) {
177 /* for 9 bits, it uses 2 places in the fifo */
178 if (CIRBUF_GET_LEN(&g_tx_fifo[num]) < 2) {
179 cbi(*uart_regs[num].ucsrb, UDRIE);
183 cirbuf_get_buf_tail(&g_tx_fifo[num], (char *)&elt, 2);
184 cirbuf_del_buf_tail(&g_tx_fifo[num], 2);
186 uart_set_udr_9bits(num, elt);
187 sbi(*uart_regs[num].ucsrb, UDRIE);
189 else /* 5, 6, 7 or 8 bits */
190 #endif /* CONFIG_MODULE_UART_9BITS */
194 if (CIRBUF_IS_EMPTY(&g_tx_fifo[num])) {
195 cbi(*uart_regs[num].ucsrb, UDRIE);
199 elt = cirbuf_get_tail(&g_tx_fifo[num]);
200 cirbuf_del_tail(&g_tx_fifo[num]);
201 uart_set_udr(num, elt);
202 sbi(*uart_regs[num].ucsrb, UDRIE);
209 static void uart_recv_next_char(uint8_t num)
211 #ifdef CONFIG_MODULE_UART_9BITS
212 if (uart_getconf_nbits() == 9) {
215 elt = uart_get_udr_9bits(num);
216 if (CIRBUF_GET_FREELEN(&g_rx_fifo[num]) >= 2) {
217 cirbuf_add_buf_head(&g_rx_fifo[num], (char *)&elt, 2);
221 ((event_9bits *)rx_event[num])(elt);
224 #endif /* CONFIG_MODULE_UART_9BITS */
228 elt = uart_get_udr(num);
229 if (!CIRBUF_IS_FULL(&g_rx_fifo[num])) {
230 cirbuf_add_head(&g_rx_fifo[num], elt);
241 #if (defined UDR0) && (defined UART0_COMPILE)
242 uart_setconf(0, NULL);
245 #if (defined UDR1) && (defined UART1_COMPILE)
246 uart_setconf(1, NULL);
249 #if (defined UDR2) && (defined UART2_COMPILE)
250 uart_setconf(2, NULL);
253 #if (defined UDR3) && (defined UART3_COMPILE)
254 uart_setconf(3, NULL);