This file contains basic functions for the AVR32 USART, with support for all modes, settings and clock speeds.
Definition in file usart.h.
#include <avr32/io.h>
#include "compiler.h"
Go to the source code of this file.
Data Structures | |
struct | usart_iso7816_options_t |
Input parameters when initializing ISO7816 mode. More... | |
struct | usart_options_t |
Input parameters when initializing RS232 and similar modes. More... | |
Defines | |
#define | USART_DEFAULT_TIMEOUT 10000 |
Default time-out value (number of attempts). | |
Stop Bits Settings | |
#define | USART_1_5_STOPBITS AVR32_USART_MR_NBSTOP_1_5 |
Use 1.5 stop bits. | |
#define | USART_1_STOPBIT AVR32_USART_MR_NBSTOP_1 |
Use 1 stop bit. | |
#define | USART_2_STOPBITS AVR32_USART_MR_NBSTOP_2 |
Use 2 stop bits (for more, just give the number of bits). | |
Channel Modes | |
#define | USART_AUTO_ECHO AVR32_USART_MR_CHMODE_ECHO |
Echo data. | |
#define | USART_LOCAL_LOOPBACK AVR32_USART_MR_CHMODE_LOCAL_LOOP |
Local loopback. | |
#define | USART_NORMAL_CHMODE AVR32_USART_MR_CHMODE_NORMAL |
Normal communication. | |
#define | USART_REMOTE_LOOPBACK AVR32_USART_MR_CHMODE_REMOTE_LOOP |
Remote loopback. | |
Parity Settings | |
#define | USART_EVEN_PARITY AVR32_USART_MR_PAR_EVEN |
Use even parity on character transmission. | |
#define | USART_MARK_PARITY AVR32_USART_MR_PAR_MARK |
Use a mark as parity bit. | |
#define | USART_MULTIDROP_PARITY AVR32_USART_MR_PAR_MULTI |
Parity bit is used to flag address characters. | |
#define | USART_NO_PARITY AVR32_USART_MR_PAR_NONE |
Don't use a parity bit. | |
#define | USART_ODD_PARITY AVR32_USART_MR_PAR_ODD |
Use odd parity on character transmission. | |
#define | USART_SPACE_PARITY AVR32_USART_MR_PAR_SPACE |
Use a space as parity bit. | |
Return Values | |
#define | USART_FAILURE -1 |
Failure because of some unspecified reason. | |
#define | USART_INVALID_ARGUMENT -1 |
Argument value out of range. | |
#define | USART_INVALID_INPUT 1 |
Input value out of range. | |
#define | USART_MODE_FAULT 5 |
USART not in the appropriate mode. | |
#define | USART_RX_EMPTY 3 |
Nothing was received. | |
#define | USART_RX_ERROR 4 |
Transmission error occurred. | |
#define | USART_SUCCESS 0 |
Successful completion. | |
#define | USART_TX_BUSY 2 |
Transmitter was busy. | |
Functions | |
Transmit/Receive Functions | |
__inline__ void | usart_bw_write_char (volatile avr32_usart_t *usart, int c) |
An active wait writing a character to the USART. | |
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. | |
__inline__ int | usart_test_hit (volatile avr32_usart_t *usart) |
Tests if the USART contains a received character. | |
__inline__ int | usart_tx_empty (volatile avr32_usart_t *usart) |
Tests if all requested USART transmissions are over. | |
__inline__ int | usart_tx_ready (volatile avr32_usart_t *usart) |
Tests if the USART is ready to transmit a character. | |
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. | |
Read and Reset Error Status Bits | |
__inline__ int | usart_framing_error (volatile avr32_usart_t *usart) |
Checks if a framing error has occurred since last status reset. | |
__inline__ int | usart_overrun_error (volatile avr32_usart_t *usart) |
Checks if an overrun error has occurred since last status reset. | |
__inline__ int | usart_parity_error (volatile avr32_usart_t *usart) |
Checks if a parity error has occurred since last status reset. | |
__inline__ void | usart_reset_status (volatile avr32_usart_t *usart) |
Resets the error status. | |
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. | |
ISO7816 Control Functions | |
__inline__ void | usart_iso7816_enable_receiver (volatile avr32_usart_t *usart) |
Enables the ISO7816 receiver. | |
__inline__ void | usart_iso7816_enable_transmitter (volatile avr32_usart_t *usart) |
Enables the ISO7816 transmitter. |
#define USART_1_5_STOPBITS AVR32_USART_MR_NBSTOP_1_5 |
#define USART_1_STOPBIT AVR32_USART_MR_NBSTOP_1 |
#define USART_2_STOPBITS AVR32_USART_MR_NBSTOP_2 |
Use 2 stop bits (for more, just give the number of bits).
Definition at line 88 of file usart.h.
Referenced by usart_init_rs232(), usart_init_rs232_tx_only(), usart_init_sync_master(), and usart_init_sync_slave().
#define USART_AUTO_ECHO AVR32_USART_MR_CHMODE_ECHO |
#define USART_DEFAULT_TIMEOUT 10000 |
Default time-out value (number of attempts).
Definition at line 70 of file usart.h.
Referenced by usart_putchar().
#define USART_EVEN_PARITY AVR32_USART_MR_PAR_EVEN |
#define USART_FAILURE -1 |
Failure because of some unspecified reason.
Definition at line 60 of file usart.h.
Referenced by main(), usart_get_echo_line(), usart_getchar(), and usart_putchar().
#define USART_INVALID_ARGUMENT -1 |
#define USART_INVALID_INPUT 1 |
Input value out of range.
Definition at line 61 of file usart.h.
Referenced by usart_init_hw_handshaking(), usart_init_IrDA(), usart_init_iso7816(), usart_init_modem(), usart_init_rs232(), usart_init_rs232_tx_only(), usart_init_rs485(), usart_init_sync_master(), usart_init_sync_slave(), usart_set_async_baudrate(), usart_set_iso7816_clock(), and usart_set_sync_master_baudrate().
#define USART_LOCAL_LOOPBACK AVR32_USART_MR_CHMODE_LOCAL_LOOP |
#define USART_MARK_PARITY AVR32_USART_MR_PAR_MARK |
#define USART_MODE_FAULT 5 |
USART not in the appropriate mode.
Definition at line 66 of file usart.h.
Referenced by usart_send_address().
#define USART_MULTIDROP_PARITY AVR32_USART_MR_PAR_MULTI |
#define USART_NO_PARITY AVR32_USART_MR_PAR_NONE |
#define USART_NORMAL_CHMODE AVR32_USART_MR_CHMODE_NORMAL |
#define USART_ODD_PARITY AVR32_USART_MR_PAR_ODD |
#define USART_REMOTE_LOOPBACK AVR32_USART_MR_CHMODE_REMOTE_LOOP |
#define USART_RX_EMPTY 3 |
Nothing was received.
Definition at line 64 of file usart.h.
Referenced by usart_getchar(), and usart_read_char().
#define USART_RX_ERROR 4 |
Transmission error occurred.
Definition at line 65 of file usart.h.
Referenced by usart_getchar(), and usart_read_char().
#define USART_SPACE_PARITY AVR32_USART_MR_PAR_SPACE |
#define USART_SUCCESS 0 |
Successful completion.
Definition at line 59 of file usart.h.
Referenced by usart_bw_write_char(), usart_get_echo_line(), usart_init_hw_handshaking(), usart_init_IrDA(), usart_init_iso7816(), usart_init_modem(), usart_init_rs232(), usart_init_rs232_tx_only(), usart_init_rs485(), usart_init_sync_master(), usart_init_sync_slave(), usart_putchar(), usart_read_char(), usart_send_address(), usart_set_async_baudrate(), usart_set_iso7816_clock(), usart_set_sync_master_baudrate(), usart_set_sync_slave_baudrate(), and usart_write_char().
#define USART_TX_BUSY 2 |
__inline__ void usart_bw_write_char | ( | volatile avr32_usart_t * | usart, | |
int | c | |||
) |
An active wait writing a character to the USART.
usart | Base address of the USART instance. | |
c | The character (up to 9 bits) to transmit. |
Definition at line 763 of file usart.h.
References USART_SUCCESS, and usart_write_char().
Referenced by usart_send_address().
00764 { 00765 while (usart_write_char(usart, c) != USART_SUCCESS); 00766 }
__inline__ int usart_framing_error | ( | volatile avr32_usart_t * | usart | ) |
int usart_get_echo_line | ( | volatile avr32_usart_t * | usart | ) |
Gets and echoes characters until end of line.
usart | Base address of the USART instance. |
USART_SUCCESS | Success. | |
USART_FAILURE | Low-level error detected or ETX character received. |
Definition at line 878 of file usart.c.
References USART_FAILURE, usart_getchar(), usart_putchar(), USART_SUCCESS, and usart_write_line().
Referenced by main().
00879 { 00880 int rx_char; 00881 int retval = USART_SUCCESS; 00882 00883 while (1) 00884 { 00885 rx_char = usart_getchar(usart); 00886 if (rx_char == USART_FAILURE) 00887 { 00888 usart_write_line(usart, "Error!!!\n"); 00889 retval = USART_FAILURE; 00890 break; 00891 } 00892 if (rx_char == '\x03') 00893 { 00894 retval = USART_FAILURE; 00895 break; 00896 } 00897 usart_putchar(usart, rx_char); 00898 if (rx_char == '\r') 00899 { 00900 usart_putchar(usart, '\n'); 00901 break; 00902 } 00903 } 00904 00905 return retval; 00906 }
int usart_getchar | ( | volatile avr32_usart_t * | usart | ) |
Waits until a character is received, and returns it.
usart | Base address of the USART instance. |
Definition at line 858 of file usart.c.
References USART_FAILURE, usart_read_char(), USART_RX_EMPTY, and USART_RX_ERROR.
Referenced by usart_get_echo_line().
00859 { 00860 int c, ret; 00861 00862 while ((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY); 00863 00864 if (ret == USART_RX_ERROR) 00865 return USART_FAILURE; 00866 00867 return c; 00868 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 390 of file usart.c.
References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.
00391 { 00392 // First: Setup standard RS232. 00393 if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT) 00394 return USART_INVALID_INPUT; 00395 00396 // Set hardware handshaking mode. 00397 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00398 AVR32_USART_MR_MODE_HARDWARE << AVR32_USART_MR_MODE_OFFSET; 00399 00400 return USART_SUCCESS; 00401 }
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.
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. |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 537 of file usart.c.
References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.
00539 { 00540 // First: Setup standard RS232. 00541 if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT) 00542 return USART_INVALID_INPUT; 00543 00544 // Set IrDA filter. 00545 usart->ifr = irda_filter; 00546 00547 // Set IrDA mode and activate filtering of input. 00548 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00549 AVR32_USART_MODE_IRDA << AVR32_USART_MR_MODE_OFFSET | 00550 AVR32_USART_MR_FILTER_MASK; 00551 00552 return USART_SUCCESS; 00553 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 556 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.
00557 { 00558 // Reset the USART and shutdown TX and RX. 00559 usart_reset(usart); 00560 00561 // Check input values. 00562 if (!opt || // Null pointer. 00563 opt->paritytype > 1) 00564 return USART_INVALID_INPUT; 00565 00566 if (t == 0) 00567 { 00568 // Set USART mode to ISO7816, T=0. 00569 // The T=0 protocol always uses 2 stop bits. 00570 usart->mr = AVR32_USART_MR_MODE_ISO7816_T0 << AVR32_USART_MR_MODE_OFFSET | 00571 AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET | 00572 opt->bit_order << AVR32_USART_MR_MSBF_OFFSET; // Allow MSBF in T=0. 00573 } 00574 else if (t == 1) 00575 { 00576 // Only LSB first in the T=1 protocol. 00577 // max_iterations field is only used in T=0 mode. 00578 if (opt->bit_order != 0 || 00579 opt->max_iterations != 0) 00580 return USART_INVALID_INPUT; 00581 00582 // Set USART mode to ISO7816, T=1. 00583 // The T=1 protocol always uses 1 stop bit. 00584 usart->mr = AVR32_USART_MR_MODE_ISO7816_T1 << AVR32_USART_MR_MODE_OFFSET | 00585 AVR32_USART_MR_NBSTOP_1 << AVR32_USART_MR_NBSTOP_OFFSET; 00586 } 00587 else 00588 return USART_INVALID_INPUT; 00589 00590 if (usart_set_iso7816_clock(usart, opt->iso7816_hz, pba_hz) == USART_INVALID_INPUT) 00591 return USART_INVALID_INPUT; 00592 00593 // Set FIDI register: bit rate = selected clock/FI_DI_ratio/16. 00594 usart->fidi = opt->fidi_ratio; 00595 00596 // Set ISO7816 spesific options in the MODE register. 00597 usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET | 00598 AVR32_USART_MR_CLKO_MASK | // Enable clock output. 00599 opt->inhibit_nack << AVR32_USART_MR_INACK_OFFSET | 00600 opt->dis_suc_nack << AVR32_USART_MR_DSNACK_OFFSET | 00601 opt->max_iterations << AVR32_USART_MR_MAX_ITERATION_OFFSET; 00602 00603 // Setup complete; enable the receiver by default. 00604 usart_iso7816_enable_receiver(usart); 00605 00606 return USART_SUCCESS; 00607 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 404 of file usart.c.
References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.
00405 { 00406 // First: Setup standard RS232. 00407 if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT) 00408 return USART_INVALID_INPUT; 00409 00410 // Set modem mode. 00411 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00412 AVR32_USART_MR_MODE_MODEM << AVR32_USART_MR_MODE_OFFSET; 00413 00414 return USART_SUCCESS; 00415 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 286 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().
00287 { 00288 // Reset the USART and shutdown TX and RX. 00289 usart_reset(usart); 00290 00291 // Check input values. 00292 if (!opt || // Null pointer. 00293 opt->charlength < 5 || opt->charlength > 9 || 00294 opt->paritytype > 7 || 00295 opt->stopbits > 2 + 255 || 00296 opt->channelmode > 3 || 00297 usart_set_async_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT) 00298 return USART_INVALID_INPUT; 00299 00300 if (opt->charlength == 9) 00301 { 00302 // Character length set to 9 bits. MODE9 dominates CHRL. 00303 usart->mr |= AVR32_USART_MR_MODE9_MASK; 00304 } 00305 else 00306 { 00307 // CHRL gives the character length (- 5) when MODE9 = 0. 00308 usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET; 00309 } 00310 00311 usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET | 00312 opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET; 00313 00314 if (opt->stopbits > USART_2_STOPBITS) 00315 { 00316 // Set two stop bits 00317 usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET; 00318 // and a timeguard period gives the rest. 00319 usart->ttgr = opt->stopbits - USART_2_STOPBITS; 00320 } 00321 else 00322 // Insert 1, 1.5 or 2 stop bits. 00323 usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET; 00324 00325 // Set normal mode. 00326 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00327 AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET; 00328 00329 // Setup complete; enable communication. 00330 // Enable input and output. 00331 usart->cr = AVR32_USART_CR_RXEN_MASK | 00332 AVR32_USART_CR_TXEN_MASK; 00333 00334 return USART_SUCCESS; 00335 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
1.5
stop bit is not supported in this mode. Definition at line 338 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.
00339 { 00340 // Reset the USART and shutdown TX and RX. 00341 usart_reset(usart); 00342 00343 // Check input values. 00344 if (!opt || // Null pointer. 00345 opt->charlength < 5 || opt->charlength > 9 || 00346 opt->paritytype > 7 || 00347 opt->stopbits == 1 || opt->stopbits > 2 + 255 || 00348 opt->channelmode > 3 || 00349 usart_set_sync_master_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT) 00350 return USART_INVALID_INPUT; 00351 00352 if (opt->charlength == 9) 00353 { 00354 // Character length set to 9 bits. MODE9 dominates CHRL. 00355 usart->mr |= AVR32_USART_MR_MODE9_MASK; 00356 } 00357 else 00358 { 00359 // CHRL gives the character length (- 5) when MODE9 = 0. 00360 usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET; 00361 } 00362 00363 usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET | 00364 opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET; 00365 00366 if (opt->stopbits > USART_2_STOPBITS) 00367 { 00368 // Set two stop bits 00369 usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET; 00370 // and a timeguard period gives the rest. 00371 usart->ttgr = opt->stopbits - USART_2_STOPBITS; 00372 } 00373 else 00374 // Insert 1 or 2 stop bits. 00375 usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET; 00376 00377 // Set normal mode. 00378 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00379 AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET; 00380 00381 // Setup complete; enable communication. 00382 // Enable only output as input is not possible in synchronous mode without 00383 // transferring clock. 00384 usart->cr = AVR32_USART_CR_TXEN_MASK; 00385 00386 return USART_SUCCESS; 00387 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 523 of file usart.c.
References usart_init_rs232(), USART_INVALID_INPUT, and USART_SUCCESS.
00524 { 00525 // First: Setup standard RS232. 00526 if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT) 00527 return USART_INVALID_INPUT; 00528 00529 // Set RS485 mode. 00530 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00531 AVR32_USART_MR_MODE_RS485 << AVR32_USART_MR_MODE_OFFSET; 00532 00533 return USART_SUCCESS; 00534 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 418 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.
00419 { 00420 // Reset the USART and shutdown TX and RX. 00421 usart_reset(usart); 00422 00423 // Check input values. 00424 if (!opt || // Null pointer. 00425 opt->charlength < 5 || opt->charlength > 9 || 00426 opt->paritytype > 7 || 00427 opt->stopbits == 1 || opt->stopbits > 2 + 255 || 00428 opt->channelmode > 3 || 00429 usart_set_sync_master_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT) 00430 return USART_INVALID_INPUT; 00431 00432 if (opt->charlength == 9) 00433 { 00434 // Character length set to 9 bits. MODE9 dominates CHRL. 00435 usart->mr |= AVR32_USART_MR_MODE9_MASK; 00436 } 00437 else 00438 { 00439 // CHRL gives the character length (- 5) when MODE9 = 0. 00440 usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET; 00441 } 00442 00443 usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET | 00444 opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET; 00445 00446 if (opt->stopbits > USART_2_STOPBITS) 00447 { 00448 // Set two stop bits 00449 usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET; 00450 // and a timeguard period gives the rest. 00451 usart->ttgr = opt->stopbits - USART_2_STOPBITS; 00452 } 00453 else 00454 // Insert 1 or 2 stop bits. 00455 usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET; 00456 00457 // Set normal mode. 00458 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00459 AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET | 00460 AVR32_USART_MR_CLKO_MASK; 00461 00462 // Setup complete; enable communication. 00463 // Enable input and output. 00464 usart->cr = AVR32_USART_CR_RXEN_MASK | 00465 AVR32_USART_CR_TXEN_MASK; 00466 00467 return USART_SUCCESS; 00468 }
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.
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). |
USART_SUCCESS | Mode successfully initialized. | |
USART_INVALID_INPUT | One or more of the arguments is out of valid range. |
Definition at line 471 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.
00472 { 00473 // Reset the USART and shutdown TX and RX. 00474 usart_reset(usart); 00475 00476 // Check input values. 00477 if (!opt || // Null pointer. 00478 opt->charlength < 5 || opt->charlength > 9 || 00479 opt->paritytype > 7 || 00480 opt->stopbits == 1 || opt->stopbits > 2 + 255 || 00481 opt->channelmode > 3 || 00482 usart_set_sync_slave_baudrate(usart) == USART_INVALID_INPUT) 00483 return USART_INVALID_INPUT; 00484 00485 if (opt->charlength == 9) 00486 { 00487 // Character length set to 9 bits. MODE9 dominates CHRL. 00488 usart->mr |= AVR32_USART_MR_MODE9_MASK; 00489 } 00490 else 00491 { 00492 // CHRL gives the character length (- 5) when MODE9 = 0. 00493 usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET; 00494 } 00495 00496 usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET | 00497 opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET; 00498 00499 if (opt->stopbits > USART_2_STOPBITS) 00500 { 00501 // Set two stop bits 00502 usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET; 00503 // and a timeguard period gives the rest. 00504 usart->ttgr = opt->stopbits - USART_2_STOPBITS; 00505 } 00506 else 00507 // Insert 1 or 2 stop bits. 00508 usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET; 00509 00510 // Set normal mode. 00511 usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | 00512 AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET; 00513 00514 // Setup complete; enable communication. 00515 // Enable input and output. 00516 usart->cr = AVR32_USART_CR_RXEN_MASK | 00517 AVR32_USART_CR_TXEN_MASK; 00518 00519 return USART_SUCCESS; 00520 }
__inline__ void usart_iso7816_enable_receiver | ( | volatile avr32_usart_t * | usart | ) |
Enables the ISO7816 receiver.
The ISO7816 transmitter is disabled.
usart | Base address of the USART instance. |
Definition at line 491 of file usart.h.
Referenced by usart_init_iso7816().
__inline__ void usart_iso7816_enable_transmitter | ( | volatile avr32_usart_t * | usart | ) |
__inline__ int usart_overrun_error | ( | volatile avr32_usart_t * | usart | ) |
__inline__ int usart_parity_error | ( | volatile avr32_usart_t * | usart | ) |
int usart_putchar | ( | volatile avr32_usart_t * | usart, | |
int | c | |||
) |
Sends a character with the USART.
usart | Base address of the USART instance. | |
c | Character to write. |
USART_SUCCESS | The character was written. | |
USART_FAILURE | The function timed out before the USART transmitter became ready to send. |
Definition at line 815 of file usart.c.
References USART_DEFAULT_TIMEOUT, USART_FAILURE, USART_SUCCESS, and usart_write_char().
Referenced by usart_get_echo_line(), and usart_write_line().
00816 { 00817 int timeout = USART_DEFAULT_TIMEOUT; 00818 00819 if (c == '\n') 00820 { 00821 do 00822 { 00823 if (!timeout--) return USART_FAILURE; 00824 } while (usart_write_char(usart, '\r') != USART_SUCCESS); 00825 00826 timeout = USART_DEFAULT_TIMEOUT; 00827 } 00828 00829 do 00830 { 00831 if (!timeout--) return USART_FAILURE; 00832 } while (usart_write_char(usart, c) != USART_SUCCESS); 00833 00834 return USART_SUCCESS; 00835 }
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.
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). |
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 838 of file usart.c.
References USART_RX_EMPTY, USART_RX_ERROR, USART_SUCCESS, and usart_test_hit().
Referenced by usart_getchar().
00839 { 00840 // Check for errors: frame, parity and overrun. In RS485 mode, a parity error 00841 // would mean that an address char has been received. 00842 if (usart->csr & (AVR32_USART_CSR_OVRE_MASK | 00843 AVR32_USART_CSR_FRAME_MASK | 00844 AVR32_USART_CSR_PARE_MASK)) 00845 return USART_RX_ERROR; 00846 00847 // No error; if we really did receive a char, read it and return SUCCESS. 00848 if (usart_test_hit(usart)) 00849 { 00850 *c = (usart->rhr & AVR32_USART_RHR_RXCHR_MASK) >> AVR32_USART_RHR_RXCHR_OFFSET; 00851 return USART_SUCCESS; 00852 } 00853 else 00854 return USART_RX_EMPTY; 00855 }
void usart_reset | ( | volatile avr32_usart_t * | usart | ) |
Resets the USART and disables TX and RX.
usart | Base address of the USART instance. |
Definition at line 255 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().
00256 { 00257 Bool global_interrupt_enabled = Is_global_interrupt_enabled(); 00258 00259 // Disable all USART interrupts. 00260 // Interrupts needed should be set explicitly on every reset. 00261 if (global_interrupt_enabled) Disable_global_interrupt(); 00262 usart->idr = 0xFFFFFFFF; 00263 usart->csr; 00264 if (global_interrupt_enabled) Enable_global_interrupt(); 00265 00266 // Reset mode and other registers that could cause unpredictable behavior after reset. 00267 usart->mr = 0; 00268 usart->rtor = 0; 00269 usart->ttgr = 0; 00270 00271 // Shutdown TX and RX (will be re-enabled when setup has successfully completed), 00272 // reset status bits and turn off DTR and RTS. 00273 usart->cr = AVR32_USART_CR_RSTRX_MASK | 00274 AVR32_USART_CR_RSTTX_MASK | 00275 AVR32_USART_CR_RSTSTA_MASK | 00276 AVR32_USART_CR_RSTIT_MASK | 00277 AVR32_USART_CR_RSTNACK_MASK | 00278 #ifndef AVR32_USART_440_H_INCLUDED 00279 // Note: Modem Signal Management DTR-DSR-DCD-RI are not included in USART rev.440. 00280 AVR32_USART_CR_DTRDIS_MASK | 00281 #endif 00282 AVR32_USART_CR_RTSDIS_MASK; 00283 }
__inline__ void usart_reset_status | ( | volatile avr32_usart_t * | usart | ) |
Resets the error status.
This function resets the status bits indicating that a parity error, framing error or overrun has occurred. The RXBRK bit, indicating a start/end of break condition on the RX line, is also reset.
usart | Base address of the USART instance. |
Definition at line 402 of file usart.h.
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.
usart | Base address of the USART instance. | |
address | Address of the target device. |
USART_SUCCESS | Address successfully sent (if current mode is RS485). | |
USART_MODE_FAULT | Wrong operating mode. |
Definition at line 788 of file usart.c.
References usart_bw_write_char(), USART_MODE_FAULT, usart_mode_is_multidrop(), and USART_SUCCESS.
00789 { 00790 // Check if USART is in multidrop / RS485 mode. 00791 if (!usart_mode_is_multidrop(usart)) return USART_MODE_FAULT; 00792 00793 // Prepare to send an address. 00794 usart->cr = AVR32_USART_CR_SENDA_MASK; 00795 00796 // Write the address to TX. 00797 usart_bw_write_char(usart, address); 00798 00799 return USART_SUCCESS; 00800 }
__inline__ int usart_test_hit | ( | volatile avr32_usart_t * | usart | ) |
Tests if the USART contains a received character.
usart | Base address of the USART instance. |
1
if the USART Receive Holding Register is full, otherwise 0
. Definition at line 802 of file usart.h.
Referenced by usart_read_char().
__inline__ int usart_tx_empty | ( | volatile avr32_usart_t * | usart | ) |
__inline__ int usart_tx_ready | ( | volatile avr32_usart_t * | usart | ) |
Tests if the USART is ready to transmit a character.
usart | Base address of the USART instance. |
1
if the USART Transmit Holding Register is free, otherwise 0
. Definition at line 740 of file usart.h.
Referenced by usart_write_char().
int usart_write_char | ( | volatile avr32_usart_t * | usart, | |
int | c | |||
) |
Writes the given character to the TX buffer if the transmitter is ready.
usart | Base address of the USART instance. | |
c | The character (up to 9 bits) to transmit. |
USART_SUCCESS | The transmitter was ready. | |
USART_TX_BUSY | The transmitter was busy. |
Definition at line 803 of file usart.c.
References USART_SUCCESS, USART_TX_BUSY, and usart_tx_ready().
Referenced by usart_bw_write_char(), and usart_putchar().
00804 { 00805 if (usart_tx_ready(usart)) 00806 { 00807 usart->thr = (c << AVR32_USART_THR_TXCHR_OFFSET) & AVR32_USART_THR_TXCHR_MASK; 00808 return USART_SUCCESS; 00809 } 00810 else 00811 return USART_TX_BUSY; 00812 }
void usart_write_line | ( | volatile avr32_usart_t * | usart, | |
const char * | string | |||
) |
Writes one character string to the USART.
usart | Base address of the USART instance. | |
string | String to be written. |
Definition at line 871 of file usart.c.
References usart_putchar().
Referenced by main(), and usart_get_echo_line().
00872 { 00873 while (*string != '\0') 00874 usart_putchar(usart, *string++); 00875 }