update ldscript
[protos/xbee-avr.git] / uart_send_nowait.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
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.
8  *
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.
13  *
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
17  *
18  *  Revision : $Id: uart_send_nowait.c,v 1.1.2.2 2008-12-27 16:50:01 zer0 Exp $
19  *
20  */
21
22 /* Olivier MATZ, Droids-corp 2004 - 2009 */
23
24 #include <uart.h>
25 #include <uart_defs.h>
26 #include <uart_private.h>
27
28
29 /* send a char, or put it in the fifo if uart is not ready. Return -1
30  * if fifo is full */
31 int uart_send_nowait(uint8_t num, char c)
32 {
33         uint8_t flags;
34
35         IRQ_LOCK(flags);
36
37         /* if uart intrp mode is disabled (note that we look rx */
38         /* intrp -- RXCIE is 0) */
39         if ( !(*uart_regs[num].ucsrb & (1 << RXCIE )) ) {
40                 /* we have to poll the status register before xmit */
41                 if (*uart_regs[num].ucsra & (1<<UDRE)) {
42                         uart_set_udr(num, c);
43                         IRQ_UNLOCK(flags);
44                         return 0;
45                 }
46                 else {
47                         IRQ_UNLOCK(flags);
48                         return -1;
49                 }
50         }
51
52         if (CIRBUF_IS_FULL(&g_tx_fifo[num])) {
53                 IRQ_UNLOCK(flags);
54                 return -1;
55         }
56
57         /* uart is ready to send */
58         if (CIRBUF_IS_EMPTY(&g_tx_fifo[num]) && 
59             *uart_regs[num].ucsra & (1<<UDRE)) {
60                 uart_set_udr(num, c);
61                 sbi(*uart_regs[num].ucsrb, UDRIE); /* XXX */
62         }
63         else { /* not ready, put char in fifo */
64                 cirbuf_add_head(&g_tx_fifo[num], c);
65         }
66
67         IRQ_UNLOCK(flags);
68         return 0;
69 }