Works properly tilll bbram

Dependencies:   mbed

Revision:
0:cb8104f3ae71
Child:
1:9cd986151b11
diff -r 000000000000 -r cb8104f3ae71 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 22 09:43:36 2015 +0000
@@ -0,0 +1,537 @@
+///ADF7023 From BIT BUCKET
+//TO DO SPI and CMD CHeck
+#include "mbed.h"
+#include <iostream>
+#include <stdio.h>
+using namespace std;
+#include <bitset>
+#define ENDL "\r" << endl;
+
+#define MISO_PIN PTA17
+#define THRS 20
+#define STATE_ERR_THRS 20
+#define PHY_OFF_EXEC_TIME 300
+#define PHY_ON_EXEC_TIME 300
+#define PHY_TX_EXEC_TIME 600
+/******DEFINING OMMANDS*********/
+
+#define CMD_HW_RESET 0xC8
+#define CMD_PHY_ON 0xB1
+#define CMD_PHY_OFF 0xB0
+#define CMD_PHY_TX 0xB5
+#define CMD_CONFIG_DEV 0xBB
+
+/*****FUNCTION Declaration********************/
+bool assrt_phy_off( int, int, int);
+bool assrt_phy_on( int,int,int);
+bool assrt_phy_tx(int,int,int);
+/****************************/
+SPI adf(PTA16, MISO_PIN, PTA15);
+//MOSI, MISO, CLOCK 
+DigitalOut CS(D9);
+//DigitalOut CS(PTD4);//D10==PTD4
+Serial PC(USBTX, USBRX);
+DigitalOut ledr(LED1);
+DigitalOut led2(LED2);
+InterruptIn IRQ(D8);
+//InterruptIn IRQ(PTA13);//D8==PTA13
+Ticker ticker;
+//Timer t;
+//Timer stop;
+int x=IRQ;
+int src=0;
+int irqsrc = 0;
+int j = 0;
+int k = 0;
+int irq1=0;
+int r = 0;
+unsigned char data[]={0x65,0xD3,0x06,0x08,0xBB,0xE7,0xCD,0x16,0x65,0xD3,0x06,0x08,0xBB,0xE7,0xCD,0x16,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0x10,0x83,0x11,0x24,0x67,0xCE,0x05,0x50,0x01,0x45,0x9D,0x46,0x0D,0x6B,0xC1,0xF2,0xF3,0xE8,0xFE,0x52,0x0A,0x27,0x90,0x0A,0x3F,0x15,0xD1,0x68,0xD0,0x48,0x75,0x06,0x04,0xB7,0xE6,0xA0,0x82,0xF2,0x50,0x20,0x75,0x09,0x65,0x68,0x1A,0x9A,0xDD,0x1C,0x3F,0xC6,0xF5,0xD8,0xBD,0x96,0xB1,0xA0,0x92,0x29,0x44,0x78,0x60,0x0C,0x5D,0xCE,0xED,0x7A,0x85,0xF0,0x70,0x1C,0x7F,0x52,0x7B,0x14,0x43,0x4C,0x76,0xDD,0x41,0x86,0x59,0x6D,0x18,0x7C,0x63,0xF3,0x9F,0x74,0xB8,0x00,0x77,0xC0,0x18,0xDD,0xCE,0xA2,0xD1,0x2A,0xF3,0xFE,0x22,0xB5,0x0E,0x42,0xAF,0xB7,0x89,0x38,0x15,0xB0,0xCD,0xD6,0x27,0xEB,0x3F,0x5C,0x05,0xF7,0x26,0x5A,0x6A,0x4B,0x08,0x9E,0xAA,0x74,0xFF,0x30,0x0A,0x0A,0xBD,0x72,0xA2,0x44,0x20,0xC6,0x92,0x59,0xF3,0x80,0x55,0x00,0x51,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0xD4,0x63,0x5A,0xF0,0x1F,0x1C,0xFA,0x3C,0xE5,0x22,0x89,0xE4,0x00,0xAF,0xC5,0x74,0x16,0xB4,0x12,0x1C,0x50,0x61,0x2D,0xF8,0x6A,0x20,0xBC,0x96,0x82,0x1D,0x42,0x5A,0xD6,0x86,0xA6,0xB4,0xD1,0xCF,0xF1,0xBC,0x5D,0xAF,0x65,0xAE,0x9A,0x24,0x8A,0x52,0xC7,0x98,0x03,0x14,0xDC,0xDB,0x5E,0xA0,0x5F,0x1C,0x07,0x1E,0x75,0x3E,0xC5,0x12,0xB4,0xDD,0xB7,0x50,0x18,0x76,0x5B,0x44,0x87,0xD8,0xFC,0xE4,0xF7,0x6E,0x00,0x1E,0xFC,0x06,0x37,0x70,0xEA,0x14,0x4A,0xBE,0xBF,0xE8,0xAD,0x42,0x64,0x0B,0xED,0xE2,0x13,0x85,0x6C,0x30,0xDD,0x69,0xFA,0xCC,0xF5,0xC1,0x7D,0xC8,0x65,0xBA,0x92,0xC0,0x89,0xCA,0x9D,0x3C,0xF3,0x02,0x82,0xAE,0x57,0x08,0x91,0x08,0x0C,0x44,0x96,0x7E,0xB8,0x15,0x40,0x14,0x16,0x75,0x18,0xD4,0xAF,0x07,0xC7,0x3C,0xA3,0xF9,0x48,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0x9E,0x40,0x2B,0xF0,0x57,0x45,0xAD,0x04,0x21,0xD4,0x18,0x48,0xDF,0x9A,0x88,0x2C,0xC9,0x40,0x87,0x52,0xA5,0x95,0xA1,0xA8,0x6B,0x74,0x73,0xFC,0x1B,0xD7,0x6B,0xDA,0xDA,0xC6,0x89,0x22,0x25,0x11,0xE6,0x00,0x31,0x77,0x36,0xD6,0x6A,0x17,0xC7,0x02,0xF1,0xFD,0x4F,0xB0,0x51,0x0D,0x37,0x6E,0xF5,0x06,0x1D,0x96,0x34,0x61,0xF6,0x3C,0xCE,0x7D,0xDB,0x80,0x01,0xDF,0x01,0x8C,0x77,0x3A,0x85,0x10,0xAB,0xCF,0xFA,0x2A,0x54,0x39,0x02,0xFA,0x5E,0x24,0xE1,0x58,0xC3,0x37,0x5A,0x7C,0xAC,0xFD,0x70,0x5C,0xDC,0x99,0x6E,0xA6,0xAC,0x22,0x72,0xA6,0x53,0xFC,0xC0,0xA0,0x2A,0xF5,0xC2,0x24,0x10,0x83,0x11,0x24,0x67,0xCE,0x05,0x50,0x01,0x45,0x9D,0x46,0x0D,0x6B,0xC1,0xF2,0xF3,0xE8,0xFE,0x52,0x0A,0x27,0x90,0x0A,0x3F,0x15,0xD1,0x68,0xD0,0x48,0x75,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0x04,0xB7,0xE6,0xA0,0x82,0xF2,0x50,0x20,0x75,0x09,0x65,0x68,0x1A,0x9A,0xDD,0x1C,0x3F,0xC6,0xF5,0xD8,0xBD,0x96,0xB1,0xA0,0x92,0x29,0x44,0x78,0x60,0x0C,0x5D,0xCE,0xED,0x7A,0x85,0xF0,0x70,0x1C,0x7F,0x52,0x7B,0x14,0x43,0x4C,0x76,0xDD,0x41,0x86,0x59,0x6D,0x18,0x7C,0x63,0xF3,0x9F,0x74,0xB8,0x00,0x77,0xC0,0x18,0xDD,0xCE,0xA2,0xD1,0x2A,0xF3,0xFE,0x22,0xB5,0x0E,0x42,0xAF,0xB7,0x89,0x38,0x15,0xB0,0xCD,0xD6,0x27,0xEB,0x3F,0x5C,0x05,0xF7,0x26,0x5A,0x6A,0x4B,0x08,0x9E,0xAA,0x74,0xFF,0x30,0x0A,0x0A,0xBD,0x72,0xA2,0x44,0x20,0xC6,0x92,0x59,0xF3,0x80,0x55,0x00,0x51,0x64,0xD4,0x63,0x5A,0xF0,0x1F,0x1C,0xFA,0x3C,0xE5,0x22,0x89,0xE4,0x00,0xAF,0xC5,0x74,0x16,0xB4,0x12,0x1C,0x50,0x61,0x2D,0xF8,0x6A,0x20,0xBC,0x96,0x82,0x1D,0x42,0x5A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0xD6,0x86,0xA6,0xB4,0xD1,0xCF,0xF1,0xBC,0x5D,0xAF,0x65,0xAE,0x9A,0x24,0x8A,0x52,0xC7,0x98,0x03,0x14,0xDC,0xDB,0x5E,0xA0,0x5F,0x1C,0x07,0x1E,0x75,0x3E,0xC5,0x12,0xB4,0xDD,0xB7,0x50,0x18,0x76,0x5B,0x44,0x87,0xD8,0xFC,0xE4,0xF7,0x6E,0x00,0x1E,0xFC,0x06,0x37,0x70,0xEA,0x14,0x4A,0xBE,0xBF,0xE8,0xAD,0x42,0x64,0x0B,0xED,0xE2,0x13,0x85,0x6C,0x30,0xDD,0x69,0xFA,0xCC,0xF5,0xC1,0x7D,0xC8,0x65,0xBA,0x92,0xC0,0x89,0xCA,0x9D,0x3C,0xF3,0x02,0x82,0xAE,0x57,0x08,0x91,0x08,0x0C,0x44,0x96,0x7E,0xB8,0x15,0x40,0x14,0x16,0x75,0x18,0xD4,0xAF,0x07,0xC7,0x3C,0xA3,0xF9,0x48,0xA0,0x9E,0x40,0x2B,0xF0,0x57,0x45,0xAD,0x04,0x21,0xD4,0x18,0x48,0xDF,0x9A,0x88,0x2C,0xC9,0x40,0x87,0x52,0xA5,0x95,0xA1,0xA8,0x6B,0x74,0x73,0xFC,0x1B,0xD7,0x6B,0xDA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x59,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x58,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x59,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x58,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+int a[100];
+int b[100];
+int c[100];
+
+/*****ERROR CHECKING ALGO*****/
+unsigned char status =0;
+unsigned int cmd_err_cnt=0;
+unsigned int state_err_cnt=0;
+unsigned int miso_err_cnt=0;
+unsigned int hw_reset_err_cnt=0;
+/*Err_check Flags*/
+bool bcn_flag=0;
+bool bbram_flag=0;
+/***********/
+
+#define SPI_NOP 0xFF
+/***********************************************************************************/
+void bbram_write()
+{   
+  /*  CS=0;
+    adf.write(0xB0);//PHY_OFF
+    wait_ms(2);
+    CS=1;
+  *///Commented on 21st October debugging  
+    // Write bbram
+    CS=0;
+    adf.write(0x19);
+    adf.write(0x00);
+    adf.write(0x60);
+    adf.write(0x00);
+    
+    adf.write(0x00);    
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    
+    adf.write(0x00);
+    adf.write(0x33);
+    adf.write(0x00);
+    adf.write(0xF4);//Frequency Register F9 = 435.802 MHz F4 = 435.800 MHz
+    
+    adf.write(0xC2);
+    adf.write(0x10);
+    adf.write(0xC0);    
+    adf.write(0x00);
+    
+    adf.write(0x30);
+    adf.write(0x31);
+    adf.write(0x07);    
+    adf.write(0x00);
+        
+    adf.write(0x01);
+    adf.write(0x00);
+    adf.write(0x7F);
+    adf.write(0x00);
+    
+    adf.write(0x0B);
+    adf.write(0x37);
+    adf.write(0x00);    
+    adf.write(0x00);
+    
+    adf.write(0x40);
+    adf.write(0x0C);
+    adf.write(0x00);
+    adf.write(0x05);
+    
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x18);
+    adf.write(0x12);
+    
+    adf.write(0x34);
+    adf.write(0x56);
+    adf.write(0x10);
+    adf.write(0x10);
+    
+    adf.write(0xC4); // Different
+    adf.write(0x14);
+    adf.write(0x00);
+    adf.write(0x00);
+    
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    
+    adf.write(0x04);
+    adf.write(0x00);
+    adf.write(0x00);
+    adf.write(0x00);
+    
+    adf.write(0x00);
+    adf.write(0x00);
+    CS=1;
+    wait(1);
+    
+}
+
+
+/******************************************************************************************/
+
+void initiate(void){
+    
+    CS=0;
+    adf.write(0xFF);
+    adf.write(0xFF);
+    CS=1;
+    wait_ms(2);
+
+    CS=0;
+    adf.write(0x08);    // TRANSMIT_DATA LENGTH
+    adf.write(0x14);
+    adf.write(0xFF);
+    CS=1;
+    wait_ms(2);
+    CS=0;
+    adf.write(0x08);        // TRANSMIT_DATA LENGTH
+    adf.write(0x15);
+    adf.write(0xFF);
+    CS=1;
+    wait_ms(2);
+    
+    CS=0;
+    adf.write(0x09);
+    adf.write(0x24);   // TX_BASE ADDRESS   0x20(starting Tx Byte)
+    adf.write(0x20);
+    CS=1;
+    wait_ms(2);
+    
+    CS=0;
+    adf.write(0x09);
+    adf.write(0x37);// BUFFER SIZE 0xE0=224 Bytes 0x137 is adress of buffer size
+    adf.write(0xE0);
+    CS=1;
+    wait_ms(2);
+    
+    CS=0;
+    adf.write(0x09);
+    adf.write(0x36);//BB_Tx_Buffer Signal when Buffer is half filled
+    adf.write(0x70);//0x70 = 112 >> When Half buffer interrupt is given
+    CS=1;
+    wait_ms(2);
+    
+    
+    CS=0;
+    adf.write(0x09);
+    adf.write(0x39);//BB_Tx_Buffer Signal when Buffer is half filled
+    adf.write(0x10);//0x70 = 112 >> When Half buffer interrupt is given
+    CS=1;
+    wait_ms(2);
+
+    }
+
+    
+    
+/*int irq_src(void){
+        
+        adf.write(0x2B);
+        adf.write(0x36);
+        adf.write(0xFF);
+        irqsrc = adf.write(0xFF);
+        
+        irqsrc &= 0x60;
+        if (irqsrc == 0x20)
+        return 1;//Buffer is almost full
+        else if (irqsrc == 0x40)
+        return 2;// Buffer is Full
+        else
+        return 0;
+        }
+  */  
+  int p=112;
+  int count = 0;
+  int intcount =0;
+  
+void write_data(void){
+//    t.reset();
+//    count++;
+//    cout<<"3"<<ENDL;
+//    ledr=!ledr;
+//    cout<<"irq detected"<<ENDL;
+//    src = irq_src();
+    p+=112;
+    
+    //CS=0;
+//    adf.write(0x0B);//IRQ_SOURCE_Random write //To read CMD = 2B 36 FF FF
+//    adf.write(0x36);//Address : 0x336
+//    adf.write(0xFF);//Put Low
+//    CS=1;
+//    wait_us(80);
+//    CS=0;
+//    k=0;
+    
+    if(!j){
+        adf.write(0x18);
+        adf.write(0x20);
+        while(k<=223){
+          
+          adf.write(data[k]);  
+          k++;
+            }
+        
+         }       
+    
+    else if(k<sizeof(data)){
+//                if(src == 1){
+                if(j%2){  //src==  Buffer Half Full src == 1
+                adf.write(0x18);
+                adf.write(0x20);
+                }
+            
+//            else if(src==2){
+            else{               //src==   Buffer is Full src==2
+                adf.write(0x18);
+                adf.write(0x90);
+                }
+            
+            
+            while(k<p){
+                adf.write(data[k]);
+                k++;
+                        
+            }
+//            if((k)>=sizeof(data))
+//            {
+//                ticker.detach();//Stop interrupt detection
+//                cout<<src<<ENDL;
+//                }
+        
+        
+     }
+     
+     else if(k>=(sizeof(data)) ){
+            k=0;
+            j=1;
+            p=0;  
+//            cout<<count<<" "<<sizeof(data)/112<<ENDL;   
+         }
+     
+     
+    CS=0;
+    adf.write(0x0B);//IRQ_SOURCE_Random write //To read CMD = 2B 36 FF FF
+    adf.write(0x36);//Address : 0x336
+    adf.write(0xFF);//Put Low
+    CS=1;
+    wait_us(80);
+    CS=0;
+     
+//     a[r]=t.read_us();
+     j++;
+//     r++;
+     CS=1;
+     wait_us(1);
+}     
+     
+void Interrupt(){
+    x = IRQ;
+//    intcount++;
+    if(x)
+    {
+        write_data();
+        }
+    }        
+    
+    
+    
+    
+void send_data(void){
+        
+    CS=0;
+    adf.write(CMD_CONFIG_DEV);
+    CS=1;
+    wait_ms(2); 
+    
+    CS=0;
+    adf.write(0xFF);
+    adf.write(0xFF);
+    CS=1;
+    wait_ms(2); 
+    
+    write_data();
+    
+    CS=0;
+    adf.write(0xB1);
+    CS=1;
+    wait_ms(2); 
+    assrt_phy_on(0,0,0);
+    
+    CS=0;
+    adf.write(0xFF);
+    adf.write(0xFF);
+    CS=1;
+    wait_ms(2);  
+    
+    CS=0;
+    adf.write(CMD_PHY_TX);
+    CS=1;
+    wait_ms(2);  
+    assrt_phy_tx(0,0,0);
+    
+    CS=0;
+    adf.write(0xFF);
+    adf.write(0xFF);
+    CS=1;
+    wait_ms(2);     
+    }
+/*******ADDITIONAL STUFF****************/    
+unsigned char check_status(void){
+    CS=0;
+    adf.write(0xFF);
+    unsigned char stat = adf.write(0xFF);
+    CS=1;
+    return stat;
+    }
+/*****************************************/
+bool assrt_phy_off(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
+    status=check_status();
+        if(status==0xB1){
+            return 0;
+        }
+        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
+            return 1;//You need to Reset the hardware
+        }
+        else if(state_err_cnt>STATE_ERR_THRS){
+            return 1;//Again reset the hardware
+        }
+        else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
+            CS=0;
+            adf.write(CMD_PHY_OFF);        //CMD_PHY_OFF=0xB0
+            CS=1;
+            wait_us(PHY_OFF_EXEC_TIME);// Typical = 24us  We are giving 300us 
+            return assrt_phy_off(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);
+        }
+        else if(status&0x80==0x00){
+            wait_ms(5);
+            //Error: SPI=0 Not ready CMD= Dont care
+            return assrt_phy_off(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);
+        }
+        else {//if(status&0xA0==0x80){
+            wait_ms(1);
+            //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
+            return assrt_phy_off(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);
+        }
+}
+/*****************************/
+bool assrt_phy_on(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
+    status=check_status();
+        if((status&0x1F)==0xB2){
+            return 0;
+        }
+        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
+            return 1;//You need to Reset the hardware
+        }
+        else if(state_err_cnt>STATE_ERR_THRS){
+            return 1;//Again reset the hardware
+        }
+        else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
+            CS=0;
+            adf.write(0xB1);        //CMD_PHY_OFF
+            CS=1;
+            wait_us(PHY_ON_EXEC_TIME);// Typical = 24us  We are giving 300us 
+            return assrt_phy_on(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);
+        }
+        else if(status&0x80==0x00){
+            wait_ms(5);
+            //Error: SPI=0 Not ready CMD= Dont care
+            return assrt_phy_on(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);
+        }
+        else if(status&0xA0==0x80){
+            wait_ms(1);
+            //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
+            return assrt_phy_on(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);
+        }
+}
+ /*****************************/
+ bool assrt_phy_tx(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
+    status=check_status();
+        if((status & 0x1F) == 0xB4){
+            return 0;
+        }
+        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
+            return 1;//You need to Reset the hardware
+        }
+        else if(state_err_cnt>STATE_ERR_THRS){
+            return 1;//Again reset the hardware
+        }
+        else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
+            CS=0;
+            adf.write(0xB1);        //CMD_PHY_OFF
+            CS=1;
+            wait_us(PHY_TX_EXEC_TIME);// Typical = 24us  We are giving 300us 
+            return assrt_phy_tx(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);
+        }
+        else if(status&0x80==0x00){
+            wait_ms(1);
+            //Error: SPI=0 Not ready CMD= Dont care
+            return assrt_phy_tx(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);
+        }
+        else if(status&0xA0==0x80){
+            wait_us(50);
+            //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
+            return assrt_phy_tx(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);
+        }
+}
+
+/***************************/
+bool hardware_reset(int bcn_call){
+    if (bcn_call>20){//Worst Case 20seconds will be lost !
+        return 1;
+    }
+    int count=0;
+    CS=0;
+    adf.write(CMD_HW_RESET);
+    CS=1;
+    wait_ms(2);// Typically 1 ms
+    while(count<1000 && miso_err_cnt<1000){      
+        if(MISO_PIN){
+            
+            if(!assrt_phy_off(0,0,0)){
+                break;
+            }
+            count++;
+        }
+        else{
+            wait_us(50);
+            miso_err_cnt++;
+        }
+    }
+    if(count==1000 ||miso_err_cnt==1000){
+        return hardware_reset(bcn_call+1);  
+    }
+    else
+        return 0;
+    
+}
+
+
+/*******************************************/
+int main()
+{   
+    adf.format(8,0);
+    adf.frequency(1000000);
+    /**ADDED On 21/10/2015 ***/
+    /******************/
+    /***********/
+   bool state_0=0;
+    adf.write(CMD_PHY_OFF); //0xB0
+    while(hw_reset_err_cnt<2){
+        
+        if(!assrt_phy_off(0,0,0)){  //assrt_phy_off () returns 0 if state is PHY_OFF , returns 1 if couldn't go to PHY_OFF
+            bbram_write();
+            bbram_flag=1;
+            break;
+        }
+        else{
+            hardware_reset(0);  // Asserts Hardware for 20sec(20times). PHY_OFF for 20,000 times
+        }
+    }
+    state_0=assrt_phy_off(0,0,0);// We actually do not need this but make sure "we do not need this"
+    if(!bbram_flag){
+        //Switch to beacon
+        bcn_flag=1;
+    }
+    initiate();
+    send_data();
+    cout<<"Work"<<ENDL;
+    CS=0;
+    adf.write(0x0B);//IRQ_SOURCE_Random write //To read CMD = 2B 36 FF FF  
+    adf.write(0x36);//Address : 0x336
+    adf.write(0xFF);//Put Low Clearing the MCR Value of the Source Register
+    CS=1;
+    
+    ticker.attach_us(&Interrupt,32000);  
+} 
\ No newline at end of file