AVR Z-LINKŪ


com.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00051 /*============================ INCLDUE =======================================*/
00052 #include <stdint.h>
00053 #include <stdbool.h>
00054 
00055 #ifdef UART_EXTENDED
00056 #include "config_uart_extended.h"
00057 #else
00058 #include "config_uart.h"
00059 #endif
00060 
00061 #include "compiler.h"
00062 #include "com.h"
00063 /*============================ MACROS ========================================*/
00064 #define COM_RX_MAX_BYTES (COM_RX_BUFFER_SIZE - 2) 
00065 /*============================ TYPEDEFS ======================================*/
00066 /*============================ VARIABLES =====================================*/
00067 static uint8_t com_buffer[ COM_RX_BUFFER_SIZE ]; 
00068 static uint8_t com_number_of_received_bytes; 
00069 static bool com_data_reception_finished; 
00070 static uint8_t hex_lookup[ ] = "0123456789ABCDEF"; 
00071 /*============================ PROTOTYPES ====================================*/
00072 
00077 void com_init( baud_rate_t rate ){
00078   
00079 #if defined( RZ502 )    
00080     //Initialize USART module.
00081     UBRR0H = 0x00;
00082     UBRR0L = rate;
00083   
00084     //Enable USART transmitter module. Always on.
00085     ENABLE_RECEIVER;
00086     ENABLE_TRANSMITTER;
00087         
00088     //8-N-1.
00089     UCSR0C |= ( 1 << UCSZ01 ) | ( 1 << UCSZ00 ); 
00090   
00091     com_number_of_received_bytes = 0;
00092     com_data_reception_finished = false;
00093     ENABLE_RECEIVE_COMPLETE_INTERRUPT;
00094 
00095 #elif defined( STK541 )
00096     XRAM_ENABLE( ); 
00097         
00098         /* make sure USB_RXF and USB_TXE are inputs */
00099         FTDI_ENABLE_TX( );
00100         
00101         //Enable external interrupt on FTDI_RX pin.
00102         FTDI_CONFIGURE_PIN_CHANGE_INTERRUPT( );
00103         FTDI_ENABLE_RECEIVER( );
00104 #else
00105     #error "Board Option Not Supported."  
00106 #endif  
00107 }
00108 
00114 void com_send_string( uint8_t *data, uint8_t data_length ){
00115     
00116     while (--data_length > 0) {
00117         
00118 #if defined( RZ502 )        
00119         for(; !(UCSR0A & (1 << UDRE0));) {;}
00120             UDR0 = *data++; //Put symbol in data register.
00121     
00122 #elif defined( STK541 )
00123         //Wait until the fifo is ready.
00124             while((FTDI_TX_MASK & FTDI_PIN) != LOW){;}
00125         
00126             //Write symbol to memory address.
00127             *FTDI_Fifo = ( *data++ );
00128 #else
00129     #error "Board Option Not Supported."
00130 #endif
00131     }    
00132 }
00133 
00138 void com_send_hex( uint8_t nmbr ){
00139 
00140     #if defined( RZ502 )        
00141         for(; !(UCSR0A & (1 << UDRE0));) {;}
00142             UDR0 = '0'; //Put symbol in data register.
00143     
00144         for(; !(UCSR0A & (1 << UDRE0));) {;}
00145             UDR0 = 'x'; //Put symbol in data register.
00146         
00147         for(; !(UCSR0A & (1 << UDRE0));) {;}
00148             UDR0 = hex_lookup[ ( nmbr >> 4 ) & 0x0F ];
00149         
00150         for(; !(UCSR0A & (1 << UDRE0));) {;}
00151             UDR0 = hex_lookup[ ( nmbr & 0x0F ) ];
00152 #elif defined( STK541 )
00153         
00154         //Wait until the fifo is ready.
00155             while((FTDI_TX_MASK & FTDI_PIN) != LOW){;}
00156             *FTDI_Fifo = '0'; //Put symbol in data register.
00157     
00158         while((FTDI_TX_MASK & FTDI_PIN) != LOW){;}
00159             *FTDI_Fifo = 'x'; //Put symbol in data register.
00160         
00161         while((FTDI_TX_MASK & FTDI_PIN) != LOW){;}
00162             *FTDI_Fifo = hex_lookup[ ( nmbr >> 4 ) & 0x0F ];
00163         
00164         while((FTDI_TX_MASK & FTDI_PIN) != LOW){;}
00165             *FTDI_Fifo = hex_lookup[ ( nmbr & 0x0F ) ];
00166 #else
00167     #error "Board Option Not Supported."
00168 #endif
00169 }
00170 
00177 uint8_t * com_get_received_data( void ){
00178     return &com_buffer[0];
00179 }
00180 
00187 uint8_t com_get_number_of_received_bytes( void ){
00188     
00189     if (com_data_reception_finished == true) {
00190         return com_number_of_received_bytes; 
00191     } else { return 0; }
00192 }
00193 
00198 void com_reset_receiver( void ){
00199     
00200 #if defined( RZ502 )    
00201     DISABLE_RECEIVE_COMPLETE_INTERRUPT;
00202     
00203     com_number_of_received_bytes = 0;
00204     com_data_reception_finished = false;
00205     
00206     uint8_t dummy = 0;
00207     //Following loop is used to ensure that the rx FIFO is flushed.
00208         //Sometimes it gets cloged up with old data.
00209         for( ;  UCSR0A & ( 1 << RXC0 ); ){
00210                 dummy = UDR0;  
00211         }
00212     
00213     ENABLE_RECEIVE_COMPLETE_INTERRUPT;
00214 #elif defined( STK541 )
00215     FTDI_DISABLE_RECEIVER( );
00216     
00217     com_number_of_received_bytes = 0;
00218     com_data_reception_finished = false;
00219     
00220     FTDI_ENABLE_RECEIVER( );
00221 #else
00222     #error "Board Option Not Supported."
00223 #endif    
00224 }
00225 
00234 #ifdef RZ502
00235 ISR( USART0_RX_vect ){
00236         
00237         uint8_t receivedData;
00238         
00239         receivedData = ( uint8_t )UDR0; //Collect data.
00240 #else
00241 ISR( INT7_vect ){
00242           
00243         uint8_t receivedData;
00244         
00245         receivedData = ( uint8_t )*FTDI_Fifo;   //Collect data.
00246 #endif
00247         
00248     if (com_number_of_received_bytes < COM_RX_MAX_BYTES) {
00249         
00250         //End of data stream.
00251         if (receivedData == '\n') {
00252             
00253 #if defined( RZ502 )            
00254             DISABLE_RECEIVE_COMPLETE_INTERRUPT;
00255 #elif defined( STK541 )            
00256             FTDI_DISABLE_RECEIVER( );
00257 #else
00258     #error "Board Option Not Supported."
00259 #endif            
00260             com_buffer[com_number_of_received_bytes++] = receivedData;
00261             com_buffer[com_number_of_received_bytes++] = 0x00;
00262             com_buffer[com_number_of_received_bytes++] = 0x00;
00263             
00264             com_data_reception_finished = true;
00265         } else {
00266             com_buffer[com_number_of_received_bytes++] = receivedData;
00267         }
00268     }
00269     
00270     else{
00271         
00272 #if defined( RZ502 )            
00273             DISABLE_RECEIVE_COMPLETE_INTERRUPT;
00274 #elif defined( STK541 )            
00275             FTDI_DISABLE_RECEIVER( );
00276 #else
00277     #error "Board Option Not Supported."
00278 #endif                    
00279         com_number_of_received_bytes = 1;
00280         com_data_reception_finished = true;
00281     }
00282 }
@DOC_TITLE@
Generated on Wed Jul 25 21:15:49 2007 for AVR2001 Software Programmer's Manual by doxygen 1.4.7