2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
\r
4 * This program is free software; you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation; either version 2 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program; if not, write to the Free Software
\r
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
18 * Revision : $Id: uart.h,v 1.22.4.4 2008-12-27 16:29:07 zer0 Exp $
\r
22 /* Olivier MATZ, 2004 - 2006
\r
23 * Interface of the uart module
\r
27 * \brief Interface of the UART module.
\r
29 * This module provides :
\r
30 * - Tx and Rx with fifo
\r
32 * - Parity selection (if the uC support it)
\r
33 * - 5 to 8 data bits (if the uC support it).
\r
34 * - 1 or 2 stop bits (if the uC support it).
\r
35 * - up to 4 UARTs (if the uC support it).
\r
37 * Number of bits in frame, parity, stop bits are the same for tx and
\r
38 * rx. TX fifo is useless if interrupts are disabled because the uart
\r
39 * wait that all bytes are transmitted before returning.
\r
41 * It doesn't support some USART capabilities :
\r
42 * - Synchronous mode
\r
43 * - Multiprocessor communication
\r
50 #include <aversive.h>
\r
51 #include <uart_config.h>
\r
52 #include <uart_defs.h>
\r
56 /** this structure stores the configuration of the uart */
\r
57 struct uart_config {
\r
58 uint8_t enabled : 1, /**< enable or disable the uart */
\r
59 intr_enabled : 1, /**< use interruptions or not */
\r
60 use_double_speed : 1, /**< less acurate, but can reach faster baudrate */
\r
61 parity : 2, /**< none, odd or even */
\r
62 stop_bits : 1, /**< 1 or 2 bits at the end of the frame */
\r
63 reserved : 1; /**< nothing for now */
\r
64 uint8_t nbits; /**< number of bits in frame, 5,6,7,8 or 9 */
\r
65 uint32_t baudrate; /**< speed of uart */
\r
68 /** The emission fifo of uart */
\r
69 extern struct cirbuf g_tx_fifo[UART_HW_NUM];
\r
71 /** The reception fifo of uart */
\r
72 extern struct cirbuf g_rx_fifo[UART_HW_NUM];
\r
75 * Initialisation function. This function puts the registers of the
\r
76 * microcontroler in a correct state in order to use the uart. It
\r
77 * uses the configuration file <uart_config.h> ; this function is
\r
78 * equivalent to call uartX_setconf(NULL) for each uart.
\r
80 void uart_init(void);
\r
83 * Configure the uart 'num' with the given configuration. Returns 0 on
\r
86 int8_t uart_setconf(uint8_t num, struct uart_config *u);
\r
88 /** Get the current configuration of the uart 'num' */
\r
89 void uart_getconf(uint8_t num, struct uart_config *u);
\r
92 * uart_recv returns the next character, taken from the fifo (if
\r
93 * any). If there is nothing to read, the function waits until
\r
94 * something come on the uart.
\r
96 int uart_recv(uint8_t num);
\r
99 * uart_recv returns the next character, taken from the fifo (if
\r
100 * any). If there is nothing to read, the function returns -1.
\r
102 int uart_recv_nowait(uint8_t num);
\r
105 * same than uart_recv with 9 bits.
\r
107 int uart_9bits_recv(uint8_t num);
\r
110 * same than uart_recv_nowait() with 9 bits.
\r
112 int uart_9bits_recv_nowait(uint8_t num);
\r
115 * uart_send_nowait is used to send data to the uart 'num'. The data
\r
116 * is first stored in the FIFO before beeing sent. If the FIFO is
\r
117 * full, data is dropped and the function returns -1, else it returns
\r
120 int uart_send_nowait(uint8_t num, char c);
\r
123 * uart_send is used to send data to the uart 'num'. The data is first
\r
124 * stored in the FIFO before beeing sent. If the FIFO is full, the
\r
125 * function wait until the uart is ready. The function returns c.
\r
127 int uart_send(uint8_t num, char c);
\r
130 * uart_send_9bits is the same that uart_send but arg is 16 bits so
\r
131 * data can be 9 bits wide.
\r
133 int uart_send_9bits_nowait(uint8_t num, int c);
\r
135 /* uart_send_9bits_wait is the same that uart_send_wait but arg is
\r
136 * 16 bits so data can be 9 bits wide.
\r
138 int uart_send_9bits(uint8_t num, int c);
\r
143 * This function is used to register another function which will be
\r
144 * executed at each byte transmission (5, 6 ,7 ,8 bits)
\r
146 void uart_register_tx_event(uint8_t num, void (*f)(char));
\r
149 * This function is used to register another function which will be
\r
150 * executed at each byte reception (5, 6 ,7 ,8 bits)
\r
152 void uart_register_rx_event(uint8_t num, void (*f)(char));
\r
156 * This function is used to register another function which will be
\r
157 * executed at each 9 bits frame transmission. WARNING : it uses the
\r
158 * same internal pointer that the 8 bits event, so be carreful to
\r
159 * unregister 8 bits events when doing 9 bits and vice versa.
\r
161 void uart_register_tx_9bits_event(uint8_t num, void (*f)(int));
\r
164 * This function is used to register another function which will be
\r
165 * executed at each 9 bits reception. WARNING : it uses the
\r
166 * same internal pointer that the 8 bits event, so be carreful to
\r
167 * unregister 8 bits events when doing 9 bits and vice versa.
\r
169 void uart_register_rx_9bits_event(uint8_t num, void (*f)(int));
\r
171 /* funcs for use with fdevopen (avrlibc > 1.4.0) */
\r
172 int uart0_dev_send_nowait(char c, FILE* f);
\r
173 int uart0_dev_send(char c, FILE* f);
\r
174 int uart0_dev_recv_nowait(FILE* f);
\r
175 int uart0_dev_recv(FILE* f);
\r
177 int uart1_dev_send_nowait(char c, FILE* f);
\r
178 int uart1_dev_send(char c, FILE* f);
\r
179 int uart1_dev_recv_nowait(FILE* f);
\r
180 int uart1_dev_recv(FILE* f);
\r
182 int uart2_dev_send_nowait(char c, FILE* f);
\r
183 int uart2_dev_send(char c, FILE* f);
\r
184 int uart2_dev_recv_nowait(FILE* f);
\r
185 int uart2_dev_recv(FILE* f);
\r
187 int uart3_dev_send_nowait(char c, FILE* f);
\r
188 int uart3_dev_send(char c, FILE* f);
\r
189 int uart3_dev_recv_nowait(FILE* f);
\r
190 int uart3_dev_recv(FILE* f);
\r
193 #endif /* _UART_H_ */
\r