usart.c File Reference


Detailed Description

USART driver for AVR32 UC3.

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

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file usart.c.

#include "compiler.h"
#include "usart.h"

Go to the source code of this file.

Functions

Transmit/Receive Functions
int usart_get_echo_line (volatile avr32_usart_t *usart)
 Gets and echoes characters until end of line.
int usart_getchar (volatile avr32_usart_t *usart)
 Waits until a character is received, and returns it.
int usart_putchar (volatile avr32_usart_t *usart, int c)
 Sends a character with the USART.
int usart_read_char (volatile avr32_usart_t *usart, int *c)
 Checks the RX buffer for a received character, and stores it at the given memory location.
int usart_send_address (volatile avr32_usart_t *usart, int address)
 Addresses a receiver.
int usart_write_char (volatile avr32_usart_t *usart, int c)
 Writes the given character to the TX buffer if the transmitter is ready.
void usart_write_line (volatile avr32_usart_t *usart, const char *string)
 Writes one character string to the USART.
Initialization Functions
int usart_init_hw_handshaking (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
 Sets up the USART to use hardware handshaking.
int usart_init_IrDA (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz, unsigned char irda_filter)
 Sets up the USART to use the IrDA protocol.
int usart_init_iso7816 (volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz)
 Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.
int usart_init_modem (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
 Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.
int usart_init_rs232 (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
 Sets up the USART to use the standard RS232 protocol.
int usart_init_rs232_tx_only (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
 Sets up the USART to use the standard RS232 protocol in TX-only mode.
int usart_init_rs485 (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
 Sets up the USART to use the RS485 protocol.
int usart_init_sync_master (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
 Sets up the USART to use a synchronous RS232-like protocol in master mode.
int usart_init_sync_slave (volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
 Sets up the USART to use a synchronous RS232-like protocol in slave mode.
void usart_reset (volatile avr32_usart_t *usart)
 Resets the USART and disables TX and RX.
Private Functions
static __inline__ int usart_mode_is_multidrop (volatile avr32_usart_t *usart)
 Checks if the USART is in multidrop mode.
static int usart_set_async_baudrate (volatile avr32_usart_t *usart, unsigned int baudrate, unsigned long pba_hz)
 Calculates a clock divider (CD) and a fractional part (FP) for the USART asynchronous modes to generate a baud rate as close as possible to the baud rate set point.
static int usart_set_iso7816_clock (volatile avr32_usart_t *usart, unsigned int clock, unsigned long pba_hz)
 Calculates a clock divider (CD) for the USART ISO7816 mode to generate an ISO7816 clock as close as possible to the clock set point.
static int usart_set_sync_master_baudrate (volatile avr32_usart_t *usart, unsigned int baudrate, unsigned long pba_hz)
 Calculates a clock divider (CD) for the USART synchronous master modes to generate a baud rate as close as possible to the baud rate set point.
static int usart_set_sync_slave_baudrate (volatile avr32_usart_t *usart)
 Selects the SCK pin as the source of baud rate for the USART synchronous slave modes.


Function Documentation

int usart_get_echo_line ( volatile avr32_usart_t *  usart  ) 

Gets and echoes characters until end of line.

Parameters:
usart Base address of the USART instance.
Return values:
USART_SUCCESS Success.
USART_FAILURE Low-level error detected or ETX character received.

Definition at line 881 of file usart.c.

References USART_FAILURE, usart_getchar(), usart_putchar(), USART_SUCCESS, and usart_write_line().

Referenced by main().

00882 {
00883   int rx_char;
00884   int retval = USART_SUCCESS;
00885 
00886   while (1)
00887   {
00888     rx_char = usart_getchar(usart);
00889     if (rx_char == USART_FAILURE)
00890     {
00891       usart_write_line(usart, "Error!!!\n");
00892       retval = USART_FAILURE;
00893       break;
00894     }
00895     if (rx_char == '\x03')
00896     {
00897       retval = USART_FAILURE;
00898       break;
00899     }
00900     usart_putchar(usart, rx_char);
00901     if (rx_char == '\r')
00902     {
00903       usart_putchar(usart, '\n');
00904       break;
00905     }
00906   }
00907 
00908   return retval;
00909 }

int usart_getchar ( volatile avr32_usart_t *  usart  ) 

Waits until a character is received, and returns it.

Parameters:
usart Base address of the USART instance.
Returns:
The received character, or USART_FAILURE upon error.

Definition at line 861 of file usart.c.

References USART_FAILURE, usart_read_char(), USART_RX_EMPTY, and USART_RX_ERROR.

Referenced by usart_get_echo_line().

00862 {
00863   int c, ret;
00864 
00865   while ((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY);
00866 
00867   if (ret == USART_RX_ERROR)
00868     return USART_FAILURE;
00869 
00870   return c;
00871 }

int usart_init_hw_handshaking ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz 
)

Sets up the USART to use hardware handshaking.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.
Note:
usart_init_rs232 does not need to be invoked before this function.

Definition at line 391 of file usart.c.

References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.

00392 {
00393   // First: Setup standard RS232.
00394   if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
00395     return USART_INVALID_INPUT;
00396 
00397   // Set hardware handshaking mode.
00398   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00399               AVR32_USART_MR_MODE_HARDWARE << AVR32_USART_MR_MODE_OFFSET;
00400 
00401   return USART_SUCCESS;
00402 }

int usart_init_IrDA ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz,
unsigned char  irda_filter 
)

Sets up the USART to use the IrDA protocol.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
irda_filter Counter used to distinguish received ones from zeros.
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.

Definition at line 538 of file usart.c.

References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.

00540 {
00541   // First: Setup standard RS232.
00542   if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
00543     return USART_INVALID_INPUT;
00544 
00545   // Set IrDA filter.
00546   usart->ifr = irda_filter;
00547 
00548   // Set IrDA mode and activate filtering of input.
00549   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00550               AVR32_USART_MODE_IRDA << AVR32_USART_MR_MODE_OFFSET |
00551               AVR32_USART_MR_FILTER_MASK;
00552 
00553   return USART_SUCCESS;
00554 }

int usart_init_iso7816 ( volatile avr32_usart_t *  usart,
const usart_iso7816_options_t opt,
int  t,
long  pba_hz 
)

Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.

The receiver is enabled by default. usart_iso7816_enable_receiver and usart_iso7816_enable_transmitter can be called to change the half-duplex communication direction.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up ISO7816 communication (see usart_iso7816_options_t).
t ISO7816 mode to use (T=0 or T=1).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.

Definition at line 557 of file usart.c.

References usart_iso7816_options_t::bit_order, usart_iso7816_options_t::dis_suc_nack, usart_iso7816_options_t::fidi_ratio, usart_iso7816_options_t::inhibit_nack, usart_iso7816_options_t::iso7816_hz, usart_iso7816_options_t::max_iterations, usart_iso7816_options_t::paritytype, USART_INVALID_INPUT, usart_iso7816_enable_receiver(), usart_reset(), usart_set_iso7816_clock(), and USART_SUCCESS.

00558 {
00559   // Reset the USART and shutdown TX and RX.
00560   usart_reset(usart);
00561 
00562   // Check input values.
00563   if (!opt || // Null pointer.
00564       opt->paritytype > 1)
00565     return USART_INVALID_INPUT;
00566 
00567   if (t == 0)
00568   {
00569     // Set USART mode to ISO7816, T=0.
00570     // The T=0 protocol always uses 2 stop bits.
00571     usart->mr = AVR32_USART_MR_MODE_ISO7816_T0 << AVR32_USART_MR_MODE_OFFSET |
00572                 AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET |
00573                 opt->bit_order << AVR32_USART_MR_MSBF_OFFSET; // Allow MSBF in T=0.
00574   }
00575   else if (t == 1)
00576   {
00577     // Only LSB first in the T=1 protocol.
00578     // max_iterations field is only used in T=0 mode.
00579     if (opt->bit_order != 0 ||
00580         opt->max_iterations != 0)
00581       return USART_INVALID_INPUT;
00582 
00583     // Set USART mode to ISO7816, T=1.
00584     // The T=1 protocol always uses 1 stop bit.
00585     usart->mr = AVR32_USART_MR_MODE_ISO7816_T1 << AVR32_USART_MR_MODE_OFFSET |
00586                 AVR32_USART_MR_NBSTOP_1 << AVR32_USART_MR_NBSTOP_OFFSET;
00587   }
00588   else
00589     return USART_INVALID_INPUT;
00590 
00591   if (usart_set_iso7816_clock(usart, opt->iso7816_hz, pba_hz) == USART_INVALID_INPUT)
00592     return USART_INVALID_INPUT;
00593 
00594   // Set FIDI register: bit rate = selected clock/FI_DI_ratio/16.
00595   usart->fidi = opt->fidi_ratio;
00596 
00597   // Set ISO7816 spesific options in the MODE register.
00598   usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
00599                AVR32_USART_MR_CLKO_MASK | // Enable clock output.
00600                opt->inhibit_nack << AVR32_USART_MR_INACK_OFFSET |
00601                opt->dis_suc_nack << AVR32_USART_MR_DSNACK_OFFSET |
00602                opt->max_iterations << AVR32_USART_MR_MAX_ITERATION_OFFSET;
00603 
00604   // Setup complete; enable the receiver by default.
00605   usart_iso7816_enable_receiver(usart);
00606 
00607   return USART_SUCCESS;
00608 }

int usart_init_modem ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz 
)

Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.

Definition at line 405 of file usart.c.

References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.

00406 {
00407   // First: Setup standard RS232.
00408   if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
00409     return USART_INVALID_INPUT;
00410 
00411   // Set modem mode.
00412   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00413               AVR32_USART_MR_MODE_MODEM << AVR32_USART_MR_MODE_OFFSET;
00414 
00415   return USART_SUCCESS;
00416 }

int usart_init_rs232 ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz 
)

Sets up the USART to use the standard RS232 protocol.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.

Definition at line 287 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_2_STOPBITS, USART_INVALID_INPUT, usart_reset(), usart_set_async_baudrate(), and USART_SUCCESS.

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

00288 {
00289   // Reset the USART and shutdown TX and RX.
00290   usart_reset(usart);
00291 
00292   // Check input values.
00293   if (!opt || // Null pointer.
00294       opt->charlength < 5 || opt->charlength > 9 ||
00295       opt->paritytype > 7 ||
00296       opt->stopbits > 2 + 255 ||
00297       opt->channelmode > 3 ||
00298       usart_set_async_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT)
00299     return USART_INVALID_INPUT;
00300 
00301   if (opt->charlength == 9)
00302   {
00303     // Character length set to 9 bits. MODE9 dominates CHRL.
00304     usart->mr |= AVR32_USART_MR_MODE9_MASK;
00305   }
00306   else
00307   {
00308     // CHRL gives the character length (- 5) when MODE9 = 0.
00309     usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
00310   }
00311 
00312   usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
00313                opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
00314 
00315   if (opt->stopbits > USART_2_STOPBITS)
00316   {
00317     // Set two stop bits
00318     usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
00319     // and a timeguard period gives the rest.
00320     usart->ttgr = opt->stopbits - USART_2_STOPBITS;
00321   }
00322   else
00323     // Insert 1, 1.5 or 2 stop bits.
00324     usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
00325 
00326   // Set normal mode.
00327   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00328               AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
00329 
00330   // Setup complete; enable communication.
00331   // Enable input and output.
00332   usart->cr = AVR32_USART_CR_RXEN_MASK |
00333               AVR32_USART_CR_TXEN_MASK;
00334 
00335   return USART_SUCCESS;
00336 }

int usart_init_rs232_tx_only ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz 
)

Sets up the USART to use the standard RS232 protocol in TX-only mode.

Compared to usart_init_rs232, this function allows very high baud rates (up to pba_hz instead of pba_hz / 8) at the expense of full duplex.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.
Note:
The 1.5 stop bit is not supported in this mode.

Definition at line 339 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_2_STOPBITS, USART_INVALID_INPUT, usart_reset(), usart_set_sync_master_baudrate(), and USART_SUCCESS.

00340 {
00341   // Reset the USART and shutdown TX and RX.
00342   usart_reset(usart);
00343 
00344   // Check input values.
00345   if (!opt || // Null pointer.
00346       opt->charlength < 5 || opt->charlength > 9 ||
00347       opt->paritytype > 7 ||
00348       opt->stopbits == 1 || opt->stopbits > 2 + 255 ||
00349       opt->channelmode > 3 ||
00350       usart_set_sync_master_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT)
00351     return USART_INVALID_INPUT;
00352 
00353   if (opt->charlength == 9)
00354   {
00355     // Character length set to 9 bits. MODE9 dominates CHRL.
00356     usart->mr |= AVR32_USART_MR_MODE9_MASK;
00357   }
00358   else
00359   {
00360     // CHRL gives the character length (- 5) when MODE9 = 0.
00361     usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
00362   }
00363 
00364   usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
00365                opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
00366 
00367   if (opt->stopbits > USART_2_STOPBITS)
00368   {
00369     // Set two stop bits
00370     usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
00371     // and a timeguard period gives the rest.
00372     usart->ttgr = opt->stopbits - USART_2_STOPBITS;
00373   }
00374   else
00375     // Insert 1 or 2 stop bits.
00376     usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
00377 
00378   // Set normal mode.
00379   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00380               AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
00381 
00382   // Setup complete; enable communication.
00383   // Enable only output as input is not possible in synchronous mode without
00384   // transferring clock.
00385   usart->cr = AVR32_USART_CR_TXEN_MASK;
00386 
00387   return USART_SUCCESS;
00388 }

int usart_init_rs485 ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz 
)

Sets up the USART to use the RS485 protocol.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.

Definition at line 524 of file usart.c.

References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.

00525 {
00526   // First: Setup standard RS232.
00527   if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
00528     return USART_INVALID_INPUT;
00529 
00530   // Set RS485 mode.
00531   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00532               AVR32_USART_MR_MODE_RS485 << AVR32_USART_MR_MODE_OFFSET;
00533 
00534   return USART_SUCCESS;
00535 }

int usart_init_sync_master ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz 
)

Sets up the USART to use a synchronous RS232-like protocol in master mode.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.

Definition at line 419 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_2_STOPBITS, USART_INVALID_INPUT, usart_reset(), usart_set_sync_master_baudrate(), and USART_SUCCESS.

00420 {
00421   // Reset the USART and shutdown TX and RX.
00422   usart_reset(usart);
00423 
00424   // Check input values.
00425   if (!opt || // Null pointer.
00426       opt->charlength < 5 || opt->charlength > 9 ||
00427       opt->paritytype > 7 ||
00428       opt->stopbits == 1 || opt->stopbits > 2 + 255 ||
00429       opt->channelmode > 3 ||
00430       usart_set_sync_master_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT)
00431     return USART_INVALID_INPUT;
00432 
00433   if (opt->charlength == 9)
00434   {
00435     // Character length set to 9 bits. MODE9 dominates CHRL.
00436     usart->mr |= AVR32_USART_MR_MODE9_MASK;
00437   }
00438   else
00439   {
00440     // CHRL gives the character length (- 5) when MODE9 = 0.
00441     usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
00442   }
00443 
00444   usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
00445                opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
00446 
00447   if (opt->stopbits > USART_2_STOPBITS)
00448   {
00449     // Set two stop bits
00450     usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
00451     // and a timeguard period gives the rest.
00452     usart->ttgr = opt->stopbits - USART_2_STOPBITS;
00453   }
00454   else
00455     // Insert 1 or 2 stop bits.
00456     usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
00457 
00458   // Set normal mode.
00459   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00460               AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET |
00461               AVR32_USART_MR_CLKO_MASK;
00462 
00463   // Setup complete; enable communication.
00464   // Enable input and output.
00465   usart->cr = AVR32_USART_CR_RXEN_MASK |
00466               AVR32_USART_CR_TXEN_MASK;
00467 
00468   return USART_SUCCESS;
00469 }

int usart_init_sync_slave ( volatile avr32_usart_t *  usart,
const usart_options_t opt,
long  pba_hz 
)

Sets up the USART to use a synchronous RS232-like protocol in slave mode.

Parameters:
usart Base address of the USART instance.
opt Options needed to set up RS232 communication (see usart_options_t).
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Mode successfully initialized.
USART_INVALID_INPUT One or more of the arguments is out of valid range.

Definition at line 472 of file usart.c.

References usart_options_t::channelmode, usart_options_t::charlength, usart_options_t::paritytype, usart_options_t::stopbits, USART_2_STOPBITS, USART_INVALID_INPUT, usart_reset(), usart_set_sync_slave_baudrate(), and USART_SUCCESS.

00473 {
00474   // Reset the USART and shutdown TX and RX.
00475   usart_reset(usart);
00476 
00477   // Check input values.
00478   if (!opt || // Null pointer.
00479       opt->charlength < 5 || opt->charlength > 9 ||
00480       opt->paritytype > 7 ||
00481       opt->stopbits == 1 || opt->stopbits > 2 + 255 ||
00482       opt->channelmode > 3 ||
00483       usart_set_sync_slave_baudrate(usart) == USART_INVALID_INPUT)
00484     return USART_INVALID_INPUT;
00485 
00486   if (opt->charlength == 9)
00487   {
00488     // Character length set to 9 bits. MODE9 dominates CHRL.
00489     usart->mr |= AVR32_USART_MR_MODE9_MASK;
00490   }
00491   else
00492   {
00493     // CHRL gives the character length (- 5) when MODE9 = 0.
00494     usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
00495   }
00496 
00497   usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
00498                opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
00499 
00500   if (opt->stopbits > USART_2_STOPBITS)
00501   {
00502     // Set two stop bits
00503     usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
00504     // and a timeguard period gives the rest.
00505     usart->ttgr = opt->stopbits - USART_2_STOPBITS;
00506   }
00507   else
00508     // Insert 1 or 2 stop bits.
00509     usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
00510 
00511   // Set normal mode.
00512   usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
00513               AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
00514 
00515   // Setup complete; enable communication.
00516   // Enable input and output.
00517   usart->cr = AVR32_USART_CR_RXEN_MASK |
00518               AVR32_USART_CR_TXEN_MASK;
00519 
00520   return USART_SUCCESS;
00521 }

static __inline__ int usart_mode_is_multidrop ( volatile avr32_usart_t *  usart  )  [static]

Checks if the USART is in multidrop mode.

Parameters:
usart Base address of the USART instance.
Returns:
1 if the USART is in multidrop mode, otherwise 0.

Definition at line 68 of file usart.c.

Referenced by usart_send_address().

00069 {
00070   return ((usart->mr >> AVR32_USART_MR_PAR_OFFSET) & AVR32_USART_MR_PAR_MULTI) == AVR32_USART_MR_PAR_MULTI;
00071 }

int usart_putchar ( volatile avr32_usart_t *  usart,
int  c 
)

Sends a character with the USART.

Parameters:
usart Base address of the USART instance.
c Character to write.
Return values:
USART_SUCCESS The character was written.
USART_FAILURE The function timed out before the USART transmitter became ready to send.

Definition at line 818 of file usart.c.

References USART_DEFAULT_TIMEOUT, USART_FAILURE, USART_SUCCESS, and usart_write_char().

Referenced by main(), usart_get_echo_line(), and usart_write_line().

00819 {
00820   int timeout = USART_DEFAULT_TIMEOUT;
00821 
00822   if (c == '\n')
00823   {
00824     do
00825     {
00826       if (!timeout--) return USART_FAILURE;
00827     } while (usart_write_char(usart, '\r') != USART_SUCCESS);
00828 
00829     timeout = USART_DEFAULT_TIMEOUT;
00830   }
00831 
00832   do
00833   {
00834     if (!timeout--) return USART_FAILURE;
00835   } while (usart_write_char(usart, c) != USART_SUCCESS);
00836 
00837   return USART_SUCCESS;
00838 }

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

Checks the RX buffer for a received character, and stores it at the given memory location.

Parameters:
usart Base address of the USART instance.
c Pointer to the where the read character should be stored (must be at least short in order to accept 9-bit characters).
Return values:
USART_SUCCESS The character was read successfully.
USART_RX_EMPTY The RX buffer was empty.
USART_RX_ERROR An error was deteceted.

Definition at line 841 of file usart.c.

References USART_RX_EMPTY, USART_RX_ERROR, USART_SUCCESS, and usart_test_hit().

Referenced by usart_getchar().

00842 {
00843   // Check for errors: frame, parity and overrun. In RS485 mode, a parity error
00844   // would mean that an address char has been received.
00845   if (usart->csr & (AVR32_USART_CSR_OVRE_MASK |
00846                     AVR32_USART_CSR_FRAME_MASK |
00847                     AVR32_USART_CSR_PARE_MASK))
00848     return USART_RX_ERROR;
00849 
00850   // No error; if we really did receive a char, read it and return SUCCESS.
00851   if (usart_test_hit(usart))
00852   {
00853     *c = (usart->rhr & AVR32_USART_RHR_RXCHR_MASK) >> AVR32_USART_RHR_RXCHR_OFFSET;
00854     return USART_SUCCESS;
00855   }
00856   else
00857     return USART_RX_EMPTY;
00858 }

void usart_reset ( volatile avr32_usart_t *  usart  ) 

Resets the USART and disables TX and RX.

Parameters:
usart Base address of the USART instance.

Definition at line 256 of file usart.c.

Referenced by usart_init_iso7816(), usart_init_rs232(), usart_init_rs232_tx_only(), usart_init_sync_master(), and usart_init_sync_slave().

00257 {
00258   Bool global_interrupt_enabled = Is_global_interrupt_enabled();
00259 
00260   // Disable all USART interrupts.
00261   // Interrupts needed should be set explicitly on every reset.
00262   if (global_interrupt_enabled) Disable_global_interrupt();
00263   usart->idr = 0xFFFFFFFF;
00264   usart->csr;
00265   if (global_interrupt_enabled) Enable_global_interrupt();
00266 
00267   // Reset mode and other registers that could cause unpredictable behavior after reset.
00268   usart->mr = 0;
00269   usart->rtor = 0;
00270   usart->ttgr = 0;
00271 
00272   // Shutdown TX and RX (will be re-enabled when setup has successfully completed),
00273   // reset status bits and turn off DTR and RTS.
00274   usart->cr = AVR32_USART_CR_RSTRX_MASK   |
00275               AVR32_USART_CR_RSTTX_MASK   |
00276               AVR32_USART_CR_RSTSTA_MASK  |
00277               AVR32_USART_CR_RSTIT_MASK   |
00278               AVR32_USART_CR_RSTNACK_MASK |
00279 #ifndef AVR32_USART_440_H_INCLUDED
00280 // Note: Modem Signal Management DTR-DSR-DCD-RI are not included in USART rev.440.
00281               AVR32_USART_CR_DTRDIS_MASK  |
00282 #endif
00283               AVR32_USART_CR_RTSDIS_MASK;
00284 }

int usart_send_address ( volatile avr32_usart_t *  usart,
int  address 
)

Addresses a receiver.

While in RS485 mode, receivers only accept data addressed to them. A packet/char with the address tag set has to precede any data. This function is used to address a receiver. This receiver should read all the following data, until an address packet addresses another receiver.

Parameters:
usart Base address of the USART instance.
address Address of the target device.
Return values:
USART_SUCCESS Address successfully sent (if current mode is RS485).
USART_MODE_FAULT Wrong operating mode.

Definition at line 791 of file usart.c.

References usart_bw_write_char(), USART_MODE_FAULT, usart_mode_is_multidrop(), and USART_SUCCESS.

00792 {
00793   // Check if USART is in multidrop / RS485 mode.
00794   if (!usart_mode_is_multidrop(usart)) return USART_MODE_FAULT;
00795 
00796   // Prepare to send an address.
00797   usart->cr = AVR32_USART_CR_SENDA_MASK;
00798 
00799   // Write the address to TX.
00800   usart_bw_write_char(usart, address);
00801 
00802   return USART_SUCCESS;
00803 }

static int usart_set_async_baudrate ( volatile avr32_usart_t *  usart,
unsigned int  baudrate,
unsigned long  pba_hz 
) [static]

Calculates a clock divider (CD) and a fractional part (FP) for the USART asynchronous modes to generate a baud rate as close as possible to the baud rate set point.

Baud rate calculation: $ Baudrate = \frac{SelectedClock}{Over \times (CD + \frac{FP}{8})} $, Over being 16 or 8. The maximal oversampling is selected if it allows to generate a baud rate close to the set point.

Parameters:
usart Base address of the USART instance.
baudrate Baud rate set point.
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Baud rate successfully initialized.
USART_INVALID_INPUT Baud rate set point is out of range for the given input clock frequency.

Definition at line 89 of file usart.c.

References USART_INVALID_INPUT, and USART_SUCCESS.

Referenced by usart_init_rs232().

00090 {
00091   unsigned int over = (pba_hz >= 16 * baudrate) ? 16 : 8;
00092   unsigned int cd_fp = ((1 << AVR32_USART_BRGR_FP_SIZE) * pba_hz + (over * baudrate) / 2) / (over * baudrate);
00093   unsigned int cd = cd_fp >> AVR32_USART_BRGR_FP_SIZE;
00094   unsigned int fp = cd_fp & ((1 << AVR32_USART_BRGR_FP_SIZE) - 1);
00095 
00096   if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
00097     return USART_INVALID_INPUT;
00098 
00099   usart->mr = (usart->mr & ~(AVR32_USART_MR_USCLKS_MASK |
00100                              AVR32_USART_MR_SYNC_MASK |
00101                              AVR32_USART_MR_OVER_MASK)) |
00102               AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
00103               ((over == 16) ? AVR32_USART_MR_OVER_X16 : AVR32_USART_MR_OVER_X8) << AVR32_USART_MR_OVER_OFFSET;
00104 
00105   usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET |
00106                 fp << AVR32_USART_BRGR_FP_OFFSET;
00107 
00108   return USART_SUCCESS;
00109 }

static int usart_set_iso7816_clock ( volatile avr32_usart_t *  usart,
unsigned int  clock,
unsigned long  pba_hz 
) [static]

Calculates a clock divider (CD) for the USART ISO7816 mode to generate an ISO7816 clock as close as possible to the clock set point.

ISO7816 clock calculation: $ Clock = \frac{SelectedClock}{CD} $.

Parameters:
usart Base address of the USART instance.
clock ISO7816 clock set point.
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS ISO7816 clock successfully initialized.
USART_INVALID_INPUT ISO7816 clock set point is out of range for the given input clock frequency.

Definition at line 173 of file usart.c.

References USART_INVALID_INPUT, and USART_SUCCESS.

Referenced by usart_init_iso7816().

00174 {
00175   unsigned int cd = (pba_hz + clock / 2) / clock;
00176 
00177   if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
00178     return USART_INVALID_INPUT;
00179 
00180   usart->mr = (usart->mr & ~(AVR32_USART_MR_USCLKS_MASK |
00181                              AVR32_USART_MR_SYNC_MASK |
00182                              AVR32_USART_MR_OVER_MASK)) |
00183               AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
00184               AVR32_USART_MR_OVER_X16 << AVR32_USART_MR_OVER_OFFSET;
00185 
00186   usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET;
00187 
00188   return USART_SUCCESS;
00189 }

static int usart_set_sync_master_baudrate ( volatile avr32_usart_t *  usart,
unsigned int  baudrate,
unsigned long  pba_hz 
) [static]

Calculates a clock divider (CD) for the USART synchronous master modes to generate a baud rate as close as possible to the baud rate set point.

Baud rate calculation: $ Baudrate = \frac{SelectedClock}{CD} $.

Parameters:
usart Base address of the USART instance.
baudrate Baud rate set point.
pba_hz USART module input clock frequency (PBA clock, Hz).
Return values:
USART_SUCCESS Baud rate successfully initialized.
USART_INVALID_INPUT Baud rate set point is out of range for the given input clock frequency.

Definition at line 126 of file usart.c.

References USART_INVALID_INPUT, and USART_SUCCESS.

Referenced by usart_init_rs232_tx_only(), and usart_init_sync_master().

00127 {
00128   unsigned int cd = (pba_hz + baudrate / 2) / baudrate;
00129 
00130   if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
00131     return USART_INVALID_INPUT;
00132 
00133   usart->mr = (usart->mr & ~AVR32_USART_MR_USCLKS_MASK) |
00134               AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
00135               AVR32_USART_MR_SYNC_MASK;
00136 
00137   usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET;
00138 
00139   return USART_SUCCESS;
00140 }

static int usart_set_sync_slave_baudrate ( volatile avr32_usart_t *  usart  )  [static]

Selects the SCK pin as the source of baud rate for the USART synchronous slave modes.

Parameters:
usart Base address of the USART instance.
Return values:
USART_SUCCESS Baud rate successfully initialized.

Definition at line 150 of file usart.c.

References USART_SUCCESS.

Referenced by usart_init_sync_slave().

00151 {
00152   usart->mr = (usart->mr & ~AVR32_USART_MR_USCLKS_MASK) |
00153               AVR32_USART_MR_USCLKS_SCK << AVR32_USART_MR_USCLKS_OFFSET |
00154               AVR32_USART_MR_SYNC_MASK;
00155 
00156   return USART_SUCCESS;
00157 }

int usart_write_char ( volatile avr32_usart_t *  usart,
int  c 
)

Writes the given character to the TX buffer if the transmitter is ready.

Parameters:
usart Base address of the USART instance.
c The character (up to 9 bits) to transmit.
Return values:
USART_SUCCESS The transmitter was ready.
USART_TX_BUSY The transmitter was busy.

Definition at line 806 of file usart.c.

References USART_SUCCESS, USART_TX_BUSY, and usart_tx_ready().

Referenced by usart_bw_write_char(), and usart_putchar().

00807 {
00808   if (usart_tx_ready(usart))
00809   {
00810     usart->thr = (c << AVR32_USART_THR_TXCHR_OFFSET) & AVR32_USART_THR_TXCHR_MASK;
00811     return USART_SUCCESS;
00812   }
00813   else
00814     return USART_TX_BUSY;
00815 }

void usart_write_line ( volatile avr32_usart_t *  usart,
const char *  string 
)

Writes one character string to the USART.

Parameters:
usart Base address of the USART instance.
string String to be written.

Definition at line 874 of file usart.c.

References usart_putchar().

Referenced by main(), and usart_get_echo_line().

00875 {
00876   while (*string != '\0')
00877     usart_putchar(usart, *string++);
00878 }


Generated on Fri Feb 19 02:27:13 2010 for AVR32 - USART Driver - SPI Example by  doxygen 1.5.5