hostsim enhancements, some bugs remaining (freeze sometimes)
[aversive.git] / modules / comm / uart / uart.h
1 /*  \r
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)\r
3  * \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
8  *\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
13  *\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
17  *\r
18  *  Revision : $Id: uart.h,v 1.22.4.4 2008-12-27 16:29:07 zer0 Exp $\r
19  *\r
20  */\r
21 \r
22 /* Olivier MATZ, 2004 - 2006\r
23  * Interface of the uart module\r
24  */\r
25 \r
26 /** \file uart.h\r
27  *  \brief Interface of the UART module.\r
28  *\r
29  * This module provides :\r
30  *   - Tx and Rx with fifo\r
31  *   - Speed selection\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
36  * \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
40  *\r
41  * It doesn't support some USART capabilities :\r
42  *   - Synchronous mode\r
43  *   - Multiprocessor communication\r
44  */\r
45 \r
46 #ifndef _UART_H_\r
47 #define _UART_H_\r
48 \r
49 #include <stdio.h>\r
50 #include <aversive.h>\r
51 #include <uart_config.h>\r
52 \r
53 #include <cirbuf.h>\r
54 \r
55 /** this structure stores the configuration of the uart */\r
56 struct uart_config {\r
57       uint8_t enabled          : 1, /**< enable or disable the uart */\r
58       intr_enabled     : 1, /**< use interruptions or not */\r
59       use_double_speed : 1, /**< less acurate, but can reach faster baudrate */\r
60       parity           : 2, /**< none, odd or even */\r
61       stop_bits        : 1, /**< 1 or 2 bits at the end of the frame */\r
62       reserved         : 1; /**< nothing for now */\r
63       uint8_t nbits;                /**< number of bits in frame, 5,6,7,8 or 9 */\r
64       uint32_t baudrate;             /**< speed of uart */\r
65 };\r
66 \r
67 \r
68 /** \r
69  * Initialisation function. This function puts the registers of the\r
70  * microcontroler in a correct state in order to use the uart.  It\r
71  * uses the configuration file <uart_config.h> ; this function is\r
72  * equivalent to call uartX_setconf(NULL) for each uart.\r
73  */\r
74 void uart_init(void);\r
75 \r
76 /** \r
77  * Configure the uart 'num' with the given configuration. Returns 0 on\r
78  * success.\r
79  */\r
80 int8_t uart_setconf(uint8_t num, struct uart_config *u);\r
81 \r
82 /** Get the current configuration of the uart 'num' */\r
83 void uart_getconf(uint8_t num, struct uart_config *u);\r
84 \r
85 /** \r
86  * uart_recv returns the next character, taken from the fifo (if\r
87  * any). If there is nothing to read, the function waits until\r
88  * something come on the uart.\r
89  */\r
90 int uart_recv(uint8_t num);\r
91 \r
92 /** \r
93  * uart_recv returns the next character, taken from the fifo (if\r
94  * any). If there is nothing to read, the function returns -1.\r
95  */\r
96 int uart_recv_nowait(uint8_t num);\r
97 \r
98 /**\r
99  * same than uart_recv with 9 bits.\r
100  */\r
101 int uart_9bits_recv(uint8_t num);\r
102 \r
103 /** \r
104  * same than uart_recv_nowait() with 9 bits.\r
105  */\r
106 int uart_9bits_recv_nowait(uint8_t num);\r
107 \r
108 /**\r
109  * uart_send_nowait is used to send data to the uart 'num'. The data\r
110  * is first stored in the FIFO before beeing sent. If the FIFO is\r
111  * full, data is dropped and the function returns -1, else it returns\r
112  * the character c.\r
113  */\r
114 int uart_send_nowait(uint8_t num, char c);\r
115 \r
116 /**\r
117  * uart_send is used to send data to the uart 'num'. The data is first\r
118  * stored in the FIFO before beeing sent. If the FIFO is full, the\r
119  * function wait until the uart is ready. The function returns c.\r
120  */\r
121 int uart_send(uint8_t num, char c);\r
122 \r
123 /**\r
124  * uart_send_9bits is the same that uart_send but arg is 16 bits so\r
125  * data can be 9 bits wide.\r
126  */\r
127 int uart_send_9bits_nowait(uint8_t num, int c);\r
128 \r
129 /* uart_send_9bits_wait is the same that uart_send_wait but arg is\r
130  * 16 bits so data can be 9 bits wide.\r
131  */\r
132 int uart_send_9bits(uint8_t num, int c);\r
133 \r
134 \r
135 \r
136 /**\r
137  * This function is used to register another function which will be\r
138  * executed at each byte transmission (5, 6 ,7 ,8 bits)\r
139  */\r
140 void uart_register_tx_event(uint8_t num, void (*f)(char));\r
141 \r
142 /**\r
143  * This function is used to register another function which will be\r
144  * executed at each byte reception (5, 6 ,7 ,8 bits)\r
145  */\r
146 void uart_register_rx_event(uint8_t num, void (*f)(char));\r
147 \r
148 \r
149 /**\r
150  * This function is used to register another function which will be\r
151  * executed at each 9 bits frame transmission. WARNING : it uses the\r
152  * same internal pointer that the 8 bits event, so be carreful to \r
153  * unregister 8 bits events when doing 9 bits and vice versa.\r
154  */\r
155 void uart_register_tx_9bits_event(uint8_t num, void (*f)(int));\r
156 \r
157 /**\r
158  * This function is used to register another function which will be\r
159  * executed at each 9 bits reception. WARNING : it uses the\r
160  * same internal pointer that the 8 bits event, so be carreful to \r
161  * unregister 8 bits events when doing 9 bits and vice versa.\r
162  */\r
163 void uart_register_rx_9bits_event(uint8_t num, void (*f)(int));\r
164 \r
165 /* funcs for use with fdevopen (avrlibc > 1.4.0) */\r
166 int uart0_dev_send_nowait(char c, FILE* f);\r
167 int uart0_dev_send(char c, FILE* f);\r
168 int uart0_dev_recv_nowait(FILE* f);\r
169 int uart0_dev_recv(FILE* f);\r
170 \r
171 int uart1_dev_send_nowait(char c, FILE* f);\r
172 int uart1_dev_send(char c, FILE* f);\r
173 int uart1_dev_recv_nowait(FILE* f);\r
174 int uart1_dev_recv(FILE* f);\r
175 \r
176 int uart2_dev_send_nowait(char c, FILE* f);\r
177 int uart2_dev_send(char c, FILE* f);\r
178 int uart2_dev_recv_nowait(FILE* f);\r
179 int uart2_dev_recv(FILE* f);\r
180 \r
181 int uart3_dev_send_nowait(char c, FILE* f);\r
182 int uart3_dev_send(char c, FILE* f);\r
183 int uart3_dev_recv_nowait(FILE* f);\r
184 int uart3_dev_recv(FILE* f);\r
185 \r
186 \r
187 #endif /* _UART_H_ */\r
188 \r