3 This file is part of the AVR-uart_ni.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * \email bg@nerilex.org
24 * \license GPLv3 or later
26 * \brief declaration for interrupt based uart
28 * This implementation of the uart-interface of AVR microcontrollers uses the
29 * interrup architecture and can be used to handle serial communication in the
31 * The uart is configured at compile-time by some special defines starting with
32 * \a UART0_ for configuring the first uart and \a UART1_ for the second.
33 * Some settings use symbolic values defined in uart_defs.h .
34 * The following options are available:
35 * - \a *_I enables the interrupt based driver for this uart
36 * - \a 0 disables driver
37 * - \a 1 enables driver
38 * - \a *_BAUD_RATE sets the baudrate for this uart (value is the baudrate)
39 * - \a *_STOPBITS sets the amount of stop bits for this uart
40 * - \a UART_STOPBITS_1 for one stop bit
41 * - \a UART_STOPBITS_2 for two stop bits
42 * - \a *_DATABITS sets the amount of data bits for this uart
43 * - \a UART_DATABITS_5 for five data bits
44 * - \a UART_DATABITS_6 for six data bits
45 * - \a UART_DATABITS_7 for seven data bits
46 * - \a UART_DATABITS_8 for eight data bits
47 * - \a *_PARATY sets the mode for paraty calculation for this uart
48 * - \a UART_PARATY_NONE ignore paraty
49 * - \a UART_PARATY_ODD odd paraty
50 * - \a UART_PARATY_EVEN even paraty
51 * - \a *_RXBUFFER_SIZE size of the recieve buffer in bytes
52 * - \a *_TXBUFFER_SIZE size of the transmitt buffer in bytes
53 * - \a *_SWFLOWCTRL enable/diasable software flow control (via XON & XOFF)
54 * - \a 0 disable software flow control
55 * - \a 1 enable software flow control
56 * - \a *_THRESH_HIGH set upper limit for the rx buffer, which causes an XOFF
57 * to be send when crossed (only relevant if software flow
59 * - \a *_THRESH_LOW set lower limit for the rx buffer, which causes an XON to
60 * be send when crossed and an XOFF has been send previously
61 * (only relevant if software flow control is enabled)
62 * - \a *_HOOK enable/disable implementation of the hook feature
63 * (\ref uart0_sethook())
64 * - \a 0 disable hook feature
65 * - \a 1 enable hook feature
74 #include "circularbytebuffer.h"
78 * \brief storage type for uart0 context
80 * This type is used to store uart0 specific global variables.
81 * It contains a pointer to the buffer instances and when neccessary
82 * a pointer to the hook function and an indicator if the hook is
84 * If software flow control is enabled it also contains flags for flow control.
87 circularbytebuffer_t rxb; /**< recieve buffer */
88 circularbytebuffer_t txb; /**< transmitt buffer*/
90 void(*hook)(uint8_t); /**< pointer to the hook function */
91 volatile uint8_t hook_running; /**< flag indicating if the hook is running */
94 volatile uint8_t txon; /**< flag indicating if we are allowed to send data */
95 volatile uint8_t rxon; /**< flag indicating if we have send an \a XOFF */
101 * \brief storage type for uart1 context
103 * This type is used to store uart1 specific global variables.
104 * It contains a pointer to the buffer instances and when neccessary
105 * a pointer to the hook function and an indicator if the hook is
106 * currently executed.
107 * If software flow control is enabled it also contains flags for flow control.
110 circularbytebuffer_t rxb; /**< recieve buffer */
111 circularbytebuffer_t txb; /**< transmitt buffer */
113 void(*hook)(uint8_t); /**< pointer to the hook function */
114 volatile uint8_t hook_running; /**< flag indicating if the hook is running */
117 volatile uint8_t txon; /**< flag indicating if we are allowed to send data */
118 volatile uint8_t rxon; /**< flag indicating if we have send a XOFF */
125 * \brief initialize uart0.
127 * This function initializes the first uart according to the parameter specified
130 void uart0_init(void);
133 * \brief send data through uart0.
135 * This function sends data through the first uart
136 * (the data size is debfined in config.h).
137 * \param c data to send
139 void uart0_putc(uint16_t c);
142 * \brief read data from uart0.
144 * This function reads data from the first uart
145 * (the data size is debfined in config.h).
146 * \return data recived by uart0
148 uint16_t uart0_getc(void);
151 * \brief checks if data is available.
153 * This function checks the state of the input buffer of uart0 and
154 * returns if data is available or not.
155 * \return zero if no data is available else a value different from zero is returned
157 uint8_t uart0_dataavail(void);
160 * \brief flushes the internal transmit buffer.
162 * This function function waits until all data was send (the transmit-buffer
163 * does not contain any data).
165 void uart0_flush(void);
170 * \brief sets the hook for uart0.
172 * This function modifys the way the software handels incomming data.
173 * When the hook is set to \a NULL (which is the default) incomming data is buffered
174 * in a special ringbuffer and read by \ref uart0_getc(). If the hook is set to a
175 * different value, this value is interpret as a function pointer. The hook (the
176 * function where the function pointer points to) is called with the recieved data
177 * as single parameter. Any value returned by the hook is discarded.
179 * \note If the hook is set \ref uart0_getc() will not return, as the
180 * ringbuffer is bypassed.
181 * \param fpt pointer to thae handler function for recieved data
183 void uart0_sethook(void(*fpt)(uint8_t));
191 * \brief initialize uart1.
193 * This function initializes the second uart according to the parameter specifyed
196 void uart1_init(void);
199 * \brief send data through uart1.
201 * This function sends data through the second uart
202 * (the data size is debfined in config.h).
203 * \param c data to send
205 void uart1_putc(uint16_t c);
208 * \brief read data from uart1.
210 * This function reads data from the second uart
211 * (the data size is debfined in config.h).
212 * \return data recived by uart1
214 uint16_t uart1_getc(void);
217 * \brief checks if data is available.
219 * This function checks the state of the input buffer of uart1 and
220 * returns if data is available or not.
221 * \return zero if no data is available else a value different from zero is returned
223 uint8_t uart1_dataavail(void);
226 * \brief sets the hook for uart1.
228 * This function modifys the way the software handels incomming data.
229 * When the hook is set to \a NULL (which is the default) incomming data is buffered
230 * in a special ringbuffer and read by \ref uart1_getc(). If the hook is set to a
231 * different value, this value is interpret as a function pointer. The hook (the
232 * function where the function pointer points to) is called with the recieved data
233 * as single parameter. Any value returned by the hook is discarded.
235 * \note If the hook is set \ref uart1_getc() will not return, as the
236 * ringbuffer is bypassed.
237 * \param fpt pointer to thae handler function for recieved data
239 void uart1_sethook(void(*fpt)(uint8_t));
244 #endif /* UART_I_H_ */