00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00020 #include <ioavr.h> 00021 #include "global.h" 00022 #include "uart.h" 00023 #include "sm_driver.h" 00024 #include "speed_cntr.h" 00025 00027 unsigned char UART_RxBuffer[UART_RX_BUFFER_SIZE]; 00029 unsigned char UART_RxPtr; 00030 00031 // Static Variables. 00033 static unsigned char UART_TxBuffer[UART_TX_BUFFER_SIZE]; 00035 static volatile unsigned char UART_TxHead; 00037 static volatile unsigned char UART_TxTail; 00038 00045 void InitUART(void) 00046 { 00047 // Set baud rate. 19.2 kbps trasfer speed running at 8 MHz. 00048 //#define BAUD 25 00049 // Set baud rate. 19.2 kbps trasfer speed running at 3.6864 MHz. 00050 #define BAUD 11 00051 00052 UBRR0H = (unsigned char)(BAUD>>8); 00053 UBRR0L = (unsigned char)BAUD; 00054 /* Enable receiver and transmitter, rx and tx int */ 00055 UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(1<<TXCIE0); 00056 /* Set frame format: 8data, 1stop bit */ 00057 UCSR0C = (3<<UCSZ00); 00058 00059 // Flush Buffers 00060 UART_RxPtr = 0; 00061 UART_TxTail = 0; 00062 UART_TxHead = 0; 00063 } 00064 00065 00073 void uart_SendByte(unsigned char data) 00074 { 00075 unsigned char tmphead; 00076 00077 // Calculate buffer index 00078 tmphead = ( UART_TxHead + 1 ) & UART_TX_BUFFER_MASK; 00079 // Wait for free space in buffer 00080 while ( tmphead == UART_TxTail ) 00081 ; 00082 // Store data in buffer 00083 UART_TxBuffer[tmphead] = data; 00084 // Store new index 00085 UART_TxHead = tmphead; 00086 // Enable UDRE interrupt 00087 SET_UDRIE; 00088 } 00089 00097 void uart_SendString(unsigned char Str[]) 00098 { 00099 unsigned char n = 0; 00100 while(Str[n]) 00101 uart_SendByte(Str[n++]); 00102 } 00103 00111 void uart_SendInt(int x) 00112 { 00113 static const char dec[] = "0123456789"; 00114 unsigned int div_val = 10000; 00115 00116 if (x < 0){ 00117 x = - x; 00118 uart_SendByte('-'); 00119 } 00120 while (div_val > 1 && div_val > x) 00121 div_val /= 10; 00122 do{ 00123 uart_SendByte (dec[x / div_val]); 00124 x %= div_val; 00125 div_val /= 10; 00126 }while(div_val); 00127 } 00128 00135 void uart_FlushRxBuffer(void){ 00136 UART_RxPtr = 0; 00137 UART_RxBuffer[UART_RxPtr] = 0; 00138 } 00139 00145 #pragma vector=USART_RX_vect 00146 __interrupt void UART_RX_interrupt( void ) 00147 { 00148 unsigned char data; 00149 00150 // Read the received data. 00151 data = UDR0; 00152 00153 if(status.running == FALSE){ 00154 // If backspace. 00155 if(data == '\b') 00156 { 00157 if(UART_RxPtr) 00158 // Done if not at beginning of buffer. 00159 { 00160 uart_SendByte('\b'); 00161 uart_SendByte(' '); 00162 uart_SendByte('\b'); 00163 UART_RxPtr--; 00164 UART_RxBuffer[UART_RxPtr]=0x00; 00165 } 00166 } 00167 // Not backspace. 00168 else 00169 { 00170 // Put the data into RxBuf 00171 // and place 0x00 after it. If buffer is full, 00172 // data is written to UART_RX_BUFFER_SIZE - 1. 00173 if(UART_RxPtr < (UART_RX_BUFFER_SIZE - 1)){ 00174 UART_RxBuffer[UART_RxPtr] = data; 00175 UART_RxBuffer[UART_RxPtr + 1]=0x00; 00176 UART_RxPtr++; 00177 } 00178 else 00179 { 00180 UART_RxBuffer[UART_RxPtr - 1] = data; 00181 uart_SendByte('\b'); 00182 } 00183 // If enter. 00184 if(data == 13){ 00185 status.cmd = TRUE; 00186 } 00187 else 00188 uart_SendByte(data); 00189 } 00190 } 00191 } 00192 00199 #pragma vector=USART_UDRE_vect 00200 __interrupt void UART_TX_interrupt( void ) 00201 { 00202 unsigned char UART_TxTail_tmp; 00203 UART_TxTail_tmp = UART_TxTail; 00204 00205 // Check if all data is transmitted 00206 if ( UART_TxHead != UART_TxTail_tmp ) 00207 { 00208 // Calculate buffer index 00209 UART_TxTail_tmp = ( UART_TxTail + 1 ) & UART_TX_BUFFER_MASK; 00210 // Store new index 00211 UART_TxTail = UART_TxTail_tmp; 00212 // Start transmition 00213 UDR0= UART_TxBuffer[ UART_TxTail_tmp]; 00214 } 00215 else 00216 // Disable UDRE interrupt 00217 CLR_UDRIE; 00218 }