1 /*This file is prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
4 * \brief USART driver for AVR32 UC3.
\r
6 * This file contains basic functions for the AVR32 USART, with support for all
\r
7 * modes, settings and clock speeds.
\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
13 * \author Atmel Corporation: http://www.atmel.com \n
\r
14 * Support email: avr32@atmel.com
\r
16 ******************************************************************************/
\r
18 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
\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
23 * 1. Redistributions of source code must retain the above copyright notice,
\r
24 * this list of conditions and the following disclaimer.
\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
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
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
50 # include <avr32/io.h>
\r
52 # include <avr32/iouc3a0512.h>
\r
54 # error Unknown compiler
\r
57 #include "compiler.h"
\r
60 /*! \name Return Values
\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
73 //! Default time-out value (number of attempts).
\r
74 #define USART_DEFAULT_TIMEOUT 10000
\r
76 /*! \name Parity Settings
\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
87 /*! \name Operating Modes
\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
100 /*! \name Channel Modes
\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
109 /*! \name Stop Bits Settings
\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
118 //! Input parameters when initializing RS232 and similar modes.
\r
121 //! Set baudrate of the USART.
\r
122 unsigned long baudrate;
\r
124 //! Number of bits to transmit as a character (5 to 9).
\r
125 unsigned char charlength;
\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
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
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
142 //! Input parameters when initializing ISO7816 modes.
\r
145 //! Set the frequency of the ISO7816 clock.
\r
146 unsigned long iso7816_hz;
\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
152 //! Inhibit Non Acknowledge:\n
\r
153 //! - 0: the NACK is generated;\n
\r
154 //! - 1: the NACK is not generated.
\r
156 //! \note This bit will be used only in ISO7816 mode, protocol T = 0 receiver.
\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
165 //! Max number of repetitions (0 to 7).
\r
166 unsigned char max_iterations;
\r
168 //! Bit order in transmitted characters:\n
\r
169 //! - 0: LSB first;\n
\r
170 //! - 1: MSB first.
\r
172 } iso7816_options_t;
\r
175 //------------------------------------------------------------------------------
\r
176 /*! \name Initialization Functions
\r
180 /*! \brief Resets the USART and disables TX and RX.
\r
182 * \param usart Base address of the USART instance.
\r
184 extern void usart_reset(volatile avr32_usart_t *usart);
\r
186 /*! \brief Sets up the USART to use the standard RS232 protocol.
\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
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
195 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
197 /*! \brief Sets up the USART to use hardware handshaking.
\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
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
206 * \note \ref usart_init_rs232 does not need to be invoked before this function.
\r
208 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
210 /*! \brief Sets up the USART to use the IrDA protocol.
\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
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
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
223 /*! \brief Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.
\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
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
232 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
234 /*! \brief Sets up the USART to use the RS485 protocol.
\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
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
243 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
245 /*! \brief Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.
\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
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
255 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const iso7816_options_t *opt, int t, long pba_hz);
\r
260 //------------------------------------------------------------------------------
\r
261 /*! \name Read and Reset Error Status Bits
\r
265 /*! \brief Resets the error status.
\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
271 * \param usart Base address of the USART instance.
\r
274 __attribute__((__always_inline__))
\r
276 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)
\r
278 usart->cr |= AVR32_USART_CR_RSTSTA_MASK;
\r
281 /*! \brief Checks if a parity error has occurred since last status reset.
\r
283 * \param usart Base address of the USART instance.
\r
285 * \return \c 1 if a parity error has been detected, otherwise \c 0.
\r
288 __attribute__((__always_inline__))
\r
290 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)
\r
292 return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
\r
295 /*! \brief Checks if a framing error has occurred since last status reset.
\r
297 * \param usart Base address of the USART instance.
\r
299 * \return \c 1 if a framing error has been detected, otherwise \c 0.
\r
302 __attribute__((__always_inline__))
\r
304 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)
\r
306 return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
\r
309 /*! \brief Checks if an overrun error has occurred since last status reset.
\r
311 * \param usart Base address of the USART instance.
\r
313 * \return \c 1 if a overrun error has been detected, otherwise \c 0.
\r
316 __attribute__((__always_inline__))
\r
318 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)
\r
320 return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
\r
326 //------------------------------------------------------------------------------
\r
327 /*! \name Transmit/Receive Functions
\r
331 /*! \brief Addresses a receiver.
\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
338 * \param usart Base address of the USART instance.
\r
339 * \param address Address of the target device.
\r
341 * \retval USART_SUCCESS Address successfully sent (if current mode is RS485).
\r
342 * \retval USART_MODE_FAULT Wrong operating mode.
\r
344 extern int usart_send_address(volatile avr32_usart_t *usart, int address);
\r
346 /*! \brief Writes the given character to the TX buffer if the transmitter is ready.
\r
348 * \param usart Base address of the USART instance.
\r
349 * \param c The character (up to 9 bits) to transmit.
\r
351 * \retval USART_SUCCESS The transmitter was ready.
\r
352 * \retval USART_TX_BUSY The transmitter was busy.
\r
354 extern int usart_write_char(volatile avr32_usart_t *usart, int c);
\r
356 /*! \brief An active wait writing a character to the USART.
\r
358 * \param usart Base address of the USART instance.
\r
359 * \param c The character (up to 9 bits) to transmit.
\r
362 __attribute__((__always_inline__))
\r
364 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
\r
366 while (usart_write_char(usart, c) != USART_SUCCESS);
\r
369 /*! \brief Sends a character with the USART.
\r
371 * \param usart Base address of the USART instance.
\r
372 * \param c Character to write.
\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
377 extern int usart_putchar(volatile avr32_usart_t *usart, int c);
\r
379 /*! \brief Checks the RX buffer for a received character, and stores it at the
\r
380 * given memory location.
\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
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
390 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
\r
392 /*! \brief Waits until a character is received, and returns it.
\r
394 * \param usart Base address of the USART instance.
\r
396 * \return The received character, or \ref USART_FAILURE upon error.
\r
398 extern int usart_getchar(volatile avr32_usart_t *usart);
\r
400 /*! \brief Writes one character string to the USART.
\r
402 * \param usart Base address of the USART instance.
\r
403 * \param string String to be written.
\r
405 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
\r
407 /*! \brief Gets and echoes characters until end of line.
\r
409 * \param usart Base address of the USART instance.
\r
411 * \retval USART_SUCCESS Success.
\r
412 * \retval USART_FAILURE ETX character received.
\r
414 extern int usart_get_echo_line(volatile avr32_usart_t *usart);
\r
419 #endif // _USART_H_
\r