]> git.sur5r.net Git - freertos/blob - Demo/AVR32_UC3/DRIVERS/USART/usart.h
Add AVR32 port and demo files.
[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 email: avr32@atmel.com\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 #if __GNUC__\r
50 #  include <avr32/io.h>\r
51 #elif __ICCAVR32__\r
52 #  include <avr32/iouc3a0512.h>\r
53 #else\r
54 #  error Unknown compiler\r
55 #endif\r
56 \r
57 #include "compiler.h"\r
58 \r
59 \r
60 /*! \name Return Values\r
61  */\r
62 //! @{\r
63 #define USART_SUCCESS           0 //!< Successful completion.\r
64 #define USART_FAILURE          -1 //!< Failure because of some unspecified reason.\r
65 #define USART_INVALID_INPUT     1 //!< Input value out of range.\r
66 #define USART_INVALID_ARGUMENT -1 //!< Argument value out of range.\r
67 #define USART_TX_BUSY           2 //!< Transmitter was busy.\r
68 #define USART_RX_EMPTY          3 //!< Nothing was received.\r
69 #define USART_RX_ERROR          4 //!< Transmission error occurred.\r
70 #define USART_MODE_FAULT        5 //!< USART not in the appropriate mode.\r
71 //! @}\r
72 \r
73 //! Default time-out value (number of attempts).\r
74 #define USART_DEFAULT_TIMEOUT   10000\r
75 \r
76 /*! \name Parity Settings\r
77  */\r
78 //! @{\r
79 #define USART_EVEN_PARITY       AVR32_USART_MR_PAR_EVEN   //!< Use even parity on character transmission.\r
80 #define USART_ODD_PARITY        AVR32_USART_MR_PAR_ODD    //!< Use odd parity on character transmission.\r
81 #define USART_SPACE_PARITY      AVR32_USART_MR_PAR_SPACE  //!< Use a space as parity bit.\r
82 #define USART_MARK_PARITY       AVR32_USART_MR_PAR_MARK   //!< Use a mark as parity bit.\r
83 #define USART_NO_PARITY         AVR32_USART_MR_PAR_NONE   //!< Don't use a parity bit.\r
84 #define USART_MULTIDROP_PARITY  AVR32_USART_MR_PAR_MULTI  //!< Parity bit is used to flag address characters.\r
85 //! @}\r
86 \r
87 /*! \name Operating Modes\r
88  */\r
89 //! @{\r
90 #define USART_MODE_NORMAL       AVR32_USART_MR_MODE_NORMAL      //!< Normal RS232 mode.\r
91 #define USART_MODE_RS485        AVR32_USART_MR_MODE_RS485       //!< RS485 mode.\r
92 #define USART_MODE_HW_HSH       AVR32_USART_MR_MODE_HARDWARE    //!< RS232 mode with hardware handshaking.\r
93 #define USART_MODE_MODEM        AVR32_USART_MR_MODE_MODEM       //!< Modem mode.\r
94 #define USART_MODE_ISO7816_T0   AVR32_USART_MR_MODE_ISO7816_T0  //!< ISO7816, T = 0 mode.\r
95 #define USART_MODE_ISO7816_T1   AVR32_USART_MR_MODE_ISO7816_T1  //!< ISO7816, T = 1 mode.\r
96 #define USART_MODE_IRDA         AVR32_USART_MR_MODE_IRDA        //!< IrDA mode.\r
97 #define USART_MODE_SW_HSH       AVR32_USART_MR_MODE_SOFTWARE    //!< RS232 mode with software handshaking.\r
98 //! @}\r
99 \r
100 /*! \name Channel Modes\r
101  */\r
102 //! @{\r
103 #define USART_NORMAL_CHMODE     AVR32_USART_MR_CHMODE_NORMAL      //!< Normal communication.\r
104 #define USART_AUTO_ECHO         AVR32_USART_MR_CHMODE_ECHO        //!< Echo data.\r
105 #define USART_LOCAL_LOOPBACK    AVR32_USART_MR_CHMODE_LOCAL_LOOP  //!< Local loopback.\r
106 #define USART_REMOTE_LOOPBACK   AVR32_USART_MR_CHMODE_REMOTE_LOOP //!< Remote loopback.\r
107 //! @}\r
108 \r
109 /*! \name Stop Bits Settings\r
110  */\r
111 //! @{\r
112 #define USART_1_STOPBIT         AVR32_USART_MR_NBSTOP_1   //!< Use 1 stop bit.\r
113 #define USART_1_5_STOPBITS      AVR32_USART_MR_NBSTOP_1_5 //!< Use 1.5 stop bits.\r
114 #define USART_2_STOPBITS        AVR32_USART_MR_NBSTOP_2   //!< Use 2 stop bits (for more, just give the number of bits).\r
115 //! @}\r
116 \r
117 \r
118 //! Input parameters when initializing RS232 and similar modes.\r
119 typedef struct\r
120 {\r
121   //! Set baudrate of the USART.\r
122   unsigned long baudrate;\r
123 \r
124   //! Number of bits to transmit as a character (5 to 9).\r
125   unsigned char charlength;\r
126 \r
127   //! How to calculate the parity bit: \ref USART_EVEN_PARITY, \ref USART_ODD_PARITY,\r
128   //! \ref USART_SPACE_PARITY, \ref USART_MARK_PARITY, \ref USART_NO_PARITY or\r
129   //! \ref USART_MULTIDROP_PARITY.\r
130   unsigned char paritytype;\r
131 \r
132   //! Number of stop bits between two characters: \ref USART_1_STOPBIT,\r
133   //! \ref USART_1_5_STOPBITS, \ref USART_2_STOPBITS or any number from 3 to 257\r
134   //! which will result in a time guard period of that length between characters.\r
135   unsigned short stopbits;\r
136 \r
137   //! Run the channel in testmode: \ref USART_NORMAL_CHMODE, \ref USART_AUTO_ECHO,\r
138   //! \ref USART_LOCAL_LOOPBACK or \ref USART_REMOTE_LOOPBACK.\r
139   unsigned char channelmode;\r
140 } usart_options_t;\r
141 \r
142 //! Input parameters when initializing ISO7816 modes.\r
143 typedef struct\r
144 {\r
145   //! Set the frequency of the ISO7816 clock.\r
146   unsigned long iso7816_hz;\r
147 \r
148   //! The number of ISO7816 clock ticks in every bit period (1 to 2047, 0 = disable clock).\r
149   //! Bit rate = \ref iso7816_hz / \ref fidi_ratio.\r
150   unsigned short fidi_ratio;\r
151 \r
152   //! Inhibit Non Acknowledge:\n\r
153   //!   - 0: the NACK is generated;\n\r
154   //!   - 1: the NACK is not generated.\r
155   //!\r
156   //! \note This bit will be used only in ISO7816 mode, protocol T = 0 receiver.\r
157   int inhibit_nack;\r
158 \r
159   //! Disable successive NACKs.\r
160   //! Successive parity errors are counted up to the value in the \ref max_iterations field.\r
161   //! These parity errors generate a NACK on the ISO line. As soon as this value is reached,\r
162   //! no addititional NACK is sent on the ISO line. The ITERATION flag is asserted.\r
163   int dis_suc_nack;\r
164 \r
165   //! Max number of repetitions (0 to 7).\r
166   unsigned char max_iterations;\r
167 \r
168   //! Bit order in transmitted characters:\n\r
169   //!   - 0: LSB first;\n\r
170   //!   - 1: MSB first.\r
171   int bit_order;\r
172 } iso7816_options_t;\r
173 \r
174 \r
175 //------------------------------------------------------------------------------\r
176 /*! \name Initialization Functions\r
177  */\r
178 //! @{\r
179 \r
180 /*! \brief Resets the USART and disables TX and RX.\r
181  *\r
182  * \param usart Base address of the USART instance.\r
183  */\r
184 extern void usart_reset(volatile avr32_usart_t *usart);\r
185 \r
186 /*! \brief Sets up the USART to use the standard RS232 protocol.\r
187  *\r
188  * \param usart   Base address of the USART instance.\r
189  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
190  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
191  *\r
192  * \retval USART_SUCCESS        Mode successfully initialized.\r
193  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
194  */\r
195 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
196 \r
197 /*! \brief Sets up the USART to use hardware handshaking.\r
198  *\r
199  * \param usart   Base address of the USART instance.\r
200  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
201  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
202  *\r
203  * \retval USART_SUCCESS        Mode successfully initialized.\r
204  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
205  *\r
206  * \note \ref usart_init_rs232 does not need to be invoked before this function.\r
207  */\r
208 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
209 \r
210 /*! \brief Sets up the USART to use the IrDA protocol.\r
211  *\r
212  * \param usart       Base address of the USART instance.\r
213  * \param opt         Options needed to set up RS232 communication (see \ref usart_options_t).\r
214  * \param pba_hz      USART module input clock frequency (PBA clock, Hz).\r
215  * \param irda_filter Counter used to distinguish received ones from zeros.\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 extern int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,\r
221                            long pba_hz, unsigned char irda_filter);\r
222 \r
223 /*! \brief Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.\r
224  *\r
225  * \param usart   Base address of the USART instance.\r
226  * \param opt     Options needed to set up RS232 communication (see \ref usart_options_t).\r
227  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
228  *\r
229  * \retval USART_SUCCESS        Mode successfully initialized.\r
230  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
231  */\r
232 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
233 \r
234 /*! \brief Sets up the USART to use the RS485 protocol.\r
235  *\r
236  * \param usart       Base address of the USART instance.\r
237  * \param opt         Options needed to set up RS232 communication (see \ref usart_options_t).\r
238  * \param pba_hz      USART module input clock frequency (PBA clock, Hz).\r
239  *\r
240  * \retval USART_SUCCESS        Mode successfully initialized.\r
241  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
242  */\r
243 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);\r
244 \r
245 /*! \brief Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.\r
246  *\r
247  * \param usart   Base address of the USART instance.\r
248  * \param opt     Options needed to set up ISO7816 communication (see \ref iso7816_options_t).\r
249  * \param t       ISO7816 mode to use (T=0 or T=1).\r
250  * \param pba_hz  USART module input clock frequency (PBA clock, Hz).\r
251  *\r
252  * \retval USART_SUCCESS        Mode successfully initialized.\r
253  * \retval USART_INVALID_INPUT  One or more of the arguments is out of valid range.\r
254  */\r
255 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const iso7816_options_t *opt, int t, long pba_hz);\r
256 \r
257 //! @}\r
258 \r
259 \r
260 //------------------------------------------------------------------------------\r
261 /*! \name Read and Reset Error Status Bits\r
262  */\r
263 //! @{\r
264 \r
265 /*! \brief Resets the error status.\r
266  *\r
267  * This function resets the status bits indicating that a parity error,\r
268  * framing error or overrun has occurred. The RXBRK bit, indicating\r
269  * a start/end of break condition on the RX line, is also reset.\r
270  *\r
271  * \param usart Base address of the USART instance.\r
272  */\r
273 #if __GNUC__\r
274 __attribute__((__always_inline__))\r
275 #endif\r
276 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)\r
277 {\r
278   usart->cr |= AVR32_USART_CR_RSTSTA_MASK;\r
279 }\r
280 \r
281 /*! \brief Checks if a parity error has occurred since last status reset.\r
282  *\r
283  * \param usart Base address of the USART instance.\r
284  *\r
285  * \return \c 1 if a parity error has been detected, otherwise \c 0.\r
286  */\r
287 #if __GNUC__\r
288 __attribute__((__always_inline__))\r
289 #endif\r
290 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)\r
291 {\r
292   return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;\r
293 }\r
294 \r
295 /*! \brief Checks if a framing error has occurred since last status reset.\r
296  *\r
297  * \param usart Base address of the USART instance.\r
298  *\r
299  * \return \c 1 if a framing error has been detected, otherwise \c 0.\r
300  */\r
301 #if __GNUC__\r
302 __attribute__((__always_inline__))\r
303 #endif\r
304 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)\r
305 {\r
306   return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;\r
307 }\r
308 \r
309 /*! \brief Checks if an overrun error has occurred since last status reset.\r
310  *\r
311  * \param usart Base address of the USART instance.\r
312  *\r
313  * \return \c 1 if a overrun error has been detected, otherwise \c 0.\r
314  */\r
315 #if __GNUC__\r
316 __attribute__((__always_inline__))\r
317 #endif\r
318 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)\r
319 {\r
320   return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;\r
321 }\r
322 \r
323 //! @}\r
324 \r
325 \r
326 //------------------------------------------------------------------------------\r
327 /*! \name Transmit/Receive Functions\r
328  */\r
329 //! @{\r
330 \r
331 /*! \brief Addresses a receiver.\r
332  *\r
333  * While in RS485 mode, receivers only accept data addressed to them.\r
334  * A packet/char with the address tag set has to precede any data.\r
335  * This function is used to address a receiver. This receiver should read\r
336  * all the following data, until an address packet addresses another receiver.\r
337  *\r
338  * \param usart   Base address of the USART instance.\r
339  * \param address Address of the target device.\r
340  *\r
341  * \retval USART_SUCCESS    Address successfully sent (if current mode is RS485).\r
342  * \retval USART_MODE_FAULT Wrong operating mode.\r
343  */\r
344 extern int usart_send_address(volatile avr32_usart_t *usart, int address);\r
345 \r
346 /*! \brief Writes the given character to the TX buffer if the transmitter is ready.\r
347  *\r
348  * \param usart Base address of the USART instance.\r
349  * \param c     The character (up to 9 bits) to transmit.\r
350  *\r
351  * \retval USART_SUCCESS  The transmitter was ready.\r
352  * \retval USART_TX_BUSY  The transmitter was busy.\r
353  */\r
354 extern int usart_write_char(volatile avr32_usart_t *usart, int c);\r
355 \r
356 /*! \brief An active wait writing a character to the USART.\r
357  *\r
358  * \param usart Base address of the USART instance.\r
359  * \param c     The character (up to 9 bits) to transmit.\r
360  */\r
361 #if __GNUC__\r
362 __attribute__((__always_inline__))\r
363 #endif\r
364 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)\r
365 {\r
366   while (usart_write_char(usart, c) != USART_SUCCESS);\r
367 }\r
368 \r
369 /*! \brief Sends a character with the USART.\r
370  *\r
371  * \param usart Base address of the USART instance.\r
372  * \param c     Character to write.\r
373  *\r
374  * \retval USART_SUCCESS  The character was written.\r
375  * \retval USART_FAILURE  The function timed out before the USART transmitter became ready to send.\r
376  */\r
377 extern int usart_putchar(volatile avr32_usart_t *usart, int c);\r
378 \r
379 /*! \brief Checks the RX buffer for a received character, and stores it at the\r
380  *         given memory location.\r
381  *\r
382  * \param usart Base address of the USART instance.\r
383  * \param c     Pointer to the where the read character should be stored\r
384  *              (must be at least short in order to accept 9-bit characters).\r
385  *\r
386  * \retval USART_SUCCESS  The character was read successfully.\r
387  * \retval USART_RX_EMPTY The RX buffer was empty.\r
388  * \retval USART_RX_ERROR An error was deteceted.\r
389  */\r
390 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);\r
391 \r
392 /*! \brief Waits until a character is received, and returns it.\r
393  *\r
394  * \param usart Base address of the USART instance.\r
395  *\r
396  * \return The received character, or \ref USART_FAILURE upon error.\r
397  */\r
398 extern int usart_getchar(volatile avr32_usart_t *usart);\r
399 \r
400 /*! \brief Writes one character string to the USART.\r
401  *\r
402  * \param usart   Base address of the USART instance.\r
403  * \param string  String to be written.\r
404  */\r
405 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);\r
406 \r
407 /*! \brief Gets and echoes characters until end of line.\r
408  *\r
409  * \param usart Base address of the USART instance.\r
410  *\r
411  * \retval USART_SUCCESS  Success.\r
412  * \retval USART_FAILURE  ETX character received.\r
413  */\r
414 extern int usart_get_echo_line(volatile avr32_usart_t *usart);\r
415 \r
416 //! @}\r
417 \r
418 \r
419 #endif  // _USART_H_\r