usart.h File Reference

#include <avr32/io.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
 Input parameters when initializing rs232 mode. More...

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 avr32_usart_t *usart, int c)
int usart_getchar (volatile avr32_usart_t *usart)
int usart_init_rs232 (volatile avr32_usart_t *usart, struct usart_options_t *opt, long cpu_hz)
int usart_putchar (volatile avr32_usart_t *usart, int c)
 Send a character with the usart.
int usart_read_char (volatile avr32_usart_t *usart, int *c)
void usart_reset (volatile avr32_usart_t *usart)
 This function will reset the USART, and disable TX and RX.
void usart_reset_status (volatile avr32_usart_t *usart)
 Reset error status.
int usart_send_addr (volatile avr32_usart_t *usart, int addr)
int usart_write_char (volatile avr32_usart_t *usart, int c)


Define Documentation

#define INT_CTSIC   19

Definition at line 279 of file usart.h.

#define INT_DCDIC   18

Definition at line 278 of file usart.h.

#define INT_DSRIC   17

Definition at line 277 of file usart.h.

#define INT_ENDRX   3

Definition at line 265 of file usart.h.

#define INT_ENDTX   4

Definition at line 266 of file usart.h.

#define INT_FRAM   6

Definition at line 268 of file usart.h.

#define INT_ITERATION   10

Definition at line 272 of file usart.h.

#define INT_MANE   20

Definition at line 280 of file usart.h.

#define INT_NACK   13

Definition at line 275 of file usart.h.

#define INT_OVRE   5

Definition at line 267 of file usart.h.

#define INT_PARE   7

Definition at line 269 of file usart.h.

#define INT_RIIC   16

Definition at line 276 of file usart.h.

#define INT_RXBRK   2

Definition at line 264 of file usart.h.

#define INT_RXBUFF   12

Definition at line 274 of file usart.h.

#define INT_RXRDY   0

Definition at line 262 of file usart.h.

#define INT_TIMEOUT   8

Definition at line 270 of file usart.h.

#define INT_TXBUFE   11

Definition at line 273 of file usart.h.

#define INT_TXEMPTY   9

Definition at line 271 of file usart.h.

#define INT_TXRDY   1

Definition at line 263 of file usart.h.

#define USART_1_5_STOPBITS   1

Use 1.5 stop bits

Definition at line 254 of file usart.h.

#define USART_1_STOPBIT   0

Use 1 stop bit

Definition at line 252 of file usart.h.

#define USART_2_STOPBITS   2

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

Definition at line 256 of file usart.h.

#define USART_ADDR_RECEIVED   1

Value returned by receiver function when an address character was received

Definition at line 207 of file usart.h.

#define USART_AUTO_ECHO   1

Set usart channel to echo data

Definition at line 245 of file usart.h.

#define USART_DEFAULT_TIMEOUT   10000

Default timeout value; number of tries before timing out

Definition at line 210 of file usart.h.

#define USART_EVEN_PARITY   0

Use even parity on character transmission

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

#define USART_INVALID_ARGUMENT   -1

Definition at line 195 of file usart.h.

#define USART_INVALID_INPUT   1

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

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

#define USART_MARK_PARITY   3

Use a mark as parity bit

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

#define USART_MODE_HW_HSH   0x02

Operate usart in rs232 with hardware handshaking

Definition at line 230 of file usart.h.

#define USART_MODE_IRDA   0x08

Oparte usart in irda mode

Definition at line 238 of file usart.h.

#define USART_MODE_ISO7816_T0   0x04

Operate usart in iso7816, T=0 mode

Definition at line 234 of file usart.h.

#define USART_MODE_ISO7816_T1   0x06

Operate usart in iso7816, T=1 mode

Definition at line 236 of file usart.h.

#define USART_MODE_MODEM   0x03

Operate usart in modem mode

Definition at line 232 of file usart.h.

#define USART_MODE_NORMAL   0x00

Operate usart in normal rs232 mode

Definition at line 226 of file usart.h.

#define USART_MODE_RS485   0x01

Operate usart in rs485 mode

Definition at line 228 of file usart.h.

#define USART_MODE_SW_HSH   0x0C

Operate usart in rs232 with software handshaking

Definition at line 240 of file usart.h.

#define USART_MULTIDROP_PARITY   6

Parity bit is used to flag address characters

Definition at line 223 of file usart.h.

#define USART_NO_PARITY   4

Don't use a parity bit

Definition at line 221 of file usart.h.

#define USART_NORMAL_CHMODE   0

Set usart channel to normal communcation

Definition at line 243 of file usart.h.

#define USART_ODD_PARITY   1

Use odd parity on character transmission

Definition at line 215 of file usart.h.

#define USART_REMOTE_LOOPBACK   3

Set usart channel to remote loopback

Definition at line 249 of file usart.h.

#define USART_RX_EMPTY   3

Value returned by receiver function when nothing was received

Definition at line 199 of file usart.h.

#define USART_RX_ERROR   4

Value returned by receiver function when transmission error occured

Definition at line 201 of file usart.h.

#define USART_SPACE_PARITY   2

Use a space as parity bit

Definition at line 217 of file usart.h.

#define USART_SUCCESS   0

Value returned by function when it completed successfully

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


Function Documentation

void usart_bw_write_char ( volatile 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 431 of file usart.c.

References USART_SUCCESS, and usart_write_char().

00432 {
00433         while (usart_write_char(usart, c) != USART_SUCCESS) {
00434         }
00435 
00436         return;
00437 }

Here is the call graph for this function:

int usart_getchar ( volatile 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 471 of file usart.c.

References usart_read_char(), USART_RX_EMPTY, and USART_RX_ERROR.

00472 {
00473         int c, ret;
00474         while (((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY)) {
00475         }
00476 
00477         if (ret == USART_RX_ERROR)
00478                 return -1;
00479         else
00480                 return c;
00481 }

Here is the call graph for this function:

int usart_init_rs232 ( volatile 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)
Returns:
USART_SUCCESS or USART_INVALID_INPUT

Definition at line 95 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.

00096 {
00097         int retval;
00098 
00099         /* Reset the usart and shutdown RX and TX */
00100         usart_reset(usart);
00101 
00102         /* Control input values */
00103         if (opt == 0) /* Null pointer */
00104                 return USART_INVALID_INPUT;
00105         if (opt->charlength < 5 || opt->charlength > 9)
00106                 return USART_INVALID_INPUT;
00107         if (opt->paritytype > 7)
00108                 return USART_INVALID_INPUT;
00109         if (opt->stopbits > 2+255)
00110                 return USART_INVALID_INPUT;
00111         if (opt->channelmode > 3)
00112                 return USART_INVALID_INPUT;
00113 
00114         if ((retval = usart_set_baudrate(usart, opt->baudrate, cpu_hz)) != \
00115              USART_SUCCESS)
00116                 return retval;
00117 
00118         if (opt->charlength == 9) {
00119                 /* Charlength set to 9 bits; MODE9 dominates CHRL */
00120                 usart->mr |= (1<<AVR32_USART_MR_MODE9_OFFSET);
00121         } else {
00122                 /* CHRL gives the charlength( - 5) when USART_MODE9=0 */
00123                 usart->mr |=
00124                         ((opt->charlength-5) << AVR32_USART_MR_CHRL_OFFSET);
00125         }
00126 
00127         usart->mr |= (opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET) |
00128                      (opt->paritytype << AVR32_USART_MR_PAR_OFFSET);
00129 
00130         if (opt->stopbits > 2)
00131         {
00132                 /* Set two stop bits */
00133                 usart->mr |= (2 << AVR32_USART_MR_NBSTOP_OFFSET);
00134                 /* And a timeguard period gives the rest */
00135                 usart->ttgr = (opt->stopbits-2);
00136         }
00137         else
00138                 /* Insert 1, 1.5 or 2 stop bits */
00139                 usart->mr |= (opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET);
00140 
00141         /* Setup complete; enable communication */
00142         /* Enable input and output */
00143         usart->cr |= (1<<AVR32_USART_CR_TXEN_OFFSET) |
00144                      (1<<AVR32_USART_CR_RXEN_OFFSET);
00145 
00146         return USART_SUCCESS;
00147 }

Here is the call graph for this function:

int usart_putchar ( volatile avr32_usart_t *  usart,
int  character 
)

Send a character with the usart.

Parameters:
usart base address of the usart
character character to write
Returns:
status
Return values:
USART_OK on success
USART_TX_BUSY on timeout

Definition at line 191 of file usart.c.

References USART_DEFAULT_TIMEOUT, USART_OK, and USART_TX_BUSY.

00192 {
00193         int timeout = USART_DEFAULT_TIMEOUT;
00194 
00195         do {
00196                 --timeout;
00197         } while( (usart->csr & (1<<AVR32_USART_CSR_TXRDY_OFFSET)) == 0 && timeout > 0 );
00198 
00199         if( timeout == 0 ) {
00200                 return USART_TX_BUSY;
00201         }
00202 
00203         usart->thr = character;
00204 
00205         return USART_OK;
00206 }

int usart_read_char ( volatile 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 451 of file usart.c.

References USART_RX_EMPTY, USART_RX_ERROR, and USART_SUCCESS.

00452 {
00453         /* Check for errors; Frame, parity and overrun In RS485 mode a parity
00454                 error would mean that we received an address char */
00455         if (usart->csr &
00456                         ((1 << AVR32_USART_CSR_OVRE_OFFSET) |
00457                         (1 << AVR32_USART_CSR_FRAME_OFFSET) |
00458                         (1 << AVR32_USART_CSR_PARE_OFFSET))) {
00459                 return USART_RX_ERROR;
00460         }
00461         /* No error; if we really did receive a char, read it and return SUCCESS */
00462         else if ((usart->csr & (1<<AVR32_USART_CSR_RXRDY_OFFSET)) != 0) {
00463                 *c = (unsigned short)usart->rhr;
00464                 return USART_SUCCESS;
00465         } else {
00466                 return USART_RX_EMPTY;
00467         }
00468 } /* usart_read */

void usart_reset ( volatile avr32_usart_t *  usart  ) 

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

Parameters:
usart Base address of the usart

Definition at line 27 of file usart.c.

00028 {
00029         /* Disable all usart interrupts, interrupts needed should be set
00030            explicitly on every reset */
00031         usart->idr = 0xFFFFffff;
00032 
00033         /* Reset mode and other registers that could cause unpredictable
00034            behaviour after reset */
00035         usart->mr = 0;
00036         usart->rtor = 0;
00037         usart->ttgr = 0;
00038 
00039         /* Shutdown RX and TX (will be reenabled when setup
00040            is completed successfully), reset status bits and turn
00041            off DTR and RTS */
00042         usart->cr = (1<<AVR32_USART_CR_RSTRX_OFFSET)|
00043                     (1<<AVR32_USART_CR_RSTTX_OFFSET)|
00044                     (1<<AVR32_USART_CR_RSTSTA_OFFSET)|
00045                     (1<<AVR32_USART_CR_RSTIT_OFFSET)|
00046                     (1<<AVR32_USART_CR_RSTNACK_OFFSET)|
00047                     (1<<AVR32_USART_CR_DTRDIS_OFFSET)|
00048                     (1<<AVR32_USART_CR_RTSDIS_OFFSET);
00049 }

void usart_reset_status ( volatile 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 352 of file usart.c.

00353 {
00354         usart->cr |= (1<<AVR32_USART_CR_RSTSTA_OFFSET);
00355 }

int usart_send_addr ( volatile 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 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 439 of file usart.c.

References USART_SUCCESS, and USART_TX_BUSY.

00440 {
00441 
00442         if ((usart->csr & (1<<AVR32_USART_CSR_TXRDY_OFFSET)) != 0) {
00443                 usart->thr = c;
00444                 return USART_SUCCESS;
00445         }
00446         else
00447                 return USART_TX_BUSY;
00448 }


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