usart.h File Reference


Detailed Description

USART driver library.

This file contains basic drivers for the AVR32 USART, with support for all all modes, settings and clock speeds.

Author:
Atmel Corporation: http://www.atmel.com
Support email: avr32@atmel.com
$Name$
Revision
67
$RCSfile$
Date
2006-05-10 15:00:07 +0200 (Wed, 10 May 2006)

Definition in file usart.h.

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  iso7816_options_t
struct  usart_options_t

Defines

#define INT_CTSIC   19
#define INT_DCDIC   18
#define INT_DSRIC   17
#define INT_ENDRX   3
#define INT_ENDTX   4
#define INT_FRAM   6
#define INT_ITERATION   10
#define INT_MANE   20
#define INT_NACK   13
#define INT_OVRE   5
#define INT_PARE   7
#define INT_RIIC   16
#define INT_RXBRK   2
#define INT_RXBUFF   12
#define INT_RXRDY   0
#define INT_TIMEOUT   8
#define INT_TXBUFE   11
#define INT_TXEMPTY   9
#define INT_TXRDY   1
#define USART_1_5_STOPBITS   1
#define USART_1_STOPBIT   0
#define USART_2_STOPBITS   2
#define USART_ADDR_RECEIVED   1
#define USART_AUTO_ECHO   1
#define USART_DEFAULT_TIMEOUT   10000
#define USART_EVEN_PARITY   0
#define USART_FAILURE   -1
#define USART_INVALID_ARGUMENT   -1
#define USART_INVALID_INPUT   1
#define USART_LOCAL_LOOPBACK   2
#define USART_MARK_PARITY   3
#define USART_MODE_FAULT   5
#define USART_MODE_HW_HSH   0x02
#define USART_MODE_IRDA   0x08
#define USART_MODE_ISO7816_T0   0x04
#define USART_MODE_ISO7816_T1   0x06
#define USART_MODE_MODEM   0x03
#define USART_MODE_NORMAL   0x00
#define USART_MODE_RS485   0x01
#define USART_MODE_SW_HSH   0x0C
#define USART_MULTIDROP_PARITY   6
#define USART_NO_PARITY   4
#define USART_NORMAL_CHMODE   0
#define USART_ODD_PARITY   1
#define USART_REMOTE_LOOPBACK   3
#define USART_RX_EMPTY   3
#define USART_RX_ERROR   4
#define USART_SPACE_PARITY   2
#define USART_SUCCESS   0
#define USART_TX_BUSY   2

Functions

void usart_bw_write_char (volatile struct avr32_usart_t *usart, int c)
int usart_getchar (volatile struct avr32_usart_t *usart)
int usart_init_rs232 (volatile struct avr32_usart_t *usart, struct usart_options_t *opt, long cpu_hz)
int usart_putchar (volatile struct avr32_usart_t *usart, int c)
int usart_read_char (volatile struct avr32_usart_t *usart, int *c)
void usart_reset (volatile struct avr32_usart_t *usart)
void usart_reset_status (volatile struct avr32_usart_t *usart)
 Reset error status.
int usart_send_addr (volatile struct avr32_usart_t *usart, int addr)
int usart_write_char (volatile struct avr32_usart_t *usart, int c)


Define Documentation

#define INT_CTSIC   19

Definition at line 343 of file usart.h.

#define INT_DCDIC   18

Definition at line 342 of file usart.h.

#define INT_DSRIC   17

Definition at line 341 of file usart.h.

#define INT_ENDRX   3

Definition at line 329 of file usart.h.

#define INT_ENDTX   4

Definition at line 330 of file usart.h.

#define INT_FRAM   6

Definition at line 332 of file usart.h.

#define INT_ITERATION   10

Definition at line 336 of file usart.h.

#define INT_MANE   20

Definition at line 344 of file usart.h.

#define INT_NACK   13

Definition at line 339 of file usart.h.

#define INT_OVRE   5

Definition at line 331 of file usart.h.

#define INT_PARE   7

Definition at line 333 of file usart.h.

#define INT_RIIC   16

Definition at line 340 of file usart.h.

#define INT_RXBRK   2

Definition at line 328 of file usart.h.

#define INT_RXBUFF   12

Definition at line 338 of file usart.h.

#define INT_RXRDY   0

Definition at line 326 of file usart.h.

#define INT_TIMEOUT   8

Definition at line 334 of file usart.h.

#define INT_TXBUFE   11

Definition at line 337 of file usart.h.

#define INT_TXEMPTY   9

Definition at line 335 of file usart.h.

#define INT_TXRDY   1

Definition at line 327 of file usart.h.

#define USART_1_5_STOPBITS   1

Use 1.5 stop bits

Definition at line 318 of file usart.h.

#define USART_1_STOPBIT   0

Use 1 stop bit

Definition at line 316 of file usart.h.

Referenced by main().

#define USART_2_STOPBITS   2

Use 2 stop bits (for more, just give the number of bits)

Definition at line 320 of file usart.h.

#define USART_ADDR_RECEIVED   1

Value returned by receiver function when an address character was received

Definition at line 271 of file usart.h.

#define USART_AUTO_ECHO   1

Set usart channel to echo data

Definition at line 309 of file usart.h.

#define USART_DEFAULT_TIMEOUT   10000

Default timeout value; number of tries before timing out

Definition at line 274 of file usart.h.

Referenced by usart_putchar().

#define USART_EVEN_PARITY   0

Use even parity on character transmission

Definition at line 277 of file usart.h.

#define USART_FAILURE   -1

Value returned by function when it was unable to complete successfully for some unspecified reason

Definition at line 256 of file usart.h.

#define USART_INVALID_ARGUMENT   -1

Definition at line 259 of file usart.h.

#define USART_INVALID_INPUT   1

Value returned by function when the input paramters are out of range

Definition at line 258 of file usart.h.

Referenced by usart_init_handshaking(), usart_init_iso7816(), usart_init_rs232(), and usart_set_baudrate().

#define USART_LOCAL_LOOPBACK   2

Set usart channel to local loopback

Definition at line 311 of file usart.h.

#define USART_MARK_PARITY   3

Use a mark as parity bit

Definition at line 283 of file usart.h.

#define USART_MODE_FAULT   5

Value returned by a function when the usart is not in the appropriate mode

Definition at line 268 of file usart.h.

Referenced by usart_send_address().

#define USART_MODE_HW_HSH   0x02

Operate usart in rs232 with hardware handshaking

Definition at line 294 of file usart.h.

Referenced by usart_init_handshaking().

#define USART_MODE_IRDA   0x08

Oparte usart in irda mode

Definition at line 302 of file usart.h.

#define USART_MODE_ISO7816_T0   0x04

Operate usart in iso7816, T=0 mode

Definition at line 298 of file usart.h.

Referenced by usart_init_iso7816().

#define USART_MODE_ISO7816_T1   0x06

Operate usart in iso7816, T=1 mode

Definition at line 300 of file usart.h.

Referenced by usart_init_iso7816().

#define USART_MODE_MODEM   0x03

Operate usart in modem mode

Definition at line 296 of file usart.h.

Referenced by usart_init_modem().

#define USART_MODE_NORMAL   0x00

Operate usart in normal rs232 mode

Definition at line 290 of file usart.h.

#define USART_MODE_RS485   0x01

Operate usart in rs485 mode

Definition at line 292 of file usart.h.

Referenced by usart_init_rs485().

#define USART_MODE_SW_HSH   0x0C

Operate usart in rs232 with software handshaking

Definition at line 304 of file usart.h.

Referenced by usart_init_handshaking().

#define USART_MULTIDROP_PARITY   6

Parity bit is used to flag address characters

Definition at line 287 of file usart.h.

#define USART_NO_PARITY   4

Don't use a parity bit

Definition at line 285 of file usart.h.

Referenced by main().

#define USART_NORMAL_CHMODE   0

Set usart channel to normal communcation

Definition at line 307 of file usart.h.

Referenced by main().

#define USART_ODD_PARITY   1

Use odd parity on character transmission

Definition at line 279 of file usart.h.

#define USART_REMOTE_LOOPBACK   3

Set usart channel to remote loopback

Definition at line 313 of file usart.h.

#define USART_RX_EMPTY   3

Value returned by receiver function when nothing was received

Definition at line 263 of file usart.h.

Referenced by usart_getchar(), and usart_read_char().

#define USART_RX_ERROR   4

Value returned by receiver function when transmission error occured

Definition at line 265 of file usart.h.

Referenced by usart_getchar(), and usart_read_char().

#define USART_SPACE_PARITY   2

Use a space as parity bit

Definition at line 281 of file usart.h.

#define USART_SUCCESS   0

Value returned by function when it completed successfully

Definition at line 253 of file usart.h.

Referenced by usart_bw_write_char(), usart_init_handshaking(), usart_init_IrDA(), usart_init_iso7816(), usart_init_modem(), usart_init_rs232(), usart_init_rs485(), usart_putchar(), usart_read_char(), usart_send_address(), usart_set_baudrate(), and usart_write_char().

#define USART_TX_BUSY   2

Value returned by transmission function when transmitter was busy

Definition at line 261 of file usart.h.

Referenced by usart_write_char().


Function Documentation

void usart_bw_write_char ( volatile struct avr32_usart_t *  usart,
int  c 
) [inline]

A busy wait for writing a character to the usart. Use with *caution*

Parameters:
*usart Base address of the usart
c The character (up to 9 bits) to transmit
Returns:
None

Definition at line 484 of file usart.c.

References USART_SUCCESS, and usart_write_char().

Referenced by usart_send_address().

00485 {
00486         while (usart_write_char(usart, c) != USART_SUCCESS) {
00487         }
00488 
00489         return;
00490 }

Here is the call graph for this function:

int usart_getchar ( volatile struct avr32_usart_t *  usart  ) 

Wait until a character is recevied, and return this.

Parameters:
*usart Base address of the usart
Returns:
The received character, or -1 upon error

Definition at line 524 of file usart.c.

References usart_read_char(), USART_RX_EMPTY, and USART_RX_ERROR.

00525 {
00526         int c, ret;
00527 
00528         while (((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY)) {
00529         }
00530 
00531         if (ret == USART_RX_ERROR)
00532                 return -1;
00533         else
00534                 return c;
00535 }

Here is the call graph for this function:

int usart_init_rs232 ( volatile struct avr32_usart_t *  usart,
struct usart_options_t opt,
long  cpu_hz 
)

Setup the usart to use the standard RS232 protocol

Parameters:
*usart Base address of the usart
*opt Options needed to set up RS232 communcation (see usart_options_t)
cpu_hz The usart clk frequency
Returns:
USART_SUCCESS or USART_INVALID_INPUT

Definition at line 152 of file usart.c.

References usart_options_t::baudrate, usart_options_t::channelmode, usart_options_t::charlength, usart_options_t::paritytype, usart_options_t::stopbits, USART_INVALID_INPUT, usart_reset(), usart_set_baudrate(), and USART_SUCCESS.

Referenced by main(), usart_init_handshaking(), usart_init_IrDA(), usart_init_modem(), and usart_init_rs485().

00153 {
00154         int retval;
00155 
00156         /* Reset the usart and shutdown RX and TX */
00157         usart_reset(usart);
00158 
00159         /* Control input values */
00160         if (opt == 0) /* Null pointer */
00161                 return USART_INVALID_INPUT;
00162         if (opt->charlength < 5 || opt->charlength > 9)
00163                 return USART_INVALID_INPUT;
00164         if (opt->paritytype > 7)
00165                 return USART_INVALID_INPUT;
00166         if (opt->stopbits > 2+255)
00167                 return USART_INVALID_INPUT;
00168         if (opt->channelmode > 3)
00169                 return USART_INVALID_INPUT;
00170 
00171         if ((retval = usart_set_baudrate(usart, opt->baudrate, cpu_hz)) != \
00172              USART_SUCCESS)
00173                 return retval;
00174 
00175         if (opt->charlength == 9) {
00176                 /* Charlength set to 9 bits; MODE9 dominates CHRL */
00177                 usart->mr |= (1<<AVR32_USART_MR_MODE9_OFFSET);
00178         } else {
00179                 /* CHRL gives the charlength( - 5) when USART_MODE9=0 */
00180                 usart->mr |=
00181                         ((opt->charlength-5) << AVR32_USART_MR_CHRL_OFFSET);
00182         }
00183 
00184         usart->mr |= (opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET) |
00185                      (opt->paritytype << AVR32_USART_MR_PAR_OFFSET);
00186 
00187         if (opt->stopbits > 2)
00188         {
00189                 /* Set two stop bits */
00190                 usart->mr |= (2 << AVR32_USART_MR_NBSTOP_OFFSET);
00191                 /* And a timeguard period gives the rest */
00192                 usart->ttgr = (opt->stopbits-2);
00193         }
00194         else
00195                 /* Insert 1, 1.5 or 2 stop bits */
00196                 usart->mr |= (opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET);
00197 
00198         /* Setup complete; enable communication */
00199         /* Enable input and output */
00200         usart->cr |= (1<<AVR32_USART_CR_TXEN_OFFSET) |
00201                      (1<<AVR32_USART_CR_RXEN_OFFSET);
00202 
00203         return USART_SUCCESS;
00204 }

Here is the call graph for this function:

int usart_putchar ( volatile struct avr32_usart_t *  usart,
int  c 
)

Send a character with the usart

Parameters:
*usart Base address of the usart
c Character to write
Returns:
0 if char was written, -1 if the function timed out before the usart transmitter became ready to send

Definition at line 538 of file usart.c.

References USART_DEFAULT_TIMEOUT, USART_SUCCESS, and usart_write_char().

Referenced by print().

00539 {
00540         int timeout = USART_DEFAULT_TIMEOUT;
00541 
00542         if (c == '\n'){
00543           while ((usart_write_char(usart, '\r') != USART_SUCCESS) && (timeout>0) ){
00544             timeout--;
00545           }
00546         
00547           if (timeout == 0)
00548             return -1;
00549           timeout = USART_DEFAULT_TIMEOUT;
00550         }
00551 
00552         while ((usart_write_char(usart, c) != USART_SUCCESS) && ( timeout>0 )){
00553           timeout--;
00554         }
00555         if (timeout == 0)
00556                 return -1;
00557         else
00558                 return 0;
00559 }

Here is the call graph for this function:

int usart_read_char ( volatile struct avr32_usart_t *  usart,
int *  c 
)

Checks the RX buffer for a received character, and puts this at the memory location given.

Parameters:
*usart Base address of the usart
*c Pointer to the where the read charcter should be writen (must be short in order to accept 9 bit characters)
Returns:
USART_SUCCESS if the character was read successfully
USART_RX_EMPTY if the RX buffer was empty
USART_RX_ERROR if an error was deteceted
USART_ADDR_RECEIVED if an address is received while in multidrop mode

Definition at line 504 of file usart.c.

References USART_RX_EMPTY, USART_RX_ERROR, and USART_SUCCESS.

Referenced by usart_getchar().

00505 {
00506         /* Check for errors; Frame, parity and overrun In RS485 mode a parity
00507                 error would mean that we received an address char */
00508         if (usart->csr &
00509                         ((1 << AVR32_USART_CSR_OVRE_OFFSET) |
00510                         (1 << AVR32_USART_CSR_FRAME_OFFSET) |
00511                         (1 << AVR32_USART_CSR_PARE_OFFSET))) {
00512                 return USART_RX_ERROR;
00513         }
00514         /* No error; if we really did receive a char, read it and return SUCCESS */
00515         else if ((usart->csr & (1<<AVR32_USART_CSR_RXRDY_OFFSET)) != 0) {
00516                 *c = (unsigned short)usart->rhr;
00517                 return USART_SUCCESS;
00518         } else {
00519                 return USART_RX_EMPTY;
00520         }
00521 } /* usart_read */

void usart_reset ( volatile struct avr32_usart_t *  usart  ) 

This function will reset the USART, and disable TX and RX

Parameters:
*usart Base address of the usart
Returns:
nothing

Definition at line 70 of file usart.c.

Referenced by usart_init_iso7816(), and usart_init_rs232().

00071 {
00072         /* Disable all usart interrupts, interrupts needed should be set
00073            explicitly on every reset */
00074         usart->idr = 0xFFFFffff;
00075 
00076         /* Reset mode and other registers that could cause unpredictable
00077            behaviour after reset */
00078         usart->mr = 0;
00079         usart->rtor = 0;
00080         usart->ttgr = 0;
00081 
00082         /* Shutdown RX and TX (will be reenabled when setup
00083            is completed successfully), reset status bits and turn
00084            off DTR and RTS */
00085         usart->cr = (1 << AVR32_USART_CR_RSTRX_OFFSET) |
00086                     (1 << AVR32_USART_CR_RSTTX_OFFSET) |
00087                     (1 << AVR32_USART_CR_RSTSTA_OFFSET) |
00088                     (1 << AVR32_USART_CR_RSTIT_OFFSET) |
00089                     (1 << AVR32_USART_CR_RSTNACK_OFFSET) |
00090                     (1 << AVR32_USART_CR_DTRDIS_OFFSET) |
00091                     (1 << AVR32_USART_CR_RTSDIS_OFFSET);
00092 }

void usart_reset_status ( volatile struct avr32_usart_t *  usart  ) 

Reset error status.

This function resets the status bits indicating that a parity error, framing error or overrun has occured. The rxbreak bit, indicating a start/end of break condition on the rx-line, is also reset.

Parameters:
*usart Base address of the usart
Returns:
nothing

Definition at line 405 of file usart.c.

00406 {
00407         usart->cr |= (1<<AVR32_USART_CR_RSTSTA_OFFSET);
00408 }

int usart_send_addr ( volatile struct avr32_usart_t *  usart,
int  addr 
)

Description: While in RS485-mode, receviers only accept data addressed to them. A packet/char with the address tag set has to preceed any data. usart_send_addr() is used to address a receiver. This receiver should read all the following data, until an address packet addresses someone else. Arguments: *usart: Base address of the usart addr: the address of the target device Returns: USART_SUCCESS if the current mode is RS485 USART_MODE_FAULT if called while in wrong mode

int usart_write_char ( volatile struct avr32_usart_t *  usart,
int  c 
)

If the transmitter is ready; write the given character to the TX buffer

Parameters:
*usart Base address of the usart
c The character (up to 9 bits) to transmit
Returns:
USART_SUCCESS when the transmitter is ready USART_TX_BUSY when the transmitter is busy

Definition at line 492 of file usart.c.

References USART_SUCCESS, and USART_TX_BUSY.

Referenced by usart_bw_write_char(), and usart_putchar().

00493 {
00494 
00495         if ((usart->csr & (1<<AVR32_USART_CSR_TXRDY_OFFSET)) != 0) {
00496                 usart->thr = c;
00497                 return USART_SUCCESS;
00498         }
00499         else
00500                 return USART_TX_BUSY;
00501 }


Generated on Thu May 10 14:17:57 2007 for AVR321000 Communication with the AVR32 USART by  doxygen 1.5.1