AVR Z-LINKŪ


wireless_uart_extended.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00057 /*============================ INCLDUE =======================================*/
00058 #include <stdint.h>
00059 #include <stdbool.h>
00060 
00061 #include "config_uart_extended.h" // See this file for all project options.
00062 
00063 #include "compiler.h"
00064 
00065 #include "at86rf230_registermap.h"
00066 #include "hal.h"
00067 #include "tat.h"
00068 #include "com.h"
00069 /*============================ MACROS ========================================*/
00070 /*============================ TYPEDEFS ======================================*/
00071 /*============================ VARIABLES =====================================*/
00072 static uint8_t tx_frame[ 127 ]; 
00073 
00074 static hal_rx_frame_t rx_pool[ RX_POOL_SIZE ]; 
00075 static hal_rx_frame_t *rx_pool_start; 
00076 static hal_rx_frame_t *rx_pool_end; 
00077 static hal_rx_frame_t *rx_pool_head; 
00078 static hal_rx_frame_t *rx_pool_tail; 
00079 static uint8_t rx_pool_items_free; 
00080 static uint8_t rx_pool_items_used; // !< Number of used items.
00081 static bool rx_pool_overflow_flag; 
00082 
00083 static bool rx_flag; 
00084 
00085 static uint8_t debug_pll_transition[] = "State transition failed\r\n"; 
00086 static uint8_t debug_type_message[] = "\r<---Type Message:\r\n"; 
00087 static uint8_t debug_data_sent[] = "<---TX OK.\r\n"; 
00088 static uint8_t debug_data_received[] = "\r--->Rx:\r"; 
00089 static uint8_t debug_lqi[] = "LQI: "; 
00090 static uint8_t debug_rx_pool_overflow[] = "RX Buffer Overflow!\r\n"; 
00091 static uint8_t debug_transmission_failed[] = "TX Failed!\r\n"; 
00092 static uint8_t debug_transmission_length[] = "Typed Message too long!!\r\n"; 
00093 static uint8_t debug_fatal_error[] = "A fatal error. System must be reset.\r\n"; 
00094 /*============================ PROTOTYPES ====================================*/
00095 static bool trx_init( void );
00096 static void avr_init( void );
00097 static void trx_end_handler( uint32_t time_stamp );
00098 static void rx_pool_init( void );
00099 
00108 static bool trx_init( void ){
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 }    
00138 
00141 static void avr_init( void ){
00142     com_init( BR_38400 );
00143 }
00144 
00147 static void rx_pool_init( void ){
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 }
00160 
00166 static void trx_end_handler( uint32_t time_stamp ){
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 }
00194 
00195 void main( void ){
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 }
00318 /*EOF*/
@DOC_TITLE@
Generated on Wed Jul 25 21:15:49 2007 for AVR2001 Software Programmer's Manual by doxygen 1.4.7