00001
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #ifndef _USART_H_
00047 #define _USART_H_
00048
00049 #include <avr32/io.h>
00050 #include "compiler.h"
00051
00052
00055
00056 #define USART_SUCCESS 0
00057 #define USART_FAILURE -1
00058 #define USART_INVALID_INPUT 1
00059 #define USART_INVALID_ARGUMENT -1
00060 #define USART_TX_BUSY 2
00061 #define USART_RX_EMPTY 3
00062 #define USART_RX_ERROR 4
00063 #define USART_MODE_FAULT 5
00064
00065
00067 #define USART_DEFAULT_TIMEOUT 10000
00068
00071
00072 #define USART_EVEN_PARITY AVR32_USART_MR_PAR_EVEN
00073 #define USART_ODD_PARITY AVR32_USART_MR_PAR_ODD
00074 #define USART_SPACE_PARITY AVR32_USART_MR_PAR_SPACE
00075 #define USART_MARK_PARITY AVR32_USART_MR_PAR_MARK
00076 #define USART_NO_PARITY AVR32_USART_MR_PAR_NONE
00077 #define USART_MULTIDROP_PARITY AVR32_USART_MR_PAR_MULTI
00078
00079
00080
00082
00083 #define USART_1_STOPBIT AVR32_USART_MR_NBSTOP_1
00084 #define USART_1_5_STOPBITS AVR32_USART_MR_NBSTOP_1_5
00085 #define USART_2_STOPBITS AVR32_USART_MR_NBSTOP_2
00086
00087
00088
00090
00091 #define USART_NORMAL_CHMODE AVR32_USART_MR_CHMODE_NORMAL
00092 #define USART_AUTO_ECHO AVR32_USART_MR_CHMODE_ECHO
00093 #define USART_LOCAL_LOOPBACK AVR32_USART_MR_CHMODE_LOCAL_LOOP
00094 #define USART_REMOTE_LOOPBACK AVR32_USART_MR_CHMODE_REMOTE_LOOP
00095
00096
00097 #if defined(AVR32_USART_400_H_INCLUDED) || \
00098 defined(AVR32_USART_410_H_INCLUDED) || \
00099 defined(AVR32_USART_420_H_INCLUDED)
00100
00103
00104 #define USART_LIN_PUBLISH_ACTION AVR32_USART_LINMR_NACT_PUBLISH
00105 #define USART_LIN_SUBSCRIBE_ACTION AVR32_USART_LINMR_NACT_SUBSCRIBE
00106 #define USART_LIN_IGNORE_ACTION AVR32_USART_LINMR_NACT_IGNORE
00107
00108
00109
00111
00112 #define USART_LIN_ENHANCED_CHECKSUM 0
00113 #define USART_LIN_CLASSIC_CHECKSUM 1
00114
00115
00116 #endif // USART rev. >= 4.0.0
00117
00118
00120 typedef struct
00121 {
00123 unsigned long baudrate;
00124
00126 unsigned char charlength;
00127
00131 unsigned char paritytype;
00132
00137 unsigned short stopbits;
00138
00141 unsigned char channelmode;
00142 } usart_options_t;
00143
00145 typedef struct
00146 {
00148 unsigned long iso7816_hz;
00149
00152 unsigned short fidi_ratio;
00153
00156 unsigned char paritytype;
00157
00163 int inhibit_nack;
00164
00169 int dis_suc_nack;
00170
00172 unsigned char max_iterations;
00173
00177 int bit_order;
00178 } usart_iso7816_options_t;
00179
00180 #if defined(AVR32_USART_400_H_INCLUDED) || \
00181 defined(AVR32_USART_410_H_INCLUDED) || \
00182 defined(AVR32_USART_420_H_INCLUDED)
00183
00185 typedef struct
00186 {
00188 unsigned long baudrate;
00189
00191 unsigned char charlength;
00192
00194 unsigned char spimode;
00195
00198 unsigned char channelmode;
00199 } usart_spi_options_t;
00200
00201 #endif // USART rev. >= 4.0.0
00202
00203
00204
00207
00208
00213 extern void usart_reset(volatile avr32_usart_t *usart);
00214
00224 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00225
00240 extern int usart_init_rs232_tx_only(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00241
00253 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00254
00264 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00265
00275 extern int usart_init_sync_master(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00276
00286 extern int usart_init_sync_slave(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00287
00297 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00298
00309 extern int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,
00310 long pba_hz, unsigned char irda_filter);
00311
00326 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz);
00327
00328 #if defined(AVR32_USART_400_H_INCLUDED) || \
00329 defined(AVR32_USART_410_H_INCLUDED) || \
00330 defined(AVR32_USART_420_H_INCLUDED)
00331
00339 extern int usart_init_lin_master(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
00340
00348 extern int usart_init_lin_slave(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
00349
00362 extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
00363
00373 extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
00374
00375 #endif // USART rev. >= 4.0.0
00376
00378
00379
00380
00383
00384
00393 #if __GNUC__
00394 __attribute__((__always_inline__))
00395 #endif
00396 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)
00397 {
00398 usart->cr = AVR32_USART_CR_RSTSTA_MASK;
00399 }
00400
00407 #if __GNUC__
00408 __attribute__((__always_inline__))
00409 #endif
00410 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)
00411 {
00412 return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
00413 }
00414
00421 #if __GNUC__
00422 __attribute__((__always_inline__))
00423 #endif
00424 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)
00425 {
00426 return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
00427 }
00428
00435 #if __GNUC__
00436 __attribute__((__always_inline__))
00437 #endif
00438 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)
00439 {
00440 return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
00441 }
00442
00443 #if defined(AVR32_USART_400_H_INCLUDED) || \
00444 defined(AVR32_USART_410_H_INCLUDED) || \
00445 defined(AVR32_USART_420_H_INCLUDED)
00446
00453 #if __GNUC__
00454 __attribute__((__always_inline__))
00455 #endif
00456 extern __inline__ int usart_lin_get_error(volatile avr32_usart_t *usart)
00457 {
00458 return (usart->csr & (AVR32_USART_CSR_LINSNRE_MASK |
00459 AVR32_USART_CSR_LINCE_MASK |
00460 AVR32_USART_CSR_LINIPE_MASK |
00461 AVR32_USART_CSR_LINISFE_MASK |
00462 AVR32_USART_CSR_LINBE_MASK)) >> AVR32_USART_CSR_LINBE_OFFSET;
00463 }
00464
00465 #endif // USART rev. >= 4.0.0
00466
00468
00469
00470
00473
00474
00481 #if __GNUC__
00482 __attribute__((__always_inline__))
00483 #endif
00484 extern __inline__ void usart_iso7816_enable_receiver(volatile avr32_usart_t *usart)
00485 {
00486 usart->cr = AVR32_USART_CR_TXDIS_MASK | AVR32_USART_CR_RXEN_MASK;
00487 }
00488
00495 #if __GNUC__
00496 __attribute__((__always_inline__))
00497 #endif
00498 extern __inline__ void usart_iso7816_enable_transmitter(volatile avr32_usart_t *usart)
00499 {
00500 usart->cr = AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXEN_MASK;
00501 }
00502
00504
00505
00506
00507 #if defined(AVR32_USART_400_H_INCLUDED) || \
00508 defined(AVR32_USART_410_H_INCLUDED) || \
00509 defined(AVR32_USART_420_H_INCLUDED)
00510
00513
00514
00522 #if __GNUC__
00523 __attribute__((__always_inline__))
00524 #endif
00525 extern __inline__ void usart_lin_set_node_action(volatile avr32_usart_t *usart, unsigned char action)
00526 {
00527 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_NACT_MASK) |
00528 action << AVR32_USART_LINMR_NACT_OFFSET;
00529 }
00530
00536 #if __GNUC__
00537 __attribute__((__always_inline__))
00538 #endif
00539 extern __inline__ void usart_lin_enable_parity(volatile avr32_usart_t *usart, unsigned char parity)
00540 {
00541 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_PARDIS_MASK) |
00542 !parity << AVR32_USART_LINMR_PARDIS_OFFSET;
00543 }
00544
00550 #if __GNUC__
00551 __attribute__((__always_inline__))
00552 #endif
00553 extern __inline__ void usart_lin_enable_checksum(volatile avr32_usart_t *usart, unsigned char checksum)
00554 {
00555 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKDIS_MASK) |
00556 !checksum << AVR32_USART_LINMR_CHKDIS_OFFSET;
00557 }
00558
00565 #if __GNUC__
00566 __attribute__((__always_inline__))
00567 #endif
00568 extern __inline__ void usart_lin_set_checksum(volatile avr32_usart_t *usart, unsigned char chktyp)
00569 {
00570 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKTYP_MASK) |
00571 chktyp << AVR32_USART_LINMR_CHKTYP_OFFSET;
00572 }
00573
00580 #if __GNUC__
00581 __attribute__((__always_inline__))
00582 #endif
00583 extern __inline__ unsigned char usart_lin_get_data_length(volatile avr32_usart_t *usart)
00584 {
00585 if (usart->linmr & AVR32_USART_LINMR_DLM_MASK)
00586 {
00587 unsigned char data_length = 1 << ((usart->linir >> (AVR32_USART_LINIR_IDCHR_OFFSET + 4)) & 0x03);
00588 if (data_length == 1)
00589 data_length = 2;
00590 return data_length;
00591 }
00592 else
00593 return ((usart->linmr & AVR32_USART_LINMR_DLC_MASK) >> AVR32_USART_LINMR_DLC_OFFSET) + 1;
00594 }
00595
00600 #if __GNUC__
00601 __attribute__((__always_inline__))
00602 #endif
00603 extern __inline__ void usart_lin_set_data_length_lin1x(volatile avr32_usart_t *usart)
00604 {
00605 usart->linmr |= AVR32_USART_LINMR_DLM_MASK;
00606 }
00607
00613 #if __GNUC__
00614 __attribute__((__always_inline__))
00615 #endif
00616 extern __inline__ void usart_lin_set_data_length_lin2x(volatile avr32_usart_t *usart, unsigned char data_length)
00617 {
00618 usart->linmr = (usart->linmr & ~(AVR32_USART_LINMR_DLC_MASK |
00619 AVR32_USART_LINMR_DLM_MASK)) |
00620 (data_length - 1) << AVR32_USART_LINMR_DLC_OFFSET;
00621 }
00622
00629 #if __GNUC__
00630 __attribute__((__always_inline__))
00631 #endif
00632 extern __inline__ void usart_lin_enable_frameslot(volatile avr32_usart_t *usart, unsigned char frameslot)
00633 {
00634 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_FSDIS_MASK) |
00635 !frameslot << AVR32_USART_LINMR_FSDIS_OFFSET;
00636 }
00637
00644 #if __GNUC__
00645 __attribute__((__always_inline__))
00646 #endif
00647 extern __inline__ unsigned char usart_lin_get_id_char(volatile avr32_usart_t *usart)
00648 {
00649 return (usart->linir & AVR32_USART_LINIR_IDCHR_MASK) >> AVR32_USART_LINIR_IDCHR_OFFSET;
00650 }
00651
00657 #if __GNUC__
00658 __attribute__((__always_inline__))
00659 #endif
00660 extern __inline__ void usart_lin_set_id_char(volatile avr32_usart_t *usart, unsigned char id_char)
00661 {
00662 usart->linir = (usart->linir & ~AVR32_USART_LINIR_IDCHR_MASK) |
00663 id_char << AVR32_USART_LINIR_IDCHR_OFFSET;
00664 }
00665
00667
00668 #endif // USART rev. >= 4.0.0
00669
00670
00671
00672 #if defined(AVR32_USART_400_H_INCLUDED) || \
00673 defined(AVR32_USART_410_H_INCLUDED) || \
00674 defined(AVR32_USART_420_H_INCLUDED)
00675
00678
00679
00686 extern int usart_spi_selectChip(volatile avr32_usart_t *usart);
00687
00695 extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);
00696
00698
00699 #endif // USART rev. >= 4.0.0
00700
00701
00702
00705
00706
00720 extern int usart_send_address(volatile avr32_usart_t *usart, int address);
00721
00728 #if __GNUC__
00729 __attribute__((__always_inline__))
00730 #endif
00731 extern __inline__ int usart_tx_ready(volatile avr32_usart_t *usart)
00732 {
00733 return (usart->csr & AVR32_USART_CSR_TXRDY_MASK) != 0;
00734 }
00735
00744 extern int usart_write_char(volatile avr32_usart_t *usart, int c);
00745
00751 #if __GNUC__
00752 __attribute__((__always_inline__))
00753 #endif
00754 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
00755 {
00756 while (usart_write_char(usart, c) != USART_SUCCESS);
00757 }
00758
00767 extern int usart_putchar(volatile avr32_usart_t *usart, int c);
00768
00776 #if __GNUC__
00777 __attribute__((__always_inline__))
00778 #endif
00779 extern __inline__ int usart_tx_empty(volatile avr32_usart_t *usart)
00780 {
00781 return (usart->csr & AVR32_USART_CSR_TXEMPTY_MASK) != 0;
00782 }
00783
00790 #if __GNUC__
00791 __attribute__((__always_inline__))
00792 #endif
00793 extern __inline__ int usart_test_hit(volatile avr32_usart_t *usart)
00794 {
00795 return (usart->csr & AVR32_USART_CSR_RXRDY_MASK) != 0;
00796 }
00797
00809 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
00810
00817 extern int usart_getchar(volatile avr32_usart_t *usart);
00818
00824 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
00825
00833 extern int usart_get_echo_line(volatile avr32_usart_t *usart);
00834
00835 #if defined(AVR32_USART_400_H_INCLUDED) || \
00836 defined(AVR32_USART_410_H_INCLUDED) || \
00837 defined(AVR32_USART_420_H_INCLUDED)
00838
00843 #if __GNUC__
00844 __attribute__((__always_inline__))
00845 #endif
00846 extern __inline__ void usart_lin_abort(volatile avr32_usart_t *usart)
00847 {
00848 usart->cr = AVR32_USART_LINABT_MASK;
00849 }
00850
00857 #if __GNUC__
00858 __attribute__((__always_inline__))
00859 #endif
00860 extern __inline__ int usart_lin_transfer_completed(volatile avr32_usart_t *usart)
00861 {
00862 return (usart->csr & AVR32_USART_CSR_LINTC_MASK) != 0;
00863 }
00864
00865 #endif // USART rev. >= 4.0.0
00866
00868
00869
00870 #endif // _USART_H_