AVR Z-LINKŪ


com.c File Reference


Detailed Description

This files implements a communications library for the FTDI chip on the STK541 board and the AVR USART used for the RZ502 version.

Note:
The code must be compiled with either STK541 (USB) or RZ502 (RS232) defined.
Application note:
AVR2001: Transceiver Access Toolbox for the AT86RF230
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
$Name$
Revision
613
$RCSfile$
Date
2006-04-07 14:40:07 +0200 (fr, 07 apr 2006)

Copyright (c) 2006, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file com.c.

#include <stdint.h>
#include <stdbool.h>
#include "config_uart.h"
#include "compiler.h"
#include "com.h"

Include dependency graph for com.c:

Go to the source code of this file.

Defines

#define COM_RX_MAX_BYTES   (COM_RX_BUFFER_SIZE - 2)
 Maximal number of bytes that one message can contain (PSDU_LENGTH - CRC_LENGTH).

Functions

uint8_t com_get_number_of_received_bytes (void)
 This function returns number of bytes received during last data reception.
uint8_t * com_get_received_data (void)
 This function retruns the address to the first byte in the buffer where received data is stored3.
void com_init (baud_rate_t rate)
 This function initializes the chosen communication interface (USB or USART).
void com_reset_receiver (void)
 This function is used to reset the commuincation interface after each data reception is done, and the end-user has read data.
void com_send_hex (uint8_t nmbr)
 This function prints the supplied argument as a hex number.
void com_send_string (uint8_t *data, uint8_t data_length)
 This function sends data on the chosen communication interface (USB or USART).
 ISR (USART0_RX_vect)
 Universal receive interrupt service routine for both USART0 and the FTDI USB chip.

Variables

static uint8_t com_buffer [COM_RX_BUFFER_SIZE]
 Array storing rx data.
static bool com_data_reception_finished
 Flag indicating EOT (That "\r\n is received.").
static uint8_t com_number_of_received_bytes
 Number of bytes in com_buffer.
static uint8_t hex_lookup [] = "0123456789ABCDEF"
 Look up table for hexadecimal number conversion.


Define Documentation

#define COM_RX_MAX_BYTES   (COM_RX_BUFFER_SIZE - 2)

Maximal number of bytes that one message can contain (PSDU_LENGTH - CRC_LENGTH).

Definition at line 64 of file com.c.

Referenced by ISR().


Function Documentation

uint8_t com_get_number_of_received_bytes ( void   ) 

This function returns number of bytes received during last data reception.

Return values:
0 No data is available. Data reception is not done.
1 Error: Typed Frame Too Long.
Returns:
Any non zero value returned indicatest that data is available and should be read.

Definition at line 187 of file com.c.

References com_data_reception_finished, and com_number_of_received_bytes.

Referenced by main().

00187                                                 {
00188     
00189     if (com_data_reception_finished == true) {
00190         return com_number_of_received_bytes; 
00191     } else { return 0; }
00192 }

uint8_t* com_get_received_data ( void   ) 

This function retruns the address to the first byte in the buffer where received data is stored3.

Note:
This function should only be called after it has been verified that the data reception is done (com_get_number_of_received_bytes() != 0).
Returns:
Pointer to the first byte in the array of received data.

Definition at line 177 of file com.c.

References com_buffer.

Referenced by main().

00177                                        {
00178     return &com_buffer[0];
00179 }

void com_init ( baud_rate_t  rate  ) 

This function initializes the chosen communication interface (USB or USART).

Parameters:
[in] rate Baudrate used by the AVR's USART.

Definition at line 77 of file com.c.

References com_data_reception_finished, com_number_of_received_bytes, ENABLE_RECEIVE_COMPLETE_INTERRUPT, ENABLE_RECEIVER, ENABLE_TRANSMITTER, FTDI_CONFIGURE_PIN_CHANGE_INTERRUPT, FTDI_ENABLE_RECEIVER, FTDI_ENABLE_TX, and XRAM_ENABLE.

Referenced by avr_init().

00077                                  {
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 }

void com_reset_receiver ( void   ) 

This function is used to reset the commuincation interface after each data reception is done, and the end-user has read data.

Definition at line 198 of file com.c.

References com_data_reception_finished, com_number_of_received_bytes, DISABLE_RECEIVE_COMPLETE_INTERRUPT, ENABLE_RECEIVE_COMPLETE_INTERRUPT, FTDI_DISABLE_RECEIVER, and FTDI_ENABLE_RECEIVER.

Referenced by main().

00198                                {
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 }

void com_send_hex ( uint8_t  nmbr  ) 

This function prints the supplied argument as a hex number.

Parameters:
[in] nmbr Number to be printed as a hexadescimal number.

Definition at line 138 of file com.c.

References FTDI_Fifo, FTDI_PIN, FTDI_TX_MASK, hex_lookup, and LOW.

Referenced by main().

00138                                  {
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 }

void com_send_string ( uint8_t *  data,
uint8_t  data_length 
)

This function sends data on the chosen communication interface (USB or USART).

Parameters:
[in] data Pointer to data that is to be sent on the communication interface.
[in] data_length Number of bytes to read from the array pointed to by data.

Definition at line 114 of file com.c.

References FTDI_Fifo, FTDI_PIN, FTDI_TX_MASK, and LOW.

Referenced by main().

00114                                                           {
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 }

ISR ( USART0_RX_vect   ) 

Universal receive interrupt service routine for both USART0 and the FTDI USB chip.

This routine is called whenever a new byte is available to be read. This service routine does also implement a sort of pre parsing of the incoming data stream. The stream is terminated when the EOT character is detected ('
'). The serial interface will be stoped and the since the com_data_reception_finished flag set to true. The routine appends two characters at

Definition at line 235 of file com.c.

References com_buffer, com_data_reception_finished, com_number_of_received_bytes, COM_RX_MAX_BYTES, DISABLE_RECEIVE_COMPLETE_INTERRUPT, FTDI_DISABLE_RECEIVER, and FTDI_Fifo.

00235                      {
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 }


Variable Documentation

uint8_t com_buffer[COM_RX_BUFFER_SIZE] [static]

Array storing rx data.

Definition at line 67 of file com.c.

Referenced by com_get_received_data(), and ISR().

bool com_data_reception_finished [static]

Flag indicating EOT (That "\r\n is received.").

Definition at line 69 of file com.c.

Referenced by com_get_number_of_received_bytes(), com_init(), com_reset_receiver(), and ISR().

uint8_t com_number_of_received_bytes [static]

Number of bytes in com_buffer.

Definition at line 68 of file com.c.

Referenced by com_get_number_of_received_bytes(), com_init(), com_reset_receiver(), and ISR().

uint8_t hex_lookup[] = "0123456789ABCDEF" [static]

Look up table for hexadecimal number conversion.

Definition at line 70 of file com.c.

Referenced by com_send_hex().

@DOC_TITLE@
Generated on Wed Jul 25 21:15:49 2007 for AVR2001 Software Programmer's Manual by doxygen 1.4.7