bcn code modification for new short bcn format

Dependencies:   mbed

Fork of BEACON_CODE by pradeep shekhar

Committer:
azaddevarm
Date:
Tue May 17 15:13:49 2016 +0000
Revision:
26:e5d6c0cffa82
Parent:
25:90cdfbadea91
Modifying bcn as per new format

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shekhar 18:3d8e70286f67 1 #include "BCN.h"
shekhar 24:ca96cddf25d0 2 #include <stdio.h>
shekhar 24:ca96cddf25d0 3 #include "pin_config.h"
shekhar 17:afadd6b988a2 4 //Check the pin names
shekhar 17:afadd6b988a2 5 //Takes max 4.3 sec in void FCTN_BCN_TX_MAIN() (temp.calc. + long_beacon + short_beacon)
shekhar 17:afadd6b988a2 6
shekhar 24:ca96cddf25d0 7 Serial pc_bcn(USBTX, USBRX); //tx,rx
azaddevarm 26:e5d6c0cffa82 8 SPI spi(D11, D12, D13); // mosi, miso, sclk
azaddevarm 26:e5d6c0cffa82 9 DigitalOut cs(D10); //slave select or chip select
azaddevarm 26:e5d6c0cffa82 10 //SPI spi(PIN16, PIN17, PIN15); // mosi, miso, sclk
azaddevarm 26:e5d6c0cffa82 11 //DigitalOut cs(PIN6); //slave select or chip select
shekhar 24:ca96cddf25d0 12 Timer t_i;//timer for checking the time taken by (temp.calc. + long_beacon + short_beacon)
azaddevarm 26:e5d6c0cffa82 13 //Timer t_is;//timer for short_beacon
azaddevarm 26:e5d6c0cffa82 14 //Timer t_il;//timer for long_beacon
shekhar 24:ca96cddf25d0 15 Timeout rf_sl_timeout;//RF_silence timer
shekhar 24:ca96cddf25d0 16 Ticker loop;//for transmitting every 10 secs
shekhar 17:afadd6b988a2 17
shekhar 17:afadd6b988a2 18 //GLOBAL VARIABLES
shekhar 17:afadd6b988a2 19 uint8_t BCN_INIT_STATUS = 0;
shekhar 17:afadd6b988a2 20 uint8_t BCN_TX_MAIN_STATUS = 0;
shekhar 17:afadd6b988a2 21 uint8_t BCN_TX_STATUS = 0;
shekhar 17:afadd6b988a2 22 uint8_t BCN_TX_EN = 1; //hardcoding for now //check where is this variable toggled??
shekhar 19:6863457ea3d1 23 uint8_t BCN_FEN = 0; //hardcoding for now //write this value to flash
shekhar 17:afadd6b988a2 24 uint8_t BCN_STANDBY = 0; //hardcoding for now //check where is this variable toggled??
shekhar 25:90cdfbadea91 25 uint8_t BCN_TS_BUFFER = 0; // For Temperature
shekhar 25:90cdfbadea91 26 uint8_t ERROR_CHECK = 0;
shekhar 25:90cdfbadea91 27 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)
shekhar 25:90cdfbadea91 28 //This Flag when exceeds a threshold, uC should reset.
shekhar 17:afadd6b988a2 29 void FCTN_BCN_INIT()
shekhar 17:afadd6b988a2 30 {
shekhar 24:ca96cddf25d0 31 pc_bcn.printf("FCTN_BCN_INIT\n");
shekhar 24:ca96cddf25d0 32 Init_BEACON_HW();
shekhar 17:afadd6b988a2 33 BCN_INIT_STATUS = 1;
shekhar 24:ca96cddf25d0 34 if(BCN_FEN == 0)//BCN_FEN is in flash
shekhar 24:ca96cddf25d0 35 rf_sl_timeout.attach(&FCTN_BCN_FEN, RF_SILENCE_TIME);
shekhar 17:afadd6b988a2 36 BCN_INIT_STATUS = 0;
shekhar 17:afadd6b988a2 37 }
shekhar 17:afadd6b988a2 38 void FCTN_BCN_FEN()
shekhar 17:afadd6b988a2 39 {
shekhar 24:ca96cddf25d0 40 pc_bcn.printf("FCTN_FEN\n");
shekhar 17:afadd6b988a2 41 BCN_FEN = 1;//write this value to flash
shekhar 17:afadd6b988a2 42 }
shekhar 17:afadd6b988a2 43 void FCTN_BCN_TX_MAIN()
shekhar 17:afadd6b988a2 44 {
shekhar 25:90cdfbadea91 45 ERROR_CHECK=0;
shekhar 24:ca96cddf25d0 46 pc_bcn.printf("FCTN_BCN_TX_MAIN\n");
shekhar 17:afadd6b988a2 47 t_i.start();
shekhar 17:afadd6b988a2 48 int begin = t_i.read_us();
azaddevarm 26:e5d6c0cffa82 49 int begins,beginl,ends,endl;
shekhar 17:afadd6b988a2 50 BCN_TX_MAIN_STATUS = 1;
shekhar 17:afadd6b988a2 51
shekhar 17:afadd6b988a2 52 if(BCN_FEN == 1)
shekhar 17:afadd6b988a2 53 {
shekhar 17:afadd6b988a2 54 if(BCN_TX_EN == 1)
shekhar 17:afadd6b988a2 55 {
shekhar 17:afadd6b988a2 56 //Measure and store BCN temperature in BCN_TS_BUFFER
shekhar 17:afadd6b988a2 57 BCN_TS_BUFFER = check_Temperature();
shekhar 24:ca96cddf25d0 58 pc_bcn.printf("\n\ntemperature = %d\n\n",BCN_TS_BUFFER);
shekhar 17:afadd6b988a2 59 //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER
shekhar 17:afadd6b988a2 60 if(BCN_STANDBY == 1 )
shekhar 17:afadd6b988a2 61 {
shekhar 17:afadd6b988a2 62 Set_BCN_TX_STATUS(BCN_TX_STANDBY);
shekhar 17:afadd6b988a2 63 BCN_TX_MAIN_STATUS = 0;
shekhar 17:afadd6b988a2 64
shekhar 17:afadd6b988a2 65 // break;
shekhar 17:afadd6b988a2 66 }
shekhar 17:afadd6b988a2 67 else
shekhar 17:afadd6b988a2 68 {
shekhar 17:afadd6b988a2 69 //transmit short beacon and long beacon
azaddevarm 26:e5d6c0cffa82 70 //t_is.start();
azaddevarm 26:e5d6c0cffa82 71 begins = t_i.read_us();
shekhar 17:afadd6b988a2 72 SHORT_BCN_TX();
azaddevarm 26:e5d6c0cffa82 73 //t_is.stop();
azaddevarm 26:e5d6c0cffa82 74 ends = t_i.read_us();
azaddevarm 26:e5d6c0cffa82 75
azaddevarm 26:e5d6c0cffa82 76 //t_il.start();
azaddevarm 26:e5d6c0cffa82 77 beginl = t_i.read_us();
shekhar 23:a864d57f10c4 78 LONG_BCN_TX();
azaddevarm 26:e5d6c0cffa82 79 //t_il.stop();
azaddevarm 26:e5d6c0cffa82 80 endl = t_i.read_us();
shekhar 17:afadd6b988a2 81
shekhar 17:afadd6b988a2 82 if(Check_ACK_RECEIVED() == 1)
shekhar 17:afadd6b988a2 83 {
shekhar 17:afadd6b988a2 84 Set_BCN_TX_STATUS(BCN_TX_SUCCESS);
shekhar 17:afadd6b988a2 85 BCN_TX_MAIN_STATUS = 0;
shekhar 17:afadd6b988a2 86 }
shekhar 17:afadd6b988a2 87 else
shekhar 17:afadd6b988a2 88 {
shekhar 17:afadd6b988a2 89 Set_BCN_TX_STATUS(BCN_TX_FAILURE);
shekhar 17:afadd6b988a2 90 BCN_TX_MAIN_STATUS = 0;
shekhar 17:afadd6b988a2 91
shekhar 17:afadd6b988a2 92 }
shekhar 17:afadd6b988a2 93 }
shekhar 17:afadd6b988a2 94 }
shekhar 17:afadd6b988a2 95 else
shekhar 17:afadd6b988a2 96 {
shekhar 17:afadd6b988a2 97 Set_BCN_TX_STATUS(BCN_TX_DISABLED);
shekhar 17:afadd6b988a2 98 BCN_TX_MAIN_STATUS = 0;
shekhar 17:afadd6b988a2 99 }
shekhar 17:afadd6b988a2 100 }
shekhar 17:afadd6b988a2 101 else
shekhar 17:afadd6b988a2 102 {
shekhar 17:afadd6b988a2 103 Set_BCN_TX_STATUS(BCN_RF_SILENCE); //Window of RF Silence: None of the Txs should be on.
shekhar 17:afadd6b988a2 104 BCN_TX_MAIN_STATUS = 0;
shekhar 17:afadd6b988a2 105 }
shekhar 17:afadd6b988a2 106 t_i.stop();
shekhar 17:afadd6b988a2 107 int end = t_i.read_us();
shekhar 24:ca96cddf25d0 108 pc_bcn.printf("The time required for FCTN_BCN_TX_MAIN is %d useconds\r\n", end-begin);
azaddevarm 26:e5d6c0cffa82 109 pc_bcn.printf("The time required for Short_BCN is %d useconds\r\n", ends-begins);
azaddevarm 26:e5d6c0cffa82 110 pc_bcn.printf("The time required for Long_BCN is %d useconds\r\n", endl-beginl);
shekhar 25:90cdfbadea91 111 ERROR_CHECK = 0;
shekhar 17:afadd6b988a2 112
shekhar 17:afadd6b988a2 113 }
shekhar 17:afadd6b988a2 114
shekhar 17:afadd6b988a2 115 void Set_BCN_TX_STATUS(uint8_t STATUS)
shekhar 17:afadd6b988a2 116 {
shekhar 17:afadd6b988a2 117 BCN_TX_STATUS = STATUS;
shekhar 17:afadd6b988a2 118 }
shekhar 17:afadd6b988a2 119
shekhar 17:afadd6b988a2 120 uint8_t check_Temperature()
shekhar 17:afadd6b988a2 121 {
shekhar 17:afadd6b988a2 122 uint8_t temperature;
shekhar 24:ca96cddf25d0 123 writereg(RF22_REG_0F_ADC_CONFIGURATION,0x00);//set ADC to temp measurement
shekhar 24:ca96cddf25d0 124 writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20);//measure in degree celsius
shekhar 24:ca96cddf25d0 125 writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80);//start adc
shekhar 17:afadd6b988a2 126 wait(0.1);
shekhar 17:afadd6b988a2 127 temperature = readreg(RF22_REG_11_ADC_Value);
shekhar 17:afadd6b988a2 128 temperature = (float)temperature*0.5 - 64;
shekhar 17:afadd6b988a2 129 return temperature;
shekhar 17:afadd6b988a2 130 }
shekhar 17:afadd6b988a2 131
shekhar 17:afadd6b988a2 132 void SHORT_BCN_TX()
shekhar 17:afadd6b988a2 133 {
azaddevarm 26:e5d6c0cffa82 134 /*
shekhar 17:afadd6b988a2 135 writereg(RF22_REG_6E_TX_DATA_RATE,0x01);
shekhar 20:89b6bb0859a1 136 writereg(RF22_REG_6F_TX_DATA_RATE,0x50);//160bps
azaddevarm 26:e5d6c0cffa82 137 */
azaddevarm 26:e5d6c0cffa82 138 writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
azaddevarm 26:e5d6c0cffa82 139 writereg(RF22_REG_6F_TX_DATA_RATE,0x7D);//1280bps
azaddevarm 26:e5d6c0cffa82 140
shekhar 17:afadd6b988a2 141 writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length
shekhar 17:afadd6b988a2 142 wait(0.02);
shekhar 17:afadd6b988a2 143
shekhar 17:afadd6b988a2 144 uint32_t timeout_count = 10e5;
shekhar 17:afadd6b988a2 145 //extract values from short_beacon[]
shekhar 17:afadd6b988a2 146
shekhar 17:afadd6b988a2 147 struct Short_beacon
shekhar 17:afadd6b988a2 148 {
shekhar 17:afadd6b988a2 149 uint8_t Voltage[1];
shekhar 17:afadd6b988a2 150 uint8_t AngularSpeed[2];
shekhar 17:afadd6b988a2 151 uint8_t SubsystemStatus[1];
shekhar 17:afadd6b988a2 152 uint8_t Temp[3];
shekhar 17:afadd6b988a2 153 uint8_t ErrorFlag[1];
shekhar 17:afadd6b988a2 154 }Shortbeacon = { {0x88}, {0x99, 0xAA} , {0xAA},{0xAA,0xDD,0xEE}, {0x00} };
shekhar 17:afadd6b988a2 155
shekhar 17:afadd6b988a2 156 //filling hk data
shekhar 17:afadd6b988a2 157 //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]};
shekhar 17:afadd6b988a2 158 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]};
shekhar 17:afadd6b988a2 159
shekhar 17:afadd6b988a2 160 //writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode ??
azaddevarm 26:e5d6c0cffa82 161 clearTxBuf(); //writing data first time
azaddevarm 26:e5d6c0cffa82 162
azaddevarm 26:e5d6c0cffa82 163 int byte = 0,byte_count=0;
azaddevarm 26:e5d6c0cffa82 164 //int spi_start,spi_end,spi_total=0;
shekhar 17:afadd6b988a2 165 for (int byte_counter = 0; byte_counter <15 ; byte_counter++)
shekhar 17:afadd6b988a2 166 {
azaddevarm 26:e5d6c0cffa82 167 //pc_bcn.printf("byte-counter=%d\r\n", byte_counter);
azaddevarm 26:e5d6c0cffa82 168 /*
shekhar 17:afadd6b988a2 169 for(int j = 3; j >= 0 ; j--)
shekhar 17:afadd6b988a2 170 {
shekhar 17:afadd6b988a2 171 if((short_beacon[byte_counter] & (uint8_t) pow(2.0,(j*2+1)))!= pow(2.0,(j*2+1)))
shekhar 17:afadd6b988a2 172 {
shekhar 17:afadd6b988a2 173 byte=0x00;
shekhar 17:afadd6b988a2 174 }
shekhar 17:afadd6b988a2 175 else
shekhar 17:afadd6b988a2 176 {
shekhar 17:afadd6b988a2 177 byte=0xF0;
shekhar 17:afadd6b988a2 178 }
shekhar 17:afadd6b988a2 179 if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j*2))!= pow(2.0,j*2))
shekhar 17:afadd6b988a2 180 {
shekhar 17:afadd6b988a2 181 byte=byte | 0x00;
shekhar 17:afadd6b988a2 182 }
shekhar 17:afadd6b988a2 183 else
shekhar 17:afadd6b988a2 184 {
shekhar 17:afadd6b988a2 185 byte=byte | 0x0F;
shekhar 17:afadd6b988a2 186 }
azaddevarm 26:e5d6c0cffa82 187 cs = 0;
azaddevarm 26:e5d6c0cffa82 188 spi.write(0xFF);
shekhar 17:afadd6b988a2 189 spi.write(byte);
azaddevarm 26:e5d6c0cffa82 190 cs = 1;
shekhar 17:afadd6b988a2 191 }
azaddevarm 26:e5d6c0cffa82 192 */
azaddevarm 26:e5d6c0cffa82 193 for(int j = 7; j >= 0 ; j--)
azaddevarm 26:e5d6c0cffa82 194 {
azaddevarm 26:e5d6c0cffa82 195 //spi_start = t_i.read_us();
azaddevarm 26:e5d6c0cffa82 196 if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j))!= pow(2.0,j))
azaddevarm 26:e5d6c0cffa82 197 {
azaddevarm 26:e5d6c0cffa82 198 byte=0x00;
azaddevarm 26:e5d6c0cffa82 199 }
azaddevarm 26:e5d6c0cffa82 200 else
azaddevarm 26:e5d6c0cffa82 201 {
azaddevarm 26:e5d6c0cffa82 202 byte=0xFF;
azaddevarm 26:e5d6c0cffa82 203 }
azaddevarm 26:e5d6c0cffa82 204
azaddevarm 26:e5d6c0cffa82 205 cs = 0;
azaddevarm 26:e5d6c0cffa82 206 spi.write(0xFF);
azaddevarm 26:e5d6c0cffa82 207 spi.write(byte);
azaddevarm 26:e5d6c0cffa82 208 spi.write(byte);
azaddevarm 26:e5d6c0cffa82 209 spi.write(byte);
azaddevarm 26:e5d6c0cffa82 210 spi.write(byte); //Each bit is written 32 times
azaddevarm 26:e5d6c0cffa82 211 byte_count+=4;
azaddevarm 26:e5d6c0cffa82 212 cs = 1;
azaddevarm 26:e5d6c0cffa82 213
azaddevarm 26:e5d6c0cffa82 214 //spi_end = t_i.read_us();
azaddevarm 26:e5d6c0cffa82 215 //spi_total += spi_end-spi_start;
azaddevarm 26:e5d6c0cffa82 216 //pc_bcn.printf("Time taken to write one byte = %d\r\n",spi_end-spi_start);
azaddevarm 26:e5d6c0cffa82 217 }
azaddevarm 26:e5d6c0cffa82 218
azaddevarm 26:e5d6c0cffa82 219 if(byte_counter % 2 == 1)
azaddevarm 26:e5d6c0cffa82 220 {
azaddevarm 26:e5d6c0cffa82 221
azaddevarm 26:e5d6c0cffa82 222 //Set to Tx mode
azaddevarm 26:e5d6c0cffa82 223 writereg(RF22_REG_07_OPERATING_MODE1,0x08); //txon
azaddevarm 26:e5d6c0cffa82 224 wait(0.1);// takes time to set to tx mode hence the delay of 0.1.
azaddevarm 26:e5d6c0cffa82 225
azaddevarm 26:e5d6c0cffa82 226 //Check for fifoThresh
azaddevarm 26:e5d6c0cffa82 227 while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1)
azaddevarm 26:e5d6c0cffa82 228 {
azaddevarm 26:e5d6c0cffa82 229 pc_bcn.printf("Reset\n");
azaddevarm 26:e5d6c0cffa82 230 reset_rfm(1);}
azaddevarm 26:e5d6c0cffa82 231 timeout_count=10e5;
azaddevarm 26:e5d6c0cffa82 232
azaddevarm 26:e5d6c0cffa82 233 }
azaddevarm 26:e5d6c0cffa82 234
shekhar 17:afadd6b988a2 235 }
azaddevarm 26:e5d6c0cffa82 236
shekhar 17:afadd6b988a2 237 //Set to Tx mode
shekhar 24:ca96cddf25d0 238 writereg(RF22_REG_07_OPERATING_MODE1,0x08); //txon
shekhar 24:ca96cddf25d0 239 wait(0.1);// takes time to set to tx mode hence the delay of 0.1.
shekhar 24:ca96cddf25d0 240
shekhar 17:afadd6b988a2 241 //Check for fifoThresh
azaddevarm 26:e5d6c0cffa82 242 while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1)
azaddevarm 26:e5d6c0cffa82 243 {
azaddevarm 26:e5d6c0cffa82 244 pc_bcn.printf("Reset\n");
azaddevarm 26:e5d6c0cffa82 245 reset_rfm(1);}
shekhar 17:afadd6b988a2 246 timeout_count=10e5;
shekhar 17:afadd6b988a2 247
azaddevarm 26:e5d6c0cffa82 248 //Check for packetsent interrupt
shekhar 25:90cdfbadea91 249 while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;else if(timeout_count == 1) reset_rfm(1);
shekhar 17:afadd6b988a2 250
azaddevarm 26:e5d6c0cffa82 251 pc_bcn.printf("Short packet sent, bytes = %d\r\n",byte_count);
shekhar 17:afadd6b988a2 252
shekhar 17:afadd6b988a2 253 writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode
shekhar 17:afadd6b988a2 254
shekhar 17:afadd6b988a2 255 }
shekhar 17:afadd6b988a2 256 void LONG_BCN_TX()
shekhar 17:afadd6b988a2 257 {
azaddevarm 26:e5d6c0cffa82 258 /*
shekhar 17:afadd6b988a2 259 writereg(RF22_REG_6E_TX_DATA_RATE,0x04);
shekhar 17:afadd6b988a2 260 writereg(RF22_REG_6F_TX_DATA_RATE,0xEA);//600 bps
azaddevarm 26:e5d6c0cffa82 261 */
shekhar 17:afadd6b988a2 262 writereg(RF22_REG_3E_PACKET_LENGTH,LONG_TX_DATA); //long packet length
shekhar 17:afadd6b988a2 263 wait(0.02);
shekhar 17:afadd6b988a2 264 uint32_t timeout_count=10e5;
shekhar 17:afadd6b988a2 265
shekhar 17:afadd6b988a2 266 //get long_beacon array
shekhar 17:afadd6b988a2 267
azaddevarm 26:e5d6c0cffa82 268 uint8_t Long_beacon[LONG_TX_DATA];
azaddevarm 26:e5d6c0cffa82 269 for(int i = 0;i<LONG_TX_DATA;i++)
shekhar 17:afadd6b988a2 270 {
shekhar 17:afadd6b988a2 271 Long_beacon[i] = 0xA0;
shekhar 17:afadd6b988a2 272 }
shekhar 17:afadd6b988a2 273
shekhar 17:afadd6b988a2 274 clearTxBuf();
shekhar 17:afadd6b988a2 275 //writing data first time
shekhar 17:afadd6b988a2 276 cs = 0;
shekhar 17:afadd6b988a2 277 spi.write(0xFF);
azaddevarm 26:e5d6c0cffa82 278 for(int i=0; i<64;i++)
shekhar 17:afadd6b988a2 279 {
shekhar 17:afadd6b988a2 280 spi.write(Long_beacon[i]);
shekhar 17:afadd6b988a2 281 }
shekhar 17:afadd6b988a2 282 cs = 1;
shekhar 17:afadd6b988a2 283
shekhar 17:afadd6b988a2 284 //Set to Tx mode
shekhar 23:a864d57f10c4 285 writereg(RF22_REG_07_OPERATING_MODE1,0x08);
shekhar 24:ca96cddf25d0 286 wait(0.1);//necessary
shekhar 17:afadd6b988a2 287
shekhar 17:afadd6b988a2 288 //Check for fifoThresh
azaddevarm 26:e5d6c0cffa82 289 while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1)
azaddevarm 26:e5d6c0cffa82 290 {
azaddevarm 26:e5d6c0cffa82 291 pc_bcn.printf("Reset\n");
azaddevarm 26:e5d6c0cffa82 292 reset_rfm(1);}
shekhar 17:afadd6b988a2 293 timeout_count=10e5;
shekhar 17:afadd6b988a2 294
shekhar 17:afadd6b988a2 295 cs = 0;
shekhar 17:afadd6b988a2 296 spi.write(0xFF);
azaddevarm 26:e5d6c0cffa82 297 for(int i=64; i<127;i++)
shekhar 17:afadd6b988a2 298 {
shekhar 17:afadd6b988a2 299 spi.write(Long_beacon[i]);
shekhar 17:afadd6b988a2 300 }
shekhar 17:afadd6b988a2 301 cs = 1;
shekhar 17:afadd6b988a2 302 wait(0.1);
shekhar 17:afadd6b988a2 303 //Check for fifoThresh
azaddevarm 26:e5d6c0cffa82 304 while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;else if(timeout_count == 1)
azaddevarm 26:e5d6c0cffa82 305 {
azaddevarm 26:e5d6c0cffa82 306 pc_bcn.printf("Reset\n");
azaddevarm 26:e5d6c0cffa82 307 reset_rfm(1);}
shekhar 17:afadd6b988a2 308 timeout_count=10e5;
shekhar 17:afadd6b988a2 309
shekhar 17:afadd6b988a2 310 //Check for packetsent interrupt
shekhar 25:90cdfbadea91 311 while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;else if(timeout_count == 1) reset_rfm(1);
shekhar 17:afadd6b988a2 312
shekhar 24:ca96cddf25d0 313 //pc_bcn.printf("Long packet sent\r\n");
shekhar 17:afadd6b988a2 314
shekhar 17:afadd6b988a2 315 writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode
shekhar 17:afadd6b988a2 316 }
shekhar 25:90cdfbadea91 317 void reset_rfm(uint8_t fl)
shekhar 17:afadd6b988a2 318 {
shekhar 25:90cdfbadea91 319 if (fl ==1 && ERROR_CHECK ==0)
shekhar 25:90cdfbadea91 320 {BCN_FAIL_COUNT++;
shekhar 25:90cdfbadea91 321 ERROR_CHECK=1;
shekhar 25:90cdfbadea91 322 printf("BCN_FAIL_COUNT++\n");}
shekhar 25:90cdfbadea91 323 else if(fl == 0)
shekhar 25:90cdfbadea91 324 {BCN_FAIL_COUNT = 0;
shekhar 25:90cdfbadea91 325 ERROR_CHECK=0;}
shekhar 17:afadd6b988a2 326 }
shekhar 17:afadd6b988a2 327 void writereg(uint8_t reg,uint8_t val)
shekhar 17:afadd6b988a2 328 {
shekhar 25:90cdfbadea91 329
shekhar 17:afadd6b988a2 330 uint8_t count = 0;
shekhar 17:afadd6b988a2 331 for(;;count++)
shekhar 17:afadd6b988a2 332 {
shekhar 17:afadd6b988a2 333 int read_val =0; cs = 0;spi.write(reg | 0x80);spi.write(val);cs = 1;
shekhar 17:afadd6b988a2 334 if(reg != 0x7 && reg != 0x58 && reg != 0xF)
shekhar 17:afadd6b988a2 335 {
shekhar 17:afadd6b988a2 336 read_val = readreg(reg);
shekhar 17:afadd6b988a2 337 if (read_val == val)
shekhar 17:afadd6b988a2 338 {
shekhar 17:afadd6b988a2 339 break;
shekhar 17:afadd6b988a2 340 }
shekhar 17:afadd6b988a2 341 else if(count == 5)
shekhar 17:afadd6b988a2 342 {
shekhar 25:90cdfbadea91 343 reset_rfm(1); printf("reg = 0x%X\n",reg);break;
shekhar 17:afadd6b988a2 344 }
shekhar 25:90cdfbadea91 345 else init_spi();
shekhar 17:afadd6b988a2 346 }
shekhar 17:afadd6b988a2 347 else
shekhar 17:afadd6b988a2 348 break;
shekhar 17:afadd6b988a2 349 }
shekhar 17:afadd6b988a2 350 }
shekhar 17:afadd6b988a2 351 uint8_t readreg(uint8_t reg)
shekhar 17:afadd6b988a2 352 {
shekhar 17:afadd6b988a2 353 uint8_t val;cs = 0;spi.write(reg & ~0x80);val = spi.write(0);cs = 1;return val;
shekhar 17:afadd6b988a2 354 }
shekhar 17:afadd6b988a2 355 void clearTxBuf()
shekhar 17:afadd6b988a2 356 {
shekhar 17:afadd6b988a2 357 writereg(RF22_REG_08_OPERATING_MODE2,0x01);
shekhar 17:afadd6b988a2 358 writereg(RF22_REG_08_OPERATING_MODE2,0x00);
shekhar 17:afadd6b988a2 359 }
shekhar 23:a864d57f10c4 360 uint8_t setFrequency(double centre)
shekhar 17:afadd6b988a2 361 {
shekhar 17:afadd6b988a2 362 uint8_t fbsel = 0x40;
shekhar 17:afadd6b988a2 363 if (centre >= 480.0) {
shekhar 17:afadd6b988a2 364 centre /= 2;
shekhar 17:afadd6b988a2 365 fbsel |= 0x20;
shekhar 23:a864d57f10c4 366 }
shekhar 17:afadd6b988a2 367 centre /= 10.0;
shekhar 17:afadd6b988a2 368 double integerPart = floor(centre);
shekhar 17:afadd6b988a2 369 double fractionalPart = centre - integerPart;
shekhar 17:afadd6b988a2 370
shekhar 17:afadd6b988a2 371 uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23
shekhar 17:afadd6b988a2 372 fbsel |= fb;
shekhar 17:afadd6b988a2 373 uint16_t fc = fractionalPart * 64000;
shekhar 17:afadd6b988a2 374 writereg(RF22_REG_73_FREQUENCY_OFFSET1, 0); // REVISIT
shekhar 17:afadd6b988a2 375 writereg(RF22_REG_74_FREQUENCY_OFFSET2, 0);
shekhar 17:afadd6b988a2 376 writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel);
shekhar 17:afadd6b988a2 377 writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8);
shekhar 17:afadd6b988a2 378 writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff);
shekhar 17:afadd6b988a2 379 return 0;
shekhar 17:afadd6b988a2 380 }
shekhar 25:90cdfbadea91 381 void init_spi()
shekhar 17:afadd6b988a2 382 {
shekhar 17:afadd6b988a2 383 cs=1; // chip must be deselected
shekhar 17:afadd6b988a2 384 wait(0.1);
shekhar 17:afadd6b988a2 385 spi.format(8,0);
shekhar 17:afadd6b988a2 386 spi.frequency(10000000); //10MHz SCLK
shekhar 25:90cdfbadea91 387 }
shekhar 25:90cdfbadea91 388 void Init_BEACON_HW()
shekhar 25:90cdfbadea91 389 {
shekhar 25:90cdfbadea91 390 ERROR_CHECK=0;
shekhar 25:90cdfbadea91 391 wait(0.1);
shekhar 25:90cdfbadea91 392 int i = 5;
shekhar 25:90cdfbadea91 393 while(i--)
shekhar 25:90cdfbadea91 394 {
shekhar 25:90cdfbadea91 395 init_spi();
shekhar 17:afadd6b988a2 396
shekhar 17:afadd6b988a2 397 //should either have a flag for invalid SPI or discard this for actual case or add reset
shekhar 17:afadd6b988a2 398 if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08)
shekhar 25:90cdfbadea91 399 {pc_bcn.printf("spi connection valid\r\n");
shekhar 25:90cdfbadea91 400 reset_rfm(0);break;}
shekhar 25:90cdfbadea91 401 else if (i == 1)
shekhar 24:ca96cddf25d0 402 {pc_bcn.printf("error in spi connection\r\n");
shekhar 25:90cdfbadea91 403 reset_rfm(1);
shekhar 25:90cdfbadea91 404 return;
shekhar 17:afadd6b988a2 405 }
shekhar 25:90cdfbadea91 406 }
shekhar 17:afadd6b988a2 407
shekhar 17:afadd6b988a2 408 writereg(RF22_REG_07_OPERATING_MODE1,0x80); //sw_reset
shekhar 17:afadd6b988a2 409 wait(0.1); //takes time to reset
shekhar 17:afadd6b988a2 410
shekhar 17:afadd6b988a2 411 clearTxBuf();
shekhar 17:afadd6b988a2 412
shekhar 17:afadd6b988a2 413 writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode
shekhar 17:afadd6b988a2 414
shekhar 17:afadd6b988a2 415 //txfifoalmostempty
shekhar 17:afadd6b988a2 416 writereg(RF22_REG_7D_TX_FIFO_CONTROL2,30);
shekhar 17:afadd6b988a2 417
shekhar 17:afadd6b988a2 418 //Packet-engine registers
shekhar 17:afadd6b988a2 419 writereg(RF22_REG_30_DATA_ACCESS_CONTROL,0x00);
shekhar 17:afadd6b988a2 420
shekhar 17:afadd6b988a2 421 writereg(RF22_REG_33_HEADER_CONTROL2,0x08);
shekhar 17:afadd6b988a2 422 writereg(RF22_REG_34_PREAMBLE_LENGTH,0x00);
shekhar 17:afadd6b988a2 423
shekhar 17:afadd6b988a2 424 writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state
shekhar 17:afadd6b988a2 425 writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state
shekhar 17:afadd6b988a2 426
shekhar 23:a864d57f10c4 427 setFrequency(435.0);
shekhar 25:90cdfbadea91 428
shekhar 25:90cdfbadea91 429 i=3;
shekhar 25:90cdfbadea91 430 while(i--)
shekhar 17:afadd6b988a2 431 if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00)
shekhar 17:afadd6b988a2 432 {
shekhar 25:90cdfbadea91 433 setFrequency(435.0);
shekhar 25:90cdfbadea91 434 if (i==1)
shekhar 25:90cdfbadea91 435 {pc_bcn.printf("frequency not set properly\r\n");
shekhar 25:90cdfbadea91 436 reset_rfm(1);}
shekhar 17:afadd6b988a2 437 }
shekhar 17:afadd6b988a2 438
shekhar 17:afadd6b988a2 439 //set Modem Configuration
shekhar 17:afadd6b988a2 440 writereg(RF22_REG_58,0x80);
shekhar 17:afadd6b988a2 441
azaddevarm 26:e5d6c0cffa82 442 //Set Data rate - same for both long and short beacon
azaddevarm 26:e5d6c0cffa82 443
azaddevarm 26:e5d6c0cffa82 444 writereg(RF22_REG_6E_TX_DATA_RATE,0x0A);
azaddevarm 26:e5d6c0cffa82 445 writereg(RF22_REG_6F_TX_DATA_RATE,0x7D);//1280bps
azaddevarm 26:e5d6c0cffa82 446
shekhar 17:afadd6b988a2 447
shekhar 17:afadd6b988a2 448 writereg(RF22_REG_70_MODULATION_CONTROL1,0x20);
shekhar 17:afadd6b988a2 449 writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//ook = 0x21
shekhar 17:afadd6b988a2 450
shekhar 17:afadd6b988a2 451 //set tx power
shekhar 17:afadd6b988a2 452 writereg(RF22_REG_6D_TX_POWER,0x07); //20dbm
shekhar 17:afadd6b988a2 453
shekhar 17:afadd6b988a2 454 //TX_packet_length written later
shekhar 25:90cdfbadea91 455 ERROR_CHECK = 0;
shekhar 17:afadd6b988a2 456 }
shekhar 17:afadd6b988a2 457 bool Check_ACK_RECEIVED()
shekhar 17:afadd6b988a2 458 {
shekhar 17:afadd6b988a2 459 if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)
shekhar 17:afadd6b988a2 460 {
shekhar 17:afadd6b988a2 461 printf("Packet sent: ACK received\r\n");
shekhar 17:afadd6b988a2 462 return 1;
shekhar 17:afadd6b988a2 463 }
shekhar 17:afadd6b988a2 464 else
shekhar 17:afadd6b988a2 465 {
shekhar 24:ca96cddf25d0 466 pc_bcn.printf("Packet not sent\r\n");
shekhar 17:afadd6b988a2 467 return 0;
shekhar 17:afadd6b988a2 468 }
shekhar 17:afadd6b988a2 469 }
shekhar 25:90cdfbadea91 470 int main()
shekhar 17:afadd6b988a2 471 {
shekhar 17:afadd6b988a2 472 FCTN_BCN_INIT();
shekhar 17:afadd6b988a2 473
shekhar 17:afadd6b988a2 474 loop.attach(&FCTN_BCN_TX_MAIN, 10.0);//in actual case its 30.0
shekhar 17:afadd6b988a2 475
shekhar 17:afadd6b988a2 476 while(1);
shekhar 17:afadd6b988a2 477
shekhar 25:90cdfbadea91 478 }