1 /* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
\r
3 /*This file is prepared for Doxygen automatic documentation generation.*/
\r
4 /*! \file *********************************************************************
\r
6 * \brief USART driver for AVR32 UC3.
\r
8 * This file contains basic functions for the AVR32 USART, with support for all
\r
9 * modes, settings and clock speeds.
\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
15 * \author Atmel Corporation: http://www.atmel.com \n
\r
16 * Support and FAQ: http://support.atmel.no/
\r
18 ******************************************************************************/
\r
20 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
\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
25 * 1. Redistributions of source code must retain the above copyright notice, this
\r
26 * list of conditions and the following disclaimer.
\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
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
35 * 4. This software may only be redistributed and used in connection with an Atmel
\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
54 #include <avr32/io.h>
\r
55 #include "compiler.h"
\r
58 /*! \name Return Values
\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
71 //! Default time-out value (number of attempts).
\r
72 #define USART_DEFAULT_TIMEOUT 10000
\r
74 /*! \name Parity Settings
\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
85 /*! \name Stop Bits Settings
\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
93 /*! \name Channel Modes
\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
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
108 /*! \name LIN Node Actions
\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
116 /*! \name LIN Checksum Types
\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
123 #endif // USART rev. >= 4.0.0
\r
126 //! Input parameters when initializing RS232 and similar modes.
\r
129 //! Set baud rate of the USART (unused in slave modes).
\r
130 unsigned long baudrate;
\r
132 //! Number of bits to transmit as a character (5 to 9).
\r
133 unsigned char charlength;
\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
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
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
151 //! Input parameters when initializing ISO7816 mode.
\r
154 //! Set the frequency of the ISO7816 clock.
\r
155 unsigned long iso7816_hz;
\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
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
165 //! Inhibit Non Acknowledge:\n
\r
166 //! - 0: the NACK is generated;\n
\r
167 //! - 1: the NACK is not generated.
\r
169 //! \note This bit will be used only in ISO7816 mode, protocol T = 0 receiver.
\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
178 //! Max number of repetitions (0 to 7).
\r
179 unsigned char max_iterations;
\r
181 //! Bit order in transmitted characters:\n
\r
182 //! - 0: LSB first;\n
\r
183 //! - 1: MSB first.
\r
185 } usart_iso7816_options_t;
\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
193 //! Input parameters when initializing SPI mode.
\r
196 //! Set the frequency of the SPI clock (unused in slave mode).
\r
197 unsigned long baudrate;
\r
199 //! Number of bits to transmit as a character (5 to 9).
\r
200 unsigned char charlength;
\r
202 //! Which SPI mode to use.
\r
203 unsigned char spimode;
\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
210 #endif // USART rev. >= 4.0.0
\r
213 //------------------------------------------------------------------------------
\r
214 /*! \name Initialization Functions
\r
218 /*! \brief Resets the USART and disables TX and RX.
\r
220 * \param usart Base address of the USART instance.
\r
222 extern void usart_reset(volatile avr32_usart_t *usart);
\r
224 /*! \brief Sets up the USART to use the standard RS232 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
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
233 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
235 /*! \brief Sets up the USART to use the standard RS232 protocol in TX-only mode.
\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
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
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
247 * \note The \c 1.5 stop bit is not supported in this mode.
\r
249 extern int usart_init_rs232_tx_only(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
251 /*! \brief Sets up the USART to use hardware handshaking.
\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
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
260 * \note \ref usart_init_rs232 does not need to be invoked before this function.
\r
262 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
264 /*! \brief Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.
\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
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
273 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
275 /*! \brief Sets up the USART to use a synchronous RS232-like protocol in master mode.
\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
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
284 extern int usart_init_sync_master(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
286 /*! \brief Sets up the USART to use a synchronous RS232-like protocol in slave mode.
\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
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
295 extern int usart_init_sync_slave(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
297 /*! \brief Sets up the USART to use the RS485 protocol.
\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
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
306 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
\r
308 /*! \brief Sets up the USART to use the IrDA protocol.
\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
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
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
321 /*! \brief Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.
\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
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
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
335 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz);
\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
343 /*! \brief Sets up the USART to use the LIN master mode.
\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
350 extern int usart_init_lin_master(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
\r
352 /*! \brief Sets up the USART to use the LIN slave mode.
\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
359 extern int usart_init_lin_slave(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
\r
361 /*! \brief Sets up the USART to use the SPI master mode.
\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
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
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
373 extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
\r
375 /*! \brief Sets up the USART to use the SPI slave mode.
\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
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
384 extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
\r
386 #endif // USART rev. >= 4.0.0
\r
391 //------------------------------------------------------------------------------
\r
392 /*! \name Read and Reset Error Status Bits
\r
396 /*! \brief Resets the error status.
\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
402 * \param usart Base address of the USART instance.
\r
404 #if (defined __GNUC__)
\r
405 __attribute__((__always_inline__))
\r
407 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)
\r
409 usart->cr = AVR32_USART_CR_RSTSTA_MASK;
\r
412 /*! \brief Checks if a parity error has occurred since last status reset.
\r
414 * \param usart Base address of the USART instance.
\r
416 * \return \c 1 if a parity error has been detected, otherwise \c 0.
\r
418 #if (defined __GNUC__)
\r
419 __attribute__((__always_inline__))
\r
421 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)
\r
423 return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
\r
426 /*! \brief Checks if a framing error has occurred since last status reset.
\r
428 * \param usart Base address of the USART instance.
\r
430 * \return \c 1 if a framing error has been detected, otherwise \c 0.
\r
432 #if (defined __GNUC__)
\r
433 __attribute__((__always_inline__))
\r
435 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)
\r
437 return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
\r
440 /*! \brief Checks if an overrun error has occurred since last status reset.
\r
442 * \param usart Base address of the USART instance.
\r
444 * \return \c 1 if a overrun error has been detected, otherwise \c 0.
\r
446 #if (defined __GNUC__)
\r
447 __attribute__((__always_inline__))
\r
449 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)
\r
451 return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
\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
460 /*! \brief Get LIN Error Status
\r
462 * \param usart Base address of the USART instance.
\r
464 * \retval The binary value of the error field.
\r
466 #if (defined __GNUC__)
\r
467 __attribute__((__always_inline__))
\r
469 extern __inline__ int usart_lin_get_error(volatile avr32_usart_t *usart)
\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
478 #endif // USART rev. >= 4.0.0
\r
483 //------------------------------------------------------------------------------
\r
484 /*! \name ISO7816 Control Functions
\r
488 /*! \brief Enables the ISO7816 receiver.
\r
490 * The ISO7816 transmitter is disabled.
\r
492 * \param usart Base address of the USART instance.
\r
494 #if (defined __GNUC__)
\r
495 __attribute__((__always_inline__))
\r
497 extern __inline__ void usart_iso7816_enable_receiver(volatile avr32_usart_t *usart)
\r
499 usart->cr = AVR32_USART_CR_TXDIS_MASK | AVR32_USART_CR_RXEN_MASK;
\r
502 /*! \brief Enables the ISO7816 transmitter.
\r
504 * The ISO7816 receiver is disabled.
\r
506 * \param usart Base address of the USART instance.
\r
508 #if (defined __GNUC__)
\r
509 __attribute__((__always_inline__))
\r
511 extern __inline__ void usart_iso7816_enable_transmitter(volatile avr32_usart_t *usart)
\r
513 usart->cr = AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXEN_MASK;
\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
526 /*! \name LIN Control Functions
\r
530 /*! \brief Sets the node action.
\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
537 #if (defined __GNUC__)
\r
538 __attribute__((__always_inline__))
\r
540 extern __inline__ void usart_lin_set_node_action(volatile avr32_usart_t *usart, unsigned char action)
\r
542 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_NACT_MASK) |
\r
543 action << AVR32_USART_LINMR_NACT_OFFSET;
\r
546 /*! \brief Enables or disables the Identifier parity.
\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
551 #if (defined __GNUC__)
\r
552 __attribute__((__always_inline__))
\r
554 extern __inline__ void usart_lin_enable_parity(volatile avr32_usart_t *usart, unsigned char parity)
\r
556 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_PARDIS_MASK) |
\r
557 !parity << AVR32_USART_LINMR_PARDIS_OFFSET;
\r
560 /*! \brief Enables or disables the checksum.
\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
565 #if (defined __GNUC__)
\r
566 __attribute__((__always_inline__))
\r
568 extern __inline__ void usart_lin_enable_checksum(volatile avr32_usart_t *usart, unsigned char checksum)
\r
570 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKDIS_MASK) |
\r
571 !checksum << AVR32_USART_LINMR_CHKDIS_OFFSET;
\r
574 /*! \brief Sets the checksum type.
\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
580 #if (defined __GNUC__)
\r
581 __attribute__((__always_inline__))
\r
583 extern __inline__ void usart_lin_set_checksum(volatile avr32_usart_t *usart, unsigned char chktyp)
\r
585 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKTYP_MASK) |
\r
586 chktyp << AVR32_USART_LINMR_CHKTYP_OFFSET;
\r
589 /*! \brief Gets the response data length.
\r
591 * \param usart Base address of the USART instance.
\r
593 * \return The response data length.
\r
595 #if (defined __GNUC__)
\r
596 __attribute__((__always_inline__))
\r
598 extern __inline__ unsigned char usart_lin_get_data_length(volatile avr32_usart_t *usart)
\r
600 if (usart->linmr & AVR32_USART_LINMR_DLM_MASK)
\r
602 unsigned char data_length = 1 << ((usart->linir >> (AVR32_USART_LINIR_IDCHR_OFFSET + 4)) & 0x03);
\r
603 if (data_length == 1)
\r
605 return data_length;
\r
608 return ((usart->linmr & AVR32_USART_LINMR_DLC_MASK) >> AVR32_USART_LINMR_DLC_OFFSET) + 1;
\r
611 /*! \brief Sets the response data length for LIN 1.x.
\r
613 * \param usart Base address of the USART instance.
\r
615 #if (defined __GNUC__)
\r
616 __attribute__((__always_inline__))
\r
618 extern __inline__ void usart_lin_set_data_length_lin1x(volatile avr32_usart_t *usart)
\r
620 usart->linmr |= AVR32_USART_LINMR_DLM_MASK;
\r
623 /*! \brief Sets the response data length for LIN 2.x.
\r
625 * \param usart Base address of the USART instance.
\r
626 * \param data_length The response data length.
\r
628 #if (defined __GNUC__)
\r
629 __attribute__((__always_inline__))
\r
631 extern __inline__ void usart_lin_set_data_length_lin2x(volatile avr32_usart_t *usart, unsigned char data_length)
\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
638 /*! \brief Enables or disables the frame slot mode.
\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
644 #if (defined __GNUC__)
\r
645 __attribute__((__always_inline__))
\r
647 extern __inline__ void usart_lin_enable_frameslot(volatile avr32_usart_t *usart, unsigned char frameslot)
\r
649 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_FSDIS_MASK) |
\r
650 !frameslot << AVR32_USART_LINMR_FSDIS_OFFSET;
\r
653 /*! \brief Gets the Identifier character.
\r
655 * \param usart Base address of the USART instance.
\r
657 * \return The Identifier character.
\r
659 #if (defined __GNUC__)
\r
660 __attribute__((__always_inline__))
\r
662 extern __inline__ unsigned char usart_lin_get_id_char(volatile avr32_usart_t *usart)
\r
664 return (usart->linir & AVR32_USART_LINIR_IDCHR_MASK) >> AVR32_USART_LINIR_IDCHR_OFFSET;
\r
667 /*! \brief Sets the Identifier character.
\r
669 * \param usart Base address of the USART instance.
\r
670 * \param id_char The Identifier character.
\r
672 #if (defined __GNUC__)
\r
673 __attribute__((__always_inline__))
\r
675 extern __inline__ void usart_lin_set_id_char(volatile avr32_usart_t *usart, unsigned char id_char)
\r
677 usart->linir = (usart->linir & ~AVR32_USART_LINIR_IDCHR_MASK) |
\r
678 id_char << AVR32_USART_LINIR_IDCHR_OFFSET;
\r
683 #endif // USART rev. >= 4.0.0
\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
693 /*! \name SPI Control Functions
\r
697 /*! \brief Selects SPI slave chip.
\r
699 * \param usart Base address of the USART instance.
\r
701 * \retval USART_SUCCESS Success.
\r
703 extern int usart_spi_selectChip(volatile avr32_usart_t *usart);
\r
705 /*! \brief Unselects SPI slave chip.
\r
707 * \param usart Base address of the USART instance.
\r
709 * \retval USART_SUCCESS Success.
\r
710 * \retval USART_FAILURE Time-out.
\r
712 extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);
\r
716 #endif // USART rev. >= 4.0.0
\r
719 //------------------------------------------------------------------------------
\r
720 /*! \name Transmit/Receive Functions
\r
724 /*! \brief Addresses a receiver.
\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
731 * \param usart Base address of the USART instance.
\r
732 * \param address Address of the target device.
\r
734 * \retval USART_SUCCESS Address successfully sent (if current mode is RS485).
\r
735 * \retval USART_MODE_FAULT Wrong operating mode.
\r
737 extern int usart_send_address(volatile avr32_usart_t *usart, int address);
\r
739 /*! \brief Tests if the USART is ready to transmit a character.
\r
741 * \param usart Base address of the USART instance.
\r
743 * \return \c 1 if the USART Transmit Holding Register is free, otherwise \c 0.
\r
745 #if (defined __GNUC__)
\r
746 __attribute__((__always_inline__))
\r
748 extern __inline__ int usart_tx_ready(volatile avr32_usart_t *usart)
\r
750 return (usart->csr & AVR32_USART_CSR_TXRDY_MASK) != 0;
\r
753 /*! \brief Writes the given character to the TX buffer if the transmitter is ready.
\r
755 * \param usart Base address of the USART instance.
\r
756 * \param c The character (up to 9 bits) to transmit.
\r
758 * \retval USART_SUCCESS The transmitter was ready.
\r
759 * \retval USART_TX_BUSY The transmitter was busy.
\r
761 extern int usart_write_char(volatile avr32_usart_t *usart, int c);
\r
763 /*! \brief An active wait writing a character to the USART.
\r
765 * \param usart Base address of the USART instance.
\r
766 * \param c The character (up to 9 bits) to transmit.
\r
768 #if (defined __GNUC__)
\r
769 __attribute__((__always_inline__))
\r
771 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
\r
773 while (usart_write_char(usart, c) != USART_SUCCESS);
\r
776 /*! \brief Sends a character with the USART.
\r
778 * \param usart Base address of the USART instance.
\r
779 * \param c Character to write.
\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
784 extern int usart_putchar(volatile avr32_usart_t *usart, int c);
\r
786 /*! \brief Tests if all requested USART transmissions are over.
\r
788 * \param usart Base address of the USART instance.
\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
793 #if (defined __GNUC__)
\r
794 __attribute__((__always_inline__))
\r
796 extern __inline__ int usart_tx_empty(volatile avr32_usart_t *usart)
\r
798 return (usart->csr & AVR32_USART_CSR_TXEMPTY_MASK) != 0;
\r
801 /*! \brief Tests if the USART contains a received character.
\r
803 * \param usart Base address of the USART instance.
\r
805 * \return \c 1 if the USART Receive Holding Register is full, otherwise \c 0.
\r
807 #if (defined __GNUC__)
\r
808 __attribute__((__always_inline__))
\r
810 extern __inline__ int usart_test_hit(volatile avr32_usart_t *usart)
\r
812 return (usart->csr & AVR32_USART_CSR_RXRDY_MASK) != 0;
\r
815 /*! \brief Checks the RX buffer for a received character, and stores it at the
\r
816 * given memory location.
\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
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
826 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
\r
828 /*! \brief Waits until a character is received, and returns it.
\r
830 * \param usart Base address of the USART instance.
\r
832 * \return The received character, or \ref USART_FAILURE upon error.
\r
834 extern int usart_getchar(volatile avr32_usart_t *usart);
\r
836 /*! \brief Writes one character string to the USART.
\r
838 * \param usart Base address of the USART instance.
\r
839 * \param string String to be written.
\r
841 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
\r
843 /*! \brief Gets and echoes characters until end of line.
\r
845 * \param usart Base address of the USART instance.
\r
847 * \retval USART_SUCCESS Success.
\r
848 * \retval USART_FAILURE Low-level error detected or ETX character received.
\r
850 extern int usart_get_echo_line(volatile avr32_usart_t *usart);
\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
858 /*! \brief Abort LIN transmission.
\r
860 * \param usart Base address of the USART instance.
\r
862 #if (defined __GNUC__)
\r
863 __attribute__((__always_inline__))
\r
865 extern __inline__ void usart_lin_abort(volatile avr32_usart_t *usart)
\r
867 usart->cr = AVR32_USART_LINABT_MASK;
\r
870 /*! \brief Tests if a LIN transfer has been completed.
\r
872 * \param usart Base address of the USART instance.
\r
874 * \return \c 1 if a LIN transfer has been completed, otherwise \c 0.
\r
876 #if (defined __GNUC__)
\r
877 __attribute__((__always_inline__))
\r
879 extern __inline__ int usart_lin_transfer_completed(volatile avr32_usart_t *usart)
\r
881 return (usart->csr & AVR32_USART_CSR_LINTC_MASK) != 0;
\r
884 #endif // USART rev. >= 4.0.0
\r
889 #endif // _USART_H_
\r