for testing

Dependencies:   mbed

Revision:
2:9b4647a9558c
Parent:
1:24f2092e3add
Child:
3:3572f6b06485
--- a/main.cpp	Tue Oct 27 17:39:35 2015 +0000
+++ b/main.cpp	Wed Oct 28 13:31:18 2015 +0000
@@ -1,5 +1,6 @@
 #include "beacon.h"
 #include <stdio.h>
+//Takes approx. 4.3 sec in void FCTN_BCN_TX_MAIN() (temp.calc. + long_beacon + short_beacon) 
 
 Serial pc(USBTX, USBRX);        //tx,rx
 SPI spi(D11, D12, D13);              // mosi, miso, sclk
@@ -8,23 +9,35 @@
 Timer t_i;
 Ticker loop;
 
-void P_BCN_INIT()
+//GLOBAL VARIABLES
+uint8_t BCN_INIT_STATUS = 0;
+uint8_t BCN_TX_MAIN_STATUS = 0;
+uint8_t BCN_TX_STATUS = 0;
+uint8_t BCN_TX_EN = 1;              //hardcoding for now    //check where is this variable toggled??
+uint8_t BCN_FEN = 0;                //write this to non-erasable memory.
+uint8_t BCN_STANDBY = 0;            //hardcoding for now    //check where is this variable toggled??
+uint8_t BCN_TS_BUFFER = 0;              // For Temperature 
+
+void FCTN_BCN_INIT()
 {
-    pc.printf("P_BCN_INIT\n");
+    //t_i.start();
+    pc.printf("FCTN_BCN_INIT\n");
     BCN_INIT_STATUS = 1;
     Init_BEACON_HW();
     if (BCN_FEN == 0)
         t.start();//Start the timer for RF_Silence
     BCN_INIT_STATUS = 0;
 }
-void P_BCN_FEN()
+void FCTN_BCN_FEN()
 {
-    pc.printf("P_FEN\n");
+    pc.printf("FCTN_FEN\n");
     BCN_FEN = 1;//write this value to flash
 }
-void P_BCN_TX_MAIN()
+void FCTN_BCN_TX_MAIN()
 {
-    pc.printf("P_BCN_TX_MAIN\n");
+    pc.printf("FCTN_BCN_TX_MAIN\n");
+    t_i.start();
+    int begin = t_i.read_us();
     BCN_TX_MAIN_STATUS = 1;
     
     if(BCN_FEN == 1)
@@ -33,7 +46,7 @@
         {
             //Measure and store BCN temperature in BCN_TS_BUFFER
             BCN_TS_BUFFER = check_Temperature();
-            pc.printf("\n\ntemperature = %d\n\n",BCN_TS_BUFFER);
+            //pc.printf("\n\ntemperature = %d\n\n",BCN_TS_BUFFER);
             //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER
             if(BCN_STANDBY == 1 )
             {
@@ -45,13 +58,8 @@
             else
             {       
                     //transmit short beacon and long beacon
-                    t_i.start();
-                    int begin = t_i.read_us();
                     SHORT_BCN_TX();
                     LONG_BCN_TX();
-                    t_i.stop();
-                    int end = t_i.read_us();
-                    pc.printf("The time required for short and long is %d seconds\r\n", end-begin);
                     
                     if(Check_ACK_RECEIVED() == 1)
                     {
@@ -77,6 +85,10 @@
         Set_BCN_TX_STATUS(BCN_RF_SILENCE);  //Window of RF Silence: None of the Txs should be on.
         BCN_TX_MAIN_STATUS = 0;
     }
+    t_i.stop();
+    int end = t_i.read_us();
+    pc.printf("The time required for short and long is %d seconds\r\n", end-begin);
+    
 }
 
 void Set_BCN_TX_STATUS(uint8_t STATUS)
@@ -89,7 +101,7 @@
     uint8_t temperature;
     writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80);            
     writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20);
-    wait(1);
+    wait(0.1);
     temperature = readreg(RF22_REG_11_ADC_Value);
     temperature = (float)temperature*0.5 - 64;
     return temperature;
@@ -99,16 +111,9 @@
 {
     writereg(RF22_REG_6E_TX_DATA_RATE,0x01);
     writereg(RF22_REG_6F_TX_DATA_RATE,0x4F);//160bps
-    writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length
-    /*
-    init();
-    //init complete
-    pc.printf("init complete.....press t to send\n");
-    while(pc.getc()=='t')
-    {   */
-    //button.rise(&interrupt_func);         //interrupt enabled ( rising edge of pin 9)
-    wait(0.02);                                                           // pl. update this value  or even avoid it!!!                  
-    int i=0;
+    writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length 
+    wait(0.02);                                                                             
+
     //extract values from short_beacon[]
     struct Short_beacon
     {
@@ -122,20 +127,14 @@
     //filling hk data
     //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]};
     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]};
-    
-    //uint8_t short_beacon[15];
-//    for(int i = 0;i<15;i++)
-//    {
-//        short_beacon[i] = 0xAA;
-//    }
-    
+        
     //writereg(RF22_REG_07_OPERATING_MODE1,0x01);        //ready mode       ??    
     clearTxBuf();     
     //writing data first time
     int byte = 0;
     cs = 0;
     spi.write(0xFF);   
-    pc.printf("line 151");
+
     for (int byte_counter = 0; byte_counter <15 ; byte_counter++)
     {
         for(int j = 3; j >= 0 ; j--)
@@ -161,18 +160,18 @@
     }   
     
     cs = 1;
-    pc.printf("line 177\n");
+    
     //Set to Tx mode
     writereg(RF22_REG_07_OPERATING_MODE1,0x09);
-      
+    wait(0.1);  
     //Check for fifoThresh
     while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20);
-    //pc.printf("fifothresh1?\n");
-    pc.printf("line 184\n");
-    //rf22.waitPacketSent();
+
+
+    //Check for packet_sent
     while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04);
-        //pc.printf(" chk pkt sent!\r\n");     
-    pc.printf("Short packet sent\r\n");
+    
+    //pc.printf("Short packet sent\r\n");
     
     writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
     
@@ -210,7 +209,7 @@
     
     //Set to Tx mode
     writereg(RF22_REG_07_OPERATING_MODE1,0x09);
-
+    wait(0.1);
     //Check for fifoThresh
     while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20);
     
@@ -221,20 +220,20 @@
         spi.write(Long_beacon[i]);
     }
     cs = 1;
-    wait(0.01);
+    wait(0.1);
     //Check for fifoThresh
     while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20);
     
     //Check for packetsent interrupt
     while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04);
          
-    pc.printf("Long packet sent\r\n");
+    //pc.printf("Long packet sent\r\n");
     
     writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
 }
 void reset_uC()
 {
-    P_BCN_INIT();
+    FCTN_BCN_INIT();
 }
 void writereg(uint8_t reg,uint8_t val)
 {
@@ -299,20 +298,22 @@
 
 void Init_BEACON_HW()
 {
+    wait(0.1);
     cs=1;                          // chip must be deselected
+    wait(0.1);
     spi.format(8,0);
     spi.frequency(10000000);       //10MHz SCLK
     
     //should either have a flag for invalid SPI or discard this for actual case or add reset
     if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08)
-        /*pc.printf("spi connection valid\r\n")*/;
+        pc.printf("spi connection valid\r\n");
     else
-        {//pc.printf("error in spi connection\r\n");
+        {pc.printf("error in spi connection\r\n");
         reset_uC();
         }
     
     writereg(RF22_REG_07_OPERATING_MODE1,0x80);        //sw_reset
-    wait(1);                    //takes time to reset                                  
+    wait(0.1);                    //takes time to reset                                  
 
     clearTxBuf();                                                                                                                        
     
@@ -334,7 +335,7 @@
 
     if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00)
         {
-            //pc.printf("frequency not set properly\r\n");
+            pc.printf("frequency not set properly\r\n");
             reset_uC();
         }
 
@@ -378,13 +379,13 @@
 }
 int main()
 {
-    P_BCN_INIT(); 
+    FCTN_BCN_INIT(); 
     
-    loop.attach(&P_BCN_TX_MAIN, 10.0);
+    loop.attach(&FCTN_BCN_TX_MAIN, 10.0);//in actual case its 30.0
     
     while(t.read() < RF_SILENCE_TIME);
         
-    P_BCN_FEN();
+    FCTN_BCN_FEN();
     
     while(1);