bcn code modification for new short bcn format

Dependencies:   mbed

Fork of BEACON_CODE by pradeep shekhar

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BCN.cpp Source File

BCN.cpp

00001 #include "BCN.h"
00002 #include <stdio.h>
00003 #include "pin_config.h"
00004 //Check the pin names
00005 //Takes max 4.3 sec in void FCTN_BCN_TX_MAIN() (temp.calc. + long_beacon + short_beacon) 
00006 
00007 Serial pc_bcn(USBTX, USBRX);        //tx,rx
00008 SPI spi(D11, D12, D13);              // mosi, miso, sclk 
00009 DigitalOut cs(D10);                //slave select or chip select
00010 //SPI spi(PIN16, PIN17, PIN15);              // mosi, miso, sclk 
00011 //DigitalOut cs(PIN6);                //slave select or chip select
00012 Timer t_i;//timer for checking the time taken by (temp.calc. + long_beacon + short_beacon)
00013 //Timer t_is;//timer for short_beacon
00014 //Timer t_il;//timer for long_beacon
00015 Timeout rf_sl_timeout;//RF_silence timer
00016 Ticker loop;//for transmitting every 10 secs
00017 
00018 //GLOBAL VARIABLES
00019 uint8_t BCN_INIT_STATUS = 0;
00020 uint8_t BCN_TX_MAIN_STATUS = 0;
00021 uint8_t BCN_TX_STATUS = 0;
00022 uint8_t BCN_TX_EN = 1;              //hardcoding for now    //check where is this variable toggled??
00023 uint8_t BCN_FEN = 0;                //hardcoding for now    //write this value to flash
00024 uint8_t BCN_STANDBY = 0;            //hardcoding for now    //check where is this variable toggled??
00025 uint8_t BCN_TS_BUFFER = 0;          // For Temperature
00026 uint8_t ERROR_CHECK = 0;
00027 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)
00028                                     //This Flag when exceeds a threshold, uC should reset.
00029 void FCTN_BCN_INIT()
00030 {
00031     pc_bcn.printf("FCTN_BCN_INIT\n");
00032     Init_BEACON_HW();
00033     BCN_INIT_STATUS = 1;
00034     if(BCN_FEN == 0)//BCN_FEN is in flash
00035         rf_sl_timeout.attach(&FCTN_BCN_FEN, RF_SILENCE_TIME);
00036     BCN_INIT_STATUS = 0;
00037 }
00038 void FCTN_BCN_FEN()
00039 {
00040     pc_bcn.printf("FCTN_FEN\n");
00041     BCN_FEN = 1;//write this value to flash
00042 }
00043 void FCTN_BCN_TX_MAIN()
00044 {
00045     ERROR_CHECK=0;
00046     pc_bcn.printf("FCTN_BCN_TX_MAIN\n");
00047     t_i.start();
00048     int begin = t_i.read_us();
00049     int begins,beginl,ends,endl;
00050     BCN_TX_MAIN_STATUS = 1;
00051     
00052     if(BCN_FEN == 1)
00053     {
00054         if(BCN_TX_EN == 1)
00055         {
00056             //Measure and store BCN temperature in BCN_TS_BUFFER
00057             BCN_TS_BUFFER = check_Temperature();
00058             pc_bcn.printf("\n\ntemperature = %d\n\n",BCN_TS_BUFFER);
00059             //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER
00060             if(BCN_STANDBY == 1 )
00061             {
00062                 Set_BCN_TX_STATUS(BCN_TX_STANDBY);
00063                 BCN_TX_MAIN_STATUS = 0;
00064                 
00065                 // break;
00066             }
00067             else
00068             {       
00069                     //transmit short beacon and long beacon
00070                     //t_is.start();
00071                     begins = t_i.read_us();
00072                     SHORT_BCN_TX();
00073                     //t_is.stop();
00074                     ends = t_i.read_us();
00075                     
00076                     //t_il.start();
00077                     beginl = t_i.read_us();
00078                     LONG_BCN_TX();
00079                     //t_il.stop();
00080                     endl = t_i.read_us();
00081                     
00082                     if(Check_ACK_RECEIVED() == 1)
00083                     {
00084                         Set_BCN_TX_STATUS(BCN_TX_SUCCESS);
00085                         BCN_TX_MAIN_STATUS = 0;   
00086                     }
00087                     else
00088                     {
00089                         Set_BCN_TX_STATUS(BCN_TX_FAILURE);
00090                         BCN_TX_MAIN_STATUS = 0;
00091                         
00092                     }
00093              }
00094         }
00095         else
00096         {
00097             Set_BCN_TX_STATUS(BCN_TX_DISABLED);
00098             BCN_TX_MAIN_STATUS = 0;
00099         }
00100     }
00101     else
00102     {
00103         Set_BCN_TX_STATUS(BCN_RF_SILENCE);  //Window of RF Silence: None of the Txs should be on.
00104         BCN_TX_MAIN_STATUS = 0;
00105     }
00106     t_i.stop();
00107     int end = t_i.read_us();
00108     pc_bcn.printf("The time required for FCTN_BCN_TX_MAIN is %d useconds\r\n", end-begin);
00109     pc_bcn.printf("The time required for Short_BCN is %d useconds\r\n", ends-begins);
00110     pc_bcn.printf("The time required for Long_BCN is %d useconds\r\n", endl-beginl);
00111     ERROR_CHECK = 0;
00112     
00113 }
00114 
00115 void Set_BCN_TX_STATUS(uint8_t STATUS)
00116 {
00117     BCN_TX_STATUS = STATUS;
00118 }
00119 
00120 uint8_t check_Temperature()
00121 {   
00122     uint8_t temperature;
00123     writereg(RF22_REG_0F_ADC_CONFIGURATION,0x00);//set ADC to temp measurement            
00124     writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20);//measure in degree celsius
00125     writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80);//start adc
00126     wait(0.1);
00127     temperature = readreg(RF22_REG_11_ADC_Value);
00128     temperature = (float)temperature*0.5 - 64;
00129     return temperature;
00130 }
00131 
00132 void SHORT_BCN_TX()
00133 {
00134     /*
00135     writereg(RF22_REG_6E_TX_DATA_RATE,0x01);
00136     writereg(RF22_REG_6F_TX_DATA_RATE,0x50);//160bps
00137     */
00138     writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
00139     writereg(RF22_REG_6F_TX_DATA_RATE,0x7D);//1280bps
00140     
00141     writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length 
00142     wait(0.02);      
00143                                                                            
00144     uint32_t timeout_count = 10e5;
00145     //extract values from short_beacon[]
00146     
00147     struct Short_beacon
00148     {
00149         uint8_t Voltage[1];
00150         uint8_t AngularSpeed[2];
00151         uint8_t SubsystemStatus[1];
00152         uint8_t Temp[3];
00153         uint8_t ErrorFlag[1];
00154     }Shortbeacon = { {0x88}, {0x99, 0xAA} , {0xAA},{0xAA,0xDD,0xEE}, {0x00} };
00155     
00156     //filling hk data
00157     //uint8_t short_beacon[] = { 0xAB, 0x8A, 0xE2, 0xBB, 0xB8, 0xA2, 0x8E,Shortbeacon.Voltage[0],Shortbeacon.AngularSpeed[0], Shortbeacon.AngularSpeed[1],Shortbeacon.SubsystemStatus[0],Shortbeacon.Temp[0],Shortbeacon.Temp[1],Shortbeacon.Temp[2],Shortbeacon.ErrorFlag[0]};
00158     uint8_t short_beacon[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,Shortbeacon.Voltage[0],Shortbeacon.AngularSpeed[0], Shortbeacon.AngularSpeed[1],Shortbeacon.SubsystemStatus[0],Shortbeacon.Temp[0],Shortbeacon.Temp[1],Shortbeacon.Temp[2],Shortbeacon.ErrorFlag[0]};
00159         
00160     //writereg(RF22_REG_07_OPERATING_MODE1,0x01);        //ready mode       ??    
00161     clearTxBuf();     //writing data first time
00162     
00163     int byte = 0,byte_count=0;
00164     //int spi_start,spi_end,spi_total=0;
00165     for (int byte_counter = 0; byte_counter <15 ; byte_counter++)
00166     {
00167         //pc_bcn.printf("byte-counter=%d\r\n", byte_counter);
00168         /*
00169         for(int j = 3; j >= 0 ; j--)
00170         {
00171             if((short_beacon[byte_counter] & (uint8_t) pow(2.0,(j*2+1)))!= pow(2.0,(j*2+1)))
00172             {
00173                 byte=0x00;
00174             }
00175             else
00176             { 
00177                 byte=0xF0;
00178             }  
00179             if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j*2))!= pow(2.0,j*2))
00180             {
00181                 byte=byte | 0x00;
00182             }
00183             else
00184             { 
00185                byte=byte | 0x0F;
00186             }
00187             cs = 0;
00188             spi.write(0xFF);   
00189             spi.write(byte);
00190             cs = 1;
00191          }
00192          */
00193          for(int j = 7; j >= 0 ; j--)
00194         {
00195             //spi_start = t_i.read_us();
00196             if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j))!= pow(2.0,j))
00197             {
00198                 byte=0x00;
00199             }
00200             else
00201             { 
00202                 byte=0xFF;
00203             } 
00204             
00205             cs = 0;
00206             spi.write(0xFF);   
00207             spi.write(byte);
00208             spi.write(byte);
00209             spi.write(byte);
00210             spi.write(byte); //Each bit is written 32 times
00211             byte_count+=4;
00212             cs = 1;
00213         
00214             //spi_end = t_i.read_us();
00215             //spi_total += spi_end-spi_start;
00216             //pc_bcn.printf("Time taken to write one byte = %d\r\n",spi_end-spi_start);
00217         }     
00218         
00219         if(byte_counter % 2 == 1)
00220         {
00221            
00222             //Set to Tx mode
00223             writereg(RF22_REG_07_OPERATING_MODE1,0x08);       //txon
00224             wait(0.1);// takes time to set to tx mode hence the delay of 0.1.  
00225             
00226             //Check for fifoThresh
00227             while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1)
00228             {
00229                 pc_bcn.printf("Reset\n");
00230                  reset_rfm(1);}
00231             timeout_count=10e5;
00232             
00233         }
00234         
00235     }   
00236      
00237     //Set to Tx mode
00238     writereg(RF22_REG_07_OPERATING_MODE1,0x08);       //txon
00239     wait(0.1);// takes time to set to tx mode hence the delay of 0.1.  
00240     
00241     //Check for fifoThresh
00242     while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) 
00243     {
00244         pc_bcn.printf("Reset\n");
00245     reset_rfm(1);}
00246     timeout_count=10e5;
00247     
00248     //Check for packetsent interrupt
00249     while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;else if(timeout_count == 1) reset_rfm(1);
00250     
00251     pc_bcn.printf("Short packet sent, bytes = %d\r\n",byte_count);
00252     
00253     writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
00254     
00255 }
00256 void LONG_BCN_TX()
00257 {
00258     /*
00259     writereg(RF22_REG_6E_TX_DATA_RATE,0x04);
00260     writereg(RF22_REG_6F_TX_DATA_RATE,0xEA);//600 bps
00261     */
00262     writereg(RF22_REG_3E_PACKET_LENGTH,LONG_TX_DATA); //long packet length
00263     wait(0.02);                                                           
00264     uint32_t timeout_count=10e5;
00265    
00266     //get long_beacon array
00267     
00268     uint8_t Long_beacon[LONG_TX_DATA];
00269     for(int i = 0;i<LONG_TX_DATA;i++)
00270     {
00271         Long_beacon[i] = 0xA0;
00272     }
00273     
00274     clearTxBuf();     
00275     //writing data first time
00276     cs = 0;
00277     spi.write(0xFF);   
00278     for(int i=0; i<64;i++)
00279     {
00280         spi.write(Long_beacon[i]);
00281     }
00282     cs = 1;
00283     
00284     //Set to Tx mode
00285     writereg(RF22_REG_07_OPERATING_MODE1,0x08);
00286     wait(0.1);//necessary
00287     
00288     //Check for fifoThresh
00289     while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) 
00290     {
00291         pc_bcn.printf("Reset\n");
00292         reset_rfm(1);}
00293     timeout_count=10e5;
00294     
00295     cs = 0;
00296     spi.write(0xFF);   
00297     for(int i=64; i<127;i++)
00298     {
00299         spi.write(Long_beacon[i]);
00300     }
00301     cs = 1;
00302     wait(0.1);
00303     //Check for fifoThresh
00304     while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) 
00305     {
00306         pc_bcn.printf("Reset\n");
00307         reset_rfm(1);}
00308     timeout_count=10e5;
00309     
00310     //Check for packetsent interrupt
00311     while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;else if(timeout_count == 1) reset_rfm(1);
00312          
00313     //pc_bcn.printf("Long packet sent\r\n");
00314     
00315     writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
00316 }
00317 void reset_rfm(uint8_t fl)
00318 {
00319 if (fl ==1 && ERROR_CHECK ==0)  
00320         {BCN_FAIL_COUNT++;
00321         ERROR_CHECK=1;
00322         printf("BCN_FAIL_COUNT++\n");}
00323     else if(fl == 0)
00324         {BCN_FAIL_COUNT = 0;
00325         ERROR_CHECK=0;}
00326 }
00327 void writereg(uint8_t reg,uint8_t val)
00328 {
00329     
00330     uint8_t count = 0; 
00331     for(;;count++)
00332     {
00333         int read_val =0; cs = 0;spi.write(reg | 0x80);spi.write(val);cs = 1;
00334         if(reg != 0x7 && reg != 0x58 && reg != 0xF) 
00335         {        
00336             read_val = readreg(reg);
00337             if (read_val == val)
00338             {
00339                 break;    
00340             }
00341             else if(count == 5)
00342             {
00343                 reset_rfm(1); printf("reg = 0x%X\n",reg);break;
00344             }
00345             else init_spi();
00346         }
00347         else
00348         break;
00349     }
00350 }   
00351 uint8_t readreg(uint8_t reg)
00352 {
00353     uint8_t val;cs = 0;spi.write(reg & ~0x80);val = spi.write(0);cs = 1;return val;
00354 }
00355 void clearTxBuf()
00356 {
00357     writereg(RF22_REG_08_OPERATING_MODE2,0x01);
00358     writereg(RF22_REG_08_OPERATING_MODE2,0x00);
00359 }
00360 uint8_t setFrequency(double centre)
00361 {
00362     uint8_t fbsel = 0x40;
00363     if (centre >= 480.0) {
00364         centre /= 2;
00365         fbsel |= 0x20;
00366     } 
00367     centre /= 10.0;
00368     double integerPart = floor(centre);
00369     double fractionalPart = centre - integerPart;
00370  
00371     uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23
00372     fbsel |= fb;
00373     uint16_t fc = fractionalPart * 64000;
00374     writereg(RF22_REG_73_FREQUENCY_OFFSET1, 0);  // REVISIT
00375     writereg(RF22_REG_74_FREQUENCY_OFFSET2, 0);
00376     writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel);
00377     writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8);
00378     writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff);
00379     return 0;
00380 }
00381 void init_spi()
00382 {
00383     cs=1;                          // chip must be deselected
00384     wait(0.1);
00385     spi.format(8,0);
00386     spi.frequency(10000000);       //10MHz SCLK
00387 }
00388 void Init_BEACON_HW()
00389 {
00390     ERROR_CHECK=0;
00391     wait(0.1);
00392     int i = 5;
00393     while(i--)
00394     {
00395     init_spi();
00396     
00397     //should either have a flag for invalid SPI or discard this for actual case or add reset
00398     if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08)
00399         {pc_bcn.printf("spi connection valid\r\n");
00400         reset_rfm(0);break;}
00401     else if (i == 1)
00402         {pc_bcn.printf("error in spi connection\r\n");
00403         reset_rfm(1);
00404         return;
00405         }
00406     }
00407     
00408     writereg(RF22_REG_07_OPERATING_MODE1,0x80);        //sw_reset
00409     wait(0.1);                    //takes time to reset                                  
00410 
00411     clearTxBuf();                                                                                                                        
00412     
00413     writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
00414        
00415     //txfifoalmostempty
00416     writereg(RF22_REG_7D_TX_FIFO_CONTROL2,30);
00417 
00418     //Packet-engine registers
00419     writereg(RF22_REG_30_DATA_ACCESS_CONTROL,0x00); 
00420     
00421     writereg(RF22_REG_33_HEADER_CONTROL2,0x08);    
00422     writereg(RF22_REG_34_PREAMBLE_LENGTH,0x00);       
00423 
00424     writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state                        
00425     writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state                        
00426 
00427     setFrequency(435.0);
00428     
00429     i=3;
00430     while(i--)
00431     if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00)
00432         {
00433             setFrequency(435.0);
00434             if (i==1)
00435             {pc_bcn.printf("frequency not set properly\r\n");
00436             reset_rfm(1);}
00437         }
00438 
00439     //set Modem Configuration
00440     writereg(RF22_REG_58,0x80);
00441     
00442     //Set Data rate - same for both long and short beacon
00443     
00444     writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
00445     writereg(RF22_REG_6F_TX_DATA_RATE,0x7D);//1280bps
00446     
00447       
00448     writereg(RF22_REG_70_MODULATION_CONTROL1,0x20);
00449     writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//ook = 0x21
00450     
00451     //set tx power
00452     writereg(RF22_REG_6D_TX_POWER,0x07);    //20dbm
00453     
00454     //TX_packet_length written later
00455     ERROR_CHECK = 0;
00456 }
00457 bool Check_ACK_RECEIVED()
00458 {
00459     if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)  
00460     {
00461         printf("Packet sent: ACK received\r\n");
00462         return 1;   
00463     }
00464     else
00465     {
00466         pc_bcn.printf("Packet not sent\r\n");
00467         return 0;
00468     }
00469 }
00470 int main()
00471 {
00472     FCTN_BCN_INIT(); 
00473     
00474     loop.attach(&FCTN_BCN_TX_MAIN, 10.0);//in actual case its 30.0
00475     
00476     while(1);
00477     
00478 }