AVR Z-LINKŪ


wireless_uart_extended.c File Reference


Detailed Description

This files implements a wireless IEEE 802.15.4 serial communication link between two nodes.

The Extended Wireless Uart Example

This particular example implements a more advanced wireless serial communication link. This implementation follows the IEEE 802.15.4 standard and uses some of the more advanced features in the AT86RF230 radio transceiver. Data is sent as IEEE 802.15.4 data frames, and each frame is acknowledged as specified in the standard by using the extended operating modes of the radio transceiver (TX_ARET and RX_AACK).

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 wireless_uart_extended.c.

#include <stdint.h>
#include <stdbool.h>
#include "config_uart_extended.h"
#include "compiler.h"
#include "at86rf230_registermap.h"
#include "hal.h"
#include "tat.h"
#include "com.h"

Include dependency graph for wireless_uart_extended.c:

Go to the source code of this file.

Functions

static void avr_init (void)
 This function configure the necessary IO modules on the AVR.
void main (void)
static void rx_pool_init (void)
 This function initialize the rx_pool. The rx_pool is in essence a FIFO.
static void trx_end_handler (uint32_t time_stamp)
 This function is the TRX_END event handler that is called from the TRX isr if assigned.
static bool trx_init (void)
 This function is used to initialize the TRX.

Variables

static uint8_t debug_data_received [] = "\r--->Rx:\r"
 Debug Text.
static uint8_t debug_data_sent [] = "<---TX OK.\r\n"
 Debug Text.
static uint8_t debug_fatal_error [] = "A fatal error. System must be reset.\r\n"
 Debug Text.
static uint8_t debug_lqi [] = "LQI: "
 Debug Text.
static uint8_t debug_pll_transition [] = "State transition failed\r\n"
 Debug Text.
static uint8_t debug_rx_pool_overflow [] = "RX Buffer Overflow!\r\n"
 Debug Text.
static uint8_t debug_transmission_failed [] = "TX Failed!\r\n"
 Debug Text.
static uint8_t debug_transmission_length [] = "Typed Message too long!!\r\n"
 Debug Text.
static uint8_t debug_type_message [] = "\r<---Type Message:\r\n"
 Debug Text.
static bool rx_flag
 Flag used to mask between the two possible TRX_END events.
static hal_rx_frame_t rx_pool [RX_POOL_SIZE]
 Pool of hal_rx_frame_t's.
static hal_rx_frame_t * rx_pool_end
 Pointer to end of pool.
static hal_rx_frame_t * rx_pool_head
 Pointer to next hal_rx_frame_t it is possible to write.
static uint8_t rx_pool_items_free
 Number of free items (hal_rx_frame_t) in the pool.
static uint8_t rx_pool_items_used
static bool rx_pool_overflow_flag
 Flag that is used to signal a pool overflow.
static hal_rx_frame_t * rx_pool_start
 Pointer to start of pool.
static hal_rx_frame_t * rx_pool_tail
 Pointer to next hal_rx_frame_t that can be read from the pool.
static uint8_t tx_frame [127]
 Buffer used to build TX frames. (Size must be max PSDU length.).


Function Documentation

static void avr_init ( void   )  [static]

This function configure the necessary IO modules on the AVR.

Definition at line 141 of file wireless_uart_extended.c.

References BR_38400, and com_init().

00141                             {
00142     com_init( BR_38400 );
00143 }

Here is the call graph for this function:

void main ( void   ) 

Definition at line 195 of file wireless_uart_extended.c.

References avr_init(), com_get_number_of_received_bytes(), com_get_received_data(), com_reset_receiver(), COM_RX_BUFFER_SIZE, com_send_hex(), com_send_string(), debug_data_received, debug_data_sent, debug_fatal_error, debug_lqi, debug_pll_transition, debug_rx_pool_overflow, debug_transmission_failed, debug_transmission_length, debug_type_message, DEST_ADDRESS, PAN_ID, rx_flag, rx_pool_end, rx_pool_init(), rx_pool_items_free, rx_pool_items_used, rx_pool_overflow_flag, rx_pool_start, rx_pool_tail, SHORT_ADDRESS, trx_init(), and tx_frame.

00195                  {
00196     
00197     static uint8_t length_of_received_data = 0;
00198     static uint8_t frame_sequence_number = 0;
00199     rx_flag = true;
00200     
00201     /*Pre Build Header of IEEE 802.15.4 Data frame.*/
00202     tx_frame[ 0 ] = 0x61; //FCF.
00203     tx_frame[ 1 ] = 0x88; //FCF.
00204                            //Sequence number set during frame transmission.
00205     tx_frame[ 3 ] = PAN_ID & 0xFF; //Dest. PANID.
00206     tx_frame[ 4 ] = (PAN_ID >> 8 ) & 0xFF; //Dest. PANID.
00207     tx_frame[ 5 ] = DEST_ADDRESS & 0xFF; //Dest. Addr.
00208     tx_frame[ 6 ] = (DEST_ADDRESS >> 8 ) & 0xFF; //Dest. Addr.
00209     tx_frame[ 7 ] = SHORT_ADDRESS & 0xFF; //Source Addr.
00210     tx_frame[ 8 ] = (SHORT_ADDRESS >> 8 ) & 0xFF; //Source Addr.
00211     
00212     rx_pool_init( );
00213     avr_init( );
00214     trx_init( );
00215     
00216     //Set system state to RX_AACK_ON
00217     if (tat_set_trx_state( RX_AACK_ON ) != TAT_SUCCESS) {
00218         com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00219     } // end: if (tat_set_trx_state( RX_AACK_ON ) != TAT_SUCCESS) ...
00220     
00221     sei( );
00222     
00223     //Give the user an indication that the system is ready.
00224     com_send_string( debug_type_message, sizeof( debug_type_message ) );
00225     
00226     /*Enter Normal Program Flow:
00227         - Check for newly received frames. Print them if something is received.
00228         - Notify on rx_pool overflow.
00229         - Try to send data on air interface, if something is received on UART/USB.
00230         - Notify if the typed message was too long.
00231      */
00232     while (true) {
00233         
00234         //Check if we have received something on the air interface.
00235         if (rx_pool_items_used != 0) {
00236         
00237             //Handle wrapping of rx_pool.
00238             if (rx_pool_tail == rx_pool_end) {
00239                 rx_pool_tail = rx_pool_start;
00240             } else {
00241                 ++rx_pool_tail;
00242             } // end: if (rx_pool_tail == rx_pool_end) ...
00243             
00244             //Turn interrupts off for a short while to protect when status
00245             //information about the rx_pool is updated.
00246             cli( );
00247             
00248             ++rx_pool_items_free;
00249             --rx_pool_items_used;
00250             
00251             sei( );
00252             
00253             //Send the frame to the user:
00254             com_send_string( debug_data_received, sizeof( debug_data_received ) );
00255             com_send_string( &(rx_pool_tail->data[ 9 ]), ((rx_pool_tail->length) - 9 - 2 ) );
00256             com_send_string( debug_lqi, sizeof( debug_lqi ) );
00257             com_send_hex( rx_pool_tail->lqi );
00258             com_send_string( debug_type_message, sizeof( debug_type_message ) );
00259         } // end: if (rx_pool_items_used != 0) ...
00260         
00261         //Check for rx_pool overflow.
00262         if (rx_pool_overflow_flag == true) {
00263             
00264             cli();
00265             rx_pool_init( );
00266             com_send_string( debug_rx_pool_overflow, sizeof( debug_rx_pool_overflow ) );
00267             sei();
00268         } // end: if (rx_pool_overflow_flag == true) ...
00269         
00270         //Check for new data on the serial interface.
00271         //Check if data is ready to be sent.
00272         length_of_received_data = com_get_number_of_received_bytes( );
00273         
00274         if (length_of_received_data == 1) {
00275             
00276             //length_of_received_data == 1 indicates a buffer overflow. Received data too long.
00277             com_send_string( debug_transmission_length, sizeof( debug_transmission_length ) );
00278             com_reset_receiver( );
00279         } else {
00280             
00281             if ((length_of_received_data >= 3) && (length_of_received_data <= COM_RX_BUFFER_SIZE)) {
00282             
00283                 //Change state to TX_ARET_ON and send data if the state transition was successful.
00284                 if (tat_set_trx_state( TX_ARET_ON ) == TAT_SUCCESS) {
00285                 
00286                     uint8_t *rx_frame = com_get_received_data( );
00287                 
00288                     uint8_t tx_frame_length = 9; // Length of prebuilt frame header.
00289                     tx_frame[ 2 ] = frame_sequence_number++; //Sequence Number.
00290                 
00291                     //Copy data into the TX frame buffer.
00292                     do {
00293                         tx_frame[ tx_frame_length++ ] = *rx_frame++;
00294                     } while (--length_of_received_data > 0);
00295                 
00296                     rx_flag = false; // Set the flag false, so that the TRX_END event is not misinterpreted.
00297                 
00298                     if (tat_send_data_with_retry( tx_frame_length, tx_frame, 1 ) == TAT_SUCCESS) {   
00299                         com_send_string( debug_data_sent, sizeof( debug_data_sent ) );
00300                     } else {
00301                         com_send_string( debug_transmission_failed, sizeof( debug_transmission_failed ) );
00302                     } // end:  if (tat_send_data_with_retry( tx_frame_length, tx_frame, 1 ) ...
00303                 } else {
00304                     com_send_string( debug_pll_transition, sizeof( debug_pll_transition ) );
00305                 } // end: if (tat_set_trx_state( TX_ARET_ON ) == TAT_SUCCESS) ...
00306             
00307                 if (tat_set_trx_state( RX_AACK_ON ) != TAT_SUCCESS) {
00308                     com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00309                 } // end: if (tat_set_trx_state( RX_AACK_ON ) != TAT_SUCCESS) ...
00310             
00311                 rx_flag = true; // Set the flag back again. Only used to protec the frame transmission.
00312                 com_reset_receiver( );
00313                 com_send_string( debug_type_message, sizeof( debug_type_message ) );
00314             } // end: 
00315         } // end: if (length_of_received_data == 1) ...
00316     } // emd: while (true) ...
00317 }

Here is the call graph for this function:

static void rx_pool_init ( void   )  [static]

This function initialize the rx_pool. The rx_pool is in essence a FIFO.

Definition at line 147 of file wireless_uart_extended.c.

References rx_pool, rx_pool_end, rx_pool_head, rx_pool_items_free, rx_pool_items_used, rx_pool_overflow_flag, RX_POOL_SIZE, rx_pool_start, and rx_pool_tail.

00147                                 {
00148 
00149     rx_pool_start = rx_pool;
00150     rx_pool_end = &rx_pool[ RX_POOL_SIZE - 1 ];
00151     
00152     rx_pool_head = rx_pool_start;
00153     rx_pool_tail = rx_pool_end;
00154     
00155     rx_pool_items_free = RX_POOL_SIZE;
00156     rx_pool_items_used = 0;
00157     
00158     rx_pool_overflow_flag = false;
00159 }

static void trx_end_handler ( uint32_t  time_stamp  )  [static]

This function is the TRX_END event handler that is called from the TRX isr if assigned.

Parameters:
[in] time_stamp Interrupt timestamp in IEEE 802.15.4 symbols.

Definition at line 166 of file wireless_uart_extended.c.

References rx_flag, rx_pool_end, rx_pool_head, rx_pool_items_free, rx_pool_items_used, rx_pool_overflow_flag, and rx_pool_start.

00166                                                   {
00167     
00168     if (rx_flag == true) {
00169         
00170         //Check if these is space left in the rx_pool.
00171         if (rx_pool_items_free == 0) {
00172             rx_pool_overflow_flag = true;
00173         } else {
00174         
00175             //Space left, so upload the received frame.
00176             hal_frame_read( rx_pool_head );
00177             
00178             //Then check the CRC. Will not store frames with invalid CRC.
00179             if (rx_pool_head->crc == true) {
00180                 
00181                 //Handle wrapping of rx_pool.
00182                 if (rx_pool_head == rx_pool_end) {
00183                     rx_pool_head = rx_pool_start;
00184                 } else {
00185                     ++rx_pool_head;
00186                 } // end: if (rx_pool_head == rx_pool_end) ...
00187                 
00188                 --rx_pool_items_free;
00189                 ++rx_pool_items_used;
00190             } // end: if (rx_pool_head->crc == true) ...
00191         } // end: if (rx_pool_items_free == 0) ...
00192     } // end:  if (rx_flag == true) ...
00193 }

static bool trx_init ( void   )  [static]

This function is used to initialize the TRX.

The TAT will be set up to run on the chosen operating channel, with CLKM diabled, and then configure the RX_AACK and TX_ARET modes.

Return values:
true if the TRX was successfully configured.
false if the TRX was not configured properly.

Definition at line 108 of file wireless_uart_extended.c.

References OPERATING_CHANNEL, PAN_ID, SHORT_ADDRESS, and trx_end_handler().

00108                             {
00109     
00110     static bool status;
00111     
00112     if (tat_init( ) != TAT_SUCCESS) {
00113         status = false;
00114     } else if (tat_set_operating_channel( OPERATING_CHANNEL ) != TAT_SUCCESS) {
00115         status = false;
00116     } else if (tat_set_clock_speed( true, CLKM_DISABLED ) != TAT_SUCCESS) {
00117         status = false; 
00118     } else{
00119 
00120         /*Set up the extended modes:*/
00121         //RX_AACK:
00122         tat_set_short_address( SHORT_ADDRESS ); //Short Address.
00123         tat_set_pan_id( PAN_ID ); //PAN ID.
00124         tat_set_device_role( false ); // No Coordintor support is necessary.
00125         
00126         //TX_ARET:
00127         tat_configure_csma( 234, 0xE2 ); // Default CSMA_SEED_0, MIN_BE = 3, MAX_CSMA_RETRIES = , and CSMA_SEED_1 = 
00128         
00129         //Both Modes:
00130         tat_use_auto_tx_crc( true ); //Automatic CRC must be enabled.
00131         hal_set_trx_end_event_handler( trx_end_handler ); // Event handler for TRX_END events.
00132         
00133         status = true;
00134     } // end: if (tat_init( ) != TAT_SUCCESS) ...
00135 
00136     return status;
00137 }    

Here is the call graph for this function:


Variable Documentation

uint8_t debug_data_received[] = "\r--->Rx:\r" [static]

Debug Text.

Definition at line 88 of file wireless_uart_extended.c.

uint8_t debug_data_sent[] = "<---TX OK.\r\n" [static]

Debug Text.

Definition at line 87 of file wireless_uart_extended.c.

uint8_t debug_fatal_error[] = "A fatal error. System must be reset.\r\n" [static]

Debug Text.

Definition at line 93 of file wireless_uart_extended.c.

uint8_t debug_lqi[] = "LQI: " [static]

Debug Text.

Definition at line 89 of file wireless_uart_extended.c.

uint8_t debug_pll_transition[] = "State transition failed\r\n" [static]

Debug Text.

Definition at line 85 of file wireless_uart_extended.c.

uint8_t debug_rx_pool_overflow[] = "RX Buffer Overflow!\r\n" [static]

Debug Text.

Definition at line 90 of file wireless_uart_extended.c.

uint8_t debug_transmission_failed[] = "TX Failed!\r\n" [static]

Debug Text.

Definition at line 91 of file wireless_uart_extended.c.

uint8_t debug_transmission_length[] = "Typed Message too long!!\r\n" [static]

Debug Text.

Definition at line 92 of file wireless_uart_extended.c.

uint8_t debug_type_message[] = "\r<---Type Message:\r\n" [static]

Debug Text.

Definition at line 86 of file wireless_uart_extended.c.

bool rx_flag [static]

Flag used to mask between the two possible TRX_END events.

Definition at line 83 of file wireless_uart_extended.c.

hal_rx_frame_t rx_pool[RX_POOL_SIZE] [static]

Pool of hal_rx_frame_t's.

Definition at line 74 of file wireless_uart_extended.c.

hal_rx_frame_t* rx_pool_end [static]

Pointer to end of pool.

Definition at line 76 of file wireless_uart_extended.c.

hal_rx_frame_t* rx_pool_head [static]

Pointer to next hal_rx_frame_t it is possible to write.

Definition at line 77 of file wireless_uart_extended.c.

uint8_t rx_pool_items_free [static]

Number of free items (hal_rx_frame_t) in the pool.

Definition at line 79 of file wireless_uart_extended.c.

uint8_t rx_pool_items_used [static]

Definition at line 80 of file wireless_uart_extended.c.

bool rx_pool_overflow_flag [static]

Flag that is used to signal a pool overflow.

Definition at line 81 of file wireless_uart_extended.c.

hal_rx_frame_t* rx_pool_start [static]

Pointer to start of pool.

Definition at line 75 of file wireless_uart_extended.c.

hal_rx_frame_t* rx_pool_tail [static]

Pointer to next hal_rx_frame_t that can be read from the pool.

Definition at line 78 of file wireless_uart_extended.c.

uint8_t tx_frame[127] [static]

Buffer used to build TX frames. (Size must be max PSDU length.).

Definition at line 72 of file wireless_uart_extended.c.

Referenced by main().

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