]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR32_UC3/DRIVERS/USART/usart.h
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / AVR32_UC3 / DRIVERS / USART / usart.h
1 /*This file is prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief USART driver for AVR32 UC3.\r
5  *\r
6  * This file contains basic functions for the AVR32 USART, with support for all\r
7  * modes, settings and clock speeds.\r
8  *\r
9  * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32\r
10  * - Supported devices:  All AVR32 devices with a USART module can be used.\r
11  * - AppNote:\r
12  *\r
13  * \author               Atmel Corporation: http://www.atmel.com \n\r
14  *                       Support and FAQ: http://support.atmel.no/\r
15  *\r
16  ******************************************************************************/\r
17 \r
18 /* Copyright (c) 2007, Atmel Corporation All rights reserved.\r
19  *\r
20  * Redistribution and use in source and binary forms, with or without\r
21  * modification, are permitted provided that the following conditions are met:\r
22  *\r
23  * 1. Redistributions of source code must retain the above copyright notice,\r
24  * this list of conditions and the following disclaimer.\r
25  *\r
26  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
27  * this list of conditions and the following disclaimer in the documentation\r
28  * and/or other materials provided with the distribution.\r
29  *\r
30  * 3. The name of ATMEL may not be used to endorse or promote products derived\r
31  * from this software without specific prior written permission.\r
32  *\r
33  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
35  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND\r
36  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,\r
37  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
38  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
39  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
42  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
43  */\r
44 \r
45 \r
46 #ifndef _USART_H_\r
47 #define _USART_H_\r
48 \r
49 #include <avr32/io.h>\r
50 #include "compiler.h"\r
51 \r
52 \r
53 /*! \name Return Values\r
54  */\r
55 //! @{\r
56 #define USART_SUCCESS           0 //!< Successful completion.\r
57 #define USART_FAILURE          -1 //!< Failure because of some unspecified reason.\r
58 #define USART_INVALID_INPUT     1 //!< Input value out of range.\r
59 #define USART_INVALID_ARGUMENT -1 //!< Argument value out of range.\r
60 #define USART_TX_BUSY           2 //!< Transmitter was busy.\r
61 #define USART_RX_EMPTY          3 //!< Nothing was received.\r
62 #define USART_RX_ERROR          4 //!< Transmission error occurred.\r
63 #define USART_MODE_FAULT        5 //!< USART not in the appropriate mode.\r
64 //! @}\r
65 \r
66 //! Default time-out value (number of attempts).\r
67 #define USART_DEFAULT_TIMEOUT   10000\r
68 \r
69 /*! \name Parity Settings\r
70  */\r
71 //! @{\r
72 #define USART_EVEN_PARITY       AVR32_USART_MR_PAR_EVEN   //!< Use even parity on character transmission.\r
73 #define USART_ODD_PARITY        AVR32_USART_MR_PAR_ODD    //!< Use odd parity on character transmission.\r
74 #define USART_SPACE_PARITY      AVR32_USART_MR_PAR_SPACE  //!< Use a space as parity bit.\r
75 #define USART_MARK_PARITY       AVR32_USART_MR_PAR_MARK   //!< Use a mark as parity bit.\r
76 #define USART_NO_PARITY         AVR32_USART_MR_PAR_NONE   //!< Don't use a parity bit.\r
77 #define USART_MULTIDROP_PARITY  AVR32_USART_MR_PAR_MULTI  //!< Parity bit is used to flag address characters.\r
78 //! @}\r
79 \r
80 /*! \name Operating Modes\r
81  */\r
82 //! @{\r
83 #define USART_MODE_NORMAL       AVR32_USART_MR_MODE_NORMAL      //!< Normal RS232 mode.\r
84 #define USART_MODE_RS485        AVR32_USART_MR_MODE_RS485       //!< RS485 mode.\r
85 #define USART_MODE_HW_HSH       AVR32_USART_MR_MODE_HARDWARE    //!< RS232 mode with hardware handshaking.\r
86 #define USART_MODE_MODEM        AVR32_USART_MR_MODE_MODEM       //!< Modem mode.\r
87 #define USART_MODE_ISO7816_T0   AVR32_USART_MR_MODE_ISO7816_T0  //!< ISO7816, T = 0 mode.\r
88 #define USART_MODE_ISO7816_T1   AVR32_USART_MR_MODE_ISO7816_T1  //!< ISO7816, T = 1 mode.\r
89 #define USART_MODE_IRDA         AVR32_USART_MR_MODE_IRDA        //!< IrDA mode.\r
90 #define USART_MODE_SW_HSH       AVR32_USART_MR_MODE_SOFTWARE    //!< RS232 mode with software handshaking.\r
91 //! @}\r
92 \r
93 \r
94 /*! \name Channel Modes\r
95  */\r
96 //! @{\r
97 #define USART_NORMAL_CHMODE     AVR32_USART_MR_CHMODE_NORMAL      //!< Normal communication.\r
98 #define USART_AUTO_ECHO         AVR32_USART_MR_CHMODE_ECHO        //!< Echo data.\r
99 #define USART_LOCAL_LOOPBACK    AVR32_USART_MR_CHMODE_LOCAL_LOOP  //!< Local loopback.\r
100 #define USART_REMOTE_LOOPBACK   AVR32_USART_MR_CHMODE_REMOTE_LOOP //!< Remote loopback.\r
101 //! @}\r
102 \r
103 /*! \name Stop Bits Settings\r
104  */\r
105 //! @{\r
106 #define USART_1_STOPBIT         AVR32_USART_MR_NBSTOP_1   //!< Use 1 stop bit.\r
107 #define USART_1_5_STOPBITS      AVR32_USART_MR_NBSTOP_1_5 //!< Use 1.5 stop bits.\r
108 #define USART_2_STOPBITS        AVR32_USART_MR_NBSTOP_2   //!< Use 2 stop bits (for more, just give the number of bits).\r
109 //! @}\r
110 \r
111 \r
112 //! Input parameters when initializing RS232 and similar modes.\r
113 typedef struct\r
114 {\r
115   //! Set baudrate of the USART.\r
116   unsigned long baudrate;\r
117 \r
118   //! Number of bits to transmit as a character (5 to 9).\r
119   unsigned char charlength;\r
120 \r
121   //! How to calculate the parity bit: \ref USART_EVEN_PARITY, \ref USART_ODD_PARITY,\r
122   //! \ref USART_SPACE_PARITY, \ref USART_MARK_PARITY, \ref USART_NO_PARITY or\r
123   //! \ref USART_MULTIDROP_PARITY.\r
124   unsigned char paritytype;\r
125 \r
126   //! Number of stop bits between two characters: \ref USART_1_STOPBIT,\r
127   //! \ref USART_1_5_STOPBITS, \ref USART_2_STOPBITS or any number from 3 to 257\r
128   //! which will result in a time guard period of that length between characters.\r
129   unsigned short stopbits;\r
130 \r
131   //! Run the channel in testmode: \ref USART_NORMAL_CHMODE, \ref USART_AUTO_ECHO,\r
132   //! \ref USART_LOCAL_LOOPBACK or \ref USART_REMOTE_LOOPBACK.\r
133   unsigned char channelmode;\r
134 } usart_options_t;\r
135 \r
136 //! Input parameters when initializing ISO7816 modes.\r
137 typedef struct\r
138 {\r
139   //! Set the frequency of the ISO7816 clock.\r
140   unsigned long iso7816_hz;\r
141 \r
142   //! The number of ISO7816 clock ticks in every bit period (1 to 2047, 0 = disable clock).\r
143   //! Bit rate = \ref iso7816_hz / \ref fidi_ratio.\r
144   unsigned short fidi_ratio;\r
145 \r
146   //! Inhibit Non Acknowledge:\n\r
147   //!   - 0: the NACK is generated;\n\r
148   //!   - 1: the NACK is not generated.\r
149   //!\r
150   //! \note This bit will be used only in ISO7816 mode, protocol T = 0 receiver.\r
151   int inhibit_nack;\r
152 \r
153   //! Disable successive NACKs.\r
154   //! Successive parity errors are counted up to the value in the \ref max_iterations field.\r
155   //! These parity errors generate a NACK on the ISO line. As soon as this value is reached,\r
156   //! no addititional NACK is sent on the ISO line. The ITERATION flag is asserted.\r
157   int dis_suc_nack;\r
158 \r
159   //! Max number of repetitions (0 to 7).\r
160   unsigned char max_iterations;\r
161 \r
162   //! Bit order in transmitted characters:\n\r
163   //!   - 0: LSB first;\n\r
164   //!   - 1: MSB first.\r
165   int bit_order;\r
166 } iso7816_options_t;\r
167 \r
168 //! Input parameters when initializing ISO7816 modes.\r
169 typedef struct\r
170 {\r
171   //! Set the frequency of the SPI clock.\r
172   unsigned long baudrate;\r
173 \r
174   //! Number of bits to transmit as a character (5 to 9).\r
175   unsigned char charlength;\r
176 \r
177   //! Run the channel in testmode: \ref USART_NORMAL_CHMODE, \ref USART_AUTO_ECHO,\r
178   //! \ref USART_LOCAL_LOOPBACK or \ref USART_REMOTE_LOOPBACK.\r
179   unsigned char channelmode;  \r
180   \r
181   //! Which SPI mode to use when transmitting.\r
182   unsigned char spimode;\r
183 } usart_spi_options_t;\r
184   \r
185   \r
186 \r
187 \r
188 \r
189 //------------------------------------------------------------------------------\r
190 /*! \name Initialization Functions\r
191  */\r
192 //! @{\r
193 \r
194 /*! \brief Resets the USART and disables TX and RX.\r
195  *\r
196  * \param usart Base address of the USART instance.\r
197  */\r
198 extern void usart_reset(volatile avr32_usart_t *usart);\r
199 \r
200 /*! \brief Sets up the USART to use the standard RS232 protocol.\r
201  *\r
202  * \param usart   Base address of the USART instance.\r
203  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
204  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
205  *\r
206  * \retval USART_SUCCESS        Mode successfully initialized.\r
207  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
208  */\r
209 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
210 \r
211 /*! \brief Sets up the USART to use hardware handshaking.\r
212  *\r
213  * \param usart   Base address of the USART instance.\r
214  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
215  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
216  *\r
217  * \retval USART_SUCCESS        Mode successfully initialized.\r
218  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
219  *\r
220  * \note \ref usart_init_rs232 does not need to be invoked before this function.\r
221  */\r
222 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
223 \r
224 /*! \brief Sets up the USART to use the IrDA protocol.\r
225  *\r
226  * \param usart       Base address of the USART instance.\r
227  * \param opt         Options needed to set up RS232 communication (see \ref usart_options_t).\r
228  * \param pba_hz      USART module input clock frequency (PBA clock, Hz).\r
229  * \param irda_filter Counter used to distinguish received ones from zeros.\r
230  *\r
231  * \retval USART_SUCCESS        Mode successfully initialized.\r
232  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
233  */\r
234 extern int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,\r
235                            long pba_hz, unsigned char irda_filter);\r
236 \r
237 /*! \brief Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.\r
238  *\r
239  * \param usart   Base address of the USART instance.\r
240  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
241  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
242  *\r
243  * \retval USART_SUCCESS        Mode successfully initialized.\r
244  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
245  */\r
246 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
247 \r
248 /*! \brief Sets up the USART to use the RS485 protocol.\r
249  *\r
250  * \param usart       Base address of the USART instance.\r
251  * \param opt         Options needed to set up RS232 communication (see \ref usart_options_t).\r
252  * \param pba_hz      USART module input clock frequency (PBA clock, Hz).\r
253  *\r
254  * \retval USART_SUCCESS        Mode successfully initialized.\r
255  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
256  */\r
257 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
258 \r
259 /*! \brief Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.\r
260  *\r
261  * \param usart   Base address of the USART instance.\r
262  * \param opt     Options needed to set up ISO7816 communication (see \ref iso7816_options_t).\r
263  * \param t       ISO7816 mode to use (T=0 or T=1).\r
264  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
265  *\r
266  * \retval USART_SUCCESS        Mode successfully initialized.\r
267  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
268  */\r
269 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const iso7816_options_t *opt, int t, long pba_hz);\r
270 \r
271 /*! \brief Sets up the USART to use the SPI mode as master.\r
272  *\r
273  * \param usart   Base address of the USART instance.\r
274  * \param opt     Options needed to set up SPI mode (see \ref usart_spi_options_t).\r
275  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
276  *\r
277  * \retval USART_SUCCESS        Mode successfully initialized.\r
278  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
279  */\r
280 extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);\r
281 \r
282 \r
283 /*! \brief Sets up the USART to use the SPI mode as slave.\r
284  *\r
285  * \param usart   Base address of the USART instance.\r
286  * \param opt     Options needed to set up SPI mode (see \ref usart_spi_options_t).\r
287  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
288  *\r
289  * \retval USART_SUCCESS        Mode successfully initialized.\r
290  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
291  */\r
292 extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);\r
293 \r
294 //! @}\r
295 \r
296 //------------------------------------------------------------------------------\r
297 /*! \brief Selects slave chip.\r
298  *\r
299  * \param usart   Base address of the USART instance.\r
300  *\r
301  * \return Status.\r
302  *   \retval USART_SUCCESS             Success.\r
303  */\r
304 extern int usart_spi_selectChip(volatile avr32_usart_t *usart);\r
305 \r
306 /*! \brief Unselects slave chip.\r
307  *\r
308  * \param usart   Base address of the USART instance.\r
309  *\r
310  * \return Status.\r
311  *   \retval USART_SUCCESS             Success.\r
312  *   \retval USART_FAILURE             Time out.\r
313  */\r
314 extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);\r
315 \r
316 //------------------------------------------------------------------------------\r
317 /*! \name Read and Reset Error Status Bits\r
318  */\r
319 //! @{\r
320 \r
321 /*! \brief Resets the error status.\r
322  *\r
323  * This function resets the status bits indicating that a parity error,\r
324  * framing error or overrun has occurred. The RXBRK bit, indicating\r
325  * a start/end of break condition on the RX line, is also reset.\r
326  *\r
327  * \param usart Base address of the USART instance.\r
328  */\r
329 #if __GNUC__\r
330 __attribute__((__always_inline__))\r
331 #endif\r
332 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)\r
333 {\r
334   usart->cr |= AVR32_USART_CR_RSTSTA_MASK;\r
335 }\r
336 \r
337 /*! \brief Checks if a parity error has occurred since last status reset.\r
338  *\r
339  * \param usart Base address of the USART instance.\r
340  *\r
341  * \return \c 1 if a parity error has been detected, otherwise \c 0.\r
342  */\r
343 #if __GNUC__\r
344 __attribute__((__always_inline__))\r
345 #endif\r
346 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)\r
347 {\r
348   return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;\r
349 }\r
350 \r
351 /*! \brief Checks if a framing error has occurred since last status reset.\r
352  *\r
353  * \param usart Base address of the USART instance.\r
354  *\r
355  * \return \c 1 if a framing error has been detected, otherwise \c 0.\r
356  */\r
357 #if __GNUC__\r
358 __attribute__((__always_inline__))\r
359 #endif\r
360 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)\r
361 {\r
362   return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;\r
363 }\r
364 \r
365 /*! \brief Checks if an overrun error has occurred since last status reset.\r
366  *\r
367  * \param usart Base address of the USART instance.\r
368  *\r
369  * \return \c 1 if a overrun error has been detected, otherwise \c 0.\r
370  */\r
371 #if __GNUC__\r
372 __attribute__((__always_inline__))\r
373 #endif\r
374 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)\r
375 {\r
376   return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;\r
377 }\r
378 \r
379 //! @}\r
380 \r
381 \r
382 //------------------------------------------------------------------------------\r
383 /*! \name Transmit/Receive Functions\r
384  */\r
385 //! @{\r
386 \r
387 /*! \brief Addresses a receiver.\r
388  *\r
389  * While in RS485 mode, receivers only accept data addressed to them.\r
390  * A packet/char with the address tag set has to precede any data.\r
391  * This function is used to address a receiver. This receiver should read\r
392  * all the following data, until an address packet addresses another receiver.\r
393  *\r
394  * \param usart   Base address of the USART instance.\r
395  * \param address Address of the target device.\r
396  *\r
397  * \retval USART_SUCCESS    Address successfully sent (if current mode is RS485).\r
398  * \retval USART_MODE_FAULT Wrong operating mode.\r
399  */\r
400 extern int usart_send_address(volatile avr32_usart_t *usart, int address);\r
401 \r
402 /*! \brief Writes the given character to the TX buffer if the transmitter is ready.\r
403  *\r
404  * \param usart Base address of the USART instance.\r
405  * \param c     The character (up to 9 bits) to transmit.\r
406  *\r
407  * \retval USART_SUCCESS  The transmitter was ready.\r
408  * \retval USART_TX_BUSY  The transmitter was busy.\r
409  */\r
410 extern int usart_write_char(volatile avr32_usart_t *usart, int c);\r
411 \r
412 /*! \brief An active wait writing a character to the USART.\r
413  *\r
414  * \param usart Base address of the USART instance.\r
415  * \param c     The character (up to 9 bits) to transmit.\r
416  */\r
417 #if __GNUC__\r
418 __attribute__((__always_inline__))\r
419 #endif\r
420 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)\r
421 {\r
422   while (usart_write_char(usart, c) != USART_SUCCESS);\r
423 }\r
424 \r
425 /*! \brief Sends a character with the USART.\r
426  *\r
427  * \param usart Base address of the USART instance.\r
428  * \param c     Character to write.\r
429  *\r
430  * \retval USART_SUCCESS  The character was written.\r
431  * \retval USART_FAILURE  The function timed out before the USART transmitter became ready to send.\r
432  */\r
433 extern int usart_putchar(volatile avr32_usart_t *usart, int c);\r
434 \r
435 /*! \brief Checks the RX buffer for a received character, and stores it at the\r
436  *         given memory location.\r
437  *\r
438  * \param usart Base address of the USART instance.\r
439  * \param c     Pointer to the where the read character should be stored\r
440  *              (must be at least short in order to accept 9-bit characters).\r
441  *\r
442  * \retval USART_SUCCESS  The character was read successfully.\r
443  * \retval USART_RX_EMPTY The RX buffer was empty.\r
444  * \retval USART_RX_ERROR An error was deteceted.\r
445  */\r
446 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);\r
447 \r
448 /*! \brief Waits until a character is received, and returns it.\r
449  *\r
450  * \param usart Base address of the USART instance.\r
451  *\r
452  * \return The received character, or \ref USART_FAILURE upon error.\r
453  */\r
454 extern int usart_getchar(volatile avr32_usart_t *usart);\r
455 \r
456 /*! \brief Writes one character string to the USART.\r
457  *\r
458  * \param usart   Base address of the USART instance.\r
459  * \param string  String to be written.\r
460  */\r
461 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);\r
462 \r
463 /*! \brief Gets and echoes characters until end of line.\r
464  *\r
465  * \param usart Base address of the USART instance.\r
466  *\r
467  * \retval USART_SUCCESS  Success.\r
468  * \retval USART_FAILURE  ETX character received.\r
469  */\r
470 extern int usart_get_echo_line(volatile avr32_usart_t *usart);\r
471 \r
472 //! @}\r
473 \r
474 \r
475 #endif  // _USART_H_\r