00001
00057
00058 #include <stdint.h>
00059 #include <stdbool.h>
00060
00061 #include "config_uart_extended.h"
00062
00063 #include "compiler.h"
00064
00065 #include "at86rf230_registermap.h"
00066 #include "hal.h"
00067 #include "tat.h"
00068 #include "com.h"
00069
00070
00071
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;
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
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
00121
00122 tat_set_short_address( SHORT_ADDRESS );
00123 tat_set_pan_id( PAN_ID );
00124 tat_set_device_role( false );
00125
00126
00127 tat_configure_csma( 234, 0xE2 );
00128
00129
00130 tat_use_auto_tx_crc( true );
00131 hal_set_trx_end_event_handler( trx_end_handler );
00132
00133 status = true;
00134 }
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
00171 if (rx_pool_items_free == 0) {
00172 rx_pool_overflow_flag = true;
00173 } else {
00174
00175
00176 hal_frame_read( rx_pool_head );
00177
00178
00179 if (rx_pool_head->crc == true) {
00180
00181
00182 if (rx_pool_head == rx_pool_end) {
00183 rx_pool_head = rx_pool_start;
00184 } else {
00185 ++rx_pool_head;
00186 }
00187
00188 --rx_pool_items_free;
00189 ++rx_pool_items_used;
00190 }
00191 }
00192 }
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
00202 tx_frame[ 0 ] = 0x61;
00203 tx_frame[ 1 ] = 0x88;
00204
00205 tx_frame[ 3 ] = PAN_ID & 0xFF;
00206 tx_frame[ 4 ] = (PAN_ID >> 8 ) & 0xFF;
00207 tx_frame[ 5 ] = DEST_ADDRESS & 0xFF;
00208 tx_frame[ 6 ] = (DEST_ADDRESS >> 8 ) & 0xFF;
00209 tx_frame[ 7 ] = SHORT_ADDRESS & 0xFF;
00210 tx_frame[ 8 ] = (SHORT_ADDRESS >> 8 ) & 0xFF;
00211
00212 rx_pool_init( );
00213 avr_init( );
00214 trx_init( );
00215
00216
00217 if (tat_set_trx_state( RX_AACK_ON ) != TAT_SUCCESS) {
00218 com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00219 }
00220
00221 sei( );
00222
00223
00224 com_send_string( debug_type_message, sizeof( debug_type_message ) );
00225
00226
00227
00228
00229
00230
00231
00232 while (true) {
00233
00234
00235 if (rx_pool_items_used != 0) {
00236
00237
00238 if (rx_pool_tail == rx_pool_end) {
00239 rx_pool_tail = rx_pool_start;
00240 } else {
00241 ++rx_pool_tail;
00242 }
00243
00244
00245
00246 cli( );
00247
00248 ++rx_pool_items_free;
00249 --rx_pool_items_used;
00250
00251 sei( );
00252
00253
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 }
00260
00261
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 }
00269
00270
00271
00272 length_of_received_data = com_get_number_of_received_bytes( );
00273
00274 if (length_of_received_data == 1) {
00275
00276
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
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;
00289 tx_frame[ 2 ] = frame_sequence_number++;
00290
00291
00292 do {
00293 tx_frame[ tx_frame_length++ ] = *rx_frame++;
00294 } while (--length_of_received_data > 0);
00295
00296 rx_flag = false;
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 }
00303 } else {
00304 com_send_string( debug_pll_transition, sizeof( debug_pll_transition ) );
00305 }
00306
00307 if (tat_set_trx_state( RX_AACK_ON ) != TAT_SUCCESS) {
00308 com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00309 }
00310
00311 rx_flag = true;
00312 com_reset_receiver( );
00313 com_send_string( debug_type_message, sizeof( debug_type_message ) );
00314 }
00315 }
00316 }
00317 }
00318