AVR Z-LINKŪ


wireless_uart.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00058 /*============================ INCLDUE =======================================*/
00059 #include <stdint.h>
00060 #include <stdbool.h>
00061 
00062 #include "config_uart.h" // See this file for all project options.
00063 
00064 #include "compiler.h"
00065 
00066 #include "at86rf230_registermap.h"
00067 #include "hal.h"
00068 #include "tat.h"
00069 #include "com.h"
00070 /*============================ MACROS ========================================*/
00071 /*============================ TYPEDEFS ======================================*/
00072 /*============================ VARIABLES =====================================*/
00073 static hal_rx_frame_t rx_pool[ RX_POOL_SIZE ]; 
00074 static hal_rx_frame_t *rx_pool_start; 
00075 static hal_rx_frame_t *rx_pool_end; 
00076 static hal_rx_frame_t *rx_pool_head; 
00077 static hal_rx_frame_t *rx_pool_tail; 
00078 static uint8_t rx_pool_items_free; 
00079 static uint8_t rx_pool_items_used; // !< Number of used items.
00080 static bool rx_pool_overflow_flag; 
00081 
00082 static bool rx_flag; 
00083 
00084 static uint8_t debug_pll_transition[] = "State transition failed\r\n"; 
00085 static uint8_t debug_type_message[] = "\r<---Type Message:\r\n"; 
00086 static uint8_t debug_data_sent[] = "<---TX OK.\r\n"; 
00087 static uint8_t debug_data_received[] = "\r--->Rx:\r"; 
00088 static uint8_t debug_lqi[] = "LQI: "; 
00089 static uint8_t debug_rx_pool_overflow[] = "RX Buffer Overflow!\r\n"; 
00090 static uint8_t debug_transmission_failed[] = "TX Failed!\r\n"; 
00091 static uint8_t debug_transmission_length[] = "Typed Message too long!!\r\n"; 
00092 static uint8_t debug_fatal_error[] = "A fatal error. System must be reset.\r\n"; 
00093 /*============================ PROTOTYPES ====================================*/
00094 static bool trx_init( void );
00095 static void avr_init( void );
00096 static void trx_end_handler( uint32_t time_stamp );
00097 static void rx_pool_init( void );
00098 
00104 static bool trx_init( void ){
00105     
00106     static bool status;
00107     
00108     if (tat_init( ) != TAT_SUCCESS) {
00109         status = false;
00110     } else if (tat_set_operating_channel( OPERATING_CHANNEL ) != TAT_SUCCESS) {
00111         status = false;
00112     } else if (tat_set_clock_speed( true, CLKM_DISABLED ) != TAT_SUCCESS) {
00113         status = false; 
00114     } else{
00115         
00116         tat_use_auto_tx_crc( true ); //Automatic CRC must be enabled.
00117         hal_set_trx_end_event_handler( trx_end_handler ); // Event handler for TRX_END events.
00118         
00119         status = true;
00120     } // end: if (tat_init( ) != TAT_SUCCESS) ...
00121 
00122     return status;
00123 }    
00124 
00127 static void avr_init( void ){
00128     com_init( BR_38400 );
00129 }
00130 
00133 static void rx_pool_init( void ){
00134 
00135     rx_pool_start = rx_pool;
00136     rx_pool_end = &rx_pool[ RX_POOL_SIZE - 1 ];
00137     
00138     rx_pool_head = rx_pool_start;
00139     rx_pool_tail = rx_pool_end;
00140     
00141     rx_pool_items_free = RX_POOL_SIZE;
00142     rx_pool_items_used = 0;
00143     
00144     rx_pool_overflow_flag = false;
00145 }
00146 
00152 static void trx_end_handler( uint32_t time_stamp ){
00153     
00154     if (rx_flag == true) {
00155         
00156         //Check if these is space left in the rx_pool.
00157         if (rx_pool_items_free == 0) {
00158             rx_pool_overflow_flag = true;
00159         } else {
00160         
00161             //Space left, so upload the received frame.
00162             hal_frame_read( rx_pool_head );
00163             
00164             //Then check the CRC. Will not store frames with invalid CRC.
00165             if (rx_pool_head->crc == true) {
00166                 
00167                 //Handle wrapping of rx_pool.
00168                 if (rx_pool_head == rx_pool_end) {
00169                     rx_pool_head = rx_pool_start;
00170                 } else {
00171                     ++rx_pool_head;
00172                 } // end: if (rx_pool_head == rx_pool_end) ...
00173                 
00174                 --rx_pool_items_free;
00175                 ++rx_pool_items_used;
00176             } // end: if (rx_pool_head->crc == true) ...
00177         } // end: if (rx_pool_items_free == 0) ...
00178     } // end:  if (rx_flag == true) ...
00179 }
00180 
00181 void main( void ){
00182     
00183     static uint8_t length_of_received_data = 0;
00184     rx_flag = true;
00185     
00186     rx_pool_init( );
00187     avr_init( );
00188     trx_init( );
00189     
00190     //Set system state to RX_ON
00191     if (tat_set_trx_state( RX_ON ) != TAT_SUCCESS) {
00192         com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00193     } // end: if (tat_set_trx_state( RX_ON ) != TAT_SUCCESS) ...
00194     
00195     sei( );
00196     
00197     //Give the user an indication that the system is ready.
00198     com_send_string( debug_type_message, sizeof( debug_type_message ) );
00199     
00200     /*Enter Normal Program Flow:
00201         - Check for newly received frames. Print them if something is received.
00202         - Notify on rx_pool overflow.
00203         - Try to send data on air interface, if something is received on UART/USB.
00204         - Notify if the typed message was too long.
00205      */
00206     while (true) {
00207         
00208         //Check if we have received something on the air interface.
00209         if (rx_pool_items_used != 0) {
00210         
00211             //Handle wrapping of rx_pool.
00212             if (rx_pool_tail == rx_pool_end) {
00213                 rx_pool_tail = rx_pool_start;
00214             } else {
00215                 ++rx_pool_tail;
00216             } // end: if (rx_pool_tail == rx_pool_end) ...
00217             
00218             //Turn interrupts off for a short while to protect when status
00219             //information about the rx_pool is updated.
00220             cli( );
00221             
00222             ++rx_pool_items_free;
00223             --rx_pool_items_used;
00224             
00225             sei( );
00226             
00227             //Send the frame to the user:
00228             com_send_string( debug_data_received, sizeof( debug_data_received ) );
00229             com_send_string( rx_pool_tail->data, ((rx_pool_tail->length) - 2 ) );
00230             com_send_string( debug_lqi, sizeof( debug_lqi ) );
00231             com_send_hex( rx_pool_tail->lqi );
00232             com_send_string( debug_type_message, sizeof( debug_type_message ) );
00233         } // end: if (rx_pool_items_used != 0) ...
00234         
00235         //Check for rx_pool overflow.
00236         if (rx_pool_overflow_flag == true) {
00237             
00238             cli();
00239             rx_pool_init( );
00240             com_send_string( debug_rx_pool_overflow, sizeof( debug_rx_pool_overflow ) );
00241             sei();
00242         } // end: if (rx_pool_overflow_flag == true) ...
00243         
00244         //Check for new data on the serial interface.
00245         //Check if data is ready to be sent.
00246         length_of_received_data = com_get_number_of_received_bytes( );
00247         
00248         if (length_of_received_data == 1) {
00249             
00250             //length_of_received_data == 1 indicates a buffer overflow. Received data too long.
00251             com_send_string( debug_transmission_length, sizeof( debug_transmission_length ) );
00252             com_reset_receiver( );
00253         } else {
00254             
00255             if ((length_of_received_data >= 3) && (length_of_received_data <= COM_RX_BUFFER_SIZE)) {
00256             
00257                 //Change state to PLL_ON and send data if the state transition was successful.
00258                 if (tat_set_trx_state( PLL_ON ) == TAT_SUCCESS) {
00259                 
00260                     uint8_t *rx_frame = com_get_received_data( );
00261                 
00262                     rx_flag = false; // Set the flag false, so that the TRX_END event is not misinterpreted.
00263                 
00264                     if (tat_send_data( length_of_received_data, rx_frame ) == TAT_SUCCESS) {   
00265                         com_send_string( debug_data_sent, sizeof( debug_data_sent ) );
00266                     } else {
00267                         com_send_string( debug_transmission_failed, sizeof( debug_transmission_failed ) );
00268                     } // end:  if (tat_send_data_with_retry( tx_frame_length, tx_frame, 1 ) ...
00269                     
00270                     //Wait for the TRX FSM to go back to PLL_ON.
00271                     while (tat_get_trx_state( ) != PLL_ON) {;}
00272                 } else {
00273                     com_send_string( debug_pll_transition, sizeof( debug_pll_transition ) );
00274                 } // end: if (tat_set_trx_state( PLL_ON ) == TAT_SUCCESS) ...
00275                 
00276                 if (tat_set_trx_state( RX_ON ) != TAT_SUCCESS) {
00277                     com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00278                 } // end: if (tat_set_trx_state( RX_ON ) != TAT_SUCCESS) ...
00279             
00280                 rx_flag = true; // Set the flag back again. Only used to protec the frame transmission.
00281                 com_reset_receiver( );
00282                 com_send_string( debug_type_message, sizeof( debug_type_message ) );
00283             } // end: 
00284         } // end: if (length_of_received_data == 1) ...
00285     } // emd: while (true) ...
00286 }
00287 /*EOF*/
@DOC_TITLE@
Generated on Wed Jul 25 21:15:49 2007 for AVR2001 Software Programmer's Manual by doxygen 1.4.7