bcn code modification for new short bcn format

Dependencies:   mbed

Fork of BEACON_CODE by pradeep shekhar

Revision:
26:e5d6c0cffa82
Parent:
25:90cdfbadea91
diff -r 90cdfbadea91 -r e5d6c0cffa82 BCN.cpp
--- a/BCN.cpp	Sat Nov 07 10:23:10 2015 +0000
+++ b/BCN.cpp	Tue May 17 15:13:49 2016 +0000
@@ -5,11 +5,13 @@
 //Takes max 4.3 sec in void FCTN_BCN_TX_MAIN() (temp.calc. + long_beacon + short_beacon) 
 
 Serial pc_bcn(USBTX, USBRX);        //tx,rx
-//SPI spi(D11, D12, D13);              // mosi, miso, sclk 
-//DigitalOut cs(D10);                //slave select or chip select
-SPI spi(PIN16, PIN17, PIN15);              // mosi, miso, sclk 
-DigitalOut cs(PIN6);                //slave select or chip select
+SPI spi(D11, D12, D13);              // mosi, miso, sclk 
+DigitalOut cs(D10);                //slave select or chip select
+//SPI spi(PIN16, PIN17, PIN15);              // mosi, miso, sclk 
+//DigitalOut cs(PIN6);                //slave select or chip select
 Timer t_i;//timer for checking the time taken by (temp.calc. + long_beacon + short_beacon)
+//Timer t_is;//timer for short_beacon
+//Timer t_il;//timer for long_beacon
 Timeout rf_sl_timeout;//RF_silence timer
 Ticker loop;//for transmitting every 10 secs
 
@@ -44,6 +46,7 @@
     pc_bcn.printf("FCTN_BCN_TX_MAIN\n");
     t_i.start();
     int begin = t_i.read_us();
+    int begins,beginl,ends,endl;
     BCN_TX_MAIN_STATUS = 1;
     
     if(BCN_FEN == 1)
@@ -64,8 +67,17 @@
             else
             {       
                     //transmit short beacon and long beacon
+                    //t_is.start();
+                    begins = t_i.read_us();
                     SHORT_BCN_TX();
+                    //t_is.stop();
+                    ends = t_i.read_us();
+                    
+                    //t_il.start();
+                    beginl = t_i.read_us();
                     LONG_BCN_TX();
+                    //t_il.stop();
+                    endl = t_i.read_us();
                     
                     if(Check_ACK_RECEIVED() == 1)
                     {
@@ -94,6 +106,8 @@
     t_i.stop();
     int end = t_i.read_us();
     pc_bcn.printf("The time required for FCTN_BCN_TX_MAIN is %d useconds\r\n", end-begin);
+    pc_bcn.printf("The time required for Short_BCN is %d useconds\r\n", ends-begins);
+    pc_bcn.printf("The time required for Long_BCN is %d useconds\r\n", endl-beginl);
     ERROR_CHECK = 0;
     
 }
@@ -117,8 +131,13 @@
 
 void SHORT_BCN_TX()
 {
+    /*
     writereg(RF22_REG_6E_TX_DATA_RATE,0x01);
     writereg(RF22_REG_6F_TX_DATA_RATE,0x50);//160bps
+    */
+    writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
+    writereg(RF22_REG_6F_TX_DATA_RATE,0x7D);//1280bps
+    
     writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length 
     wait(0.02);      
                                                                            
@@ -139,14 +158,14 @@
     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]};
         
     //writereg(RF22_REG_07_OPERATING_MODE1,0x01);        //ready mode       ??    
-    clearTxBuf();     
-    //writing data first time
-    int byte = 0;
-    cs = 0;
-    spi.write(0xFF);   
-
+    clearTxBuf();     //writing data first time
+    
+    int byte = 0,byte_count=0;
+    //int spi_start,spi_end,spi_total=0;
     for (int byte_counter = 0; byte_counter <15 ; byte_counter++)
     {
+        //pc_bcn.printf("byte-counter=%d\r\n", byte_counter);
+        /*
         for(int j = 3; j >= 0 ; j--)
         {
             if((short_beacon[byte_counter] & (uint8_t) pow(2.0,(j*2+1)))!= pow(2.0,(j*2+1)))
@@ -165,40 +184,89 @@
             { 
                byte=byte | 0x0F;
             }
+            cs = 0;
+            spi.write(0xFF);   
             spi.write(byte);
+            cs = 1;
          }
+         */
+         for(int j = 7; j >= 0 ; j--)
+        {
+            //spi_start = t_i.read_us();
+            if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j))!= pow(2.0,j))
+            {
+                byte=0x00;
+            }
+            else
+            { 
+                byte=0xFF;
+            } 
+            
+            cs = 0;
+            spi.write(0xFF);   
+            spi.write(byte);
+            spi.write(byte);
+            spi.write(byte);
+            spi.write(byte); //Each bit is written 32 times
+            byte_count+=4;
+            cs = 1;
+        
+            //spi_end = t_i.read_us();
+            //spi_total += spi_end-spi_start;
+            //pc_bcn.printf("Time taken to write one byte = %d\r\n",spi_end-spi_start);
+        }     
+        
+        if(byte_counter % 2 == 1)
+        {
+           
+            //Set to Tx mode
+            writereg(RF22_REG_07_OPERATING_MODE1,0x08);       //txon
+            wait(0.1);// takes time to set to tx mode hence the delay of 0.1.  
+            
+            //Check for fifoThresh
+            while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1)
+            {
+                pc_bcn.printf("Reset\n");
+                 reset_rfm(1);}
+            timeout_count=10e5;
+            
+        }
+        
     }   
-    
-    cs = 1;
-    
+     
     //Set to Tx mode
     writereg(RF22_REG_07_OPERATING_MODE1,0x08);       //txon
     wait(0.1);// takes time to set to tx mode hence the delay of 0.1.  
     
     //Check for fifoThresh
-    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) 
+    {
+        pc_bcn.printf("Reset\n");
+    reset_rfm(1);}
     timeout_count=10e5;
     
-    //Check for packet_sent
+    //Check for packetsent interrupt
     while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;else if(timeout_count == 1) reset_rfm(1);
     
-    //pc_bcn.printf("Short packet sent\r\n");
+    pc_bcn.printf("Short packet sent, bytes = %d\r\n",byte_count);
     
     writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
     
 }
 void LONG_BCN_TX()
 {
+    /*
     writereg(RF22_REG_6E_TX_DATA_RATE,0x04);
     writereg(RF22_REG_6F_TX_DATA_RATE,0xEA);//600 bps
+    */
     writereg(RF22_REG_3E_PACKET_LENGTH,LONG_TX_DATA); //long packet length
     wait(0.02);                                                           
     uint32_t timeout_count=10e5;
    
     //get long_beacon array
     
-    uint8_t Long_beacon[75];
-    for(int i = 0;i<75;i++)
+    uint8_t Long_beacon[LONG_TX_DATA];
+    for(int i = 0;i<LONG_TX_DATA;i++)
     {
         Long_beacon[i] = 0xA0;
     }
@@ -207,7 +275,7 @@
     //writing data first time
     cs = 0;
     spi.write(0xFF);   
-    for(int i=0; i<60;i++)
+    for(int i=0; i<64;i++)
     {
         spi.write(Long_beacon[i]);
     }
@@ -218,19 +286,25 @@
     wait(0.1);//necessary
     
     //Check for fifoThresh
-    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) 
+    {
+        pc_bcn.printf("Reset\n");
+        reset_rfm(1);}
     timeout_count=10e5;
     
     cs = 0;
     spi.write(0xFF);   
-    for(int i=60; i<75;i++)
+    for(int i=64; i<127;i++)
     {
         spi.write(Long_beacon[i]);
     }
     cs = 1;
     wait(0.1);
     //Check for fifoThresh
-    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) 
+    {
+        pc_bcn.printf("Reset\n");
+        reset_rfm(1);}
     timeout_count=10e5;
     
     //Check for packetsent interrupt
@@ -365,7 +439,11 @@
     //set Modem Configuration
     writereg(RF22_REG_58,0x80);
     
-    //Data rate set later
+    //Set Data rate - same for both long and short beacon
+    
+    writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
+    writereg(RF22_REG_6F_TX_DATA_RATE,0x7D);//1280bps
+    
       
     writereg(RF22_REG_70_MODULATION_CONTROL1,0x20);
     writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//ook = 0x21