uart1.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <mc1322x.h>
00037 #include <stdint.h>
00038
00039 volatile char u1_tx_buf[UART1_TX_BUFFERSIZE];
00040 volatile uint32_t u1_tx_head, u1_tx_tail;
00041
00042 #if UART1_RX_BUFFERSIZE > 32
00043 volatile char u1_rx_buf[UART1_RX_BUFFERSIZE-32];
00044 volatile uint16_t u1_rx_head, u1_rx_tail,u1_rx_hyst;
00045 #endif
00046
00047 void uart1_isr(void) {
00048
00049 #if UART1_RX_BUFFERSIZE > 32
00050 if (*UART1_USTAT & ( 1 << 6)) {
00051 while( *UART1_URXCON != 0 ) {
00052 uint32_t u1_rx_tail_next;
00053 u1_rx_tail_next = u1_rx_tail+1;
00054 if (u1_rx_tail_next >= sizeof(u1_rx_buf))
00055 u1_rx_tail_next = 0;
00056 if (u1_rx_head != u1_rx_tail_next) {
00057 u1_rx_buf[u1_rx_tail]= *UART1_UDATA;
00058 u1_rx_tail = u1_rx_tail_next;
00059 } else {
00060 *UART1_UCON |= (1<<14);
00061
00062 if (sizeof(u1_rx_buf)>30) u1_rx_hyst=30;else u1_rx_hyst=sizeof(u1_rx_buf);
00063 return;
00064 }
00065 }
00066 return;
00067 }
00068 #endif
00069
00070 while( *UART1_UTXCON != 0 ) {
00071 if (u1_tx_head == u1_tx_tail) {
00072 #if UART1_RX_BUFFERSIZE > 32
00073 *UART1_UCON |= (1 << 13);
00074 #else
00075 disable_irq(UART1);
00076 #endif
00077 return;
00078 }
00079
00080 *UART1_UDATA = u1_tx_buf[u1_tx_tail];
00081 u1_tx_tail++;
00082 if (u1_tx_tail >= sizeof(u1_tx_buf))
00083 u1_tx_tail = 0;
00084 }
00085 }
00086
00087 void uart1_putc(char c) {
00088
00089
00090 #if UART1_RX_BUFFERSIZE > 32
00091 *UART1_UCON |= (1 << 13);
00092 #else
00093 disable_irq(UART1);
00094 #endif
00095
00096 if( (u1_tx_head == u1_tx_tail) &&
00097 (*UART1_UTXCON != 0)) {
00098 *UART1_UDATA = c;
00099 } else {
00100 u1_tx_buf[u1_tx_head] = c;
00101 u1_tx_head += 1;
00102 if (u1_tx_head >= sizeof(u1_tx_buf))
00103 u1_tx_head = 0;
00104 if (u1_tx_head == u1_tx_tail) {
00105 if (u1_tx_head) { u1_tx_head -=1; } else { u1_tx_head = sizeof(u1_tx_buf); }
00106 }
00107
00108 #if UART1_RX_BUFFERSIZE > 32
00109 *UART1_UCON &= ~(1 << 13);
00110 #else
00111 enable_irq(UART1);
00112 #endif
00113
00114 }
00115 }
00116
00117 uint8_t uart1_getc(void) {
00118 #if UART1_RX_BUFFERSIZE > 32
00119 uint8_t c=0;
00120
00121 if (u1_rx_head != u1_rx_tail) {
00122 c = u1_rx_buf[u1_rx_head++];
00123 if (u1_rx_head >= sizeof(u1_rx_buf))
00124 u1_rx_head=0;
00125 if (u1_rx_hyst) {
00126 if (--u1_rx_hyst == 0)
00127 *UART1_UCON &= ~(1<<14);
00128 }
00129 return c;
00130 }
00131 #endif
00132
00133
00134 return *UART1_UDATA;
00135 }