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
00107
00108 #define USART_LIN_PUBLISH_ACTION AVR32_USART_LINMR_NACT_PUBLISH
00109 #define USART_LIN_SUBSCRIBE_ACTION AVR32_USART_LINMR_NACT_SUBSCRIBE
00110 #define USART_LIN_IGNORE_ACTION AVR32_USART_LINMR_NACT_IGNORE
00111
00112
00113
00115
00116 #define USART_LIN_ENHANCED_CHECKSUM 0
00117 #define USART_LIN_CLASSIC_CHECKSUM 1
00118
00119
00120 #endif // USART rev. >= 4.0.0
00121
00122
00124 typedef struct
00125 {
00127 unsigned long baudrate;
00128
00130 unsigned char charlength;
00131
00135 unsigned char paritytype;
00136
00141 unsigned short stopbits;
00142
00145 unsigned char channelmode;
00146 } usart_options_t;
00147
00149 typedef struct
00150 {
00152 unsigned long iso7816_hz;
00153
00156 unsigned short fidi_ratio;
00157
00160 unsigned char paritytype;
00161
00167 int inhibit_nack;
00168
00173 int dis_suc_nack;
00174
00176 unsigned char max_iterations;
00177
00181 int bit_order;
00182 } usart_iso7816_options_t;
00183
00184 #if defined(AVR32_USART_400_H_INCLUDED) || \
00185 defined(AVR32_USART_410_H_INCLUDED) || \
00186 defined(AVR32_USART_420_H_INCLUDED) || \
00187 defined(AVR32_USART_440_H_INCLUDED)
00188
00190 typedef struct
00191 {
00193 unsigned long baudrate;
00194
00196 unsigned char charlength;
00197
00199 unsigned char spimode;
00200
00203 unsigned char channelmode;
00204 } usart_spi_options_t;
00205
00206 #endif // USART rev. >= 4.0.0
00207
00208
00209
00212
00213
00218 extern void usart_reset(volatile avr32_usart_t *usart);
00219
00229 extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00230
00245 extern int usart_init_rs232_tx_only(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00246
00258 extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00259
00269 extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00270
00280 extern int usart_init_sync_master(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00281
00291 extern int usart_init_sync_slave(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00292
00302 extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
00303
00314 extern int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,
00315 long pba_hz, unsigned char irda_filter);
00316
00331 extern int usart_init_iso7816(volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz);
00332
00333 #if defined(AVR32_USART_400_H_INCLUDED) || \
00334 defined(AVR32_USART_410_H_INCLUDED) || \
00335 defined(AVR32_USART_420_H_INCLUDED) || \
00336 defined(AVR32_USART_440_H_INCLUDED)
00337
00345 extern int usart_init_lin_master(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
00346
00354 extern int usart_init_lin_slave(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
00355
00368 extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
00369
00379 extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
00380
00381 #endif // USART rev. >= 4.0.0
00382
00384
00385
00386
00389
00390
00399 #if (defined __GNUC__)
00400 __attribute__((__always_inline__))
00401 #endif
00402 extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)
00403 {
00404 usart->cr = AVR32_USART_CR_RSTSTA_MASK;
00405 }
00406
00413 #if (defined __GNUC__)
00414 __attribute__((__always_inline__))
00415 #endif
00416 extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)
00417 {
00418 return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
00419 }
00420
00427 #if (defined __GNUC__)
00428 __attribute__((__always_inline__))
00429 #endif
00430 extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)
00431 {
00432 return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
00433 }
00434
00441 #if (defined __GNUC__)
00442 __attribute__((__always_inline__))
00443 #endif
00444 extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)
00445 {
00446 return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
00447 }
00448
00449 #if defined(AVR32_USART_400_H_INCLUDED) || \
00450 defined(AVR32_USART_410_H_INCLUDED) || \
00451 defined(AVR32_USART_420_H_INCLUDED) || \
00452 defined(AVR32_USART_440_H_INCLUDED)
00453
00460 #if (defined __GNUC__)
00461 __attribute__((__always_inline__))
00462 #endif
00463 extern __inline__ int usart_lin_get_error(volatile avr32_usart_t *usart)
00464 {
00465 return (usart->csr & (AVR32_USART_CSR_LINSNRE_MASK |
00466 AVR32_USART_CSR_LINCE_MASK |
00467 AVR32_USART_CSR_LINIPE_MASK |
00468 AVR32_USART_CSR_LINISFE_MASK |
00469 AVR32_USART_CSR_LINBE_MASK)) >> AVR32_USART_CSR_LINBE_OFFSET;
00470 }
00471
00472 #endif // USART rev. >= 4.0.0
00473
00475
00476
00477
00480
00481
00488 #if (defined __GNUC__)
00489 __attribute__((__always_inline__))
00490 #endif
00491 extern __inline__ void usart_iso7816_enable_receiver(volatile avr32_usart_t *usart)
00492 {
00493 usart->cr = AVR32_USART_CR_TXDIS_MASK | AVR32_USART_CR_RXEN_MASK;
00494 }
00495
00502 #if (defined __GNUC__)
00503 __attribute__((__always_inline__))
00504 #endif
00505 extern __inline__ void usart_iso7816_enable_transmitter(volatile avr32_usart_t *usart)
00506 {
00507 usart->cr = AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXEN_MASK;
00508 }
00509
00511
00512
00513
00514 #if defined(AVR32_USART_400_H_INCLUDED) || \
00515 defined(AVR32_USART_410_H_INCLUDED) || \
00516 defined(AVR32_USART_420_H_INCLUDED) || \
00517 defined(AVR32_USART_440_H_INCLUDED)
00518
00521
00522
00530 #if (defined __GNUC__)
00531 __attribute__((__always_inline__))
00532 #endif
00533 extern __inline__ void usart_lin_set_node_action(volatile avr32_usart_t *usart, unsigned char action)
00534 {
00535 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_NACT_MASK) |
00536 action << AVR32_USART_LINMR_NACT_OFFSET;
00537 }
00538
00544 #if (defined __GNUC__)
00545 __attribute__((__always_inline__))
00546 #endif
00547 extern __inline__ void usart_lin_enable_parity(volatile avr32_usart_t *usart, unsigned char parity)
00548 {
00549 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_PARDIS_MASK) |
00550 !parity << AVR32_USART_LINMR_PARDIS_OFFSET;
00551 }
00552
00558 #if (defined __GNUC__)
00559 __attribute__((__always_inline__))
00560 #endif
00561 extern __inline__ void usart_lin_enable_checksum(volatile avr32_usart_t *usart, unsigned char checksum)
00562 {
00563 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKDIS_MASK) |
00564 !checksum << AVR32_USART_LINMR_CHKDIS_OFFSET;
00565 }
00566
00573 #if (defined __GNUC__)
00574 __attribute__((__always_inline__))
00575 #endif
00576 extern __inline__ void usart_lin_set_checksum(volatile avr32_usart_t *usart, unsigned char chktyp)
00577 {
00578 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKTYP_MASK) |
00579 chktyp << AVR32_USART_LINMR_CHKTYP_OFFSET;
00580 }
00581
00588 #if (defined __GNUC__)
00589 __attribute__((__always_inline__))
00590 #endif
00591 extern __inline__ unsigned char usart_lin_get_data_length(volatile avr32_usart_t *usart)
00592 {
00593 if (usart->linmr & AVR32_USART_LINMR_DLM_MASK)
00594 {
00595 unsigned char data_length = 1 << ((usart->linir >> (AVR32_USART_LINIR_IDCHR_OFFSET + 4)) & 0x03);
00596 if (data_length == 1)
00597 data_length = 2;
00598 return data_length;
00599 }
00600 else
00601 return ((usart->linmr & AVR32_USART_LINMR_DLC_MASK) >> AVR32_USART_LINMR_DLC_OFFSET) + 1;
00602 }
00603
00608 #if (defined __GNUC__)
00609 __attribute__((__always_inline__))
00610 #endif
00611 extern __inline__ void usart_lin_set_data_length_lin1x(volatile avr32_usart_t *usart)
00612 {
00613 usart->linmr |= AVR32_USART_LINMR_DLM_MASK;
00614 }
00615
00621 #if (defined __GNUC__)
00622 __attribute__((__always_inline__))
00623 #endif
00624 extern __inline__ void usart_lin_set_data_length_lin2x(volatile avr32_usart_t *usart, unsigned char data_length)
00625 {
00626 usart->linmr = (usart->linmr & ~(AVR32_USART_LINMR_DLC_MASK |
00627 AVR32_USART_LINMR_DLM_MASK)) |
00628 (data_length - 1) << AVR32_USART_LINMR_DLC_OFFSET;
00629 }
00630
00637 #if (defined __GNUC__)
00638 __attribute__((__always_inline__))
00639 #endif
00640 extern __inline__ void usart_lin_enable_frameslot(volatile avr32_usart_t *usart, unsigned char frameslot)
00641 {
00642 usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_FSDIS_MASK) |
00643 !frameslot << AVR32_USART_LINMR_FSDIS_OFFSET;
00644 }
00645
00652 #if (defined __GNUC__)
00653 __attribute__((__always_inline__))
00654 #endif
00655 extern __inline__ unsigned char usart_lin_get_id_char(volatile avr32_usart_t *usart)
00656 {
00657 return (usart->linir & AVR32_USART_LINIR_IDCHR_MASK) >> AVR32_USART_LINIR_IDCHR_OFFSET;
00658 }
00659
00665 #if (defined __GNUC__)
00666 __attribute__((__always_inline__))
00667 #endif
00668 extern __inline__ void usart_lin_set_id_char(volatile avr32_usart_t *usart, unsigned char id_char)
00669 {
00670 usart->linir = (usart->linir & ~AVR32_USART_LINIR_IDCHR_MASK) |
00671 id_char << AVR32_USART_LINIR_IDCHR_OFFSET;
00672 }
00673
00675
00676 #endif // USART rev. >= 4.0.0
00677
00678
00679
00680 #if defined(AVR32_USART_400_H_INCLUDED) || \
00681 defined(AVR32_USART_410_H_INCLUDED) || \
00682 defined(AVR32_USART_420_H_INCLUDED) || \
00683 defined(AVR32_USART_440_H_INCLUDED)
00684
00687
00688
00695 extern int usart_spi_selectChip(volatile avr32_usart_t *usart);
00696
00704 extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);
00705
00707
00708 #endif // USART rev. >= 4.0.0
00709
00710
00711
00714
00715
00729 extern int usart_send_address(volatile avr32_usart_t *usart, int address);
00730
00737 #if (defined __GNUC__)
00738 __attribute__((__always_inline__))
00739 #endif
00740 extern __inline__ int usart_tx_ready(volatile avr32_usart_t *usart)
00741 {
00742 return (usart->csr & AVR32_USART_CSR_TXRDY_MASK) != 0;
00743 }
00744
00753 extern int usart_write_char(volatile avr32_usart_t *usart, int c);
00754
00760 #if (defined __GNUC__)
00761 __attribute__((__always_inline__))
00762 #endif
00763 extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
00764 {
00765 while (usart_write_char(usart, c) != USART_SUCCESS);
00766 }
00767
00776 extern int usart_putchar(volatile avr32_usart_t *usart, int c);
00777
00785 #if (defined __GNUC__)
00786 __attribute__((__always_inline__))
00787 #endif
00788 extern __inline__ int usart_tx_empty(volatile avr32_usart_t *usart)
00789 {
00790 return (usart->csr & AVR32_USART_CSR_TXEMPTY_MASK) != 0;
00791 }
00792
00799 #if (defined __GNUC__)
00800 __attribute__((__always_inline__))
00801 #endif
00802 extern __inline__ int usart_test_hit(volatile avr32_usart_t *usart)
00803 {
00804 return (usart->csr & AVR32_USART_CSR_RXRDY_MASK) != 0;
00805 }
00806
00818 extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
00819
00826 extern int usart_getchar(volatile avr32_usart_t *usart);
00827
00833 extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
00834
00842 extern int usart_get_echo_line(volatile avr32_usart_t *usart);
00843
00844 #if defined(AVR32_USART_400_H_INCLUDED) || \
00845 defined(AVR32_USART_410_H_INCLUDED) || \
00846 defined(AVR32_USART_420_H_INCLUDED) || \
00847 defined(AVR32_USART_440_H_INCLUDED)
00848
00853 #if (defined __GNUC__)
00854 __attribute__((__always_inline__))
00855 #endif
00856 extern __inline__ void usart_lin_abort(volatile avr32_usart_t *usart)
00857 {
00858 usart->cr = AVR32_USART_LINABT_MASK;
00859 }
00860
00867 #if (defined __GNUC__)
00868 __attribute__((__always_inline__))
00869 #endif
00870 extern __inline__ int usart_lin_transfer_completed(volatile avr32_usart_t *usart)
00871 {
00872 return (usart->csr & AVR32_USART_CSR_LINTC_MASK) != 0;
00873 }
00874
00875 #endif // USART rev. >= 4.0.0
00876
00878
00879
00880 #endif // _USART_H_