![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
for GS integration
main.cpp@0:b4106205ed03, 2015-10-26 (annotated)
- Committer:
- shekhar
- Date:
- Mon Oct 26 08:16:03 2015 +0000
- Revision:
- 0:b4106205ed03
Integration done. This is the working code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shekhar | 0:b4106205ed03 | 1 | #include "tx.h" |
shekhar | 0:b4106205ed03 | 2 | #include <stdio.h> |
shekhar | 0:b4106205ed03 | 3 | |
shekhar | 0:b4106205ed03 | 4 | Ticker loop; |
shekhar | 0:b4106205ed03 | 5 | Serial pc(USBTX, USBRX); //tx,rx |
shekhar | 0:b4106205ed03 | 6 | SPI spi(D11, D12, D13); // mosi, miso, sclk |
shekhar | 0:b4106205ed03 | 7 | DigitalOut cs(D10); //slave select or chip select |
shekhar | 0:b4106205ed03 | 8 | |
shekhar | 0:b4106205ed03 | 9 | Timer t; |
shekhar | 0:b4106205ed03 | 10 | |
shekhar | 0:b4106205ed03 | 11 | void P_BCN_INIT() |
shekhar | 0:b4106205ed03 | 12 | { |
shekhar | 0:b4106205ed03 | 13 | BCN_INIT_STATUS = 1; |
shekhar | 0:b4106205ed03 | 14 | //check initialization parameters |
shekhar | 0:b4106205ed03 | 15 | Init_BEACON_HW(); |
shekhar | 0:b4106205ed03 | 16 | //t.start(); |
shekhar | 0:b4106205ed03 | 17 | BCN_INIT_STATUS = 0; |
shekhar | 0:b4106205ed03 | 18 | } |
shekhar | 0:b4106205ed03 | 19 | void P_BCN_FEN() |
shekhar | 0:b4106205ed03 | 20 | { |
shekhar | 0:b4106205ed03 | 21 | BCN_FEN = 1; |
shekhar | 0:b4106205ed03 | 22 | } |
shekhar | 0:b4106205ed03 | 23 | void P_BCN_TX_MAIN() |
shekhar | 0:b4106205ed03 | 24 | { |
shekhar | 0:b4106205ed03 | 25 | pc.printf("line 25"); |
shekhar | 0:b4106205ed03 | 26 | BCN_TX_MAIN_STATUS = 1; |
shekhar | 0:b4106205ed03 | 27 | |
shekhar | 0:b4106205ed03 | 28 | if(BCN_FEN == 1) |
shekhar | 0:b4106205ed03 | 29 | { |
shekhar | 0:b4106205ed03 | 30 | pc.printf("line 30"); |
shekhar | 0:b4106205ed03 | 31 | if(BCN_TX_EN == 1) |
shekhar | 0:b4106205ed03 | 32 | { |
shekhar | 0:b4106205ed03 | 33 | pc.printf("line 33"); |
shekhar | 0:b4106205ed03 | 34 | //Measure and store BCN temperature in BCN_TS_BUFFER |
shekhar | 0:b4106205ed03 | 35 | BCN_TS_BUFFER = check_Temperature(); |
shekhar | 0:b4106205ed03 | 36 | pc.printf("\n\ntemperature = %d\n\n",BCN_TS_BUFFER); |
shekhar | 0:b4106205ed03 | 37 | pc.printf("line 36"); |
shekhar | 0:b4106205ed03 | 38 | //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER |
shekhar | 0:b4106205ed03 | 39 | if(BCN_STANDBY == 1 ) |
shekhar | 0:b4106205ed03 | 40 | { |
shekhar | 0:b4106205ed03 | 41 | pc.printf("line 40"); |
shekhar | 0:b4106205ed03 | 42 | Set_BCN_TX_STATUS(BCN_TX_STANDBY); |
shekhar | 0:b4106205ed03 | 43 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 0:b4106205ed03 | 44 | |
shekhar | 0:b4106205ed03 | 45 | // break; |
shekhar | 0:b4106205ed03 | 46 | } |
shekhar | 0:b4106205ed03 | 47 | else |
shekhar | 0:b4106205ed03 | 48 | { |
shekhar | 0:b4106205ed03 | 49 | //pc.printf("in BCN_TX_STANDBY != 1\r\n"); |
shekhar | 0:b4106205ed03 | 50 | pc.printf("line 49"); |
shekhar | 0:b4106205ed03 | 51 | t.start(); |
shekhar | 0:b4106205ed03 | 52 | int begin = t.read_us(); |
shekhar | 0:b4106205ed03 | 53 | //pc.printf("line 52"); |
shekhar | 0:b4106205ed03 | 54 | SHORT_BCN_TX(); |
shekhar | 0:b4106205ed03 | 55 | LONG_BCN_TX(); |
shekhar | 0:b4106205ed03 | 56 | //pc.printf("line 55"); |
shekhar | 0:b4106205ed03 | 57 | t.stop(); |
shekhar | 0:b4106205ed03 | 58 | int end = t.read_us(); |
shekhar | 0:b4106205ed03 | 59 | pc.printf("\n\nThe time required for short and long is %d seconds\r\n\n", end-begin); |
shekhar | 0:b4106205ed03 | 60 | pc.printf("Short and Long packets sent\r\n"); |
shekhar | 0:b4106205ed03 | 61 | //include LONG_BCN_TX also |
shekhar | 0:b4106205ed03 | 62 | if(Check_ACK_RECEIVED() == 1) |
shekhar | 0:b4106205ed03 | 63 | { |
shekhar | 0:b4106205ed03 | 64 | Set_BCN_TX_STATUS(BCN_TX_SUCCESS); |
shekhar | 0:b4106205ed03 | 65 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 0:b4106205ed03 | 66 | // break; |
shekhar | 0:b4106205ed03 | 67 | |
shekhar | 0:b4106205ed03 | 68 | } |
shekhar | 0:b4106205ed03 | 69 | else |
shekhar | 0:b4106205ed03 | 70 | { |
shekhar | 0:b4106205ed03 | 71 | Set_BCN_TX_STATUS(BCN_TX_FAILURE); |
shekhar | 0:b4106205ed03 | 72 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 0:b4106205ed03 | 73 | // break; |
shekhar | 0:b4106205ed03 | 74 | |
shekhar | 0:b4106205ed03 | 75 | } |
shekhar | 0:b4106205ed03 | 76 | } |
shekhar | 0:b4106205ed03 | 77 | } |
shekhar | 0:b4106205ed03 | 78 | else |
shekhar | 0:b4106205ed03 | 79 | { |
shekhar | 0:b4106205ed03 | 80 | Set_BCN_TX_STATUS(BCN_TX_DISABLED); |
shekhar | 0:b4106205ed03 | 81 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 0:b4106205ed03 | 82 | // break; |
shekhar | 0:b4106205ed03 | 83 | |
shekhar | 0:b4106205ed03 | 84 | } |
shekhar | 0:b4106205ed03 | 85 | } |
shekhar | 0:b4106205ed03 | 86 | else |
shekhar | 0:b4106205ed03 | 87 | { |
shekhar | 0:b4106205ed03 | 88 | Set_BCN_TX_STATUS(BCN_RF_SILENCE); //Window of RF Silence: None of the Txs should be on. |
shekhar | 0:b4106205ed03 | 89 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 0:b4106205ed03 | 90 | // break; |
shekhar | 0:b4106205ed03 | 91 | } |
shekhar | 0:b4106205ed03 | 92 | } |
shekhar | 0:b4106205ed03 | 93 | |
shekhar | 0:b4106205ed03 | 94 | void Set_BCN_TX_STATUS(uint8_t STATUS) |
shekhar | 0:b4106205ed03 | 95 | { |
shekhar | 0:b4106205ed03 | 96 | BCN_TX_STATUS = STATUS; |
shekhar | 0:b4106205ed03 | 97 | } |
shekhar | 0:b4106205ed03 | 98 | |
shekhar | 0:b4106205ed03 | 99 | int check_Temperature() |
shekhar | 0:b4106205ed03 | 100 | { |
shekhar | 0:b4106205ed03 | 101 | int temperature; |
shekhar | 0:b4106205ed03 | 102 | //P_BCN_INIT(); |
shekhar | 0:b4106205ed03 | 103 | writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80); //128 = 10000000 |
shekhar | 0:b4106205ed03 | 104 | writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20); //160 == 10100000 32 == 00100000 |
shekhar | 0:b4106205ed03 | 105 | wait(1); |
shekhar | 0:b4106205ed03 | 106 | temperature = readreg(RF22_REG_11_ADC_Value); |
shekhar | 0:b4106205ed03 | 107 | temperature = (float)temperature*0.5 - 64; |
shekhar | 0:b4106205ed03 | 108 | return temperature; |
shekhar | 0:b4106205ed03 | 109 | } |
shekhar | 0:b4106205ed03 | 110 | void SHORT_BCN_TX() |
shekhar | 0:b4106205ed03 | 111 | { |
shekhar | 0:b4106205ed03 | 112 | writereg(RF22_REG_6E_TX_DATA_RATE,0x01); |
shekhar | 0:b4106205ed03 | 113 | writereg(RF22_REG_6F_TX_DATA_RATE,0x4F);//160bps |
shekhar | 0:b4106205ed03 | 114 | writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length |
shekhar | 0:b4106205ed03 | 115 | /* |
shekhar | 0:b4106205ed03 | 116 | init(); |
shekhar | 0:b4106205ed03 | 117 | //init complete |
shekhar | 0:b4106205ed03 | 118 | pc.printf("init complete.....press t to send\n"); |
shekhar | 0:b4106205ed03 | 119 | while(pc.getc()=='t') |
shekhar | 0:b4106205ed03 | 120 | { */ |
shekhar | 0:b4106205ed03 | 121 | //button.rise(&interrupt_func); //interrupt enabled ( rising edge of pin 9) |
shekhar | 0:b4106205ed03 | 122 | wait(0.02); // pl. update this value or even avoid it!!! |
shekhar | 0:b4106205ed03 | 123 | int i=0; |
shekhar | 0:b4106205ed03 | 124 | |
shekhar | 0:b4106205ed03 | 125 | //extract values from short_beacon[] |
shekhar | 0:b4106205ed03 | 126 | struct Short_beacon |
shekhar | 0:b4106205ed03 | 127 | { |
shekhar | 0:b4106205ed03 | 128 | uint8_t Voltage[1]; |
shekhar | 0:b4106205ed03 | 129 | uint8_t AngularSpeed[2]; |
shekhar | 0:b4106205ed03 | 130 | uint8_t SubsystemStatus[1]; |
shekhar | 0:b4106205ed03 | 131 | uint8_t Temp[3]; |
shekhar | 0:b4106205ed03 | 132 | uint8_t ErrorFlag[1]; |
shekhar | 0:b4106205ed03 | 133 | }Shortbeacon = { {0x88}, {0x99, 0xAA} , {0xAA},{0xAA,0xDD,0xEE}, {0x00} }; |
shekhar | 0:b4106205ed03 | 134 | |
shekhar | 0:b4106205ed03 | 135 | //filling hk data |
shekhar | 0:b4106205ed03 | 136 | 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 | 0:b4106205ed03 | 137 | //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 | 0:b4106205ed03 | 138 | |
shekhar | 0:b4106205ed03 | 139 | /*uint8_t short_beacon[15]; |
shekhar | 0:b4106205ed03 | 140 | for(int i = 0;i<15;i++) |
shekhar | 0:b4106205ed03 | 141 | { |
shekhar | 0:b4106205ed03 | 142 | short_beacon[i] = 0xAA; |
shekhar | 0:b4106205ed03 | 143 | }*/ |
shekhar | 0:b4106205ed03 | 144 | |
shekhar | 0:b4106205ed03 | 145 | //writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode ?? |
shekhar | 0:b4106205ed03 | 146 | clearTxBuf(); |
shekhar | 0:b4106205ed03 | 147 | //writing data first time |
shekhar | 0:b4106205ed03 | 148 | int byte = 0; |
shekhar | 0:b4106205ed03 | 149 | cs = 0; |
shekhar | 0:b4106205ed03 | 150 | spi.write(0xFF); |
shekhar | 0:b4106205ed03 | 151 | //pc.printf("line 151"); |
shekhar | 0:b4106205ed03 | 152 | for (int byte_counter = 0; byte_counter <15 ; byte_counter++) |
shekhar | 0:b4106205ed03 | 153 | { |
shekhar | 0:b4106205ed03 | 154 | for(int j = 3; j >= 0 ; j--) |
shekhar | 0:b4106205ed03 | 155 | { |
shekhar | 0:b4106205ed03 | 156 | if((short_beacon[byte_counter] & (uint8_t) pow(2.0,(j*2+1)))!= pow(2.0,(j*2+1))) |
shekhar | 0:b4106205ed03 | 157 | { |
shekhar | 0:b4106205ed03 | 158 | byte=0x00; |
shekhar | 0:b4106205ed03 | 159 | } |
shekhar | 0:b4106205ed03 | 160 | else |
shekhar | 0:b4106205ed03 | 161 | { |
shekhar | 0:b4106205ed03 | 162 | byte=0xF0; |
shekhar | 0:b4106205ed03 | 163 | } |
shekhar | 0:b4106205ed03 | 164 | if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j*2))!= pow(2.0,j*2)) |
shekhar | 0:b4106205ed03 | 165 | { |
shekhar | 0:b4106205ed03 | 166 | byte=byte | 0x00; |
shekhar | 0:b4106205ed03 | 167 | } |
shekhar | 0:b4106205ed03 | 168 | else |
shekhar | 0:b4106205ed03 | 169 | { |
shekhar | 0:b4106205ed03 | 170 | byte=byte | 0x0F; |
shekhar | 0:b4106205ed03 | 171 | } |
shekhar | 0:b4106205ed03 | 172 | spi.write(byte); |
shekhar | 0:b4106205ed03 | 173 | } |
shekhar | 0:b4106205ed03 | 174 | } |
shekhar | 0:b4106205ed03 | 175 | |
shekhar | 0:b4106205ed03 | 176 | cs = 1; |
shekhar | 0:b4106205ed03 | 177 | //pc.printf("line 177\n"); |
shekhar | 0:b4106205ed03 | 178 | //Set to Tx mode |
shekhar | 0:b4106205ed03 | 179 | writereg(RF22_REG_07_OPERATING_MODE1,0x09); |
shekhar | 0:b4106205ed03 | 180 | |
shekhar | 0:b4106205ed03 | 181 | //Check for fifoThresh |
shekhar | 0:b4106205ed03 | 182 | while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20); |
shekhar | 0:b4106205ed03 | 183 | //pc.printf("fifothresh1?\n"); |
shekhar | 0:b4106205ed03 | 184 | //pc.printf("line 184\n"); |
shekhar | 0:b4106205ed03 | 185 | //rf22.waitPacketSent(); |
shekhar | 0:b4106205ed03 | 186 | while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04); |
shekhar | 0:b4106205ed03 | 187 | //pc.printf(" chk pkt sent!\r\n"); |
shekhar | 0:b4106205ed03 | 188 | //pc.printf("Short packet sent\r\n"); |
shekhar | 0:b4106205ed03 | 189 | |
shekhar | 0:b4106205ed03 | 190 | writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode |
shekhar | 0:b4106205ed03 | 191 | |
shekhar | 0:b4106205ed03 | 192 | } |
shekhar | 0:b4106205ed03 | 193 | void LONG_BCN_TX() |
shekhar | 0:b4106205ed03 | 194 | { |
shekhar | 0:b4106205ed03 | 195 | writereg(RF22_REG_6E_TX_DATA_RATE,0x04); |
shekhar | 0:b4106205ed03 | 196 | writereg(RF22_REG_6F_TX_DATA_RATE,0xEA); |
shekhar | 0:b4106205ed03 | 197 | writereg(RF22_REG_3E_PACKET_LENGTH,LONG_TX_DATA); //long packet length |
shekhar | 0:b4106205ed03 | 198 | wait(0.02); // pl. update this value or even avoid it!!! |
shekhar | 0:b4106205ed03 | 199 | //extract values from long_beacon[] |
shekhar | 0:b4106205ed03 | 200 | |
shekhar | 0:b4106205ed03 | 201 | //uint8_t Long_beacon[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,Longbeacon.Voltage[0],Longbeacon.AngularSpeed[0], Longbeacon.AngularSpeed[1],Longbeacon.SubsystemStatus[0],Longbeacon.Temp[0],Longbeacon.Temp[1],Longbeacon.Temp[2],Longbeacon.ErrorFlag[0]}; |
shekhar | 0:b4106205ed03 | 202 | |
shekhar | 0:b4106205ed03 | 203 | uint8_t Long_beacon[75]; |
shekhar | 0:b4106205ed03 | 204 | for(int i = 0;i<75;) |
shekhar | 0:b4106205ed03 | 205 | { |
shekhar | 0:b4106205ed03 | 206 | Long_beacon[i++] = 0xAB;Long_beacon[i++] = 0xCD;Long_beacon[i++] = 0xAB;Long_beacon[i++] = 0xCD;Long_beacon[i++] = 0x00; |
shekhar | 0:b4106205ed03 | 207 | } |
shekhar | 0:b4106205ed03 | 208 | |
shekhar | 0:b4106205ed03 | 209 | //setModeIdle(); |
shekhar | 0:b4106205ed03 | 210 | writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode |
shekhar | 0:b4106205ed03 | 211 | clearTxBuf(); |
shekhar | 0:b4106205ed03 | 212 | //writing data first time |
shekhar | 0:b4106205ed03 | 213 | cs = 0; |
shekhar | 0:b4106205ed03 | 214 | spi.write(0xFF); |
shekhar | 0:b4106205ed03 | 215 | for(int i=0; i<60;i++) |
shekhar | 0:b4106205ed03 | 216 | { |
shekhar | 0:b4106205ed03 | 217 | spi.write(Long_beacon[i]); |
shekhar | 0:b4106205ed03 | 218 | } |
shekhar | 0:b4106205ed03 | 219 | cs = 1; |
shekhar | 0:b4106205ed03 | 220 | |
shekhar | 0:b4106205ed03 | 221 | //Set to Tx mode |
shekhar | 0:b4106205ed03 | 222 | writereg(RF22_REG_07_OPERATING_MODE1,0x09); |
shekhar | 0:b4106205ed03 | 223 | |
shekhar | 0:b4106205ed03 | 224 | //Check for fifoThresh |
shekhar | 0:b4106205ed03 | 225 | while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20); |
shekhar | 0:b4106205ed03 | 226 | //pc.printf("fifothresh1?\n"); |
shekhar | 0:b4106205ed03 | 227 | //writing data first time |
shekhar | 0:b4106205ed03 | 228 | cs = 0; |
shekhar | 0:b4106205ed03 | 229 | spi.write(0xFF); |
shekhar | 0:b4106205ed03 | 230 | for(int i=60; i<75;i++) |
shekhar | 0:b4106205ed03 | 231 | { |
shekhar | 0:b4106205ed03 | 232 | spi.write(Long_beacon[i]); |
shekhar | 0:b4106205ed03 | 233 | } |
shekhar | 0:b4106205ed03 | 234 | cs = 1; |
shekhar | 0:b4106205ed03 | 235 | wait(0.01); |
shekhar | 0:b4106205ed03 | 236 | //Check for fifoThresh |
shekhar | 0:b4106205ed03 | 237 | while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20); |
shekhar | 0:b4106205ed03 | 238 | //pc.printf("fifothresh2?\n"); |
shekhar | 0:b4106205ed03 | 239 | //rf22.waitPacketSent(); |
shekhar | 0:b4106205ed03 | 240 | while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04); |
shekhar | 0:b4106205ed03 | 241 | //pc.printf(" chk pkt sent!]\r\n"); |
shekhar | 0:b4106205ed03 | 242 | pc.printf("Long packet sent\r\n"); |
shekhar | 0:b4106205ed03 | 243 | writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode |
shekhar | 0:b4106205ed03 | 244 | } |
shekhar | 0:b4106205ed03 | 245 | void reset_uC() |
shekhar | 0:b4106205ed03 | 246 | { |
shekhar | 0:b4106205ed03 | 247 | printf("reset uC"); |
shekhar | 0:b4106205ed03 | 248 | } |
shekhar | 0:b4106205ed03 | 249 | void writereg(uint8_t reg,uint8_t val) |
shekhar | 0:b4106205ed03 | 250 | { |
shekhar | 0:b4106205ed03 | 251 | int read_val =0; cs = 0;spi.write(reg | 0x80);spi.write(val);cs = 1;// wait(1);read_val = readreg(reg);if(read_val != val) {reset_uC();pc.printf("value for 0x%X written incorrectly to the uC\n",reg);} |
shekhar | 0:b4106205ed03 | 252 | } |
shekhar | 0:b4106205ed03 | 253 | uint8_t readreg(uint8_t reg) |
shekhar | 0:b4106205ed03 | 254 | { |
shekhar | 0:b4106205ed03 | 255 | int val;cs = 0;spi.write(reg & ~0x80);val = spi.write(0);cs = 1;return val; |
shekhar | 0:b4106205ed03 | 256 | } |
shekhar | 0:b4106205ed03 | 257 | void clearTxBuf() |
shekhar | 0:b4106205ed03 | 258 | { |
shekhar | 0:b4106205ed03 | 259 | writereg(RF22_REG_08_OPERATING_MODE2,0x01); |
shekhar | 0:b4106205ed03 | 260 | writereg(RF22_REG_08_OPERATING_MODE2,0x00); |
shekhar | 0:b4106205ed03 | 261 | } |
shekhar | 0:b4106205ed03 | 262 | int setFrequency(double centre,float afcPullInRange) |
shekhar | 0:b4106205ed03 | 263 | { |
shekhar | 0:b4106205ed03 | 264 | //freq setting begins |
shekhar | 0:b4106205ed03 | 265 | uint8_t fbsel = 0x40; |
shekhar | 0:b4106205ed03 | 266 | uint8_t afclimiter; |
shekhar | 0:b4106205ed03 | 267 | if (centre >= 480.0) { |
shekhar | 0:b4106205ed03 | 268 | centre /= 2; |
shekhar | 0:b4106205ed03 | 269 | fbsel |= 0x20; |
shekhar | 0:b4106205ed03 | 270 | afclimiter = afcPullInRange * 1000000.0 / 1250.0; |
shekhar | 0:b4106205ed03 | 271 | } else { |
shekhar | 0:b4106205ed03 | 272 | if (afcPullInRange < 0.0 || afcPullInRange > 0.159375) |
shekhar | 0:b4106205ed03 | 273 | return false; |
shekhar | 0:b4106205ed03 | 274 | afclimiter = afcPullInRange * 1000000.0 / 625.0; |
shekhar | 0:b4106205ed03 | 275 | } |
shekhar | 0:b4106205ed03 | 276 | centre /= 10.0; |
shekhar | 0:b4106205ed03 | 277 | double integerPart = floor(centre); |
shekhar | 0:b4106205ed03 | 278 | double fractionalPart = centre - integerPart; |
shekhar | 0:b4106205ed03 | 279 | |
shekhar | 0:b4106205ed03 | 280 | uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23 |
shekhar | 0:b4106205ed03 | 281 | fbsel |= fb; |
shekhar | 0:b4106205ed03 | 282 | uint16_t fc = fractionalPart * 64000; |
shekhar | 0:b4106205ed03 | 283 | writereg(RF22_REG_73_FREQUENCY_OFFSET1, 0); // REVISIT |
shekhar | 0:b4106205ed03 | 284 | writereg(RF22_REG_74_FREQUENCY_OFFSET2, 0); |
shekhar | 0:b4106205ed03 | 285 | writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel); |
shekhar | 0:b4106205ed03 | 286 | writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8); |
shekhar | 0:b4106205ed03 | 287 | writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff); |
shekhar | 0:b4106205ed03 | 288 | writereg(RF22_REG_2A_AFC_LIMITER, afclimiter); |
shekhar | 0:b4106205ed03 | 289 | return 0; |
shekhar | 0:b4106205ed03 | 290 | } |
shekhar | 0:b4106205ed03 | 291 | void Init_BEACON_HW() |
shekhar | 0:b4106205ed03 | 292 | { |
shekhar | 0:b4106205ed03 | 293 | cs=1; // chip must be deselected |
shekhar | 0:b4106205ed03 | 294 | //wait(1); //change the time later |
shekhar | 0:b4106205ed03 | 295 | spi.format(8,0); |
shekhar | 0:b4106205ed03 | 296 | spi.frequency(10000000); //10MHz SCLK |
shekhar | 0:b4106205ed03 | 297 | |
shekhar | 0:b4106205ed03 | 298 | //should either have a flag for invalid SPI or discard this for actual case or add reset |
shekhar | 0:b4106205ed03 | 299 | if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08) |
shekhar | 0:b4106205ed03 | 300 | pc.printf("spi connection valid\r\n"); |
shekhar | 0:b4106205ed03 | 301 | else |
shekhar | 0:b4106205ed03 | 302 | pc.printf("error in spi connection\r\n"); |
shekhar | 0:b4106205ed03 | 303 | //reset() |
shekhar | 0:b4106205ed03 | 304 | |
shekhar | 0:b4106205ed03 | 305 | |
shekhar | 0:b4106205ed03 | 306 | writereg(RF22_REG_07_OPERATING_MODE1,0x80); //sw_reset |
shekhar | 0:b4106205ed03 | 307 | wait(1); //takes time to reset |
shekhar | 0:b4106205ed03 | 308 | |
shekhar | 0:b4106205ed03 | 309 | clearTxBuf(); |
shekhar | 0:b4106205ed03 | 310 | |
shekhar | 0:b4106205ed03 | 311 | writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode |
shekhar | 0:b4106205ed03 | 312 | |
shekhar | 0:b4106205ed03 | 313 | //txfifoalmostempty |
shekhar | 0:b4106205ed03 | 314 | writereg(RF22_REG_7D_TX_FIFO_CONTROL2,30); |
shekhar | 0:b4106205ed03 | 315 | |
shekhar | 0:b4106205ed03 | 316 | //Packet-engine registers |
shekhar | 0:b4106205ed03 | 317 | writereg(RF22_REG_30_DATA_ACCESS_CONTROL,0x00); //RF22_REG_30_DATA_ACCESS_CONTROL, RF22_ENPACRX | RF22_ENPACTX | RF22_ENCRC | RF22_CRC_CRC_16_IBM |
shekhar | 0:b4106205ed03 | 318 | //&0x77 = diasable packet rx-tx handling |
shekhar | 0:b4106205ed03 | 319 | |
shekhar | 0:b4106205ed03 | 320 | writereg(RF22_REG_33_HEADER_CONTROL2,0x08); //RF22_REG_33_HEADER_CONTROL2, RF22_HDLEN_4 | RF22_SYNCLEN_2 |
shekhar | 0:b4106205ed03 | 321 | writereg(RF22_REG_34_PREAMBLE_LENGTH,0x00); //RF22_REG_34_PREAMBLE_LENGTH, nibbles); preamble length = 8; |
shekhar | 0:b4106205ed03 | 322 | //may be redundant since packethandler is disabled |
shekhar | 0:b4106205ed03 | 323 | |
shekhar | 0:b4106205ed03 | 324 | writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state ?? |
shekhar | 0:b4106205ed03 | 325 | writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state ?? |
shekhar | 0:b4106205ed03 | 326 | |
shekhar | 0:b4106205ed03 | 327 | //interrupts |
shekhar | 0:b4106205ed03 | 328 | // spiWrite(RF22_REG_05_INTERRUPT_ENABLE1, RF22_ENTXFFAEM |RF22_ENRXFFAFULL | RF22_ENPKSENT |RF22_ENPKVALID| RF22_ENCRCERROR); |
shekhar | 0:b4106205ed03 | 329 | // spiWrite(RF22_REG_06_INTERRUPT_ENABLE2, RF22_ENPREAVAL); |
shekhar | 0:b4106205ed03 | 330 | |
shekhar | 0:b4106205ed03 | 331 | setFrequency(435.00015625,0.05);//435.000156.25, observed = 434.99818100 |
shekhar | 0:b4106205ed03 | 332 | |
shekhar | 0:b4106205ed03 | 333 | //This may not be required |
shekhar | 0:b4106205ed03 | 334 | if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00) |
shekhar | 0:b4106205ed03 | 335 | {pc.printf("frequency not set properly\r\n");reset_uC();} |
shekhar | 0:b4106205ed03 | 336 | |
shekhar | 0:b4106205ed03 | 337 | //frequency set |
shekhar | 0:b4106205ed03 | 338 | |
shekhar | 0:b4106205ed03 | 339 | //setModemConfig(FSK_Rb2_4Fd36); FSK_Rb2_4Fd36, ///< FSK, No Manchester, Rb = 2.4kbs, Fd = 36kHz |
shekhar | 0:b4106205ed03 | 340 | //setmodemregisters |
shekhar | 0:b4106205ed03 | 341 | //0x1b, 0x03, 0x41, 0x60, 0x27, 0x52, 0x00, 0x07, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x22, 0x3a = FSK_RB2_4FD36 |
shekhar | 0:b4106205ed03 | 342 | //0xc8, 0x03, 0x39, 0x20, 0x68, 0xdc, 0x00, 0x6b, 0x2a, 0x08, 0x2a, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x21, 0x08 = OOK,2.4, 335 |
shekhar | 0:b4106205ed03 | 343 | writereg(RF22_REG_1C_IF_FILTER_BANDWIDTH,0xdf); |
shekhar | 0:b4106205ed03 | 344 | writereg(RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE,0x03); |
shekhar | 0:b4106205ed03 | 345 | writereg(RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE,0x39); |
shekhar | 0:b4106205ed03 | 346 | writereg(RF22_REG_21_CLOCK_RECOVERY_OFFSET2,0x20); |
shekhar | 0:b4106205ed03 | 347 | writereg(RF22_REG_22_CLOCK_RECOVERY_OFFSET1,0x68); //updated 20 to 25 reg values from excel sheet for 1.2 Khz freq. deviation,fsk |
shekhar | 0:b4106205ed03 | 348 | writereg(RF22_REG_23_CLOCK_RECOVERY_OFFSET0,0xdc); |
shekhar | 0:b4106205ed03 | 349 | writereg(RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1,0x00); |
shekhar | 0:b4106205ed03 | 350 | writereg(RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0,0x6B); |
shekhar | 0:b4106205ed03 | 351 | writereg(RF22_REG_2C_OOK_COUNTER_VALUE_1,0x2C); |
shekhar | 0:b4106205ed03 | 352 | writereg(RF22_REG_2D_OOK_COUNTER_VALUE_2,0x11); //not required for fsk (OOK counter value) |
shekhar | 0:b4106205ed03 | 353 | writereg(RF22_REG_2E_SLICER_PEAK_HOLD,0x2A); //?? |
shekhar | 0:b4106205ed03 | 354 | writereg(RF22_REG_58,0x80); |
shekhar | 0:b4106205ed03 | 355 | writereg(RF22_REG_69_AGC_OVERRIDE1,0x60); |
shekhar | 0:b4106205ed03 | 356 | |
shekhar | 0:b4106205ed03 | 357 | |
shekhar | 0:b4106205ed03 | 358 | //writereg(RF22_REG_6E_TX_DATA_RATE_0.125_1,0x09);//written later |
shekhar | 0:b4106205ed03 | 359 | //writereg(RF22_REG_6F_TX_DATA_RATE_0.125_0,0xd5);//written later |
shekhar | 0:b4106205ed03 | 360 | |
shekhar | 0:b4106205ed03 | 361 | writereg(RF22_REG_70_MODULATION_CONTROL1,0x20);//changed from 0x2c to 0x20 since machester is not required |
shekhar | 0:b4106205ed03 | 362 | writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//ook = 0x21 //fsk = 0x22 |
shekhar | 0:b4106205ed03 | 363 | |
shekhar | 0:b4106205ed03 | 364 | //writereg(RF22_REG_72_FREQUENCY_DEVIATION,0x50); |
shekhar | 0:b4106205ed03 | 365 | //required for OOK??? |
shekhar | 0:b4106205ed03 | 366 | |
shekhar | 0:b4106205ed03 | 367 | //set tx power |
shekhar | 0:b4106205ed03 | 368 | writereg(RF22_REG_6D_TX_POWER,0x07); //20dbm |
shekhar | 0:b4106205ed03 | 369 | //why 0x06?? 0x07 is for max//is the previous bits required? |
shekhar | 0:b4106205ed03 | 370 | |
shekhar | 0:b4106205ed03 | 371 | //writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length //written later |
shekhar | 0:b4106205ed03 | 372 | } |
shekhar | 0:b4106205ed03 | 373 | bool Check_ACK_RECEIVED() |
shekhar | 0:b4106205ed03 | 374 | { |
shekhar | 0:b4106205ed03 | 375 | //logic: |
shekhar | 0:b4106205ed03 | 376 | if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04) //bit is set if the data packet is sent correctly |
shekhar | 0:b4106205ed03 | 377 | { |
shekhar | 0:b4106205ed03 | 378 | printf("Packet sent: ACK received\r\n"); |
shekhar | 0:b4106205ed03 | 379 | return 1; //for ACK received: yes |
shekhar | 0:b4106205ed03 | 380 | |
shekhar | 0:b4106205ed03 | 381 | } |
shekhar | 0:b4106205ed03 | 382 | else |
shekhar | 0:b4106205ed03 | 383 | { |
shekhar | 0:b4106205ed03 | 384 | pc.printf("Packet not sent\r\n"); |
shekhar | 0:b4106205ed03 | 385 | return 0; //actual value |
shekhar | 0:b4106205ed03 | 386 | //return 1; //to make this function work in any condition //Why?? |
shekhar | 0:b4106205ed03 | 387 | |
shekhar | 0:b4106205ed03 | 388 | } |
shekhar | 0:b4106205ed03 | 389 | } |
shekhar | 0:b4106205ed03 | 390 | int main() |
shekhar | 0:b4106205ed03 | 391 | { |
shekhar | 0:b4106205ed03 | 392 | |
shekhar | 0:b4106205ed03 | 393 | pc.printf("entered main"); |
shekhar | 0:b4106205ed03 | 394 | int temp, final_temp; |
shekhar | 0:b4106205ed03 | 395 | |
shekhar | 0:b4106205ed03 | 396 | P_BCN_INIT(); |
shekhar | 0:b4106205ed03 | 397 | pc.printf("completed bcn_init"); |
shekhar | 0:b4106205ed03 | 398 | |
shekhar | 0:b4106205ed03 | 399 | P_BCN_FEN(); |
shekhar | 0:b4106205ed03 | 400 | pc.printf("press 't' to start..."); |
shekhar | 0:b4106205ed03 | 401 | //while(1) |
shekhar | 0:b4106205ed03 | 402 | //{ |
shekhar | 0:b4106205ed03 | 403 | t.start(); |
shekhar | 0:b4106205ed03 | 404 | int begin = t.read_us(); |
shekhar | 0:b4106205ed03 | 405 | //SHORT_BCN_TX(); |
shekhar | 0:b4106205ed03 | 406 | //LONG_BCN_TX(); |
shekhar | 0:b4106205ed03 | 407 | P_BCN_TX_MAIN(); |
shekhar | 0:b4106205ed03 | 408 | t.stop(); |
shekhar | 0:b4106205ed03 | 409 | int end = t.read_us(); |
shekhar | 0:b4106205ed03 | 410 | pc.printf("\n\nThe time taken : %d seconds\r\n\n", end-begin); |
shekhar | 0:b4106205ed03 | 411 | pc.printf("packets sent\r\n"); |
shekhar | 0:b4106205ed03 | 412 | //P_BCN_TX_MAIN(); |
shekhar | 0:b4106205ed03 | 413 | //} |
shekhar | 0:b4106205ed03 | 414 | |
shekhar | 0:b4106205ed03 | 415 | } |