Repository for final Beacon code of IITMSAT

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
shekhar
Date:
Sat Nov 07 10:23:10 2015 +0000
Parent:
24:ca96cddf25d0
Commit message:
Removed the infi 'init' issue

Changed in this revision

BCN.cpp Show annotated file Show diff for this revision Revisions of this file
BCN.h Show annotated file Show diff for this revision Revisions of this file
pin_config.h Show annotated file Show diff for this revision Revisions of this file
--- a/BCN.cpp	Mon Nov 02 14:46:12 2015 +0000
+++ b/BCN.cpp	Sat Nov 07 10:23:10 2015 +0000
@@ -5,6 +5,8 @@
 //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
 Timer t_i;//timer for checking the time taken by (temp.calc. + long_beacon + short_beacon)
@@ -18,8 +20,10 @@
 uint8_t BCN_TX_EN = 1;              //hardcoding for now    //check where is this variable toggled??
 uint8_t BCN_FEN = 0;                //hardcoding for now    //write this value to flash
 uint8_t BCN_STANDBY = 0;            //hardcoding for now    //check where is this variable toggled??
-uint8_t BCN_TS_BUFFER = 0;              // For Temperature 
-
+uint8_t BCN_TS_BUFFER = 0;          // For Temperature
+uint8_t ERROR_CHECK = 0;
+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.
 void FCTN_BCN_INIT()
 {
     pc_bcn.printf("FCTN_BCN_INIT\n");
@@ -36,6 +40,7 @@
 }
 void FCTN_BCN_TX_MAIN()
 {
+    ERROR_CHECK=0;
     pc_bcn.printf("FCTN_BCN_TX_MAIN\n");
     t_i.start();
     int begin = t_i.read_us();
@@ -89,6 +94,7 @@
     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);
+    ERROR_CHECK = 0;
     
 }
 
@@ -170,11 +176,11 @@
     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;
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
     timeout_count=10e5;
     
     //Check for packet_sent
-    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;
+    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");
     
@@ -212,7 +218,7 @@
     wait(0.1);//necessary
     
     //Check for fifoThresh
-    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
     timeout_count=10e5;
     
     cs = 0;
@@ -224,22 +230,29 @@
     cs = 1;
     wait(0.1);
     //Check for fifoThresh
-    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1) reset_rfm(1);
     timeout_count=10e5;
     
     //Check for packetsent interrupt
-    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;
+    while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;else if(timeout_count == 1) reset_rfm(1);
          
     //pc_bcn.printf("Long packet sent\r\n");
     
     writereg(RF22_REG_07_OPERATING_MODE1,0x00);        //standby mode
 }
-void reset_rfm()
+void reset_rfm(uint8_t fl)
 {
-    FCTN_BCN_INIT();
+if (fl ==1 && ERROR_CHECK ==0)  
+        {BCN_FAIL_COUNT++;
+        ERROR_CHECK=1;
+        printf("BCN_FAIL_COUNT++\n");}
+    else if(fl == 0)
+        {BCN_FAIL_COUNT = 0;
+        ERROR_CHECK=0;}
 }
 void writereg(uint8_t reg,uint8_t val)
 {
+    
     uint8_t count = 0; 
     for(;;count++)
     {
@@ -253,8 +266,9 @@
             }
             else if(count == 5)
             {
-                reset_rfm(), printf("reg = 0x%X\n",reg);break;
+                reset_rfm(1); printf("reg = 0x%X\n",reg);break;
             }
+            else init_spi();
         }
         else
         break;
@@ -290,23 +304,32 @@
     writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff);
     return 0;
 }
-
-
-void Init_BEACON_HW()
+void init_spi()
 {
-    wait(0.1);
     cs=1;                          // chip must be deselected
     wait(0.1);
     spi.format(8,0);
     spi.frequency(10000000);       //10MHz SCLK
+}
+void Init_BEACON_HW()
+{
+    ERROR_CHECK=0;
+    wait(0.1);
+    int i = 5;
+    while(i--)
+    {
+    init_spi();
     
     //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_bcn.printf("spi connection valid\r\n");
-    else
+        {pc_bcn.printf("spi connection valid\r\n");
+        reset_rfm(0);break;}
+    else if (i == 1)
         {pc_bcn.printf("error in spi connection\r\n");
-        reset_rfm();
+        reset_rfm(1);
+        return;
         }
+    }
     
     writereg(RF22_REG_07_OPERATING_MODE1,0x80);        //sw_reset
     wait(0.1);                    //takes time to reset                                  
@@ -328,11 +351,15 @@
     writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state                        
 
     setFrequency(435.0);
-
+    
+    i=3;
+    while(i--)
     if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00)
         {
-            pc_bcn.printf("frequency not set properly\r\n");
-            reset_rfm();
+            setFrequency(435.0);
+            if (i==1)
+            {pc_bcn.printf("frequency not set properly\r\n");
+            reset_rfm(1);}
         }
 
     //set Modem Configuration
@@ -347,6 +374,7 @@
     writereg(RF22_REG_6D_TX_POWER,0x07);    //20dbm
     
     //TX_packet_length written later
+    ERROR_CHECK = 0;
 }
 bool Check_ACK_RECEIVED()
 {
@@ -361,7 +389,7 @@
         return 0;
     }
 }
-/*int main()
+int main()
 {
     FCTN_BCN_INIT(); 
     
@@ -369,4 +397,4 @@
     
     while(1);
     
-}*/
\ No newline at end of file
+}
\ No newline at end of file
--- a/BCN.h	Mon Nov 02 14:46:12 2015 +0000
+++ b/BCN.h	Sat Nov 07 10:23:10 2015 +0000
@@ -12,7 +12,7 @@
 #define LONG_TX_DATA 75    //in bytes      
 
 //#define RF_SILENCE_TIME 35*60
-#define RF_SILENCE_TIME 10 //changed for testing
+#define RF_SILENCE_TIME 5 //changed for testing
 
 //FUNCTION PROTOTYPING
 void FCTN_BCN_INIT();
@@ -28,7 +28,8 @@
 void clearTxBuf();
 uint8_t setFrequency(double);
 bool Check_ACK_RECEIVED();
-void reset_rfm();
+void init_spi();
+void reset_rfm(uint8_t);
 
 #define RF22_MAX_MESSAGE_LEN 255
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pin_config.h	Sat Nov 07 10:23:10 2015 +0000
@@ -0,0 +1,101 @@
+// 100 LQFP format pin assignment
+#define PIN1 PTE0
+#define PIN2 PTE1
+#define PIN3 PTE2
+#define PIN4 PTE3
+#define PIN5 PTE4
+#define PIN6 PTE5
+#define PIN7 PTE6
+//#define 8 
+//#define 9 
+//#define 10 
+//#define 11 
+//#define 12 
+//#define 13 
+#define PIN14 PTE16
+#define PIN15 PTE17
+#define PIN16 PTE18
+#define PIN17 PTE19
+#define PIN18 PTE20
+#define PIN19 PTE21
+#define PIN20 PTE22
+#define PIN21 PTE23
+//#define 22 
+//#define 23 
+//#define 24 
+//#define 25 
+#define PIN26 PTE29
+#define PIN27 PTE30
+#define PIN28 PTE31
+//#define 29 
+//#define 30 
+#define PIN31 PTE24
+#define PIN32 PTE25
+#define PIN33 PTE26
+#define PIN34 PTA0
+#define PIN35 PTA1
+#define PIN36 PTA2
+#define PIN37 PTA3
+#define PIN38 PTA4
+#define PIN39 PTA5
+#define PIN40 PTA6
+#define PIN41 PTA7
+#define PIN42 PTA12
+#define PIN43 PTA13
+#define PIN44 PTA14
+#define PIN45 PTA15
+#define PIN46 PTA16
+#define PIN47 PTA17
+//#define 48 
+//#define 49 
+#define PIN50 PTA18
+#define PIN51 PTA19
+#define PIN52 PTA20
+#define PIN53 PTB0
+#define PIN54 PTB1
+#define PIN55 PTB2
+#define PIN56 PTB3
+#define PIN57 PTB7
+#define PIN58 PTB8
+#define PIN59 PTB9
+#define PIN60 PTB10
+#define PIN61 PTB11
+#define PIN62 PTB16
+#define PIN63 PTB17
+#define PIN64 PTB18
+#define PIN65 PTB19
+#define PIN66 PTB20
+#define PIN67 PTB21
+#define PIN68 PTB22
+#define PIN69 PTB23
+#define PIN70 PTC0
+#define PIN71 PTC1
+#define PIN72 PTC2
+#define PIN73 PTC3
+//#define 74 
+//#define 75 
+#define PIN76 PTC20
+#define PIN77 PTC21
+#define PIN78 PTC22
+#define PIN79 PTC23
+#define PIN80 PTC4
+#define PIN81 PTC5
+#define PIN82 PTC6
+#define PIN83 PTC7
+#define PIN84 PTC8
+#define PIN85 PTC9
+#define PIN86 PTC10
+#define PIN87 PTC11
+#define PIN88 PTC12
+#define PIN89 PTC13
+#define PIN90 PTC16
+#define PIN91 PTC17
+#define PIN92 PTC18
+#define PIN93 PTD0
+#define PIN94 PTD1
+#define PIN95 PTD2
+#define PIN96 PTD3
+#define PIN97 PTD4
+#define PIN98 PTD5
+#define PIN99 PTD6
+#define PIN100 PTD7
\ No newline at end of file