• Main Page
  • Modules
  • Index
  • File List
  • Globals

lib/rf_test/rf_test.c

Go to the documentation of this file.
00001 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is confidential property of Nordic 
00004  * Semiconductor ASA.Terms and conditions of usage are described in detail 
00005  * in NORDIC SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *              
00011  * $LastChangedRevision: 5717 $
00012  */
00013 
00018 #include "hal_nrf.h"
00019 #include "hal_nrf_hw.h"
00020 
00021 
00022 #include "pn9.h"
00023 
00024 #include "rf_test.h"
00025 #include <stdbool.h>
00026 #include <intrins.h>
00027 
00028 #define DEFAULT_SIZE_PACKET 32
00029 #define NB_BITS_FOR_ERROR_RATE_CALC 100000
00030 #define NB_BYTES_FOR_ERROR_RATE_CALC 12500
00031 #define MAX_SIZE_PACKET 32
00032 #define ADDR_WIDTH 5
00033 
00034 
00035 #define RF_ENABLE() (RFCKEN = 1);
00036 #define RF_IRQ_ENABLE() (RF = 1);
00037 #define RF_IRQ_DISABLE() (RF = 0);
00038 
00039 
00040 typedef struct rf_test_ctx_t 
00041 {
00042   uint16_t nb_computed_bytes;
00043   uint32_t bit_errors;
00044   uint8_t actual_packet_size;
00045   uint8_t payload[MAX_SIZE_PACKET];
00046   uint8_t sweep_first_channel;
00047   uint8_t sweep_last_channel;
00048   uint8_t sweep_current_channel;
00049   uint8_t mod_tx_addr[ADDR_WIDTH];
00050   uint16_t bit_error_rate;
00051 } rf_test_ctx_t;
00052 
00053 
00054 static volatile rf_test_ctx_t rf_test_ctx;
00055 
00056 void rf_test_init()
00057 {
00058   uint8_t index;
00059   
00060   rf_test_ctx.nb_computed_bytes = 0;
00061   rf_test_ctx.actual_packet_size = 0xFF;
00062   rf_test_ctx.bit_errors = 0;
00063   rf_test_ctx.bit_error_rate = 0xFFFF;
00064   rf_test_ctx.sweep_first_channel = 0;
00065   rf_test_ctx.sweep_last_channel = 80;
00066   rf_test_ctx.sweep_current_channel = 0;
00067   
00068   for(index = 0; index < ADDR_WIDTH; index ++)
00069   {
00070     rf_test_ctx.mod_tx_addr[index] = 0;
00071   }
00072   for(index = 0; index < MAX_SIZE_PACKET; index ++)
00073   {
00074     rf_test_ctx.payload[index] = 0;
00075   }
00076 
00077 }
00078 
00079 void rf_test_sensitivity_set_expected_data(uint8_t data_size, uint8_t *p_expected_data)
00080 {
00081   uint8_t byte_index;
00082   
00083   CE_LOW();
00084   if (NULL == p_expected_data)
00085   {
00086     hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, rf_test_ctx.actual_packet_size);
00087   }
00088   else
00089   {
00090     rf_test_ctx.actual_packet_size = data_size;
00091     hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, rf_test_ctx.actual_packet_size); // pipe0 expect 32 byte payload
00092     for(byte_index = 0; byte_index < data_size; byte_index++)
00093     {
00094       rf_test_ctx.payload[byte_index] = p_expected_data[byte_index];
00095     }
00096   }
00097   CE_HIGH();
00098 }
00099 
00100 
00101 rf_test_ret_t rf_test_sensitivity_init(uint8_t ch, hal_nrf_output_power_t pwr, hal_nrf_datarate_t datarate, uint8_t *address)
00102 {
00103   uint8_t default_payload[MAX_SIZE_PACKET] = {0xaa, 0x1b, 0x2c, 0x10, 0x81, 0x01, 0x11, 0x1a,
00104                                         0xfe, 0xee, 0xcd, 0x12, 0x13, 0xa9, 0x92, 0x13,
00105                                         0x14, 0x17, 0x7d, 0x12, 0xdf, 0xfd, 0xaa, 0xaf,
00106                                         0x27, 0x65, 0x41, 0x43, 0x3a, 0x2d, 0xc1, 0x55};
00107 
00108   rf_test_ctx.bit_errors = 0;
00109   rf_test_ctx.bit_error_rate = 0xFFFF;
00110 
00111 
00112   CE_LOW();
00113   hal_nrf_set_output_power(pwr);
00114   hal_nrf_set_datarate(datarate);
00115   hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);
00116   hal_nrf_flush_rx();
00117   hal_nrf_set_operation_mode(HAL_NRF_PRX); 
00118   hal_nrf_close_pipe(HAL_NRF_ALL);                // first close all radio pipes...
00119   hal_nrf_open_pipe(HAL_NRF_PIPE0, false); // Turn off all auto acknowledge functionality
00120   hal_nrf_set_auto_retr(HAL_NRF_PIPE0,0);
00121   hal_nrf_set_rf_channel(ch);
00122   hal_nrf_set_pll_mode(false);
00123   hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF);
00124   hal_nrf_set_address(HAL_NRF_PIPE0, address);
00125 
00126   if (0xFF == rf_test_ctx.actual_packet_size)
00127   {
00128     //the size and expected packet has not been set
00129     rf_test_sensitivity_set_expected_data(DEFAULT_SIZE_PACKET, default_payload); //set the default values
00130   }
00131   else
00132   {
00133     rf_test_sensitivity_set_expected_data(0, NULL); //otherwise reset the previous values
00134   }
00135   CE_HIGH();
00136   return(RF_TEST_RC_OK);
00137 }
00138 
00139 
00140 static void rf_test_compute_error_rate_for_one_packet(uint8_t packet_size, uint8_t *in_packet)
00141 {
00142   uint8_t byte_index;
00143   uint8_t xored;
00144   uint8_t nb_err_bits_array[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
00145 
00146   for(byte_index=0; byte_index<packet_size;byte_index++)
00147   {
00148     if (in_packet[byte_index] != rf_test_ctx.payload[byte_index])  //check against expected value stored in payload
00149     {
00150       xored = in_packet[byte_index] ^ rf_test_ctx.payload[byte_index];
00151       rf_test_ctx.bit_errors += nb_err_bits_array[(xored&0x0F)];
00152       rf_test_ctx.bit_errors += nb_err_bits_array[(xored&0xF0)>>4];
00153     }
00154   }
00155   rf_test_ctx.nb_computed_bytes += packet_size;
00156 }
00157 
00158 rf_test_ret_t rf_test_receive_and_compute_packet()
00159 {
00160   volatile uint16_t nb_received_byte;
00161   uint8_t pload[MAX_SIZE_PACKET]; // recived from RX
00162   rf_test_ret_t ret_code = RF_TEST_RC_OK;
00163 
00164   nb_received_byte = hal_nrf_read_rx_payload(pload);  // read received data
00165   nb_received_byte &= 0x00FF;     //the received number of bytes is on the LSB
00166   if(rf_test_ctx.actual_packet_size != nb_received_byte)
00167   {
00168     ret_code = RF_TEST_RC_ERR_NB_RX_BYTES;
00169   }
00170   rf_test_compute_error_rate_for_one_packet(nb_received_byte, pload);
00171   if (rf_test_ctx.nb_computed_bytes >= NB_BYTES_FOR_ERROR_RATE_CALC)
00172   {
00173     ret_code = RF_TEST_RC_FINISHED;
00174   }
00175   return(ret_code);
00176 
00177 }
00178 
00179 
00180 uint16_t rf_test_compute_error_rate()
00181 {
00182   uint32_t local_nb_computed_bits;
00183   uint16_t local_nb_error;
00184   local_nb_error = (uint16_t)rf_test_ctx.bit_errors;
00185   if (0xFFFF == rf_test_ctx.bit_error_rate)
00186   {
00187     local_nb_computed_bits = (uint32_t)rf_test_ctx.nb_computed_bytes;
00188     local_nb_computed_bits = local_nb_computed_bits << 3;             //multiply by 8 to get the number of bits.
00189     if (rf_test_ctx.nb_computed_bytes >= NB_BYTES_FOR_ERROR_RATE_CALC)
00190     {
00191       if (local_nb_computed_bits == rf_test_ctx.bit_errors)
00192       {
00193         rf_test_ctx.bit_error_rate = 9999;
00194       }
00195       else
00196       {
00197         //calculate bit error rate
00198         //multiplied by 10000 to fit in two bytes, this value must be divided by 100 to get BER in percent.
00199         rf_test_ctx.bit_error_rate =(uint16_t)((rf_test_ctx.bit_errors*10000)/local_nb_computed_bits);
00200       }
00201       rf_test_ctx.nb_computed_bytes = 0;
00202       rf_test_ctx.bit_errors  = 0;
00203     }
00204   }
00205   return(rf_test_ctx.bit_error_rate);
00206 }
00207 
00208 
00209 rf_test_ret_t rf_test_init_rx_sweep(uint8_t pwr, uint8_t datarate, uint8_t *rx_mode_address, uint8_t first_channel, uint8_t last_channel)
00210 {
00211   rf_test_ctx.sweep_first_channel = first_channel;
00212   rf_test_ctx.sweep_last_channel = last_channel;
00213   rf_test_ctx.sweep_current_channel = rf_test_ctx.sweep_first_channel;
00214 
00215   CE_LOW();
00216   hal_nrf_set_output_power(pwr);
00217   hal_nrf_set_datarate(datarate);
00218   hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);
00219   hal_nrf_flush_rx();
00220   hal_nrf_set_operation_mode(HAL_NRF_PRX); 
00221   hal_nrf_close_pipe(HAL_NRF_ALL);                // first close all radio pipes...
00222   hal_nrf_open_pipe(HAL_NRF_PIPE0, false);        // Turn off all auto acknowledge functionality  
00223   hal_nrf_set_auto_retr(HAL_NRF_PIPE0,0);
00224   hal_nrf_set_pll_mode(false);
00225   hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF);
00226   hal_nrf_set_address(HAL_NRF_PIPE0, rx_mode_address);
00227   hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, DEFAULT_SIZE_PACKET); // pipe0 expect 32 byte payload
00228 
00229   CE_HIGH();
00230   return(RF_TEST_RC_OK);
00231 }
00232 
00233 rf_test_ret_t rf_test_init_tx_sweep(uint8_t pwr, uint8_t datarate, uint8_t first_channel, uint8_t last_channel)
00234 {
00235   rf_test_ctx.sweep_first_channel = first_channel;
00236   rf_test_ctx.sweep_last_channel = last_channel;
00237   rf_test_ctx.sweep_current_channel = rf_test_ctx.sweep_first_channel;
00238 
00239   CE_LOW();
00240   hal_nrf_set_operation_mode(HAL_NRF_PTX);
00241   hal_nrf_set_pll_mode(true);
00242   hal_nrf_set_output_power(pwr);
00243   hal_nrf_set_datarate(datarate);
00244   hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF);
00245   hal_nrf_enable_continious_wave(true);
00246   CE_HIGH();
00247   return(RF_TEST_RC_OK);
00248 }
00249 
00250 
00251 void rf_test_txrx_sweep()
00252 {
00253   CE_LOW();
00254   hal_nrf_set_rf_channel(rf_test_ctx.sweep_current_channel);
00255   CE_HIGH();
00256 
00257   rf_test_ctx.sweep_current_channel++; 
00258   if (rf_test_ctx.sweep_current_channel > rf_test_ctx.sweep_last_channel)
00259   {
00260     rf_test_ctx.sweep_current_channel = rf_test_ctx.sweep_first_channel;
00261   }
00262 }
00263 
00264 
00265 void rf_test_set_modulation_payload()
00266 {
00267   uint8_t i;
00268 
00269   pn9_init();
00270   
00271   for (i = 0; i < ADDR_WIDTH; i++)
00272   {
00273     rf_test_ctx.mod_tx_addr[i] = pn9_get_byte();
00274   }
00275   
00276   for (i = 0; i < DEFAULT_SIZE_PACKET; i++)
00277   {
00278     rf_test_ctx.payload[i] = pn9_get_byte();
00279   }
00280 }
00281 
00282 rf_test_ret_t rf_test_start_mod_carrier(uint8_t channel, uint8_t pwr, uint8_t datarate)
00283 {
00284   if (channel > RF_TEST_MAX_CHANNEL)
00285   {
00286     return(RF_TEST_RC_ERR_PARAM);
00287   }
00288   if (pwr > HAL_NRF_0DBM){
00289     return(RF_TEST_RC_ERR_PARAM);
00290   }
00291   if (datarate  > HAL_NRF_250KBPS)
00292   {
00293     return(RF_TEST_RC_ERR_PARAM);
00294   }
00295 
00296   CE_LOW();
00297 
00298   hal_nrf_set_output_power(pwr);
00299   hal_nrf_set_datarate(datarate);
00300   hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);
00301   hal_nrf_set_operation_mode(HAL_NRF_PTX); 
00302   hal_nrf_open_pipe(HAL_NRF_PIPE0, false); // Turn off all auto acknowledge functionality  
00303   hal_nrf_set_auto_retr(0,0);
00304   hal_nrf_set_rf_channel(channel);
00305   hal_nrf_set_pll_mode(true);
00306 
00307   hal_nrf_set_address(HAL_NRF_TX, rf_test_ctx.mod_tx_addr);
00308   hal_nrf_write_tx_payload(rf_test_ctx.payload, DEFAULT_SIZE_PACKET);
00309   hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF);
00310 
00311   CE_PULSE();
00312   return(RF_TEST_RC_OK);
00313 }
00314 
00315 
00316 void rf_test_const_carrier_isr(void)
00317 {
00318   static uint8_t pk_cnt_ = 0;
00319   
00320   CE_HIGH();
00321   hal_nrf_reuse_tx();
00322   
00323   hal_nrf_get_clear_irq_flags();
00324   
00325   if(pk_cnt_ > 19)
00326   {
00327     CE_LOW();
00328     pk_cnt_ = 0;
00329     CE_PULSE();  // Refresh
00330   }
00331   else
00332   {
00333     pk_cnt_++;
00334   }
00335 }
00336 
00337 rf_test_ret_t rf_test_start_unmod_carrier(uint8_t channel, uint8_t pwr, uint8_t datarate)
00338 {
00339   CE_LOW();
00340   
00341   hal_nrf_set_operation_mode(HAL_NRF_PTX);
00342   hal_nrf_set_rf_channel(channel);
00343   hal_nrf_set_pll_mode(true);
00344   hal_nrf_set_output_power(pwr);
00345   hal_nrf_set_datarate(datarate);
00346   hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF);
00347   hal_nrf_enable_continious_wave(true);
00348   CE_HIGH();
00349   return(RF_TEST_RC_OK);
00350 }
00351 
00352 rf_test_ret_t rf_test_start_rx_carrier(uint8_t channel, uint8_t pwr, uint8_t datarate, uint8_t *rx_carrier_address)
00353 {
00354   if (channel > RF_TEST_MAX_CHANNEL)
00355   {
00356     return(RF_TEST_RC_ERR_PARAM);
00357   }
00358   if (pwr > HAL_NRF_0DBM){
00359     return(RF_TEST_RC_ERR_PARAM);
00360   }
00361   if (datarate  > HAL_NRF_250KBPS)
00362   {
00363     return(RF_TEST_RC_ERR_PARAM);
00364   }
00365   if (NULL == rx_carrier_address)
00366   {
00367     return(RF_TEST_RC_ERR_PARAM);
00368   }
00369   CE_LOW();
00370   hal_nrf_set_output_power(pwr);
00371   hal_nrf_set_datarate(datarate);
00372   hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);
00373   
00374   hal_nrf_flush_rx();
00375   hal_nrf_set_operation_mode(HAL_NRF_PRX); 
00376   hal_nrf_close_pipe(HAL_NRF_ALL);                // first close all radio pipes...
00377   hal_nrf_open_pipe(HAL_NRF_PIPE0, false);        // Turn off all auto acknowledge functionality  
00378   hal_nrf_set_auto_retr(0,0);
00379   hal_nrf_set_rf_channel(channel);
00380   hal_nrf_set_pll_mode(false);
00381   
00382   hal_nrf_set_crc_mode(HAL_NRF_CRC_OFF);
00383   
00384   hal_nrf_set_address(HAL_NRF_PIPE0, rx_carrier_address);
00385   hal_nrf_set_rx_payload_width(HAL_NRF_PIPE0, DEFAULT_SIZE_PACKET); // pipe0 expect 32 byte payload
00386   
00387   CE_HIGH();
00388   return(RF_TEST_RC_OK);
00389 }
00390 

Generated on Fri Apr 20 2012 14:11:45 for nRFGo SDK by  doxygen 1.7.2