This file gives an example of using the MMU to manage the memory on AVR32 MCUs.
$Name$
Definition in file usart.h.
#include <avr32/io.h>
#include <stdio.h>
#include "settings.h"


Go to the source code of this file.
Data Structures | |
| struct | usart_options_t |
| Input parameters when initializing rs232 mode. More... | |
Defines | |
| #define | USART_1_5_STOPBITS 1 |
| Use 1.5 stop bits. | |
| #define | USART_1_STOPBIT 0 |
| Use 1 stop bit. | |
| #define | USART_2_STOPBITS 2 |
| Use 2 stop bits (for more, just give the number of bits). | |
| #define | USART_ADDR_RECEIVED 1 |
| Value returned by receiver function when an address character was received. | |
| #define | USART_AUTO_ECHO 1 |
| Set usart channel to echo data. | |
| #define | USART_DEFAULT_TIMEOUT 10000 |
| Default timeout value; number of tries before timing out. | |
| #define | USART_ERROR_ARGUMENT 1 |
| Value returned by function when the input paramters are out of range. | |
| #define | USART_EVEN_PARITY 0 |
| Use even parity on character transmission. | |
| #define | USART_FAILURE -1 |
| Value returned by function when it was unable to complete successfully for some unspecified reason. | |
| #define | USART_LOCAL_LOOPBACK 2 |
| Set usart channel to local loopback. | |
| #define | USART_MARK_PARITY 3 |
| Use a mark as parity bit. | |
| #define | USART_MODE_FAULT 5 |
| Value returned by a function when the usart is not in the appropriate mode. | |
| #define | USART_MODE_HW_HSH 0x02 |
| Operate usart in rs232 with hardware handshaking. | |
| #define | USART_MODE_IRDA 0x08 |
| Oparte usart in irda mode. | |
| #define | USART_MODE_ISO7816_T0 0x04 |
| Operate usart in iso7816, T=0 mode. | |
| #define | USART_MODE_ISO7816_T1 0x06 |
| Operate usart in iso7816, T=1 mode. | |
| #define | USART_MODE_MODEM 0x03 |
| Operate usart in modem mode. | |
| #define | USART_MODE_NORMAL 0x00 |
| Operate usart in normal rs232 mode. | |
| #define | USART_MODE_RS485 0x01 |
| Operate usart in rs485 mode. | |
| #define | USART_MODE_SW_HSH 0x0C |
| Operate usart in rs232 with software handshaking. | |
| #define | USART_MULTIDROP_PARITY 6 |
| Parity bit is used to flag address characters. | |
| #define | USART_NO_PARITY 4 |
| Don't use a parity bit. | |
| #define | USART_NORMAL_CHMODE 0 |
| Set usart channel to normal communcation. | |
| #define | USART_ODD_PARITY 1 |
| Use odd parity on character transmission. | |
| #define | USART_OK 0 |
| Value returned by function when it completed successfully. | |
| #define | USART_REMOTE_LOOPBACK 3 |
| Set usart channel to remote loopback. | |
| #define | USART_RX_EMPTY 3 |
| Value returned by receiver function when nothing was received. | |
| #define | USART_RX_ERROR 4 |
| Value returned by receiver function when transmission error occured. | |
| #define | USART_SPACE_PARITY 2 |
| Use a space as parity bit. | |
| #define | USART_TX_BUSY 2 |
| Value returned by transmission function when transmitter was busy. | |
Functions | |
| int | _write_r (struct _reent *ptr, int fd, const void *buf, size_t cnt) |
| Function to overload the standard _write_r, makes it possible to use printf() to output data. | |
| int | usart_linit (volatile avr32_usart_t *usart, const struct usart_options_t *opt, const long cpuHz) |
| Setup the usart to use the standard RS232 protocol. | |
| int | usart_putchar (volatile avr32_usart_t *usart, const short character) |
| Send a character with the usart. | |
| void | usart_reset (volatile avr32_usart_t *usart) |
| This function will reset the USART, and disable TX and RX. | |
| #define USART_1_STOPBIT 0 |
| #define USART_2_STOPBITS 2 |
| #define USART_ADDR_RECEIVED 1 |
| #define USART_DEFAULT_TIMEOUT 10000 |
Default timeout value; number of tries before timing out.
Definition at line 82 of file usart.h.
Referenced by usart_putchar().
| #define USART_ERROR_ARGUMENT 1 |
Value returned by function when the input paramters are out of range.
Definition at line 65 of file usart.h.
Referenced by usart_linit(), and usart_set_baudrate().
| #define USART_EVEN_PARITY 0 |
| #define USART_FAILURE -1 |
| #define USART_LOCAL_LOOPBACK 2 |
| #define USART_MODE_FAULT 5 |
| #define USART_MODE_HW_HSH 0x02 |
| #define USART_MODE_ISO7816_T0 0x04 |
| #define USART_MODE_ISO7816_T1 0x06 |
| #define USART_MODE_NORMAL 0x00 |
| #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_OK 0 |
Value returned by function when it completed successfully.
Definition at line 58 of file usart.h.
Referenced by main(), usart_linit(), usart_putchar(), and usart_set_baudrate().
| #define USART_REMOTE_LOOPBACK 3 |
| #define USART_RX_EMPTY 3 |
| #define USART_RX_ERROR 4 |
| #define USART_TX_BUSY 2 |
Value returned by transmission function when transmitter was busy.
Definition at line 67 of file usart.h.
Referenced by usart_putchar().
| int _write_r | ( | struct _reent * | ptr, | |
| int | fd, | |||
| const void * | buf, | |||
| size_t | cnt | |||
| ) |
Function to overload the standard _write_r, makes it possible to use printf() to output data.
| ptr | not used | |
| fd | not used | |
| buf | the buffer to send | |
| cnt | number of bytes to send |
Definition at line 252 of file usart.c.
References USART_MODULE, and usart_putchar().
00253 { 00254 volatile char *msg = (char *) buf; 00255 volatile int i; 00256 00257 for (i=0; i < cnt; i++) { 00258 usart_putchar(&USART_MODULE, *msg++); 00259 } 00260 00261 return cnt; 00262 }

| int usart_linit | ( | volatile avr32_usart_t * | usart, | |
| const struct usart_options_t * | opt, | |||
| const long | cpuHz | |||
| ) |
Setup the usart to use the standard RS232 protocol.
| usart | base address of the usart | |
| opt | options needed to set up RS232 communcation (see usart_options_t) | |
| cpuHz | frequency of the selected clock |
| USART_OK | on success | |
| USART_ERROR_ARGUMENT | on wrong arguments |
Definition at line 152 of file usart.c.
References usart_options_t::baudrate, usart_options_t::channelmode, usart_options_t::charlength, usart_options_t::paritytype, usart_options_t::stopbits, USART_ERROR_ARGUMENT, USART_OK, usart_reset(), and usart_set_baudrate().
00155 { 00156 int retval; 00157 00158 /* reset the usart and shutdown RX and TX */ 00159 usart_reset(usart); 00160 00161 /* control input values */ 00162 if (opt == 0) { 00163 return USART_ERROR_ARGUMENT; 00164 } 00165 if (opt->charlength < 5 || opt->charlength > 9) { 00166 return USART_ERROR_ARGUMENT; 00167 } 00168 if (opt->paritytype > 7) { 00169 return USART_ERROR_ARGUMENT; 00170 } 00171 if (opt->stopbits > 2+255) { 00172 return USART_ERROR_ARGUMENT; 00173 } 00174 if (opt->channelmode > 3) { 00175 return USART_ERROR_ARGUMENT; 00176 } 00177 00178 retval = usart_set_baudrate(usart, opt->baudrate, cpuHz); 00179 00180 if (retval != USART_OK) { 00181 return retval; 00182 } 00183 00184 if (opt->charlength == 9) { 00185 /* charlength set to 9 bits; MODE9 dominates CHRL */ 00186 usart->mr |= AVR32_USART_MR_MODE9_MASK; 00187 } else { 00188 /* CHRL gives the charlength(- 5) when USART_MODE9=0 */ 00189 usart->mr |= 00190 ((opt->charlength-5)<<AVR32_USART_MR_CHRL_OFFSET); 00191 } 00192 00193 usart->mr |= (opt->channelmode<<AVR32_USART_MR_CHMODE_OFFSET)| 00194 (opt->paritytype<<AVR32_USART_MR_PAR_OFFSET); 00195 00196 if (opt->stopbits > 2) { 00197 /* set two stop bits */ 00198 usart->mr |= (2<<AVR32_USART_MR_NBSTOP_OFFSET); 00199 /* and a timeguard period gives the rest */ 00200 usart->ttgr = (opt->stopbits-2); 00201 } else { 00202 /* insert 1, 1.5 or 2 stop bits */ 00203 usart->mr |= (opt->stopbits<<AVR32_USART_MR_NBSTOP_OFFSET); 00204 } 00205 00206 /* enable TX and RX */ 00207 usart->cr |= AVR32_USART_CR_TXEN_MASK|AVR32_USART_CR_RXEN_MASK; 00208 00209 return USART_OK; 00210 }

| int usart_putchar | ( | volatile avr32_usart_t * | usart, | |
| const short | character | |||
| ) |
Send a character with the usart.
| usart | base address of the usart | |
| character | character to write |
| USART_OK | on success | |
| USART_TX_BUSY | on timeout |
Definition at line 222 of file usart.c.
References USART_DEFAULT_TIMEOUT, USART_OK, and USART_TX_BUSY.
00223 { 00224 int timeout = USART_DEFAULT_TIMEOUT; 00225 00226 do { 00227 --timeout; 00228 } while ((usart->csr & AVR32_USART_CSR_TXRDY_MASK) == 0 && timeout > 0); 00229 00230 if (timeout == 0) { 00231 return USART_TX_BUSY; 00232 } 00233 00234 usart->thr = character; 00235 00236 return USART_OK; 00237 }
| void usart_reset | ( | volatile avr32_usart_t * | usart | ) |
This function will reset the USART, and disable TX and RX.
| usart | Base address of the usart |
Definition at line 56 of file usart.c.
00057 { 00058 /* Disable all usart interrupts, interrupts needed should be set 00059 explicitly on every reset */ 00060 usart->idr = 0xFFFFffff; 00061 00062 /* Reset mode and other registers that could cause unpredictable 00063 behaviour after reset */ 00064 usart->mr = 0; 00065 usart->rtor = 0; 00066 usart->ttgr = 0; 00067 00068 /* Shutdown RX and TX (will be reenabled when setup 00069 is completed successfully), reset status bits and turn 00070 off DTR and RTS */ 00071 usart->cr = AVR32_USART_CR_RSTRX_MASK| 00072 AVR32_USART_CR_RSTTX_MASK| 00073 AVR32_USART_CR_RSTSTA_MASK| 00074 AVR32_USART_CR_RSTIT_MASK| 00075 AVR32_USART_CR_RSTNACK_MASK| 00076 AVR32_USART_CR_DTRDIS_MASK| 00077 AVR32_USART_CR_RTSDIS_MASK; 00078 }
1.5.3