latest BAE code 10 dec

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of RAJANGAM_REVIEW_BAE_CODE by Team Fox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BCN.cpp Source File

BCN.cpp

00001 #include "BCN.h"
00002 #include "EPS.h"
00003 #include <stdio.h>
00004 #include "pin_config.h"
00005 //Check the pin names
00006 //Takes max 4 sec in void FCTN_BCN_TX_MAIN() (temp.calc. + long_beacon + short_beacon) 
00007 
00008 Timer timer_Init_BEACON_HW;
00009 Timer timer_Set_BCN_TX_STATUS_DISABLED;
00010 Timer timer_Set_BCN_TX_STATUS_SUSPENDED;
00011 Timer timer_Set_BCN_TX_STATUS_RF_SILENCE;
00012 Timer timer_Set_BCN_TX_STATUS_SUCCESS;
00013 Timer timer_Set_BCN_TX_STATUS_FAILURE;
00014 
00015 extern BAE_HK_actual actual_data;
00016 
00017 Serial pc_bcn(USBTX, USBRX);        //tx,rx //see if it creates a problem
00018 //SPI spi(D11, D12, D13);              // mosi, miso, sclk 
00019 //DigitalOut cs(D10);                //slave select or chip select
00020 SPI spi(PIN16, PIN17, PIN15);              // mosi, miso, sclk 
00021 DigitalOut cs(PIN6);                //slave select or chip select
00022 Timer t_i;//timer for checking the time taken by (temp.calc. + long_beacon + short_beacon)
00023 //Timer t_is;//timer for short_beacon
00024 //Timer t_il;//timer for long_beacon
00025 Timeout rf_sl_timeout;//RF_silence timer
00026 Ticker loop;//for transmitting every 10 secs
00027 
00028 //GLOBAL VARIABLES
00029 uint8_t BCN_INIT_STATUS = 0;
00030 uint8_t BCN_TX_MAIN_STATUS = 0;
00031 uint8_t BCN_TX_STATUS = BCN_RF_SILENCE;
00032 //uint8_t BCN_TX_ENABLE = 1;              //hardcoding for now    //check where is this variable toggled??
00033 uint8_t BCN_TX_SW_STATUS = 1;
00034 uint8_t BCN_FEN = 0;                //hardcoding for now    //write this value to flash
00035 uint8_t BCN_SPND_TX = 0;            //hardcoding for now    //check where is this variable toggled??
00036 uint8_t BCN_TMP = 5;          // For Temperature
00037 uint8_t ERROR_CHECK = 0;
00038 uint8_t BCN_FAIL_COUNT = 0;         //Flag for keeping count of no. of times of BCN failure in init or one transmission in 30 secs (failure in spi communication)
00039                                     //This Flag when exceeds a threshold, uC should reset.
00040 uint16_t BCN_TX_MAIN_COUNTER = 0;
00041 uint8_t SHORT_HK_data[15];
00042 extern uint8_t BCN_LONG_MSG_TYPE;
00043 extern uint8_t LONG_HK_data[2][134];
00044 extern DigitalOut BCN_SW;
00045 extern float EPS_BTRY_TMP_AVG;
00046 
00047 extern BAE_HK_quant quant_data;
00048 extern uint8_t BAE_RESET_COUNTER;
00049 extern Timer BAE_uptime;
00050 extern Timer I2C_last;
00051 extern void RETURN_UPTIME(float,uint8_t*,uint8_t*,uint8_t*);
00052 extern DigitalIn CDMS_OC_FAULT;
00053 extern DigitalIn EPS_CHARGER_STATUS;
00054 extern uint8_t crc8_short();
00055 
00056 extern uint8_t float_to_uint8(float min,float max,float val);
00057 
00058 void SHORT_HK_data_AQ()
00059 {   
00060     uint8_t days,hours,mins;//for calculating the timing data in bae i2c uptime
00061     
00062     //1st 0-7 call sign
00063     //SHORT_HK_data[0] _ SHORT_HK_data[6]
00064     SHORT_HK_data[0] = 0xF3;
00065     SHORT_HK_data[1] = 0x02;
00066     SHORT_HK_data[2] = 0xFA;
00067     SHORT_HK_data[3] = 0xC6;
00068     SHORT_HK_data[4] = 0xD4;
00069     SHORT_HK_data[5] = 0x28;
00070     SHORT_HK_data[6] = 0x8A;
00071     
00072     SHORT_HK_data[7] = quant_data.voltage_quant[15];
00073     SHORT_HK_data[7] = (SHORT_HK_data[7] & 0xF0) | (quant_data.current_quant[1]>>4);
00074     
00075     //taking only the most significant bits
00076     SHORT_HK_data[8] = actual_data.bit_data_acs_mg[0]>>8;
00077     SHORT_HK_data[8] = (SHORT_HK_data[8] & 0xF0) | BCN_TMP>>4;
00078     //SHORT_HK_data[9] = 0xFF;
00079     SHORT_HK_data[9] = float_to_uint8(-50,100,EPS_BTRY_TMP_AVG);
00080     SHORT_HK_data[9] = (SHORT_HK_data[9]&0xF0) | BAE_RESET_COUNTER>>4;
00081     
00082     
00083     RETURN_UPTIME(BAE_uptime.read(),&days,&hours,&mins);
00084     SHORT_HK_data[10] = CDMS_OC_FAULT;
00085     SHORT_HK_data[10] = (SHORT_HK_data[10]<<1) | 0;//receiver_oc_fault//to be diss;
00086     SHORT_HK_data[10] = (SHORT_HK_data[10]<<1) | ((quant_data.voltage_quant[5]>>2)&0x3C);
00087     SHORT_HK_data[10] = (SHORT_HK_data[10]<<2) | ((days>>3)&0x03);
00088     SHORT_HK_data[11] = (days<<5) | (hours&0x1F);
00089     
00090     RETURN_UPTIME(I2C_last.read(),&days,&hours,&mins);
00091     SHORT_HK_data[12] = EPS_CHARGER_STATUS;
00092     SHORT_HK_data[12] = (SHORT_HK_data[12]<<4) | (quant_data.voltage_quant[6]>>4);
00093     SHORT_HK_data[12] = (SHORT_HK_data[12]<<3) | (days>>2);    
00094     SHORT_HK_data[13] = (days<<6) | (mins&0x3F);
00095 
00096     SHORT_HK_data[14] = crc8_short();    
00097 }
00098 
00099 void FCTN_BCN_INIT()
00100 {
00101     pc_bcn.printf("FCTN_BCN_INIT\n");
00102     BCN_INIT_STATUS = 1;
00103     if( BCN_TX_SW_STATUS == 0b00000001)
00104         {   
00105             timer_Init_BEACON_HW.reset();
00106             timer_Init_BEACON_HW.start();
00107             Init_BEACON_HW();
00108             timer_Init_BEACON_HW.stop();
00109         }
00110     else 
00111         {
00112             timer_Set_BCN_TX_STATUS_DISABLED.reset();
00113             timer_Set_BCN_TX_STATUS_DISABLED.start();
00114             BCN_TX_STATUS = BCN_TX_DISABLED;
00115             timer_Set_BCN_TX_STATUS_DISABLED.stop();
00116         }
00117  // if(BCN_FEN == 0)//BCN_FEN is in flash
00118  //     rf_sl_timeout.attach(&FCTN_BCN_FEN, RF_SILENCE_TIME);
00119     BCN_INIT_STATUS = 0;
00120 }
00121 
00122 extern uint32_t FCTN_BAE_RD_FLASH_ENTITY(uint16_t);
00123 extern void FCTN_BAE_WR_FLASH(uint16_t ,uint32_t );
00124 
00125 void FCTN_BCN_FEN(const void*)
00126 {
00127     pc_bcn.printf("FCTN_FEN\n\r");
00128     BCN_FEN = 1;//write this value to flash
00129     uint32_t FLASH_DATA;
00130     FLASH_DATA = FCTN_BAE_RD_FLASH_ENTITY(0);
00131     FLASH_DATA = (FLASH_DATA | 0x00000800);//see if uint8 to uint32 conversion works
00132     FCTN_BAE_WR_FLASH(0,FLASH_DATA);
00133 }
00134 void FCTN_BCN_TX_MAIN()
00135 {
00136     ERROR_CHECK=0;
00137     pc_bcn.printf("\n\nFCTN_BCN_TX_MAIN\n\r");
00138     t_i.start();
00139     int begin = t_i.read_us();
00140     //int begins,beginl,endl;
00141     int begintx,endtx;
00142     BCN_TX_MAIN_STATUS = 1; // tranmission started
00143     BCN_TX_MAIN_COUNTER++;
00144     pc_bcn.printf("BCN_FEN = %d\n\r",BCN_FEN);  //to be checked next time
00145     if(BCN_FEN == 1)
00146     {
00147         pc_bcn.printf("BCN_TX_SW_STATUS = %d\n\r",BCN_TX_SW_STATUS); // to be checked nxt time
00148         //if(BCN_TX_SW_STATUS == 0b00000001)
00149         if(BCN_TX_SW_STATUS != 0b00000000) // why have we put it as !0== why not just ==01??
00150         {
00151             timer_Init_BEACON_HW.reset();
00152             timer_Init_BEACON_HW.start();
00153             Init_BEACON_HW();
00154             timer_Init_BEACON_HW.stop();
00155             
00156             if( BCN_TX_STATUS == BCN_INIT_SUCCESS )
00157             {
00158                 pc_bcn.printf("\n\r trying to trmntng");
00159                 
00160                 //Measure and store BCN temperature in BCN_TS_BUFFER
00161                 uint8_t temp_temp = check_Temperature();
00162                 if( temp_temp != 0xFF )
00163                     BCN_TMP = temp_temp;
00164                     
00165                     pc_bcn.printf("\n\rthe BCN_TMP is %d\n",temp_temp);
00166                 pc_bcn.printf("temperature = %d\n\r",BCN_TMP);
00167                 
00168                 //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER
00169                 pc_bcn.printf("BCN_SPND_TX = %d\n\r",BCN_SPND_TX);
00170                 if(BCN_SPND_TX == 1)
00171                 {
00172                     timer_Set_BCN_TX_STATUS_SUSPENDED.reset();
00173                     timer_Set_BCN_TX_STATUS_SUSPENDED.start();
00174                     BCN_TX_STATUS = BCN_TX_SUSPENDED;
00175                     BCN_TX_MAIN_STATUS = 0;
00176                     timer_Set_BCN_TX_STATUS_SUSPENDED.stop();
00177                 }
00178                 else if(BCN_SW == 0)
00179                 {
00180                     BCN_TX_STATUS = BCN_TX_DISABLED;
00181                     BCN_TX_MAIN_STATUS = 0;
00182                 }
00183                 else
00184                 {       
00185                     //transmit short beacon and long beacon
00186                     begintx = t_i.read_us();
00187                     BCN_TX();
00188                     endtx = t_i.read_us();
00189                         
00190                     if(Check_ACK_RECEIVED() == 1)
00191                     {
00192                         BCN_TX_STATUS = BCN_TX_SUCCESS;
00193                         BCN_TX_MAIN_STATUS = 0;   
00194                     }
00195                     else if(BCN_SPND_TX == 1 )
00196                     {
00197                         timer_Set_BCN_TX_STATUS_SUSPENDED.reset();
00198                         timer_Set_BCN_TX_STATUS_SUSPENDED.start();
00199                         BCN_TX_STATUS = BCN_TX_SUSPENDED;
00200                         BCN_TX_MAIN_STATUS = 0;
00201                         timer_Set_BCN_TX_STATUS_SUSPENDED.stop();
00202                     }
00203                     else if(BCN_SW == 0)
00204                     {
00205                         BCN_TX_STATUS = BCN_TX_DISABLED;
00206                         BCN_TX_MAIN_STATUS = 0;
00207                     }
00208                     else
00209                     {
00210                         BCN_TX_STATUS = BCN_TX_FAILURE;
00211                         //BCN_FAIL_COUNT++;
00212                         //Init_BEACON_HW();
00213                         BCN_TX_MAIN_STATUS = 0;
00214                             
00215                     }
00216                 }
00217             }
00218         }
00219         else
00220         {
00221             BCN_TX_STATUS = BCN_TX_DISABLED;
00222             BCN_TX_MAIN_STATUS = 0;
00223         }
00224     }
00225     else
00226     {
00227         timer_Set_BCN_TX_STATUS_RF_SILENCE.reset();
00228         timer_Set_BCN_TX_STATUS_RF_SILENCE.start();
00229         BCN_TX_STATUS = BCN_RF_SILENCE;  //Window of RF Silence: None of the Txs should be on.
00230         BCN_TX_MAIN_STATUS = 0;
00231         timer_Set_BCN_TX_STATUS_RF_SILENCE.stop();
00232     }
00233     t_i.stop();
00234     int end = t_i.read_us();
00235     pc_bcn.printf("The time required for FCTN_BCN_TX_MAIN is %d useconds\r\n", end-begin);
00236     pc_bcn.printf("The time required for FCTN_BCN_TX is %d useconds\r\n", endtx-begintx);
00237     /*
00238     pc_bcn.printf("The time required for Short_BCN is %d useconds\r\n", beginl-begins);
00239     pc_bcn.printf("The time required for Long_BCN is %d useconds\r\n", endtx-beginl);
00240     */    
00241     ERROR_CHECK = 0;
00242     BCN_TX_MAIN_STATUS = 0;
00243 }
00244 
00245 void Set_BCN_TX_STATUS(uint8_t STATUS)
00246 {
00247     BCN_TX_STATUS = STATUS;
00248 }
00249 
00250 uint8_t check_Temperature()
00251 {   
00252     uint8_t temperature;
00253     writereg(RF22_REG_0F_ADC_CONFIGURATION,0x00);//set ADC to temp measurement            
00254     writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20);//measure in degree celsius
00255     writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80);//start adc
00256     wait(0.1);
00257     //if( readreg(RF22_REG_0F_ADC_CONFIGURATION) & 0x80 == 0x80 )
00258         {    
00259             temperature = readreg(RF22_REG_11_ADC_Value);
00260             temperature = (float)temperature*0.5 - 64;  //* 0.5 factor // to be checked
00261         }
00262     //else temperature = 0xFF;
00263    // temperature = (float)temperature*0.5 - 64;
00264    printf("\n\rthe vLKUE OF TEMPO %d",temperature);
00265     return temperature;
00266 }
00267 
00268 uint32_t timeout_count;
00269 void BCN_TX() // main function for transmitting
00270 {
00271     pc_bcn.printf("BCN_TX\n\r");
00272     
00273     writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
00274     writereg(RF22_REG_6F_TX_DATA_RATE,0x7C);//1280bps
00275     wait(0.02);      
00276                                                                            
00277     timeout_count = 10e5;
00278     clearTxBuf();     //writing data first time
00279     
00280     int byte_counter;
00281     uint8_t onebyte[4] = {0x81,0xA6,0xBE,0x4E};
00282     uint8_t zerobyte[4] = {0x7E,0x59,0x41,0xB1};
00283     for (byte_counter = 0; byte_counter <15 ; byte_counter++) // include condition in the foor loop itself
00284     {
00285          if(BCN_SPND_TX == 1) 
00286             {
00287              clearTxBuf();
00288              break;                                  //Check for flag update by COM
00289             } 
00290          else if( BCN_SW == 0 ) break;
00291          for(int j = 7; j >= 0; j--)
00292         {
00293             if(BCN_SPND_TX == 1) 
00294              {
00295                  clearTxBuf();
00296                  break;                                  //Check for flag update by COM
00297              }  
00298             else if( BCN_SW == 0 ) break;
00299             cs = 0;
00300             spi.write(0xFF);
00301             if((SHORT_HK_data[byte_counter] & (uint8_t) pow(2.0,j))!= pow(2.0,j))
00302             {
00303                 //byte=0x00;
00304                 spi.write(zerobyte[0]);
00305                 spi.write(zerobyte[1]);
00306                 spi.write(zerobyte[2]);
00307                 spi.write(zerobyte[3]);
00308             }
00309             else
00310             { 
00311                 //byte=0xFF;
00312                 spi.write(onebyte[0]);
00313                 spi.write(onebyte[1]);
00314                 spi.write(onebyte[2]);
00315                 spi.write(onebyte[3]);
00316             } 
00317             cs = 1;
00318             /*
00319             spi.write(byte);
00320             spi.write(byte);
00321             spi.write(byte);
00322             spi.write(byte); //Each bit is written 32 times
00323             */
00324            // byte_count+=4;
00325         
00326         }     
00327         
00328         if(byte_counter == 1)
00329             if(BCN_SPND_TX != 1 && BCN_SW == 1)                                  //Check for flag update by COM
00330             {          
00331                 //Set to Tx mode
00332                 writereg(RF22_REG_07_OPERATING_MODE1,0x08);       //txon
00333                 wait(0.1);// takes time to set to tx mode hence the delay of 0.1.  
00334             }
00335         
00336         //testing level
00337         /*if(byte_counter > 0)
00338         while(1)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x40) == 0x00)break;else
00339         {
00340             pc_bcn.printf("w_f_empty1\n\r");
00341             //reset_rfm(1);
00342         }*/
00343         
00344         
00345         //Check for fifoThresh
00346         
00347         if(byte_counter > 0)
00348             if(BCN_SPND_TX != 1 && BCN_SW ==1)                                  //Check for flag update by COM
00349             {
00350                 wait_ms(25);
00351                 while( timeout_count --)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
00352                 {                      
00353                     //pc_bcn.printf("w_f_empty\n");
00354                     //reset_rfm(1);
00355                 }
00356                 timeout_count = 10e5;
00357             }
00358                 
00359        /* if(byte_counter%2==0 && byte_counter)
00360         while(1)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x40) == 0x00)break;else 
00361         {
00362             pc_bcn.printf("W = %d\r\n",byte_counter);
00363         }*/
00364             //pc_bcn.printf("W = %d\r\n",byte_counter);    
00365     }   
00366     
00367     for(byte_counter = 15;byte_counter<149;byte_counter++) // include it in the loop itself
00368     {
00369         if(BCN_SPND_TX == 1) 
00370         {
00371              clearTxBuf();
00372              break;                                  //Check for flag update by COM
00373         } 
00374         else if( BCN_SW == 0 ) break;
00375         cs = 0;
00376         spi.write(0xFF);   
00377         spi.write(LONG_HK_data[BCN_LONG_MSG_TYPE][byte_counter-15]);
00378         cs = 1;
00379         
00380         if((byte_counter-15)%32==0 && BCN_SPND_TX == 0 && BCN_SW == 1)
00381         {   //Check for fifoThresh
00382             wait_ms(25);
00383             while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1); 
00384             {
00385                 //pc_bcn.printf("Reset\n");
00386                 //reset_rfm(1);
00387             }
00388         }
00389         
00390     }
00391     wait_ms(70);
00392     
00393     if( BCN_SW == 1 )
00394     {
00395         //Check for fifoThresh
00396         while(timeout_count--)
00397             if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x40) == 0x00) break;
00398             else if(timeout_count == 1)
00399                 if(BCN_SPND_TX != 1) reset_rfm(1);
00400         
00401         while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x40) == 0x00)break;else if(timeout_count == 1) reset_rfm(1);
00402         {
00403             pc_bcn.printf("Waiting for fifo to empty\r\n");
00404         }
00405         
00406         //Check for packetsent interrupt
00407         while(timeout_count--)
00408             if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04) break;
00409             else if(timeout_count == 1)
00410                 if(BCN_SPND_TX != 1) reset_rfm(1);
00411         
00412         while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;else if(timeout_count == 1) reset_rfm(1);
00413         
00414         //pc_bcn.printf("Short packet sent, bytes written = %d\r\n",byte_count);
00415         
00416         writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
00417     }
00418 }
00419 
00420 void reset_rfm(uint8_t fl)
00421 {
00422     if (fl ==1 && ERROR_CHECK ==0)  
00423     {   BCN_FAIL_COUNT++;
00424         ERROR_CHECK=1;
00425         pc_bcn.printf("BCN_FAIL_COUNT+++\n");
00426     }
00427     else if(fl == 0)
00428     {
00429         BCN_FAIL_COUNT = 0;
00430         ERROR_CHECK=0;
00431     }
00432 }
00433 void writereg(uint8_t reg,uint8_t val)
00434 {
00435     
00436     uint8_t count = 0; 
00437     for(;;count++)
00438     {
00439         int read_val =0; cs = 0;spi.write(reg | 0x80);spi.write(val);cs = 1;
00440         if(reg != 0x7 && reg != 0x58 && reg != 0xF) 
00441         {        
00442             read_val = readreg(reg);
00443             if (read_val == val)
00444             {
00445                 break;    
00446             }
00447             else if(count == 2)
00448             {
00449                 reset_rfm(1); pc_bcn.printf("reg = 0x%X\n",reg);break;
00450             }
00451             else init_spi();
00452         }
00453         else
00454         break;
00455     }
00456 }   
00457 uint8_t readreg(uint8_t reg)
00458 {
00459     uint8_t val;cs = 0;spi.write(reg & ~0x80);val = spi.write(0);cs = 1;return val;
00460 }
00461 void clearTxBuf()
00462 {
00463     writereg(RF22_REG_08_OPERATING_MODE2,0x01);
00464     writereg(RF22_REG_08_OPERATING_MODE2,0x00);
00465 }
00466 uint8_t setFrequency(double centre)
00467 {
00468     uint8_t fbsel = 0x40;
00469     if (centre >= 480.0) {
00470         centre /= 2;
00471         fbsel |= 0x20;
00472     } 
00473     centre /= 10.0;
00474     double integerPart = floor(centre);
00475     double fractionalPart = centre - integerPart;
00476  
00477     uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23
00478     fbsel |= fb;
00479     uint16_t fc = fractionalPart * 64000;
00480     writereg(RF22_REG_73_FREQUENCY_OFFSET1, 0);  // REVISIT
00481     writereg(RF22_REG_74_FREQUENCY_OFFSET2, 0);
00482     writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel);
00483     writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8);
00484     writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff);
00485     return 0;
00486 }
00487 void init_spi()
00488 {
00489     cs=1;                          // chip must be deselected
00490     wait(0.1);
00491     spi.format(8,0);
00492     spi.frequency(10000000);       //10MHz SCLK
00493 }
00494 void Init_BEACON_HW()
00495 {
00496     pc_bcn.printf("Init HW\n\r");
00497     ERROR_CHECK=0;
00498     wait(0.1);
00499     int BCN_INIT_COUNTER = 1;
00500     while(BCN_INIT_COUNTER)
00501     {
00502         init_spi();
00503         pc_bcn.printf("init spi\r\n");
00504         //should either have a flag for invalid SPI or discard this for actual case or add reset
00505         if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08)
00506         {
00507             pc_bcn.printf("spi connection valid\r\n");
00508             reset_rfm(0);
00509             
00510             timer_Set_BCN_TX_STATUS_SUCCESS.reset();
00511             timer_Set_BCN_TX_STATUS_SUCCESS.start();
00512             BCN_TX_STATUS = BCN_INIT_SUCCESS;
00513             timer_Set_BCN_TX_STATUS_SUCCESS.stop();
00514             
00515             break;
00516         }
00517         else if (BCN_INIT_COUNTER == 1)
00518         {
00519             pc_bcn.printf("error in spi connection\r\n");
00520             reset_rfm(1);
00521             writereg(RF22_REG_07_OPERATING_MODE1,0x80);        //sw_reset
00522             wait(0.1);                                         //takes time to reset          
00523         }
00524         else if (BCN_INIT_COUNTER == 2)
00525         {
00526             pc_bcn.printf("error in spi connection\r\n");
00527             reset_rfm(1);
00528             BCN_TX_SW_STATUS = 0b00000011;                      //Device disabled
00529             //reset BCN HW using switch here
00530         }
00531         else 
00532         {
00533             pc_bcn.printf("BCN_TX_FAILURE\r\n");
00534             
00535             timer_Set_BCN_TX_STATUS_FAILURE.reset();
00536             timer_Set_BCN_TX_STATUS_FAILURE.start();
00537             BCN_TX_STATUS = BCN_INIT_FAILURE;
00538             timer_Set_BCN_TX_STATUS_FAILURE.stop();
00539             
00540             break;
00541         }
00542         BCN_INIT_COUNTER++;
00543     }
00544     
00545     if(BCN_TX_STATUS == BCN_INIT_SUCCESS)
00546     {
00547         writereg(RF22_REG_07_OPERATING_MODE1,0x80);        //sw_reset
00548         wait(0.1);                    //takes time to reset                                  
00549     
00550         clearTxBuf();                                                                                                                        
00551         
00552         writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
00553            
00554         //txfifoalmostempty
00555         writereg(RF22_REG_7D_TX_FIFO_CONTROL2,30);
00556         
00557         //txfifoalmostfull
00558         writereg(RF22_REG_7C_TX_FIFO_CONTROL1,50);
00559         
00560     
00561         //Packet-engine registers
00562         writereg(RF22_REG_30_DATA_ACCESS_CONTROL,0x00); 
00563         
00564         writereg(RF22_REG_33_HEADER_CONTROL2,0x08);    
00565         writereg(RF22_REG_34_PREAMBLE_LENGTH,0x00);       
00566     
00567         writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state                        
00568         writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state                        
00569     
00570         setFrequency(435.0);
00571         
00572         int i=3;
00573         while(i--)
00574         if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00)
00575             {
00576                 setFrequency(435.0);
00577                 if (i==1)
00578                 {pc_bcn.printf("frequency not set properly\r\n");
00579                  reset_rfm(1);
00580                 }
00581             }
00582     
00583         //set Modem Configuration
00584         writereg(RF22_REG_58,0x80);
00585         
00586         //Set Data rate - same for both long and short beacon
00587         
00588         writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
00589         writereg(RF22_REG_6F_TX_DATA_RATE,0x7C);//1280bps 
00590         writereg(RF22_REG_70_MODULATION_CONTROL1,0x20);//For data rates below 30kbps
00591         
00592           
00593         writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//0x21 = FIFO mode with ook demodulation
00594          
00595         
00596         //set tx power
00597         writereg(RF22_REG_6D_TX_POWER,0x07);    //20dbm
00598         
00599         //TX_packet_length written later
00600         ERROR_CHECK = 0;
00601         pc_bcn.printf("Done Init HW\n\r");
00602     }
00603 }
00604 bool Check_ACK_RECEIVED()
00605 {
00606     if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)  
00607     {
00608         pc_bcn.printf("Packet sent: ACK received\r\n");
00609         return 1;   
00610     }
00611     else
00612     {
00613         pc_bcn.printf("Packet not sent\r\n");
00614         return 0;
00615     }
00616 }
00617 
00618 // to write in flash function moved to TCTM
00619 void  FCTN_BCN_SPND_TX()
00620 {
00621      printf("BCN_SPND\n\r");
00622      BCN_SPND_TX = 1;
00623      if( BCN_TX_MAIN_STATUS == 1 && BCN_TX_SW_STATUS == 1 )
00624      {
00625          writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
00626          if( readreg(RF22_REG_07_OPERATING_MODE1) & 0x08 == 0x08 )
00627          {
00628              BCN_SW = 0;                //active high
00629              BCN_TX_SW_STATUS = 3;
00630          }
00631      }
00632 }
00633 
00634 /*
00635 int main()
00636 {
00637     FCTN_BCN_INIT(); 
00638     
00639     loop.attach(&FCTN_BCN_TX_MAIN, 10.0);//in actual case its 30.0
00640     
00641     while(1);
00642     
00643 }
00644 */