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.

#include <avr32/io.h>
#include "errno.h"

Include dependency graph for 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_AUTO_ECHO   1
#define USART_DEFAULT_TIMEOUT   10000
#define USART_EVEN_PARITY   0
#define USART_LOCAL_LOOPBACK   2
#define USART_MARK_PARITY   3
#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_SPACE_PARITY   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 316 of file usart.h.

#define INT_DCDIC   18

Definition at line 315 of file usart.h.

#define INT_DSRIC   17

Definition at line 314 of file usart.h.

#define INT_ENDRX   3

Definition at line 302 of file usart.h.

#define INT_ENDTX   4

Definition at line 303 of file usart.h.

#define INT_FRAM   6

Definition at line 305 of file usart.h.

#define INT_ITERATION   10

Definition at line 309 of file usart.h.

#define INT_MANE   20

Definition at line 317 of file usart.h.

#define INT_NACK   13

Definition at line 312 of file usart.h.

#define INT_OVRE   5

Definition at line 304 of file usart.h.

#define INT_PARE   7

Definition at line 306 of file usart.h.

#define INT_RIIC   16

Definition at line 313 of file usart.h.

#define INT_RXBRK   2

Definition at line 301 of file usart.h.

#define INT_RXBUFF   12

Definition at line 311 of file usart.h.

#define INT_RXRDY   0

Definition at line 299 of file usart.h.

#define INT_TIMEOUT   8

Definition at line 307 of file usart.h.

#define INT_TXBUFE   11

Definition at line 310 of file usart.h.

#define INT_TXEMPTY   9

Definition at line 308 of file usart.h.

#define INT_TXRDY   1

Definition at line 300 of file usart.h.

#define USART_1_5_STOPBITS   1

Use 1.5 stop bits

Definition at line 291 of file usart.h.

#define USART_1_STOPBIT   0

Use 1 stop bit

Definition at line 289 of file usart.h.

Referenced by init_uart_a().

#define USART_2_STOPBITS   2

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

Definition at line 293 of file usart.h.

#define USART_AUTO_ECHO   1

Set usart channel to echo data

Definition at line 282 of file usart.h.

#define USART_DEFAULT_TIMEOUT   10000

Default timeout value; number of tries before timing out

Definition at line 247 of file usart.h.

Referenced by usart_putchar().

#define USART_EVEN_PARITY   0

Use even parity on character transmission

Definition at line 250 of file usart.h.

#define USART_LOCAL_LOOPBACK   2

Set usart channel to local loopback

Definition at line 284 of file usart.h.

#define USART_MARK_PARITY   3

Use a mark as parity bit

Definition at line 256 of file usart.h.

#define USART_MODE_HW_HSH   0x02

Operate usart in rs232 with hardware handshaking

Definition at line 267 of file usart.h.

Referenced by usart_init_handshaking().

#define USART_MODE_IRDA   0x08

Oparte usart in irda mode

Definition at line 275 of file usart.h.

#define USART_MODE_ISO7816_T0   0x04

Operate usart in iso7816, T=0 mode

Definition at line 271 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 273 of file usart.h.

Referenced by usart_init_iso7816().

#define USART_MODE_MODEM   0x03

Operate usart in modem mode

Definition at line 269 of file usart.h.

Referenced by usart_init_modem().

#define USART_MODE_NORMAL   0x00

Operate usart in normal rs232 mode

Definition at line 263 of file usart.h.

#define USART_MODE_RS485   0x01

Operate usart in rs485 mode

Definition at line 265 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 277 of file usart.h.

#define USART_MULTIDROP_PARITY   6

Parity bit is used to flag address characters

Definition at line 260 of file usart.h.

#define USART_NO_PARITY   4

Don't use a parity bit

Definition at line 258 of file usart.h.

Referenced by init_uart_a().

#define USART_NORMAL_CHMODE   0

Set usart channel to normal communcation

Definition at line 280 of file usart.h.

Referenced by init_uart_a().

#define USART_ODD_PARITY   1

Use odd parity on character transmission

Definition at line 252 of file usart.h.

#define USART_REMOTE_LOOPBACK   3

Set usart channel to remote loopback

Definition at line 286 of file usart.h.

#define USART_SPACE_PARITY   2

Use a space as parity bit

Definition at line 254 of file usart.h.


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 475 of file usart.c.

References SUCCESS, and usart_write_char().

Referenced by usart_send_address().

00476 {
00477         while (usart_write_char(usart, c) != SUCCESS) {
00478         }
00479 
00480         return;
00481 }

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 515 of file usart.c.

References usart_read_char(), USART_RX_EMPTY, and USART_RX_ERROR.

00516 {
00517         int c, ret;
00518 
00519         while (((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY)) {
00520         }
00521 
00522         if (ret == USART_RX_ERROR)
00523                 return -1;
00524         else
00525                 return c;
00526 }

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 153 of file usart.c.

References usart_options_t::baudrate, usart_options_t::channelmode, usart_options_t::charlength, INVALID_ARGUMENT, usart_options_t::paritytype, usart_options_t::stopbits, SUCCESS, usart_reset(), and usart_set_baudrate().

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

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

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 529 of file usart.c.

References SUCCESS, USART_DEFAULT_TIMEOUT, and usart_write_char().

Referenced by print(), and usart_print().

00530 {
00531         int timeout = USART_DEFAULT_TIMEOUT;
00532 
00533         if (c == '\n'){
00534           while ((usart_write_char(usart, '\r') != SUCCESS) && (timeout>0) ){
00535             timeout--;
00536           }
00537         
00538           if (timeout == 0)
00539             return -1;
00540           timeout = USART_DEFAULT_TIMEOUT;
00541         }
00542 
00543         while ((usart_write_char(usart, c) != SUCCESS) && ( timeout>0 )){
00544           timeout--;
00545         }
00546         if (timeout == 0)
00547                 return -1;
00548         else
00549                 return 0;
00550 }

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 495 of file usart.c.

References SUCCESS, USART_RX_EMPTY, and USART_RX_ERROR.

Referenced by usart_getchar().

00496 {
00497         /* Check for errors; Frame, parity and overrun In RS485 mode a parity
00498                 error would mean that we received an address char */
00499         if (usart->csr &
00500                         ((1 << AVR32_USART_CSR_OVRE_OFFSET) |
00501                         (1 << AVR32_USART_CSR_FRAME_OFFSET) |
00502                         (1 << AVR32_USART_CSR_PARE_OFFSET))) {
00503                 return USART_RX_ERROR;
00504         }
00505         /* No error; if we really did receive a char, read it and return SUCCESS */
00506         else if ((usart->csr & (1<<AVR32_USART_CSR_RXRDY_OFFSET)) != 0) {
00507                 *c = (unsigned short)usart->rhr;
00508                 return SUCCESS;
00509         } else {
00510                 return USART_RX_EMPTY;
00511         }
00512 } /* 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 71 of file usart.c.

Referenced by board_init(), usart_init_iso7816(), and usart_init_rs232().

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

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 396 of file usart.c.

00397 {
00398         usart->cr |= (1<<AVR32_USART_CR_RSTSTA_OFFSET);
00399 }

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 483 of file usart.c.

References SUCCESS, and USART_TX_BUSY.

Referenced by usart_bw_write_char(), and usart_putchar().

00484 {
00485 
00486         if ((usart->csr & (1<<AVR32_USART_CSR_TXRDY_OFFSET)) != 0) {
00487                 usart->thr = c;
00488                 return SUCCESS;
00489         }
00490         else
00491                 return USART_TX_BUSY;
00492 }


Generated on Wed May 7 16:03:17 2008 for AVR32114 Using the AVR32 LCD Controller by  doxygen 1.5.3-20071008