This file contains basic drivers for the AVR32 USART, with support for all all modes, settings and clock speeds.
$Name$
Definition in file usart.h.
#include <avr32/io.h>
#include "errno.h"
Go to the source code of this file.
Data Structures | |
| struct | iso7816_options_t |
| struct | usart_options_t |
Defines | |
| #define | INT_CTSIC 19 |
| #define | INT_DCDIC 18 |
| #define | INT_DSRIC 17 |
| #define | INT_ENDRX 3 |
| #define | INT_ENDTX 4 |
| #define | INT_FRAM 6 |
| #define | INT_ITERATION 10 |
| #define | INT_MANE 20 |
| #define | INT_NACK 13 |
| #define | INT_OVRE 5 |
| #define | INT_PARE 7 |
| #define | INT_RIIC 16 |
| #define | INT_RXBRK 2 |
| #define | INT_RXBUFF 12 |
| #define | INT_RXRDY 0 |
| #define | INT_TIMEOUT 8 |
| #define | INT_TXBUFE 11 |
| #define | INT_TXEMPTY 9 |
| #define | INT_TXRDY 1 |
| #define | USART_1_5_STOPBITS 1 |
| #define | USART_1_STOPBIT 0 |
| #define | USART_2_STOPBITS 2 |
| #define | USART_AUTO_ECHO 1 |
| #define | USART_DEFAULT_TIMEOUT 10000 |
| #define | USART_EVEN_PARITY 0 |
| #define | USART_LOCAL_LOOPBACK 2 |
| #define | USART_MARK_PARITY 3 |
| #define | USART_MODE_HW_HSH 0x02 |
| #define | USART_MODE_IRDA 0x08 |
| #define | USART_MODE_ISO7816_T0 0x04 |
| #define | USART_MODE_ISO7816_T1 0x06 |
| #define | USART_MODE_MODEM 0x03 |
| #define | USART_MODE_NORMAL 0x00 |
| #define | USART_MODE_RS485 0x01 |
| #define | USART_MODE_SW_HSH 0x0C |
| #define | USART_MULTIDROP_PARITY 6 |
| #define | USART_NO_PARITY 4 |
| #define | USART_NORMAL_CHMODE 0 |
| #define | USART_ODD_PARITY 1 |
| #define | USART_REMOTE_LOOPBACK 3 |
| #define | USART_SPACE_PARITY 2 |
Functions | |
| void | usart_bw_write_char (volatile struct avr32_usart_t *usart, int c) |
| int | usart_getchar (volatile struct avr32_usart_t *usart) |
| int | usart_init_rs232 (volatile struct avr32_usart_t *usart, struct usart_options_t *opt, long cpu_hz) |
| int | usart_putchar (volatile struct avr32_usart_t *usart, int c) |
| int | usart_read_char (volatile struct avr32_usart_t *usart, int *c) |
| void | usart_reset (volatile struct avr32_usart_t *usart) |
| void | usart_reset_status (volatile struct avr32_usart_t *usart) |
| Reset error status. | |
| int | usart_send_addr (volatile struct avr32_usart_t *usart, int addr) |
| int | usart_write_char (volatile struct avr32_usart_t *usart, int c) |
| #define USART_1_STOPBIT 0 |
| #define USART_2_STOPBITS 2 |
| #define USART_DEFAULT_TIMEOUT 10000 |
Default timeout value; number of tries before timing out
Definition at line 247 of file usart.h.
Referenced by usart_putchar().
| #define USART_EVEN_PARITY 0 |
| #define USART_LOCAL_LOOPBACK 2 |
| #define USART_MODE_HW_HSH 0x02 |
Operate usart in rs232 with hardware handshaking
Definition at line 267 of file usart.h.
Referenced by usart_init_handshaking().
| #define USART_MODE_ISO7816_T0 0x04 |
Operate usart in iso7816, T=0 mode
Definition at line 271 of file usart.h.
Referenced by usart_init_iso7816().
| #define USART_MODE_ISO7816_T1 0x06 |
Operate usart in iso7816, T=1 mode
Definition at line 273 of file usart.h.
Referenced by usart_init_iso7816().
| #define USART_MODE_MODEM 0x03 |
Operate usart in modem mode
Definition at line 269 of file usart.h.
Referenced by usart_init_modem().
| #define USART_MODE_NORMAL 0x00 |
| #define USART_MODE_RS485 0x01 |
Operate usart in rs485 mode
Definition at line 265 of file usart.h.
Referenced by usart_init_rs485().
| #define USART_MODE_SW_HSH 0x0C |
| #define USART_MULTIDROP_PARITY 6 |
| #define USART_NO_PARITY 4 |
| #define USART_NORMAL_CHMODE 0 |
Set usart channel to normal communcation
Definition at line 280 of file usart.h.
Referenced by init_uart_a().
| #define USART_ODD_PARITY 1 |
| #define USART_REMOTE_LOOPBACK 3 |
| void usart_bw_write_char | ( | volatile struct avr32_usart_t * | usart, | |
| int | c | |||
| ) | [inline] |
A busy wait for writing a character to the usart. Use with *caution*
| *usart | Base address of the usart | |
| c | The character (up to 9 bits) to transmit |
Definition at line 475 of file usart.c.
References SUCCESS, and usart_write_char().
Referenced by usart_send_address().
00476 { 00477 while (usart_write_char(usart, c) != SUCCESS) { 00478 } 00479 00480 return; 00481 }
| int usart_getchar | ( | volatile struct avr32_usart_t * | usart | ) |
Wait until a character is recevied, and return this.
| *usart | Base address of the usart |
Definition at line 515 of file usart.c.
References usart_read_char(), USART_RX_EMPTY, and USART_RX_ERROR.
00516 { 00517 int c, ret; 00518 00519 while (((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY)) { 00520 } 00521 00522 if (ret == USART_RX_ERROR) 00523 return -1; 00524 else 00525 return c; 00526 }
| int usart_init_rs232 | ( | volatile struct avr32_usart_t * | usart, | |
| struct usart_options_t * | opt, | |||
| long | cpu_hz | |||
| ) |
Setup the usart to use the standard RS232 protocol
| *usart | Base address of the usart | |
| *opt | Options needed to set up RS232 communcation (see usart_options_t) | |
| cpu_hz | The usart clk frequency |
Definition at line 153 of file usart.c.
References usart_options_t::baudrate, usart_options_t::channelmode, usart_options_t::charlength, INVALID_ARGUMENT, usart_options_t::paritytype, usart_options_t::stopbits, SUCCESS, usart_reset(), and usart_set_baudrate().
Referenced by init_uart_a(), usart_init_handshaking(), usart_init_IrDA(), usart_init_modem(), and usart_init_rs485().
00154 { 00155 int retval; 00156 00157 /* Reset the usart and shutdown RX and TX */ 00158 usart_reset(usart); 00159 00160 /* Control input values */ 00161 if (opt == 0) /* Null pointer */ 00162 return INVALID_ARGUMENT; 00163 if (opt->charlength < 5 || opt->charlength > 9) 00164 return INVALID_ARGUMENT; 00165 if (opt->paritytype > 7) 00166 return INVALID_ARGUMENT; 00167 if (opt->stopbits > 2+255) 00168 return INVALID_ARGUMENT; 00169 if (opt->channelmode > 3) 00170 return INVALID_ARGUMENT; 00171 00172 if ((retval = usart_set_baudrate(usart, opt->baudrate, cpu_hz)) != \ 00173 SUCCESS) 00174 return retval; 00175 00176 if (opt->charlength == 9) { 00177 /* Charlength set to 9 bits; MODE9 dominates CHRL */ 00178 usart->mr |= (1<<AVR32_USART_MR_MODE9_OFFSET); 00179 } else { 00180 /* CHRL gives the charlength( - 5) when USART_MODE9=0 */ 00181 usart->mr |= 00182 ((opt->charlength-5) << AVR32_USART_MR_CHRL_OFFSET); 00183 } 00184 00185 usart->mr |= (opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET) | 00186 (opt->paritytype << AVR32_USART_MR_PAR_OFFSET); 00187 00188 if (opt->stopbits > 2) 00189 { 00190 /* Set two stop bits */ 00191 usart->mr |= (2 << AVR32_USART_MR_NBSTOP_OFFSET); 00192 /* And a timeguard period gives the rest */ 00193 usart->ttgr = (opt->stopbits-2); 00194 } 00195 else 00196 /* Insert 1, 1.5 or 2 stop bits */ 00197 usart->mr |= (opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET); 00198 00199 /* Setup complete; enable communication */ 00200 /* Enable input and output */ 00201 usart->cr |= (1<<AVR32_USART_CR_TXEN_OFFSET) | 00202 (1<<AVR32_USART_CR_RXEN_OFFSET); 00203 00204 return SUCCESS; 00205 }
| int usart_putchar | ( | volatile struct avr32_usart_t * | usart, | |
| int | c | |||
| ) |
Send a character with the usart
| *usart | Base address of the usart | |
| c | Character to write |
Definition at line 529 of file usart.c.
References SUCCESS, USART_DEFAULT_TIMEOUT, and usart_write_char().
Referenced by print(), and usart_print().
00530 { 00531 int timeout = USART_DEFAULT_TIMEOUT; 00532 00533 if (c == '\n'){ 00534 while ((usart_write_char(usart, '\r') != SUCCESS) && (timeout>0) ){ 00535 timeout--; 00536 } 00537 00538 if (timeout == 0) 00539 return -1; 00540 timeout = USART_DEFAULT_TIMEOUT; 00541 } 00542 00543 while ((usart_write_char(usart, c) != SUCCESS) && ( timeout>0 )){ 00544 timeout--; 00545 } 00546 if (timeout == 0) 00547 return -1; 00548 else 00549 return 0; 00550 }
| int usart_read_char | ( | volatile struct avr32_usart_t * | usart, | |
| int * | c | |||
| ) |
Checks the RX buffer for a received character, and puts this at the memory location given.
| *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) |
Definition at line 495 of file usart.c.
References SUCCESS, USART_RX_EMPTY, and USART_RX_ERROR.
Referenced by usart_getchar().
00496 { 00497 /* Check for errors; Frame, parity and overrun In RS485 mode a parity 00498 error would mean that we received an address char */ 00499 if (usart->csr & 00500 ((1 << AVR32_USART_CSR_OVRE_OFFSET) | 00501 (1 << AVR32_USART_CSR_FRAME_OFFSET) | 00502 (1 << AVR32_USART_CSR_PARE_OFFSET))) { 00503 return USART_RX_ERROR; 00504 } 00505 /* No error; if we really did receive a char, read it and return SUCCESS */ 00506 else if ((usart->csr & (1<<AVR32_USART_CSR_RXRDY_OFFSET)) != 0) { 00507 *c = (unsigned short)usart->rhr; 00508 return SUCCESS; 00509 } else { 00510 return USART_RX_EMPTY; 00511 } 00512 } /* usart_read */
| void usart_reset | ( | volatile struct avr32_usart_t * | usart | ) |
This function will reset the USART, and disable TX and RX
| *usart | Base address of the usart |
Definition at line 71 of file usart.c.
Referenced by board_init(), usart_init_iso7816(), and usart_init_rs232().
00072 { 00073 /* Disable all usart interrupts, interrupts needed should be set 00074 explicitly on every reset */ 00075 usart->idr = 0xFFFFffff; 00076 00077 /* Reset mode and other registers that could cause unpredictable 00078 behaviour after reset */ 00079 usart->mr = 0; 00080 usart->rtor = 0; 00081 usart->ttgr = 0; 00082 00083 /* Shutdown RX and TX (will be reenabled when setup 00084 is completed successfully), reset status bits and turn 00085 off DTR and RTS */ 00086 usart->cr = (1 << AVR32_USART_CR_RSTRX_OFFSET) | 00087 (1 << AVR32_USART_CR_RSTTX_OFFSET) | 00088 (1 << AVR32_USART_CR_RSTSTA_OFFSET) | 00089 (1 << AVR32_USART_CR_RSTIT_OFFSET) | 00090 (1 << AVR32_USART_CR_RSTNACK_OFFSET) | 00091 (1 << AVR32_USART_CR_DTRDIS_OFFSET) | 00092 (1 << AVR32_USART_CR_RTSDIS_OFFSET); 00093 }
| void usart_reset_status | ( | volatile struct avr32_usart_t * | usart | ) |
Reset error status.
This function resets the status bits indicating that a parity error, framing error or overrun has occured. The rxbreak bit, indicating a start/end of break condition on the rx-line, is also reset.
| *usart | Base address of the usart |
Definition at line 396 of file usart.c.
00397 { 00398 usart->cr |= (1<<AVR32_USART_CR_RSTSTA_OFFSET); 00399 }
| int usart_send_addr | ( | volatile struct avr32_usart_t * | usart, | |
| int | addr | |||
| ) |
Description: While in RS485-mode, receviers only accept data addressed to them. A packet/char with the address tag set has to preceed any data. usart_send_addr() is used to address a receiver. This receiver should read all the following data, until an address packet addresses someone else. Arguments: *usart: Base address of the usart addr: the address of the target device Returns: USART_SUCCESS if the current mode is RS485 USART_MODE_FAULT if called while in wrong mode
| int usart_write_char | ( | volatile struct avr32_usart_t * | usart, | |
| int | c | |||
| ) |
If the transmitter is ready; write the given character to the TX buffer
| *usart | Base address of the usart | |
| c | The character (up to 9 bits) to transmit |
Definition at line 483 of file usart.c.
References SUCCESS, and USART_TX_BUSY.
Referenced by usart_bw_write_char(), and usart_putchar().
00484 { 00485 00486 if ((usart->csr & (1<<AVR32_USART_CSR_TXRDY_OFFSET)) != 0) { 00487 usart->thr = c; 00488 return SUCCESS; 00489 } 00490 else 00491 return USART_TX_BUSY; 00492 }
1.5.3-20071008