#include <avr32/io.h>
Include dependency graph for usart.h:
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Data Structures | |
| struct | iso7816_options_t |
| struct | usart_options_t |
| Input parameters when initializing rs232 mode. More... | |
Defines | |
| #define | INT_CTSIC 19 |
| #define | INT_DCDIC 18 |
| #define | INT_DSRIC 17 |
| #define | INT_ENDRX 3 |
| #define | INT_ENDTX 4 |
| #define | INT_FRAM 6 |
| #define | INT_ITERATION 10 |
| #define | INT_MANE 20 |
| #define | INT_NACK 13 |
| #define | INT_OVRE 5 |
| #define | INT_PARE 7 |
| #define | INT_RIIC 16 |
| #define | INT_RXBRK 2 |
| #define | INT_RXBUFF 12 |
| #define | INT_RXRDY 0 |
| #define | INT_TIMEOUT 8 |
| #define | INT_TXBUFE 11 |
| #define | INT_TXEMPTY 9 |
| #define | INT_TXRDY 1 |
| #define | USART_1_5_STOPBITS 1 |
| #define | USART_1_STOPBIT 0 |
| #define | USART_2_STOPBITS 2 |
| #define | USART_ADDR_RECEIVED 1 |
| #define | USART_AUTO_ECHO 1 |
| #define | USART_DEFAULT_TIMEOUT 10000 |
| #define | USART_EVEN_PARITY 0 |
| #define | USART_FAILURE -1 |
| #define | USART_INVALID_ARGUMENT -1 |
| #define | USART_INVALID_INPUT 1 |
| #define | USART_LOCAL_LOOPBACK 2 |
| #define | USART_MARK_PARITY 3 |
| #define | USART_MODE_FAULT 5 |
| #define | USART_MODE_HW_HSH 0x02 |
| #define | USART_MODE_IRDA 0x08 |
| #define | USART_MODE_ISO7816_T0 0x04 |
| #define | USART_MODE_ISO7816_T1 0x06 |
| #define | USART_MODE_MODEM 0x03 |
| #define | USART_MODE_NORMAL 0x00 |
| #define | USART_MODE_RS485 0x01 |
| #define | USART_MODE_SW_HSH 0x0C |
| #define | USART_MULTIDROP_PARITY 6 |
| #define | USART_NO_PARITY 4 |
| #define | USART_NORMAL_CHMODE 0 |
| #define | USART_ODD_PARITY 1 |
| #define | USART_REMOTE_LOOPBACK 3 |
| #define | USART_RX_EMPTY 3 |
| #define | USART_RX_ERROR 4 |
| #define | USART_SPACE_PARITY 2 |
| #define | USART_SUCCESS 0 |
| #define | USART_TX_BUSY 2 |
Functions | |
| void | usart_bw_write_char (volatile avr32_usart_t *usart, int c) |
| int | usart_getchar (volatile avr32_usart_t *usart) |
| int | usart_init_rs232 (volatile avr32_usart_t *usart, struct usart_options_t *opt, long cpu_hz) |
| int | usart_putchar (volatile avr32_usart_t *usart, int c) |
| Send a character with the usart. | |
| int | usart_read_char (volatile avr32_usart_t *usart, int *c) |
| void | usart_reset (volatile avr32_usart_t *usart) |
| This function will reset the USART, and disable TX and RX. | |
| void | usart_reset_status (volatile avr32_usart_t *usart) |
| Reset error status. | |
| int | usart_send_addr (volatile avr32_usart_t *usart, int addr) |
| int | usart_write_char (volatile avr32_usart_t *usart, int c) |
| #define USART_2_STOPBITS 2 |
| #define USART_ADDR_RECEIVED 1 |
| #define USART_DEFAULT_TIMEOUT 10000 |
| #define USART_EVEN_PARITY 0 |
| #define USART_FAILURE -1 |
| #define USART_INVALID_INPUT 1 |
Value returned by function when the input paramters are out of range
Definition at line 194 of file usart.h.
Referenced by usart_init_handshaking(), usart_init_iso7816(), usart_init_rs232(), and usart_set_baudrate().
| #define USART_LOCAL_LOOPBACK 2 |
| #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_NORMAL_CHMODE 0 |
| #define USART_ODD_PARITY 1 |
| #define USART_REMOTE_LOOPBACK 3 |
| #define USART_RX_EMPTY 3 |
| #define USART_RX_ERROR 4 |
| #define USART_SUCCESS 0 |
Value returned by function when it completed successfully
Definition at line 189 of file usart.h.
Referenced by usart_bw_write_char(), usart_init_handshaking(), usart_init_IrDA(), usart_init_iso7816(), usart_init_modem(), usart_init_rs232(), usart_init_rs485(), usart_putchar(), usart_read_char(), usart_send_address(), usart_set_baudrate(), and usart_write_char().
| #define USART_TX_BUSY 2 |
| void usart_bw_write_char | ( | volatile avr32_usart_t * | usart, | |
| int | c | |||
| ) | [inline] |
A busy wait for writing a character to the usart. Use with *caution*
| *usart | Base address of the usart | |
| c | The character (up to 9 bits) to transmit |
Definition at line 431 of file usart.c.
References USART_SUCCESS, and usart_write_char().
00432 { 00433 while (usart_write_char(usart, c) != USART_SUCCESS) { 00434 } 00435 00436 return; 00437 }
Here is the call graph for this function:
| int usart_getchar | ( | volatile avr32_usart_t * | usart | ) |
Wait until a character is recevied, and return this.
| *usart | Base address of the usart |
Definition at line 471 of file usart.c.
References usart_read_char(), USART_RX_EMPTY, and USART_RX_ERROR.
00472 { 00473 int c, ret; 00474 while (((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY)) { 00475 } 00476 00477 if (ret == USART_RX_ERROR) 00478 return -1; 00479 else 00480 return c; 00481 }
Here is the call graph for this function:
| int usart_init_rs232 | ( | volatile avr32_usart_t * | usart, | |
| struct usart_options_t * | opt, | |||
| long | cpu_hz | |||
| ) |
Setup the usart to use the standard RS232 protocol
| *usart | Base address of the usart | |
| *opt | Options needed to set up RS232 communcation (see usart_options_t) |
Definition at line 95 of file usart.c.
References usart_options_t::baudrate, usart_options_t::channelmode, usart_options_t::charlength, usart_options_t::paritytype, usart_options_t::stopbits, USART_INVALID_INPUT, usart_reset(), usart_set_baudrate(), and USART_SUCCESS.
00096 { 00097 int retval; 00098 00099 /* Reset the usart and shutdown RX and TX */ 00100 usart_reset(usart); 00101 00102 /* Control input values */ 00103 if (opt == 0) /* Null pointer */ 00104 return USART_INVALID_INPUT; 00105 if (opt->charlength < 5 || opt->charlength > 9) 00106 return USART_INVALID_INPUT; 00107 if (opt->paritytype > 7) 00108 return USART_INVALID_INPUT; 00109 if (opt->stopbits > 2+255) 00110 return USART_INVALID_INPUT; 00111 if (opt->channelmode > 3) 00112 return USART_INVALID_INPUT; 00113 00114 if ((retval = usart_set_baudrate(usart, opt->baudrate, cpu_hz)) != \ 00115 USART_SUCCESS) 00116 return retval; 00117 00118 if (opt->charlength == 9) { 00119 /* Charlength set to 9 bits; MODE9 dominates CHRL */ 00120 usart->mr |= (1<<AVR32_USART_MR_MODE9_OFFSET); 00121 } else { 00122 /* CHRL gives the charlength( - 5) when USART_MODE9=0 */ 00123 usart->mr |= 00124 ((opt->charlength-5) << AVR32_USART_MR_CHRL_OFFSET); 00125 } 00126 00127 usart->mr |= (opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET) | 00128 (opt->paritytype << AVR32_USART_MR_PAR_OFFSET); 00129 00130 if (opt->stopbits > 2) 00131 { 00132 /* Set two stop bits */ 00133 usart->mr |= (2 << AVR32_USART_MR_NBSTOP_OFFSET); 00134 /* And a timeguard period gives the rest */ 00135 usart->ttgr = (opt->stopbits-2); 00136 } 00137 else 00138 /* Insert 1, 1.5 or 2 stop bits */ 00139 usart->mr |= (opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET); 00140 00141 /* Setup complete; enable communication */ 00142 /* Enable input and output */ 00143 usart->cr |= (1<<AVR32_USART_CR_TXEN_OFFSET) | 00144 (1<<AVR32_USART_CR_RXEN_OFFSET); 00145 00146 return USART_SUCCESS; 00147 }
Here is the call graph for this function:
| int usart_putchar | ( | volatile avr32_usart_t * | usart, | |
| int | character | |||
| ) |
Send a character with the usart.
| usart | base address of the usart | |
| character | character to write |
| USART_OK | on success | |
| USART_TX_BUSY | on timeout |
Definition at line 191 of file usart.c.
References USART_DEFAULT_TIMEOUT, USART_OK, and USART_TX_BUSY.
00192 { 00193 int timeout = USART_DEFAULT_TIMEOUT; 00194 00195 do { 00196 --timeout; 00197 } while( (usart->csr & (1<<AVR32_USART_CSR_TXRDY_OFFSET)) == 0 && timeout > 0 ); 00198 00199 if( timeout == 0 ) { 00200 return USART_TX_BUSY; 00201 } 00202 00203 usart->thr = character; 00204 00205 return USART_OK; 00206 }
| int usart_read_char | ( | volatile avr32_usart_t * | usart, | |
| int * | c | |||
| ) |
Checks the RX buffer for a received character, and puts this at the memory location given.
| *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 451 of file usart.c.
References USART_RX_EMPTY, USART_RX_ERROR, and USART_SUCCESS.
00452 { 00453 /* Check for errors; Frame, parity and overrun In RS485 mode a parity 00454 error would mean that we received an address char */ 00455 if (usart->csr & 00456 ((1 << AVR32_USART_CSR_OVRE_OFFSET) | 00457 (1 << AVR32_USART_CSR_FRAME_OFFSET) | 00458 (1 << AVR32_USART_CSR_PARE_OFFSET))) { 00459 return USART_RX_ERROR; 00460 } 00461 /* No error; if we really did receive a char, read it and return SUCCESS */ 00462 else if ((usart->csr & (1<<AVR32_USART_CSR_RXRDY_OFFSET)) != 0) { 00463 *c = (unsigned short)usart->rhr; 00464 return USART_SUCCESS; 00465 } else { 00466 return USART_RX_EMPTY; 00467 } 00468 } /* usart_read */
| void usart_reset | ( | volatile avr32_usart_t * | usart | ) |
This function will reset the USART, and disable TX and RX.
| usart | Base address of the usart |
Definition at line 27 of file usart.c.
00028 { 00029 /* Disable all usart interrupts, interrupts needed should be set 00030 explicitly on every reset */ 00031 usart->idr = 0xFFFFffff; 00032 00033 /* Reset mode and other registers that could cause unpredictable 00034 behaviour after reset */ 00035 usart->mr = 0; 00036 usart->rtor = 0; 00037 usart->ttgr = 0; 00038 00039 /* Shutdown RX and TX (will be reenabled when setup 00040 is completed successfully), reset status bits and turn 00041 off DTR and RTS */ 00042 usart->cr = (1<<AVR32_USART_CR_RSTRX_OFFSET)| 00043 (1<<AVR32_USART_CR_RSTTX_OFFSET)| 00044 (1<<AVR32_USART_CR_RSTSTA_OFFSET)| 00045 (1<<AVR32_USART_CR_RSTIT_OFFSET)| 00046 (1<<AVR32_USART_CR_RSTNACK_OFFSET)| 00047 (1<<AVR32_USART_CR_DTRDIS_OFFSET)| 00048 (1<<AVR32_USART_CR_RTSDIS_OFFSET); 00049 }
| void usart_reset_status | ( | volatile avr32_usart_t * | usart | ) |
Reset error status.
This function resets the status bits indicating that a parity error, framing error or overrun has occured. The rxbreak bit, indicating a start/end of break condition on the rx-line, is also reset.
| *usart | Base address of the usart |
Definition at line 352 of file usart.c.
| int usart_send_addr | ( | volatile avr32_usart_t * | usart, | |
| int | addr | |||
| ) |
Description: While in RS485-mode, receviers only accept data addressed to them. A packet/char with the address tag set has to preceed any data. usart_send_addr() is used to address a receiver. This receiver should read all the following data, until an address packet addresses someone else. Arguments: *usart: Base address of the usart addr: the address of the target device Returns: USART_SUCCESS if the current mode is RS485 USART_MODE_FAULT if called while in wrong mode
| int usart_write_char | ( | volatile avr32_usart_t * | usart, | |
| int | c | |||
| ) |
If the transmitter is ready; write the given character to the TX buffer
| *usart | Base address of the usart | |
| c | The character (up to 9 bits) to transmit |
Definition at line 439 of file usart.c.
References USART_SUCCESS, and USART_TX_BUSY.
00440 { 00441 00442 if ((usart->csr & (1<<AVR32_USART_CSR_TXRDY_OFFSET)) != 0) { 00443 usart->thr = c; 00444 return USART_SUCCESS; 00445 } 00446 else 00447 return USART_TX_BUSY; 00448 }
1.5.1