00001
00058
00059 #include <stdint.h>
00060 #include <stdbool.h>
00061
00062 #include "config_uart.h"
00063
00064 #include "compiler.h"
00065
00066 #include "at86rf230_registermap.h"
00067 #include "hal.h"
00068 #include "tat.h"
00069 #include "com.h"
00070
00071
00072
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;
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
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 );
00117 hal_set_trx_end_event_handler( trx_end_handler );
00118
00119 status = true;
00120 }
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
00157 if (rx_pool_items_free == 0) {
00158 rx_pool_overflow_flag = true;
00159 } else {
00160
00161
00162 hal_frame_read( rx_pool_head );
00163
00164
00165 if (rx_pool_head->crc == true) {
00166
00167
00168 if (rx_pool_head == rx_pool_end) {
00169 rx_pool_head = rx_pool_start;
00170 } else {
00171 ++rx_pool_head;
00172 }
00173
00174 --rx_pool_items_free;
00175 ++rx_pool_items_used;
00176 }
00177 }
00178 }
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
00191 if (tat_set_trx_state( RX_ON ) != TAT_SUCCESS) {
00192 com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00193 }
00194
00195 sei( );
00196
00197
00198 com_send_string( debug_type_message, sizeof( debug_type_message ) );
00199
00200
00201
00202
00203
00204
00205
00206 while (true) {
00207
00208
00209 if (rx_pool_items_used != 0) {
00210
00211
00212 if (rx_pool_tail == rx_pool_end) {
00213 rx_pool_tail = rx_pool_start;
00214 } else {
00215 ++rx_pool_tail;
00216 }
00217
00218
00219
00220 cli( );
00221
00222 ++rx_pool_items_free;
00223 --rx_pool_items_used;
00224
00225 sei( );
00226
00227
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 }
00234
00235
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 }
00243
00244
00245
00246 length_of_received_data = com_get_number_of_received_bytes( );
00247
00248 if (length_of_received_data == 1) {
00249
00250
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
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;
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 }
00269
00270
00271 while (tat_get_trx_state( ) != PLL_ON) {;}
00272 } else {
00273 com_send_string( debug_pll_transition, sizeof( debug_pll_transition ) );
00274 }
00275
00276 if (tat_set_trx_state( RX_ON ) != TAT_SUCCESS) {
00277 com_send_string( debug_fatal_error, sizeof( debug_fatal_error ) );
00278 }
00279
00280 rx_flag = true;
00281 com_reset_receiver( );
00282 com_send_string( debug_type_message, sizeof( debug_type_message ) );
00283 }
00284 }
00285 }
00286 }
00287