source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/USART/usart.h @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 3 years ago

Adding new version

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