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 and FAQ: http://support.atmel.no/
\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
49 #include <avr32/io.h>
\r
50 #include "compiler.h"
\r
53 /*! \name Return Values
\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
66 //! Default time-out value (number of attempts).
\r
67 #define USART_DEFAULT_TIMEOUT 10000
\r
69 /*! \name Parity Settings
\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
80 /*! \name Operating Modes
\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
94 /*! \name Channel Modes
\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
103 /*! \name Stop Bits Settings
\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
112 //! Input parameters when initializing RS232 and similar modes.
\r
115 //! Set baudrate of the USART.
\r
116 unsigned long baudrate;
\r
118 //! Number of bits to transmit as a character (5 to 9).
\r
119 unsigned char charlength;
\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
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
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
136 //! Input parameters when initializing ISO7816 modes.
\r
139 //! Set the frequency of the ISO7816 clock.
\r
140 unsigned long iso7816_hz;
\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
146 //! Inhibit Non Acknowledge:\n
\r
147 //! - 0: the NACK is generated;\n
\r
148 //! - 1: the NACK is not generated.
\r
150 //! \note This bit will be used only in ISO7816 mode, protocol T = 0 receiver.
\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
159 //! Max number of repetitions (0 to 7).
\r
160 unsigned char max_iterations;
\r
162 //! Bit order in transmitted characters:\n
\r
163 //! - 0: LSB first;\n
\r
164 //! - 1: MSB first.
\r
166 } iso7816_options_t;
\r
168 //! Input parameters when initializing ISO7816 modes.
\r
171 //! Set the frequency of the SPI clock.
\r
172 unsigned long baudrate;
\r
174 //! Number of bits to transmit as a character (5 to 9).
\r
175 unsigned char charlength;
\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
181 //! Which SPI mode to use when transmitting.
\r
182 unsigned char spimode;
\r
183 } usart_spi_options_t;
\r
189 //------------------------------------------------------------------------------
\r
190 /*! \name Initialization Functions
\r
194 /*! \brief Resets the USART and disables TX and RX.
\r
196 * \param usart Base address of the USART instance.
\r
198 extern void usart_reset(volatile avr32_usart_t *usart);
\r
200 /*! \brief Sets up the USART to use the standard RS232 protocol.
\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
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
209 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
211 /*! \brief Sets up the USART to use hardware handshaking.
\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
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 * \note \ref usart_init_rs232 does not need to be invoked before this function.
\r
222 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
224 /*! \brief Sets up the USART to use the IrDA protocol.
\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
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
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
237 /*! \brief Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.
\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
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
246 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
248 /*! \brief Sets up the USART to use the RS485 protocol.
\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
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
257 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
259 /*! \brief Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.
\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
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
269 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const iso7816_options_t *opt, int t, long pba_hz);
\r
271 /*! \brief Sets up the USART to use the SPI mode as master.
\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
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
280 extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
\r
283 /*! \brief Sets up the USART to use the SPI mode as slave.
\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
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
292 extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
\r
296 //------------------------------------------------------------------------------
\r
297 /*! \brief Selects slave chip.
\r
299 * \param usart Base address of the USART instance.
\r
302 * \retval USART_SUCCESS Success.
\r
304 extern int usart_spi_selectChip(volatile avr32_usart_t *usart);
\r
306 /*! \brief Unselects slave chip.
\r
308 * \param usart Base address of the USART instance.
\r
311 * \retval USART_SUCCESS Success.
\r
312 * \retval USART_FAILURE Time out.
\r
314 extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);
\r
316 //------------------------------------------------------------------------------
\r
317 /*! \name Read and Reset Error Status Bits
\r
321 /*! \brief Resets the error status.
\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
327 * \param usart Base address of the USART instance.
\r
330 __attribute__((__always_inline__))
\r
332 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)
\r
334 usart->cr |= AVR32_USART_CR_RSTSTA_MASK;
\r
337 /*! \brief Checks if a parity error has occurred since last status reset.
\r
339 * \param usart Base address of the USART instance.
\r
341 * \return \c 1 if a parity error has been detected, otherwise \c 0.
\r
344 __attribute__((__always_inline__))
\r
346 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)
\r
348 return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
\r
351 /*! \brief Checks if a framing error has occurred since last status reset.
\r
353 * \param usart Base address of the USART instance.
\r
355 * \return \c 1 if a framing error has been detected, otherwise \c 0.
\r
358 __attribute__((__always_inline__))
\r
360 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)
\r
362 return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
\r
365 /*! \brief Checks if an overrun error has occurred since last status reset.
\r
367 * \param usart Base address of the USART instance.
\r
369 * \return \c 1 if a overrun error has been detected, otherwise \c 0.
\r
372 __attribute__((__always_inline__))
\r
374 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)
\r
376 return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
\r
382 //------------------------------------------------------------------------------
\r
383 /*! \name Transmit/Receive Functions
\r
387 /*! \brief Addresses a receiver.
\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
394 * \param usart Base address of the USART instance.
\r
395 * \param address Address of the target device.
\r
397 * \retval USART_SUCCESS Address successfully sent (if current mode is RS485).
\r
398 * \retval USART_MODE_FAULT Wrong operating mode.
\r
400 extern int usart_send_address(volatile avr32_usart_t *usart, int address);
\r
402 /*! \brief Writes the given character to the TX buffer if the transmitter is ready.
\r
404 * \param usart Base address of the USART instance.
\r
405 * \param c The character (up to 9 bits) to transmit.
\r
407 * \retval USART_SUCCESS The transmitter was ready.
\r
408 * \retval USART_TX_BUSY The transmitter was busy.
\r
410 extern int usart_write_char(volatile avr32_usart_t *usart, int c);
\r
412 /*! \brief An active wait writing a character to the USART.
\r
414 * \param usart Base address of the USART instance.
\r
415 * \param c The character (up to 9 bits) to transmit.
\r
418 __attribute__((__always_inline__))
\r
420 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
\r
422 while (usart_write_char(usart, c) != USART_SUCCESS);
\r
425 /*! \brief Sends a character with the USART.
\r
427 * \param usart Base address of the USART instance.
\r
428 * \param c Character to write.
\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
433 extern int usart_putchar(volatile avr32_usart_t *usart, int c);
\r
435 /*! \brief Checks the RX buffer for a received character, and stores it at the
\r
436 * given memory location.
\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
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
446 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
\r
448 /*! \brief Waits until a character is received, and returns it.
\r
450 * \param usart Base address of the USART instance.
\r
452 * \return The received character, or \ref USART_FAILURE upon error.
\r
454 extern int usart_getchar(volatile avr32_usart_t *usart);
\r
456 /*! \brief Writes one character string to the USART.
\r
458 * \param usart Base address of the USART instance.
\r
459 * \param string String to be written.
\r
461 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
\r
463 /*! \brief Gets and echoes characters until end of line.
\r
465 * \param usart Base address of the USART instance.
\r
467 * \retval USART_SUCCESS Success.
\r
468 * \retval USART_FAILURE ETX character received.
\r
470 extern int usart_get_echo_line(volatile avr32_usart_t *usart);
\r
475 #endif // _USART_H_
\r