]> git.sur5r.net Git - freertos/blob - Demo/AVR32_UC3A_GCC/Atmel_SW_Framework/DRIVERS/USART/usart.h
6230a5b3effd59c93bd1aa73eb2fe53ce67d3bb9
[freertos] / Demo / AVR32_UC3A_GCC / Atmel_SW_Framework / DRIVERS / USART / usart.h
1 /* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */\r
2
3 /*This file is prepared for Doxygen automatic documentation generation.*/\r
4 /*! \file *********************************************************************\r
5  *\r
6  * \brief USART driver for AVR32 UC3.\r
7  *\r
8  * This file contains basic functions for the AVR32 USART, with support for all\r
9  * modes, settings and clock speeds.\r
10  *\r
11  * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32\r
12  * - Supported devices:  All AVR32 devices with a USART module can be used.\r
13  * - AppNote:\r
14  *\r
15  * \author               Atmel Corporation: http://www.atmel.com \n\r
16  *                       Support and FAQ: http://support.atmel.no/\r
17  *\r
18  ******************************************************************************/\r
19 \r
20 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.\r
21  *\r
22  * Redistribution and use in source and binary forms, with or without\r
23  * modification, are permitted provided that the following conditions are met:\r
24  *\r
25  * 1. Redistributions of source code must retain the above copyright notice, this\r
26  * list of conditions and the following disclaimer.\r
27  *\r
28  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
29  * this list of conditions and the following disclaimer in the documentation\r
30  * and/or other materials provided with the distribution.\r
31  *\r
32  * 3. The name of Atmel may not be used to endorse or promote products derived\r
33  * from this software without specific prior written permission.\r
34  *\r
35  * 4. This software may only be redistributed and used in connection with an Atmel\r
36  * AVR product.\r
37  *\r
38  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
40  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
41  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
42  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\r
48  *\r
49  */\r
50 \r
51 #ifndef _USART_H_\r
52 #define _USART_H_\r
53 \r
54 #include <avr32/io.h>\r
55 #include "compiler.h"\r
56 \r
57 \r
58 /*! \name Return Values\r
59  */\r
60 //! @{\r
61 #define USART_SUCCESS                 0 //!< Successful completion.\r
62 #define USART_FAILURE                -1 //!< Failure because of some unspecified reason.\r
63 #define USART_INVALID_INPUT           1 //!< Input value out of range.\r
64 #define USART_INVALID_ARGUMENT       -1 //!< Argument value out of range.\r
65 #define USART_TX_BUSY                 2 //!< Transmitter was busy.\r
66 #define USART_RX_EMPTY                3 //!< Nothing was received.\r
67 #define USART_RX_ERROR                4 //!< Transmission error occurred.\r
68 #define USART_MODE_FAULT              5 //!< USART not in the appropriate mode.\r
69 //! @}\r
70 \r
71 //! Default time-out value (number of attempts).\r
72 #define USART_DEFAULT_TIMEOUT         10000\r
73 \r
74 /*! \name Parity Settings\r
75  */\r
76 //! @{\r
77 #define USART_EVEN_PARITY             AVR32_USART_MR_PAR_EVEN   //!< Use even parity on character transmission.\r
78 #define USART_ODD_PARITY              AVR32_USART_MR_PAR_ODD    //!< Use odd parity on character transmission.\r
79 #define USART_SPACE_PARITY            AVR32_USART_MR_PAR_SPACE  //!< Use a space as parity bit.\r
80 #define USART_MARK_PARITY             AVR32_USART_MR_PAR_MARK   //!< Use a mark as parity bit.\r
81 #define USART_NO_PARITY               AVR32_USART_MR_PAR_NONE   //!< Don't use a parity bit.\r
82 #define USART_MULTIDROP_PARITY        AVR32_USART_MR_PAR_MULTI  //!< Parity bit is used to flag address characters.\r
83 //! @}\r
84 \r
85 /*! \name Stop Bits Settings\r
86  */\r
87 //! @{\r
88 #define USART_1_STOPBIT               AVR32_USART_MR_NBSTOP_1   //!< Use 1 stop bit.\r
89 #define USART_1_5_STOPBITS            AVR32_USART_MR_NBSTOP_1_5 //!< Use 1.5 stop bits.\r
90 #define USART_2_STOPBITS              AVR32_USART_MR_NBSTOP_2   //!< Use 2 stop bits (for more, just give the number of bits).\r
91 //! @}\r
92 \r
93 /*! \name Channel Modes\r
94  */\r
95 //! @{\r
96 #define USART_NORMAL_CHMODE           AVR32_USART_MR_CHMODE_NORMAL      //!< Normal communication.\r
97 #define USART_AUTO_ECHO               AVR32_USART_MR_CHMODE_ECHO        //!< Echo data.\r
98 #define USART_LOCAL_LOOPBACK          AVR32_USART_MR_CHMODE_LOCAL_LOOP  //!< Local loopback.\r
99 #define USART_REMOTE_LOOPBACK         AVR32_USART_MR_CHMODE_REMOTE_LOOP //!< Remote loopback.\r
100 //! @}\r
101 \r
102 #if defined(AVR32_USART_400_H_INCLUDED) || \\r
103     defined(AVR32_USART_410_H_INCLUDED) || \\r
104     defined(AVR32_USART_420_H_INCLUDED) || \\r
105     defined(AVR32_USART_440_H_INCLUDED) || \\r
106     defined(AVR32_USART_602_H_INCLUDED)\r
107 \r
108 /*! \name LIN Node Actions\r
109  */\r
110 //! @{\r
111 #define USART_LIN_PUBLISH_ACTION      AVR32_USART_LINMR_NACT_PUBLISH    //!< The USART transmits the response.\r
112 #define USART_LIN_SUBSCRIBE_ACTION    AVR32_USART_LINMR_NACT_SUBSCRIBE  //!< The USART receives the response.\r
113 #define USART_LIN_IGNORE_ACTION       AVR32_USART_LINMR_NACT_IGNORE     //!< The USART does not transmit and does not receive the reponse.\r
114 //! @}\r
115 \r
116 /*! \name LIN Checksum Types\r
117  */\r
118 //! @{\r
119 #define USART_LIN_ENHANCED_CHECKSUM   0 //!< LIN 2.0 "enhanced" checksum.\r
120 #define USART_LIN_CLASSIC_CHECKSUM    1 //!< LIN 1.3 "classic" checksum.\r
121 //! @}\r
122 \r
123 #endif  // USART rev. >= 4.0.0\r
124 \r
125 \r
126 //! Input parameters when initializing RS232 and similar modes.\r
127 typedef struct\r
128 {\r
129   //! Set baud rate of the USART (unused in slave modes).\r
130   unsigned long baudrate;\r
131 \r
132   //! Number of bits to transmit as a character (5 to 9).\r
133   unsigned char charlength;\r
134 \r
135   //! How to calculate the parity bit: \ref USART_EVEN_PARITY, \ref USART_ODD_PARITY,\r
136   //! \ref USART_SPACE_PARITY, \ref USART_MARK_PARITY, \ref USART_NO_PARITY or\r
137   //! \ref USART_MULTIDROP_PARITY.\r
138   unsigned char paritytype;\r
139 \r
140   //! Number of stop bits between two characters: \ref USART_1_STOPBIT,\r
141   //! \ref USART_1_5_STOPBITS, \ref USART_2_STOPBITS or any number from 3 to 257\r
142   //! which will result in a time guard period of that length between characters.\r
143   //! \note \ref USART_1_5_STOPBITS is supported in asynchronous modes only.\r
144   unsigned short stopbits;\r
145 \r
146   //! Run the channel in testmode: \ref USART_NORMAL_CHMODE, \ref USART_AUTO_ECHO,\r
147   //! \ref USART_LOCAL_LOOPBACK or \ref USART_REMOTE_LOOPBACK.\r
148   unsigned char channelmode;\r
149 } usart_options_t;\r
150 \r
151 //! Input parameters when initializing ISO7816 mode.\r
152 typedef struct\r
153 {\r
154   //! Set the frequency of the ISO7816 clock.\r
155   unsigned long iso7816_hz;\r
156 \r
157   //! The number of ISO7816 clock ticks in every bit period (1 to 2047, 0 = disable clock).\r
158   //! Bit rate = \ref iso7816_hz / \ref fidi_ratio.\r
159   unsigned short fidi_ratio;\r
160 \r
161   //! How to calculate the parity bit: \ref USART_EVEN_PARITY for normal mode or\r
162   //! \ref USART_ODD_PARITY for inverse mode.\r
163   unsigned char paritytype;\r
164 \r
165   //! Inhibit Non Acknowledge:\n\r
166   //!   - 0: the NACK is generated;\n\r
167   //!   - 1: the NACK is not generated.\r
168   //!\r
169   //! \note This bit will be used only in ISO7816 mode, protocol T = 0 receiver.\r
170   int inhibit_nack;\r
171 \r
172   //! Disable successive NACKs.\r
173   //! Successive parity errors are counted up to the value in the \ref max_iterations field.\r
174   //! These parity errors generate a NACK on the ISO line. As soon as this value is reached,\r
175   //! no addititional NACK is sent on the ISO line. The ITERATION flag is asserted.\r
176   int dis_suc_nack;\r
177 \r
178   //! Max number of repetitions (0 to 7).\r
179   unsigned char max_iterations;\r
180 \r
181   //! Bit order in transmitted characters:\n\r
182   //!   - 0: LSB first;\n\r
183   //!   - 1: MSB first.\r
184   int bit_order;\r
185 } usart_iso7816_options_t;\r
186 \r
187 #if defined(AVR32_USART_400_H_INCLUDED) || \\r
188     defined(AVR32_USART_410_H_INCLUDED) || \\r
189     defined(AVR32_USART_420_H_INCLUDED) || \\r
190     defined(AVR32_USART_440_H_INCLUDED) || \\r
191     defined(AVR32_USART_602_H_INCLUDED)\r
192 \r
193 //! Input parameters when initializing SPI mode.\r
194 typedef struct\r
195 {\r
196   //! Set the frequency of the SPI clock (unused in slave mode).\r
197   unsigned long baudrate;\r
198 \r
199   //! Number of bits to transmit as a character (5 to 9).\r
200   unsigned char charlength;\r
201 \r
202   //! Which SPI mode to use.\r
203   unsigned char spimode;\r
204 \r
205   //! Run the channel in testmode: \ref USART_NORMAL_CHMODE, \ref USART_AUTO_ECHO,\r
206   //! \ref USART_LOCAL_LOOPBACK or \ref USART_REMOTE_LOOPBACK.\r
207   unsigned char channelmode;\r
208 } usart_spi_options_t;\r
209 \r
210 #endif  // USART rev. >= 4.0.0\r
211 \r
212 \r
213 //------------------------------------------------------------------------------\r
214 /*! \name Initialization Functions\r
215  */\r
216 //! @{\r
217 \r
218 /*! \brief Resets the USART and disables TX and RX.\r
219  *\r
220  * \param usart   Base address of the USART instance.\r
221  */\r
222 extern void usart_reset(volatile avr32_usart_t *usart);\r
223 \r
224 /*! \brief Sets up the USART to use the standard RS232 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  *\r
230  * \retval USART_SUCCESS        Mode successfully initialized.\r
231  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
232  */\r
233 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
234 \r
235 /*! \brief Sets up the USART to use the standard RS232 protocol in TX-only mode.\r
236  *\r
237  * Compared to \ref usart_init_rs232, this function allows very high baud rates\r
238  * (up to \a pba_hz instead of \a pba_hz / \c 8) at the expense of full duplex.\r
239  *\r
240  * \param usart   Base address of the USART instance.\r
241  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
242  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
243  *\r
244  * \retval USART_SUCCESS        Mode successfully initialized.\r
245  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
246  *\r
247  * \note The \c 1.5 stop bit is not supported in this mode.\r
248  */\r
249 extern int usart_init_rs232_tx_only(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
250 \r
251 /*! \brief Sets up the USART to use hardware handshaking.\r
252  *\r
253  * \param usart   Base address of the USART instance.\r
254  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
255  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
256  *\r
257  * \retval USART_SUCCESS        Mode successfully initialized.\r
258  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
259  *\r
260  * \note \ref usart_init_rs232 does not need to be invoked before this function.\r
261  */\r
262 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
263 \r
264 /*! \brief Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.\r
265  *\r
266  * \param usart   Base address of the USART instance.\r
267  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
268  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
269  *\r
270  * \retval USART_SUCCESS        Mode successfully initialized.\r
271  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
272  */\r
273 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
274 \r
275 /*! \brief Sets up the USART to use a synchronous RS232-like protocol in master mode.\r
276  *\r
277  * \param usart   Base address of the USART instance.\r
278  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
279  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
280  *\r
281  * \retval USART_SUCCESS        Mode successfully initialized.\r
282  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
283  */\r
284 extern int usart_init_sync_master(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
285 \r
286 /*! \brief Sets up the USART to use a synchronous RS232-like protocol in slave mode.\r
287  *\r
288  * \param usart   Base address of the USART instance.\r
289  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
290  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
291  *\r
292  * \retval USART_SUCCESS        Mode successfully initialized.\r
293  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
294  */\r
295 extern int usart_init_sync_slave(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
296 \r
297 /*! \brief Sets up the USART to use the RS485 protocol.\r
298  *\r
299  * \param usart   Base address of the USART instance.\r
300  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
301  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
302  *\r
303  * \retval USART_SUCCESS        Mode successfully initialized.\r
304  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
305  */\r
306 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
307 \r
308 /*! \brief Sets up the USART to use the IrDA protocol.\r
309  *\r
310  * \param usart         Base address of the USART instance.\r
311  * \param opt           Options needed to set up RS232 communication (see \ref usart_options_t).\r
312  * \param pba_hz        USART module input clock frequency (PBA clock, Hz).\r
313  * \param irda_filter   Counter used to distinguish received ones from zeros.\r
314  *\r
315  * \retval USART_SUCCESS        Mode successfully initialized.\r
316  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
317  */\r
318 extern int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,\r
319                            long pba_hz, unsigned char irda_filter);\r
320 \r
321 /*! \brief Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.\r
322  *\r
323  * The receiver is enabled by default. \ref usart_iso7816_enable_receiver and\r
324  * \ref usart_iso7816_enable_transmitter can be called to change the half-duplex\r
325  * communication direction.\r
326  *\r
327  * \param usart   Base address of the USART instance.\r
328  * \param opt     Options needed to set up ISO7816 communication (see \ref usart_iso7816_options_t).\r
329  * \param t       ISO7816 mode to use (T=0 or T=1).\r
330  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
331  *\r
332  * \retval USART_SUCCESS        Mode successfully initialized.\r
333  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
334  */\r
335 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz);\r
336 \r
337 #if defined(AVR32_USART_400_H_INCLUDED) || \\r
338     defined(AVR32_USART_410_H_INCLUDED) || \\r
339     defined(AVR32_USART_420_H_INCLUDED) || \\r
340     defined(AVR32_USART_440_H_INCLUDED) || \\r
341     defined(AVR32_USART_602_H_INCLUDED)\r
342 \r
343 /*! \brief Sets up the USART to use the LIN master mode.\r
344  *\r
345  * \param usart     Base address of the USART instance.\r
346  * \param baudrate  Baud rate.\r
347  * \param pba_hz    USART module input clock frequency (PBA clock, Hz).\r
348  *\r
349  */\r
350 extern int usart_init_lin_master(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);\r
351 \r
352 /*! \brief Sets up the USART to use the LIN slave mode.\r
353  *\r
354  * \param usart     Base address of the USART instance.\r
355  * \param baudrate  Baud rate.\r
356  * \param pba_hz    USART module input clock frequency (PBA clock, Hz).\r
357  *\r
358  */\r
359 extern int usart_init_lin_slave(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);\r
360 \r
361 /*! \brief Sets up the USART to use the SPI master mode.\r
362  *\r
363  * \ref usart_spi_selectChip and \ref usart_spi_unselectChip can be called to\r
364  * select or unselect the SPI slave chip.\r
365  *\r
366  * \param usart   Base address of the USART instance.\r
367  * \param opt     Options needed to set up SPI mode (see \ref usart_spi_options_t).\r
368  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
369  *\r
370  * \retval USART_SUCCESS        Mode successfully initialized.\r
371  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
372  */\r
373 extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);\r
374 \r
375 /*! \brief Sets up the USART to use the SPI slave mode.\r
376  *\r
377  * \param usart   Base address of the USART instance.\r
378  * \param opt     Options needed to set up SPI mode (see \ref usart_spi_options_t).\r
379  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
380  *\r
381  * \retval USART_SUCCESS        Mode successfully initialized.\r
382  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
383  */\r
384 extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);\r
385 \r
386 #endif  // USART rev. >= 4.0.0\r
387 \r
388 //! @}\r
389 \r
390 \r
391 //------------------------------------------------------------------------------\r
392 /*! \name Read and Reset Error Status Bits\r
393  */\r
394 //! @{\r
395 \r
396 /*! \brief Resets the error status.\r
397  *\r
398  * This function resets the status bits indicating that a parity error,\r
399  * framing error or overrun has occurred. The RXBRK bit, indicating\r
400  * a start/end of break condition on the RX line, is also reset.\r
401  *\r
402  * \param usart   Base address of the USART instance.\r
403  */\r
404 #if (defined __GNUC__)\r
405 __attribute__((__always_inline__))\r
406 #endif\r
407 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)\r
408 {\r
409   usart->cr = AVR32_USART_CR_RSTSTA_MASK;\r
410 }\r
411 \r
412 /*! \brief Checks if a parity error has occurred since last status reset.\r
413  *\r
414  * \param usart   Base address of the USART instance.\r
415  *\r
416  * \return \c 1 if a parity error has been detected, otherwise \c 0.\r
417  */\r
418 #if (defined __GNUC__)\r
419 __attribute__((__always_inline__))\r
420 #endif\r
421 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)\r
422 {\r
423   return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;\r
424 }\r
425 \r
426 /*! \brief Checks if a framing error has occurred since last status reset.\r
427  *\r
428  * \param usart   Base address of the USART instance.\r
429  *\r
430  * \return \c 1 if a framing error has been detected, otherwise \c 0.\r
431  */\r
432 #if (defined __GNUC__)\r
433 __attribute__((__always_inline__))\r
434 #endif\r
435 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)\r
436 {\r
437   return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;\r
438 }\r
439 \r
440 /*! \brief Checks if an overrun error has occurred since last status reset.\r
441  *\r
442  * \param usart   Base address of the USART instance.\r
443  *\r
444  * \return \c 1 if a overrun error has been detected, otherwise \c 0.\r
445  */\r
446 #if (defined __GNUC__)\r
447 __attribute__((__always_inline__))\r
448 #endif\r
449 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)\r
450 {\r
451   return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;\r
452 }\r
453 \r
454 #if defined(AVR32_USART_400_H_INCLUDED) || \\r
455     defined(AVR32_USART_410_H_INCLUDED) || \\r
456     defined(AVR32_USART_420_H_INCLUDED) || \\r
457     defined(AVR32_USART_440_H_INCLUDED) || \\r
458     defined(AVR32_USART_602_H_INCLUDED)\r
459 \r
460 /*! \brief Get LIN Error Status\r
461  *\r
462  * \param usart   Base address of the USART instance.\r
463  *\r
464  * \retval The binary value of the error field.\r
465  */\r
466 #if (defined __GNUC__)\r
467 __attribute__((__always_inline__))\r
468 #endif\r
469 extern __inline__ int usart_lin_get_error(volatile avr32_usart_t *usart)\r
470 {\r
471   return (usart->csr & (AVR32_USART_CSR_LINSNRE_MASK |\r
472                         AVR32_USART_CSR_LINCE_MASK |\r
473                         AVR32_USART_CSR_LINIPE_MASK |\r
474                         AVR32_USART_CSR_LINISFE_MASK |\r
475                         AVR32_USART_CSR_LINBE_MASK)) >> AVR32_USART_CSR_LINBE_OFFSET;\r
476 }\r
477 \r
478 #endif  // USART rev. >= 4.0.0\r
479 \r
480 //! @}\r
481 \r
482 \r
483 //------------------------------------------------------------------------------\r
484 /*! \name ISO7816 Control Functions\r
485  */\r
486 //! @{\r
487 \r
488 /*! \brief Enables the ISO7816 receiver.\r
489  *\r
490  * The ISO7816 transmitter is disabled.\r
491  *\r
492  * \param usart   Base address of the USART instance.\r
493  */\r
494 #if (defined __GNUC__)\r
495 __attribute__((__always_inline__))\r
496 #endif\r
497 extern __inline__ void usart_iso7816_enable_receiver(volatile avr32_usart_t *usart)\r
498 {\r
499   usart->cr = AVR32_USART_CR_TXDIS_MASK | AVR32_USART_CR_RXEN_MASK;\r
500 }\r
501 \r
502 /*! \brief Enables the ISO7816 transmitter.\r
503  *\r
504  * The ISO7816 receiver is disabled.\r
505  *\r
506  * \param usart   Base address of the USART instance.\r
507  */\r
508 #if (defined __GNUC__)\r
509 __attribute__((__always_inline__))\r
510 #endif\r
511 extern __inline__ void usart_iso7816_enable_transmitter(volatile avr32_usart_t *usart)\r
512 {\r
513   usart->cr = AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXEN_MASK;\r
514 }\r
515 \r
516 //! @}\r
517 \r
518 \r
519 //------------------------------------------------------------------------------\r
520 #if defined(AVR32_USART_400_H_INCLUDED) || \\r
521     defined(AVR32_USART_410_H_INCLUDED) || \\r
522     defined(AVR32_USART_420_H_INCLUDED) || \\r
523     defined(AVR32_USART_440_H_INCLUDED) || \\r
524     defined(AVR32_USART_602_H_INCLUDED)\r
525 \r
526 /*! \name LIN Control Functions\r
527  */\r
528 //! @{\r
529 \r
530 /*! \brief Sets the node action.\r
531  *\r
532  * \param usart   Base address of the USART instance.\r
533  * \param action  The node action: \ref USART_LIN_PUBLISH_ACTION,\r
534  *                \ref USART_LIN_SUBSCRIBE_ACTION or\r
535  *                \ref USART_LIN_IGNORE_ACTION.\r
536  */\r
537 #if (defined __GNUC__)\r
538 __attribute__((__always_inline__))\r
539 #endif\r
540 extern __inline__ void usart_lin_set_node_action(volatile avr32_usart_t *usart, unsigned char action)\r
541 {\r
542   usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_NACT_MASK) |\r
543                  action << AVR32_USART_LINMR_NACT_OFFSET;\r
544 }\r
545 \r
546 /*! \brief Enables or disables the Identifier parity.\r
547  *\r
548  * \param usart   Base address of the USART instance.\r
549  * \param parity  Whether to enable the Identifier parity: \c TRUE or \c FALSE.\r
550  */\r
551 #if (defined __GNUC__)\r
552 __attribute__((__always_inline__))\r
553 #endif\r
554 extern __inline__ void usart_lin_enable_parity(volatile avr32_usart_t *usart, unsigned char parity)\r
555 {\r
556   usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_PARDIS_MASK) |\r
557                  !parity << AVR32_USART_LINMR_PARDIS_OFFSET;\r
558 }\r
559 \r
560 /*! \brief Enables or disables the checksum.\r
561  *\r
562  * \param usart   Base address of the USART instance.\r
563  * \param parity  Whether to enable the checksum: \c TRUE or \c FALSE.\r
564  */\r
565 #if (defined __GNUC__)\r
566 __attribute__((__always_inline__))\r
567 #endif\r
568 extern __inline__ void usart_lin_enable_checksum(volatile avr32_usart_t *usart, unsigned char checksum)\r
569 {\r
570   usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKDIS_MASK) |\r
571                  !checksum << AVR32_USART_LINMR_CHKDIS_OFFSET;\r
572 }\r
573 \r
574 /*! \brief Sets the checksum type.\r
575  *\r
576  * \param usart   Base address of the USART instance.\r
577  * \param chktyp  The checksum type: \ref USART_LIN_ENHANCED_CHEKSUM or\r
578  *                \ref USART_LIN_CLASSIC_CHECKSUM.\r
579  */\r
580 #if (defined __GNUC__)\r
581 __attribute__((__always_inline__))\r
582 #endif\r
583 extern __inline__ void usart_lin_set_checksum(volatile avr32_usart_t *usart, unsigned char chktyp)\r
584 {\r
585   usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKTYP_MASK) |\r
586                  chktyp << AVR32_USART_LINMR_CHKTYP_OFFSET;\r
587 }\r
588 \r
589 /*! \brief Gets the response data length.\r
590  *\r
591  * \param usart   Base address of the USART instance.\r
592  *\r
593  * \return The response data length.\r
594  */\r
595 #if (defined __GNUC__)\r
596 __attribute__((__always_inline__))\r
597 #endif\r
598 extern __inline__ unsigned char usart_lin_get_data_length(volatile avr32_usart_t *usart)\r
599 {\r
600   if (usart->linmr & AVR32_USART_LINMR_DLM_MASK)\r
601   {\r
602     unsigned char data_length = 1 << ((usart->linir >> (AVR32_USART_LINIR_IDCHR_OFFSET + 4)) & 0x03);\r
603     if (data_length == 1)\r
604       data_length = 2;\r
605     return data_length;\r
606   }\r
607   else\r
608     return ((usart->linmr & AVR32_USART_LINMR_DLC_MASK) >> AVR32_USART_LINMR_DLC_OFFSET) + 1;\r
609 }\r
610 \r
611 /*! \brief Sets the response data length for LIN 1.x.\r
612  *\r
613  * \param usart   Base address of the USART instance.\r
614  */\r
615 #if (defined __GNUC__)\r
616 __attribute__((__always_inline__))\r
617 #endif\r
618 extern __inline__ void usart_lin_set_data_length_lin1x(volatile avr32_usart_t *usart)\r
619 {\r
620   usart->linmr |= AVR32_USART_LINMR_DLM_MASK;\r
621 }\r
622 \r
623 /*! \brief Sets the response data length for LIN 2.x.\r
624  *\r
625  * \param usart         Base address of the USART instance.\r
626  * \param data_length   The response data length.\r
627  */\r
628 #if (defined __GNUC__)\r
629 __attribute__((__always_inline__))\r
630 #endif\r
631 extern __inline__ void usart_lin_set_data_length_lin2x(volatile avr32_usart_t *usart, unsigned char data_length)\r
632 {\r
633   usart->linmr = (usart->linmr & ~(AVR32_USART_LINMR_DLC_MASK |\r
634                                    AVR32_USART_LINMR_DLM_MASK)) |\r
635                  (data_length - 1) << AVR32_USART_LINMR_DLC_OFFSET;\r
636 }\r
637 \r
638 /*! \brief Enables or disables the frame slot mode.\r
639  *\r
640  * \param usart       Base address of the USART instance.\r
641  * \param frameslot   Whether to enable the frame slot mode: \c TRUE or\r
642  *                    \c FALSE.\r
643  */\r
644 #if (defined __GNUC__)\r
645 __attribute__((__always_inline__))\r
646 #endif\r
647 extern __inline__ void usart_lin_enable_frameslot(volatile avr32_usart_t *usart, unsigned char frameslot)\r
648 {\r
649   usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_FSDIS_MASK) |\r
650                  !frameslot << AVR32_USART_LINMR_FSDIS_OFFSET;\r
651 }\r
652 \r
653 /*! \brief Gets the Identifier character.\r
654  *\r
655  * \param usart   Base address of the USART instance.\r
656  *\r
657  * \return The Identifier character.\r
658  */\r
659 #if (defined __GNUC__)\r
660 __attribute__((__always_inline__))\r
661 #endif\r
662 extern __inline__ unsigned char usart_lin_get_id_char(volatile avr32_usart_t *usart)\r
663 {\r
664   return (usart->linir & AVR32_USART_LINIR_IDCHR_MASK) >> AVR32_USART_LINIR_IDCHR_OFFSET;\r
665 }\r
666 \r
667 /*! \brief Sets the Identifier character.\r
668  *\r
669  * \param usart     Base address of the USART instance.\r
670  * \param id_char   The Identifier character.\r
671  */\r
672 #if (defined __GNUC__)\r
673 __attribute__((__always_inline__))\r
674 #endif\r
675 extern __inline__ void usart_lin_set_id_char(volatile avr32_usart_t *usart, unsigned char id_char)\r
676 {\r
677   usart->linir = (usart->linir & ~AVR32_USART_LINIR_IDCHR_MASK) |\r
678                  id_char << AVR32_USART_LINIR_IDCHR_OFFSET;\r
679 }\r
680 \r
681 //! @}\r
682 \r
683 #endif  // USART rev. >= 4.0.0\r
684 \r
685 \r
686 //------------------------------------------------------------------------------\r
687 #if defined(AVR32_USART_400_H_INCLUDED) || \\r
688     defined(AVR32_USART_410_H_INCLUDED) || \\r
689     defined(AVR32_USART_420_H_INCLUDED) || \\r
690     defined(AVR32_USART_440_H_INCLUDED) || \\r
691     defined(AVR32_USART_602_H_INCLUDED)\r
692 \r
693 /*! \name SPI Control Functions\r
694  */\r
695 //! @{\r
696 \r
697 /*! \brief Selects SPI slave chip.\r
698  *\r
699  * \param usart   Base address of the USART instance.\r
700  *\r
701  * \retval USART_SUCCESS        Success.\r
702  */\r
703 extern int usart_spi_selectChip(volatile avr32_usart_t *usart);\r
704 \r
705 /*! \brief Unselects SPI slave chip.\r
706  *\r
707  * \param usart   Base address of the USART instance.\r
708  *\r
709  * \retval USART_SUCCESS        Success.\r
710  * \retval USART_FAILURE        Time-out.\r
711  */\r
712 extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);\r
713 \r
714 //! @}\r
715 \r
716 #endif  // USART rev. >= 4.0.0\r
717 \r
718 \r
719 //------------------------------------------------------------------------------\r
720 /*! \name Transmit/Receive Functions\r
721  */\r
722 //! @{\r
723 \r
724 /*! \brief Addresses a receiver.\r
725  *\r
726  * While in RS485 mode, receivers only accept data addressed to them.\r
727  * A packet/char with the address tag set has to precede any data.\r
728  * This function is used to address a receiver. This receiver should read\r
729  * all the following data, until an address packet addresses another receiver.\r
730  *\r
731  * \param usart     Base address of the USART instance.\r
732  * \param address   Address of the target device.\r
733  *\r
734  * \retval USART_SUCCESS    Address successfully sent (if current mode is RS485).\r
735  * \retval USART_MODE_FAULT Wrong operating mode.\r
736  */\r
737 extern int usart_send_address(volatile avr32_usart_t *usart, int address);\r
738 \r
739 /*! \brief Tests if the USART is ready to transmit a character.\r
740  *\r
741  * \param usart   Base address of the USART instance.\r
742  *\r
743  * \return \c 1 if the USART Transmit Holding Register is free, otherwise \c 0.\r
744  */\r
745 #if (defined __GNUC__)\r
746 __attribute__((__always_inline__))\r
747 #endif\r
748 extern __inline__ int usart_tx_ready(volatile avr32_usart_t *usart)\r
749 {\r
750   return (usart->csr & AVR32_USART_CSR_TXRDY_MASK) != 0;\r
751 }\r
752 \r
753 /*! \brief Writes the given character to the TX buffer if the transmitter is ready.\r
754  *\r
755  * \param usart   Base address of the USART instance.\r
756  * \param c       The character (up to 9 bits) to transmit.\r
757  *\r
758  * \retval USART_SUCCESS  The transmitter was ready.\r
759  * \retval USART_TX_BUSY  The transmitter was busy.\r
760  */\r
761 extern int usart_write_char(volatile avr32_usart_t *usart, int c);\r
762 \r
763 /*! \brief An active wait writing a character to the USART.\r
764  *\r
765  * \param usart   Base address of the USART instance.\r
766  * \param c       The character (up to 9 bits) to transmit.\r
767  */\r
768 #if (defined __GNUC__)\r
769 __attribute__((__always_inline__))\r
770 #endif\r
771 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)\r
772 {\r
773   while (usart_write_char(usart, c) != USART_SUCCESS);\r
774 }\r
775 \r
776 /*! \brief Sends a character with the USART.\r
777  *\r
778  * \param usart   Base address of the USART instance.\r
779  * \param c       Character to write.\r
780  *\r
781  * \retval USART_SUCCESS  The character was written.\r
782  * \retval USART_FAILURE  The function timed out before the USART transmitter became ready to send.\r
783  */\r
784 extern int usart_putchar(volatile avr32_usart_t *usart, int c);\r
785 \r
786 /*! \brief Tests if all requested USART transmissions are over.\r
787  *\r
788  * \param usart   Base address of the USART instance.\r
789  *\r
790  * \return \c 1 if the USART Transmit Shift Register and the USART Transmit\r
791  *         Holding Register are free, otherwise \c 0.\r
792  */\r
793 #if (defined __GNUC__)\r
794 __attribute__((__always_inline__))\r
795 #endif\r
796 extern __inline__ int usart_tx_empty(volatile avr32_usart_t *usart)\r
797 {\r
798   return (usart->csr & AVR32_USART_CSR_TXEMPTY_MASK) != 0;\r
799 }\r
800 \r
801 /*! \brief Tests if the USART contains a received character.\r
802  *\r
803  * \param usart   Base address of the USART instance.\r
804  *\r
805  * \return \c 1 if the USART Receive Holding Register is full, otherwise \c 0.\r
806  */\r
807 #if (defined __GNUC__)\r
808 __attribute__((__always_inline__))\r
809 #endif\r
810 extern __inline__ int usart_test_hit(volatile avr32_usart_t *usart)\r
811 {\r
812   return (usart->csr & AVR32_USART_CSR_RXRDY_MASK) != 0;\r
813 }\r
814 \r
815 /*! \brief Checks the RX buffer for a received character, and stores it at the\r
816  *         given memory location.\r
817  *\r
818  * \param usart   Base address of the USART instance.\r
819  * \param c       Pointer to the where the read character should be stored\r
820  *                (must be at least short in order to accept 9-bit characters).\r
821  *\r
822  * \retval USART_SUCCESS  The character was read successfully.\r
823  * \retval USART_RX_EMPTY The RX buffer was empty.\r
824  * \retval USART_RX_ERROR An error was deteceted.\r
825  */\r
826 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);\r
827 \r
828 /*! \brief Waits until a character is received, and returns it.\r
829  *\r
830  * \param usart   Base address of the USART instance.\r
831  *\r
832  * \return The received character, or \ref USART_FAILURE upon error.\r
833  */\r
834 extern int usart_getchar(volatile avr32_usart_t *usart);\r
835 \r
836 /*! \brief Writes one character string to the USART.\r
837  *\r
838  * \param usart   Base address of the USART instance.\r
839  * \param string  String to be written.\r
840  */\r
841 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);\r
842 \r
843 /*! \brief Gets and echoes characters until end of line.\r
844  *\r
845  * \param usart   Base address of the USART instance.\r
846  *\r
847  * \retval USART_SUCCESS  Success.\r
848  * \retval USART_FAILURE  Low-level error detected or ETX character received.\r
849  */\r
850 extern int usart_get_echo_line(volatile avr32_usart_t *usart);\r
851 \r
852 #if defined(AVR32_USART_400_H_INCLUDED) || \\r
853     defined(AVR32_USART_410_H_INCLUDED) || \\r
854     defined(AVR32_USART_420_H_INCLUDED) || \\r
855     defined(AVR32_USART_440_H_INCLUDED) || \\r
856     defined(AVR32_USART_602_H_INCLUDED)\r
857 \r
858 /*! \brief Abort LIN transmission.\r
859  *\r
860  * \param usart   Base address of the USART instance.\r
861  */\r
862 #if (defined __GNUC__)\r
863 __attribute__((__always_inline__))\r
864 #endif\r
865 extern __inline__ void usart_lin_abort(volatile avr32_usart_t *usart)\r
866 {\r
867   usart->cr = AVR32_USART_LINABT_MASK;\r
868 }\r
869 \r
870 /*! \brief Tests if a LIN transfer has been completed.\r
871  *\r
872  * \param usart   Base address of the USART instance.\r
873  *\r
874  * \return \c 1 if a LIN transfer has been completed, otherwise \c 0.\r
875  */\r
876 #if (defined __GNUC__)\r
877 __attribute__((__always_inline__))\r
878 #endif\r
879 extern __inline__ int usart_lin_transfer_completed(volatile avr32_usart_t *usart)\r
880 {\r
881   return (usart->csr & AVR32_USART_CSR_LINTC_MASK) != 0;\r
882 }\r
883 \r
884 #endif  // USART rev. >= 4.0.0\r
885 \r
886 //! @}\r
887 \r
888 \r
889 #endif  // _USART_H_\r