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
00047
00048
00049 #ifndef _USART_H_
00050 #define _USART_H_
00051
00052 #include <avr32/io.h>
00053 #include "compiler.h"
00054
00055
00058
00059 #define USART_SUCCESS 0
00060 #define USART_FAILURE -1
00061 #define USART_INVALID_INPUT 1
00062 #define USART_INVALID_ARGUMENT -1
00063 #define USART_TX_BUSY 2
00064 #define USART_RX_EMPTY 3
00065 #define USART_RX_ERROR 4
00066 #define USART_MODE_FAULT 5
00067
00068
00070 #define USART_DEFAULT_TIMEOUT 10000
00071
00074
00075 #define USART_EVEN_PARITY AVR32_USART_MR_PAR_EVEN
00076 #define USART_ODD_PARITY AVR32_USART_MR_PAR_ODD
00077 #define USART_SPACE_PARITY AVR32_USART_MR_PAR_SPACE
00078 #define USART_MARK_PARITY AVR32_USART_MR_PAR_MARK
00079 #define USART_NO_PARITY AVR32_USART_MR_PAR_NONE
00080 #define USART_MULTIDROP_PARITY AVR32_USART_MR_PAR_MULTI
00081
00082
00083
00085
00086 #define USART_1_STOPBIT AVR32_USART_MR_NBSTOP_1
00087 #define USART_1_5_STOPBITS AVR32_USART_MR_NBSTOP_1_5
00088 #define USART_2_STOPBITS AVR32_USART_MR_NBSTOP_2
00089
00090
00091
00093
00094 #define USART_NORMAL_CHMODE AVR32_USART_MR_CHMODE_NORMAL
00095 #define USART_AUTO_ECHO AVR32_USART_MR_CHMODE_ECHO
00096 #define USART_LOCAL_LOOPBACK AVR32_USART_MR_CHMODE_LOCAL_LOOP
00097 #define USART_REMOTE_LOOPBACK AVR32_USART_MR_CHMODE_REMOTE_LOOP
00098
00099
00100 #if defined(AVR32_USART_400_H_INCLUDED) || \
00101 defined(AVR32_USART_410_H_INCLUDED) || \
00102 defined(AVR32_USART_420_H_INCLUDED) || \
00103 defined(AVR32_USART_440_H_INCLUDED) || \
00104 defined(AVR32_USART_602_H_INCLUDED)
00105
00108
00109 #define USART_LIN_PUBLISH_ACTION AVR32_USART_LINMR_NACT_PUBLISH
00110 #define USART_LIN_SUBSCRIBE_ACTION AVR32_USART_LINMR_NACT_SUBSCRIBE
00111 #define USART_LIN_IGNORE_ACTION AVR32_USART_LINMR_NACT_IGNORE
00112
00113
00114
00116
00117 #define USART_LIN_ENHANCED_CHECKSUM 0
00118 #define USART_LIN_CLASSIC_CHECKSUM 1
00119
00120
00121 #endif // USART rev. >= 4.0.0
00122
00123
00125 typedef struct
00126 {
00128 unsigned long baudrate;
00129
00131 unsigned char charlength;
00132
00136 unsigned char paritytype;
00137
00142 unsigned short stopbits;
00143
00146 unsigned char channelmode;
00147 } usart_options_t;
00148
00150 typedef struct
00151 {
00153 unsigned long iso7816_hz;
00154
00157 unsigned short fidi_ratio;
00158
00161 unsigned char paritytype;
00162
00168 int inhibit_nack;
00169
00174 int dis_suc_nack;
00175
00177 unsigned char max_iterations;
00178
00182 int bit_order;
00183 } usart_iso7816_options_t;
00184
00185 #if defined(AVR32_USART_400_H_INCLUDED) || \
00186 defined(AVR32_USART_410_H_INCLUDED) || \
00187 defined(AVR32_USART_420_H_INCLUDED) || \
00188 defined(AVR32_USART_440_H_INCLUDED) || \
00189 defined(AVR32_USART_602_H_INCLUDED)
00190
00192 typedef struct
00193 {
00195 unsigned long baudrate;
00196
00198 unsigned char charlength;
00199
00201 unsigned char spimode;
00202
00205 unsigned char channelmode;
00206 } usart_spi_options_t;
00207
00208 #endif // USART rev. >= 4.0.0
00209
00210
00211
00214
00215
00220 extern void usart_reset(volatile avr32_usart_t *usart);
00221
00231 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00232
00247 extern int usart_init_rs232_tx_only(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00248
00260 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00261
00271 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00272
00282 extern int usart_init_sync_master(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00283
00293 extern int usart_init_sync_slave(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00294
00304 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00305
00316 extern int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,
00317 long pba_hz, unsigned char irda_filter);
00318
00333 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz);
00334
00335 #if defined(AVR32_USART_400_H_INCLUDED) || \
00336 defined(AVR32_USART_410_H_INCLUDED) || \
00337 defined(AVR32_USART_420_H_INCLUDED) || \
00338 defined(AVR32_USART_440_H_INCLUDED) || \
00339 defined(AVR32_USART_602_H_INCLUDED)
00340
00348 extern int usart_init_lin_master(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
00349
00357 extern int usart_init_lin_slave(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
00358
00371 extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
00372
00382 extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
00383
00384 #endif // USART rev. >= 4.0.0
00385
00387
00388
00389
00392
00393
00402 #if (defined __GNUC__)
00403 __attribute__((__always_inline__))
00404 #endif
00405 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)
00406 {
00407 usart->cr = AVR32_USART_CR_RSTSTA_MASK;
00408 }
00409
00416 #if (defined __GNUC__)
00417 __attribute__((__always_inline__))
00418 #endif
00419 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)
00420 {
00421 return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
00422 }
00423
00430 #if (defined __GNUC__)
00431 __attribute__((__always_inline__))
00432 #endif
00433 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)
00434 {
00435 return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
00436 }
00437
00444 #if (defined __GNUC__)
00445 __attribute__((__always_inline__))
00446 #endif
00447 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)
00448 {
00449 return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
00450 }
00451
00452 #if defined(AVR32_USART_400_H_INCLUDED) || \
00453 defined(AVR32_USART_410_H_INCLUDED) || \
00454 defined(AVR32_USART_420_H_INCLUDED) || \
00455 defined(AVR32_USART_440_H_INCLUDED) || \
00456 defined(AVR32_USART_602_H_INCLUDED)
00457
00464 #if (defined __GNUC__)
00465 __attribute__((__always_inline__))
00466 #endif
00467 extern __inline__ int usart_lin_get_error(volatile avr32_usart_t *usart)
00468 {
00469 return (usart->csr & (AVR32_USART_CSR_LINSNRE_MASK |
00470 AVR32_USART_CSR_LINCE_MASK |
00471 AVR32_USART_CSR_LINIPE_MASK |
00472 AVR32_USART_CSR_LINISFE_MASK |
00473 AVR32_USART_CSR_LINBE_MASK)) >> AVR32_USART_CSR_LINBE_OFFSET;
00474 }
00475
00476 #endif // USART rev. >= 4.0.0
00477
00479
00480
00481
00484
00485
00492 #if (defined __GNUC__)
00493 __attribute__((__always_inline__))
00494 #endif
00495 extern __inline__ void usart_iso7816_enable_receiver(volatile avr32_usart_t *usart)
00496 {
00497 usart->cr = AVR32_USART_CR_TXDIS_MASK | AVR32_USART_CR_RXEN_MASK;
00498 }
00499
00506 #if (defined __GNUC__)
00507 __attribute__((__always_inline__))
00508 #endif
00509 extern __inline__ void usart_iso7816_enable_transmitter(volatile avr32_usart_t *usart)
00510 {
00511 usart->cr = AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXEN_MASK;
00512 }
00513
00515
00516
00517
00518 #if defined(AVR32_USART_400_H_INCLUDED) || \
00519 defined(AVR32_USART_410_H_INCLUDED) || \
00520 defined(AVR32_USART_420_H_INCLUDED) || \
00521 defined(AVR32_USART_440_H_INCLUDED) || \
00522 defined(AVR32_USART_602_H_INCLUDED)
00523
00526
00527
00535 #if (defined __GNUC__)
00536 __attribute__((__always_inline__))
00537 #endif
00538 extern __inline__ void usart_lin_set_node_action(volatile avr32_usart_t *usart, unsigned char action)
00539 {
00540 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_NACT_MASK) |
00541 action << AVR32_USART_LINMR_NACT_OFFSET;
00542 }
00543
00549 #if (defined __GNUC__)
00550 __attribute__((__always_inline__))
00551 #endif
00552 extern __inline__ void usart_lin_enable_parity(volatile avr32_usart_t *usart, unsigned char parity)
00553 {
00554 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_PARDIS_MASK) |
00555 !parity << AVR32_USART_LINMR_PARDIS_OFFSET;
00556 }
00557
00563 #if (defined __GNUC__)
00564 __attribute__((__always_inline__))
00565 #endif
00566 extern __inline__ void usart_lin_enable_checksum(volatile avr32_usart_t *usart, unsigned char checksum)
00567 {
00568 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKDIS_MASK) |
00569 !checksum << AVR32_USART_LINMR_CHKDIS_OFFSET;
00570 }
00571
00578 #if (defined __GNUC__)
00579 __attribute__((__always_inline__))
00580 #endif
00581 extern __inline__ void usart_lin_set_checksum(volatile avr32_usart_t *usart, unsigned char chktyp)
00582 {
00583 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKTYP_MASK) |
00584 chktyp << AVR32_USART_LINMR_CHKTYP_OFFSET;
00585 }
00586
00593 #if (defined __GNUC__)
00594 __attribute__((__always_inline__))
00595 #endif
00596 extern __inline__ unsigned char usart_lin_get_data_length(volatile avr32_usart_t *usart)
00597 {
00598 if (usart->linmr & AVR32_USART_LINMR_DLM_MASK)
00599 {
00600 unsigned char data_length = 1 << ((usart->linir >> (AVR32_USART_LINIR_IDCHR_OFFSET + 4)) & 0x03);
00601 if (data_length == 1)
00602 data_length = 2;
00603 return data_length;
00604 }
00605 else
00606 return ((usart->linmr & AVR32_USART_LINMR_DLC_MASK) >> AVR32_USART_LINMR_DLC_OFFSET) + 1;
00607 }
00608
00613 #if (defined __GNUC__)
00614 __attribute__((__always_inline__))
00615 #endif
00616 extern __inline__ void usart_lin_set_data_length_lin1x(volatile avr32_usart_t *usart)
00617 {
00618 usart->linmr |= AVR32_USART_LINMR_DLM_MASK;
00619 }
00620
00626 #if (defined __GNUC__)
00627 __attribute__((__always_inline__))
00628 #endif
00629 extern __inline__ void usart_lin_set_data_length_lin2x(volatile avr32_usart_t *usart, unsigned char data_length)
00630 {
00631 usart->linmr = (usart->linmr & ~(AVR32_USART_LINMR_DLC_MASK |
00632 AVR32_USART_LINMR_DLM_MASK)) |
00633 (data_length - 1) << AVR32_USART_LINMR_DLC_OFFSET;
00634 }
00635
00642 #if (defined __GNUC__)
00643 __attribute__((__always_inline__))
00644 #endif
00645 extern __inline__ void usart_lin_enable_frameslot(volatile avr32_usart_t *usart, unsigned char frameslot)
00646 {
00647 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_FSDIS_MASK) |
00648 !frameslot << AVR32_USART_LINMR_FSDIS_OFFSET;
00649 }
00650
00657 #if (defined __GNUC__)
00658 __attribute__((__always_inline__))
00659 #endif
00660 extern __inline__ unsigned char usart_lin_get_id_char(volatile avr32_usart_t *usart)
00661 {
00662 return (usart->linir & AVR32_USART_LINIR_IDCHR_MASK) >> AVR32_USART_LINIR_IDCHR_OFFSET;
00663 }
00664
00670 #if (defined __GNUC__)
00671 __attribute__((__always_inline__))
00672 #endif
00673 extern __inline__ void usart_lin_set_id_char(volatile avr32_usart_t *usart, unsigned char id_char)
00674 {
00675 usart->linir = (usart->linir & ~AVR32_USART_LINIR_IDCHR_MASK) |
00676 id_char << AVR32_USART_LINIR_IDCHR_OFFSET;
00677 }
00678
00680
00681 #endif // USART rev. >= 4.0.0
00682
00683
00684
00685 #if defined(AVR32_USART_400_H_INCLUDED) || \
00686 defined(AVR32_USART_410_H_INCLUDED) || \
00687 defined(AVR32_USART_420_H_INCLUDED) || \
00688 defined(AVR32_USART_440_H_INCLUDED) || \
00689 defined(AVR32_USART_602_H_INCLUDED)
00690
00693
00694
00701 extern int usart_spi_selectChip(volatile avr32_usart_t *usart);
00702
00710 extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);
00711
00713
00714 #endif // USART rev. >= 4.0.0
00715
00716
00717
00720
00721
00735 extern int usart_send_address(volatile avr32_usart_t *usart, int address);
00736
00743 #if (defined __GNUC__)
00744 __attribute__((__always_inline__))
00745 #endif
00746 extern __inline__ int usart_tx_ready(volatile avr32_usart_t *usart)
00747 {
00748 return (usart->csr & AVR32_USART_CSR_TXRDY_MASK) != 0;
00749 }
00750
00759 extern int usart_write_char(volatile avr32_usart_t *usart, int c);
00760
00766 #if (defined __GNUC__)
00767 __attribute__((__always_inline__))
00768 #endif
00769 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
00770 {
00771 while (usart_write_char(usart, c) != USART_SUCCESS);
00772 }
00773
00782 extern int usart_putchar(volatile avr32_usart_t *usart, int c);
00783
00791 #if (defined __GNUC__)
00792 __attribute__((__always_inline__))
00793 #endif
00794 extern __inline__ int usart_tx_empty(volatile avr32_usart_t *usart)
00795 {
00796 return (usart->csr & AVR32_USART_CSR_TXEMPTY_MASK) != 0;
00797 }
00798
00805 #if (defined __GNUC__)
00806 __attribute__((__always_inline__))
00807 #endif
00808 extern __inline__ int usart_test_hit(volatile avr32_usart_t *usart)
00809 {
00810 return (usart->csr & AVR32_USART_CSR_RXRDY_MASK) != 0;
00811 }
00812
00824 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
00825
00832 extern int usart_getchar(volatile avr32_usart_t *usart);
00833
00839 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
00840
00848 extern int usart_get_echo_line(volatile avr32_usart_t *usart);
00849
00850 #if defined(AVR32_USART_400_H_INCLUDED) || \
00851 defined(AVR32_USART_410_H_INCLUDED) || \
00852 defined(AVR32_USART_420_H_INCLUDED) || \
00853 defined(AVR32_USART_440_H_INCLUDED) || \
00854 defined(AVR32_USART_602_H_INCLUDED)
00855
00860 #if (defined __GNUC__)
00861 __attribute__((__always_inline__))
00862 #endif
00863 extern __inline__ void usart_lin_abort(volatile avr32_usart_t *usart)
00864 {
00865 usart->cr = AVR32_USART_LINABT_MASK;
00866 }
00867
00874 #if (defined __GNUC__)
00875 __attribute__((__always_inline__))
00876 #endif
00877 extern __inline__ int usart_lin_transfer_completed(volatile avr32_usart_t *usart)
00878 {
00879 return (usart->csr & AVR32_USART_CSR_LINTC_MASK) != 0;
00880 }
00881
00882 #endif // USART rev. >= 4.0.0
00883
00885
00886
00887 #endif // _USART_H_