Team Fox / Mbed 2 deprecated workinQM_5thJan_azad

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of workinQM_10thDec by Team Fox

Revision:
33:76f2b8735501
Parent:
27:61c856be467e
Child:
39:670133e7ffd8
--- a/BCN.cpp	Sat Jul 02 14:06:34 2016 +0000
+++ b/BCN.cpp	Mon Jul 04 04:29:59 2016 +0000
@@ -1,4 +1,5 @@
 #include "BCN.h"
+#include "EPS.h"
 #include <stdio.h>
 #include "pin_config.h"
 //Check the pin names
@@ -28,16 +29,60 @@
 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)
                                     //This Flag when exceeds a threshold, uC should reset.
 uint16_t BCN_TX_MAIN_COUNTER = 0;
+uint8_t SHORT_HK_data[15];
+extern uint8_t BCN_LONG_MSG_TYPE;
+extern uint8_t LONG_HK_data[2][134];
+extern DigitalOut BCN_SW;
+extern float EPS_BTRY_TMP_AVG;
+
+extern BAE_HK_quant quant_data;
+extern uint8_t BAE_RESET_COUNTER;
+extern Timer BAE_uptime;
+extern Timer I2C_last;
+extern void RETURN_UPTIME(float,uint8_t*,uint8_t*,uint8_t*);
+extern DigitalOut CDMS_OC_FAULT;
+extern DigitalOut EPS_CHARGER_STATUS;
+extern uint8_t crc8_short();
+
+void SHORT_HK_data_AQ()
+{   
+    uint8_t days,hours,mins;//for calculating the timing data in bae i2c uptime
+    
+    //1st 0-7 call sign
+    //SHORT_HK_data[0] _ SHORT_HK_data[6]
+    
+    SHORT_HK_data[7] = quant_data.voltage_quant[15];
+    SHORT_HK_data[7] = (SHORT_HK_data[7] & 0xF0) | (quant_data.current_quant[1]>>4);
+    //braga SHORT_HK_data[8] = acs first 4 bit
+    SHORT_HK_data[8] = (SHORT_HK_data[8] & 0xF0) | BCN_TMP>>4;
+    //SHORT_HK_data[9] = float_to_uint8(min,max,EPS_BTRY_TMP_AVG); min max diss azad
+    SHORT_HK_data[9] = (SHORT_HK_data[9]&0xF0) | BAE_RESET_COUNTER>>4;
+    
+    RETURN_UPTIME(BAE_uptime.read(),&days,&hours,&mins);
+    SHORT_HK_data[10] = CDMS_OC_FAULT;
+    //SHORT_HK_data[10] = (1<<SHORT_HK_data[10]) | receiver_oc_fault//to be diss;
+    SHORT_HK_data[10] = (SHORT_HK_data[10]<<1) | ((quant_data.voltage_quant[12]>>2)&0x3C);
+    SHORT_HK_data[10] = (SHORT_HK_data[10]<<2) | ((days>>3)&0x03);
+    SHORT_HK_data[11] = (days<<5) | (hours&0x1F);
+    
+    RETURN_UPTIME(I2C_last.read(),&days,&hours,&mins);
+    SHORT_HK_data[12] = EPS_CHARGER_STATUS;
+    SHORT_HK_data[12] = (SHORT_HK_data[12]<<4) | (quant_data.voltage_quant[13]>>4);
+    SHORT_HK_data[12] = (SHORT_HK_data[12]<<3) | (days>>2);    
+    SHORT_HK_data[13] = (days<<6) | (mins&0x3F);
+
+    SHORT_HK_data[14] = crc8_short();    
+}
 
 void FCTN_BCN_INIT()
 {
     pc_bcn.printf("FCTN_BCN_INIT\n");
     BCN_INIT_STATUS = 1;
-    if( BCN_TX_SW_STATUS == 0b00000001 )
+    if( BCN_TX_SW_STATUS == 0b00000001)
         Init_BEACON_HW();
     else Set_BCN_TX_STATUS(BCN_TX_DISABLED);
- //   if(BCN_FEN == 0)//BCN_FEN is in flash
- //       rf_sl_timeout.attach(&FCTN_BCN_FEN, RF_SILENCE_TIME);
+ // if(BCN_FEN == 0)//BCN_FEN is in flash
+ //     rf_sl_timeout.attach(&FCTN_BCN_FEN, RF_SILENCE_TIME);
     BCN_INIT_STATUS = 0;
 }
 
@@ -67,24 +112,27 @@
     {
         if(BCN_TX_SW_STATUS == 0b00000001)
         {
-            //Measure and store BCN temperature in BCN_TS_BUFFER
-            BCN_TMP = check_Temperature();
-            pc_bcn.printf("temperature = %d\n\r",BCN_TMP);
-            //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER
-            if(BCN_SPND_TX == 1 )
+            Init_BEACON_HW();
+            if( BCN_TX_STATUS == BCN_INIT_SUCCESS )
             {
-                Set_BCN_TX_STATUS(BCN_TX_STANDBY);
-                BCN_TX_MAIN_STATUS = 0;
-                
-                // break;
-            }
-            else
-            {       
+                //Measure and store BCN temperature in BCN_TS_BUFFER
+                uint8_t temp_temp = check_Temperature();
+                if( temp_temp != 0xFF )
+                    BCN_TMP = temp_temp;
+                pc_bcn.printf("temperature = %d\n\r",BCN_TMP);
+                //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER
+                if(BCN_SPND_TX == 1 )
+                {
+                    Set_BCN_TX_STATUS(BCN_TX_SUSPENDED);
+                    BCN_TX_MAIN_STATUS = 0;
+                }
+                else
+                {       
                     //transmit short beacon and long beacon
                     begintx = t_i.read_us();
                     BCN_TX();
                     endtx = t_i.read_us();
-                    
+                        
                     if(Check_ACK_RECEIVED() == 1)
                     {
                         Set_BCN_TX_STATUS(BCN_TX_SUCCESS);
@@ -92,13 +140,14 @@
                     }
                     else
                     {
-                        //Set_BCN_TX_STATUS(BCN_TX_FAILURE);
-                        BCN_FAIL_COUNT++;
-                        Init_BEACON_HW();
+                        Set_BCN_TX_STATUS(BCN_TX_FAILURE);
+                        //BCN_FAIL_COUNT++;
+                        //Init_BEACON_HW();
                         BCN_TX_MAIN_STATUS = 0;
-                        
+                            
                     }
-             }
+                }
+            }
         }
         else
         {
@@ -132,13 +181,20 @@
 {   
     uint8_t temperature;
     writereg(RF22_REG_0F_ADC_CONFIGURATION,0x00);//set ADC to temp measurement            
-    writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20);//measure in degree celsius
+    writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x60);//measure in degree celsius
     writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80);//start adc
     wait(0.1);
-    temperature = readreg(RF22_REG_11_ADC_Value);
-    temperature = (float)temperature*0.5 - 64;
+    if( readreg(RF22_REG_0F_ADC_CONFIGURATION) & 0x80 == 0x00 )
+        {    
+            temperature = readreg(RF22_REG_11_ADC_Value);
+            temperature = (float)temperature - 64;  //* 0.5 factor
+        }
+    else temperature = 0xFF;
+   // temperature = (float)temperature*0.5 - 64;
     return temperature;
 }
+
+uint32_t timeout_count;
 void BCN_TX()
 {
     printf("BCN_TX\n\r");
@@ -147,9 +203,9 @@
     
     wait(0.02);      
                                                                            
-    uint32_t timeout_count = 10e5;
+    timeout_count = 10e5;
     //extract values from short_beacon[]
-    
+  /*  
     uint8_t Long_beacon[LONG_TX_DATA];
     for(int i = 0;i<LONG_TX_DATA;i++)
     {
@@ -170,9 +226,10 @@
     uint8_t short_beacon[] = { 0xF3, 0x02, 0xFA, 0xC6, 0xD4, 0x28, 0x8A,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_count=0, byte_counter;
+    int byte_counter;
     uint8_t onebyte[4] = {0x81,0xA6,0xBE,0x4E};
     uint8_t zerobyte[4] = {0x7E,0x59,0x41,0xB1};
     for (byte_counter = 0; byte_counter <15 ; byte_counter++)
@@ -181,7 +238,7 @@
         {
             cs = 0;
             spi.write(0xFF);
-            if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j))!= pow(2.0,j))
+            if((SHORT_HK_data[byte_counter] & (uint8_t) pow(2.0,j))!= pow(2.0,j))
             {
                 //byte=0x00;
                 spi.write(zerobyte[0]);
@@ -204,17 +261,18 @@
             spi.write(byte);
             spi.write(byte); //Each bit is written 32 times
             */
-            byte_count+=4;
+           // byte_count+=4;
         
         }     
         
         if(byte_counter == 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.  
-
-        }
+            if(BCN_SPND_TX != 1)                                  //Check for flag update by COM
+               {          
+                //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.  
+            }
+        
         //testing level
         /*if(byte_counter > 0)
         while(1)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x40) == 0x00)break;else
@@ -227,12 +285,14 @@
         //Check for fifoThresh
         
         if(byte_counter > 0)
-        {wait_ms(25);
-        while(1)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else
         {
-            //pc_bcn.printf("w_f_empty\n");
-            //reset_rfm(1);
-        }
+            wait_ms(25);
+            while( timeout_count --)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
+            {
+                //pc_bcn.printf("w_f_empty\n");
+                //reset_rfm(1);
+            }
+            timeout_count = 10e5;
         }
                 
        /* if(byte_counter%2==0 && byte_counter)
@@ -243,17 +303,17 @@
             //pc_bcn.printf("W = %d\r\n",byte_counter);    
     }   
     
-    for(byte_counter = 15;byte_counter<142;byte_counter++)
+    for(byte_counter = 15;byte_counter<149;byte_counter++)
     {
         cs = 0;
         spi.write(0xFF);   
-        spi.write(Long_beacon[byte_counter-15]);
+        spi.write(LONG_HK_data[BCN_LONG_MSG_TYPE][byte_counter-15]);
         cs = 1;
         
         if((byte_counter-15)%32==0)
         {   //Check for fifoThresh
             wait_ms(25);
-            while(1)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else //if(timeout_count == 1) 
+            while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1); 
             {
                 //pc_bcn.printf("Reset\n");
                 //reset_rfm(1);
@@ -263,7 +323,7 @@
     }
     wait_ms(70);
     //Check for fifoThresh
-    while(1)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x40) == 0x00)break;else 
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x40) == 0x00)break;else if(timeout_count == 1) reset_rfm(1);
     {
         pc_bcn.printf("Waiting for fifo to empty\r\n");
     }
@@ -367,7 +427,7 @@
             {
                 break;    
             }
-            else if(count == 5)
+            else if(count == 2)
             {
                 reset_rfm(1); printf("reg = 0x%X\n",reg);break;
             }
@@ -429,7 +489,7 @@
         {
             pc_bcn.printf("spi connection valid\r\n");
             reset_rfm(0);
-            Set_BCN_TX_STATUS(BCN_TX_INITIALIZED);
+            Set_BCN_TX_STATUS(BCN_INIT_SUCCESS);
             break;
         }
         else if (BCN_INIT_COUNTER == 1)
@@ -449,13 +509,13 @@
         else 
         {
             pc_bcn.printf("BCN_TX_FAILURE\r\n");
-            Set_BCN_TX_STATUS(BCN_TX_FAILURE);
+            Set_BCN_TX_STATUS(BCN_INIT_FAILURE);
             break;
         }
         BCN_INIT_COUNTER++;
     }
     
-    if(BCN_TX_STATUS == BCN_TX_INITIALIZED)
+    if(BCN_TX_STATUS == BCN_INIT_SUCCESS)
     {
         writereg(RF22_REG_07_OPERATING_MODE1,0x80);        //sw_reset
         wait(0.1);                    //takes time to reset                                  
@@ -499,11 +559,12 @@
         //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,0x7C);//1280bps
+        writereg(RF22_REG_6F_TX_DATA_RATE,0x7C);//1280bps 
+        writereg(RF22_REG_70_MODULATION_CONTROL1,0x20);//For data rates below 30kbps
         
           
-        writereg(RF22_REG_70_MODULATION_CONTROL1,0x20);
-        writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//ook = 0x21
+        writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//0x21 = FIFO mode with ook demodulation
+         
         
         //set tx power
         writereg(RF22_REG_6D_TX_POWER,0x07);    //20dbm
@@ -536,4 +597,19 @@
     while(1);
     
 }
-*/
\ No newline at end of file
+*/
+
+void  FCTN_BCN_SPND_TX()
+{
+     BCN_SPND_TX = 1;
+     if( BCN_TX_MAIN_STATUS == 1 && BCN_TX_SW_STATUS == 1 )
+     {
+         writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
+         if( readreg(RF22_REG_07_OPERATING_MODE1) & 0x08 == 0x08 )
+         {
+             BCN_SW = 0;
+             BCN_TX_SW_STATUS = 3;
+         }
+     }
+}
+ 
\ No newline at end of file