pradeep shekhar
/
Beacon_BAE_intergration_test_3
General testings
Fork of BEACON_CODE_NEW by
BCN.cpp@28:5bac883a0372, 2015-11-01 (annotated)
- Committer:
- shekhar
- Date:
- Sun Nov 01 08:30:56 2015 +0000
- Revision:
- 28:5bac883a0372
- Parent:
- 27:eb8c593c4ea8
- Child:
- 29:a864d57f10c4
integrated with GS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shekhar | 24:3d8e70286f67 | 1 | #include "BCN.h" |
shekhar | 23:afadd6b988a2 | 2 | //Check the pin names |
shekhar | 23:afadd6b988a2 | 3 | //Takes max 4.3 sec in void FCTN_BCN_TX_MAIN() (temp.calc. + long_beacon + short_beacon) |
shekhar | 23:afadd6b988a2 | 4 | |
shekhar | 23:afadd6b988a2 | 5 | Serial pc(USBTX, USBRX); //tx,rx |
shekhar | 23:afadd6b988a2 | 6 | SPI spi(D11, D12, D13); // mosi, miso, sclk |
shekhar | 23:afadd6b988a2 | 7 | DigitalOut cs(D10); //slave select or chip select |
shekhar | 23:afadd6b988a2 | 8 | Timer t_i; |
shekhar | 25:6863457ea3d1 | 9 | Timeout rf_sl_timeout; |
shekhar | 24:3d8e70286f67 | 10 | Ticker loop; |
shekhar | 23:afadd6b988a2 | 11 | |
shekhar | 23:afadd6b988a2 | 12 | //GLOBAL VARIABLES |
shekhar | 23:afadd6b988a2 | 13 | uint8_t BCN_INIT_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 14 | uint8_t BCN_TX_MAIN_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 15 | uint8_t BCN_TX_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 16 | uint8_t BCN_TX_EN = 1; //hardcoding for now //check where is this variable toggled?? |
shekhar | 25:6863457ea3d1 | 17 | uint8_t BCN_FEN = 0; //hardcoding for now //write this value to flash |
shekhar | 23:afadd6b988a2 | 18 | uint8_t BCN_STANDBY = 0; //hardcoding for now //check where is this variable toggled?? |
shekhar | 23:afadd6b988a2 | 19 | uint8_t BCN_TS_BUFFER = 0; // For Temperature |
shekhar | 23:afadd6b988a2 | 20 | |
shekhar | 23:afadd6b988a2 | 21 | void FCTN_BCN_INIT() |
shekhar | 23:afadd6b988a2 | 22 | { |
shekhar | 23:afadd6b988a2 | 23 | pc.printf("FCTN_BCN_INIT\n"); |
shekhar | 23:afadd6b988a2 | 24 | BCN_INIT_STATUS = 1; |
shekhar | 24:3d8e70286f67 | 25 | if(BCN_FEN == 0) |
shekhar | 25:6863457ea3d1 | 26 | rf_sl_timeout.attach(&FCTN_BCN_FEN, 30); |
shekhar | 23:afadd6b988a2 | 27 | Init_BEACON_HW(); |
shekhar | 23:afadd6b988a2 | 28 | BCN_INIT_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 29 | } |
shekhar | 23:afadd6b988a2 | 30 | void FCTN_BCN_FEN() |
shekhar | 23:afadd6b988a2 | 31 | { |
shekhar | 23:afadd6b988a2 | 32 | pc.printf("FCTN_FEN\n"); |
shekhar | 23:afadd6b988a2 | 33 | BCN_FEN = 1;//write this value to flash |
shekhar | 23:afadd6b988a2 | 34 | } |
shekhar | 23:afadd6b988a2 | 35 | void FCTN_BCN_TX_MAIN() |
shekhar | 23:afadd6b988a2 | 36 | { |
shekhar | 23:afadd6b988a2 | 37 | pc.printf("FCTN_BCN_TX_MAIN\n"); |
shekhar | 23:afadd6b988a2 | 38 | t_i.start(); |
shekhar | 23:afadd6b988a2 | 39 | int begin = t_i.read_us(); |
shekhar | 23:afadd6b988a2 | 40 | BCN_TX_MAIN_STATUS = 1; |
shekhar | 23:afadd6b988a2 | 41 | |
shekhar | 23:afadd6b988a2 | 42 | if(BCN_FEN == 1) |
shekhar | 23:afadd6b988a2 | 43 | { |
shekhar | 23:afadd6b988a2 | 44 | if(BCN_TX_EN == 1) |
shekhar | 23:afadd6b988a2 | 45 | { |
shekhar | 23:afadd6b988a2 | 46 | //Measure and store BCN temperature in BCN_TS_BUFFER |
shekhar | 23:afadd6b988a2 | 47 | BCN_TS_BUFFER = check_Temperature(); |
shekhar | 23:afadd6b988a2 | 48 | pc.printf("\n\ntemperature = %d\n\n",BCN_TS_BUFFER); |
shekhar | 23:afadd6b988a2 | 49 | //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER |
shekhar | 23:afadd6b988a2 | 50 | if(BCN_STANDBY == 1 ) |
shekhar | 23:afadd6b988a2 | 51 | { |
shekhar | 23:afadd6b988a2 | 52 | Set_BCN_TX_STATUS(BCN_TX_STANDBY); |
shekhar | 23:afadd6b988a2 | 53 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 54 | |
shekhar | 23:afadd6b988a2 | 55 | // break; |
shekhar | 23:afadd6b988a2 | 56 | } |
shekhar | 23:afadd6b988a2 | 57 | else |
shekhar | 23:afadd6b988a2 | 58 | { |
shekhar | 23:afadd6b988a2 | 59 | //transmit short beacon and long beacon |
shekhar | 23:afadd6b988a2 | 60 | SHORT_BCN_TX(); |
shekhar | 28:5bac883a0372 | 61 | //LONG_BCN_TX(); |
shekhar | 23:afadd6b988a2 | 62 | |
shekhar | 23:afadd6b988a2 | 63 | if(Check_ACK_RECEIVED() == 1) |
shekhar | 23:afadd6b988a2 | 64 | { |
shekhar | 23:afadd6b988a2 | 65 | Set_BCN_TX_STATUS(BCN_TX_SUCCESS); |
shekhar | 23:afadd6b988a2 | 66 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 67 | } |
shekhar | 23:afadd6b988a2 | 68 | else |
shekhar | 23:afadd6b988a2 | 69 | { |
shekhar | 23:afadd6b988a2 | 70 | Set_BCN_TX_STATUS(BCN_TX_FAILURE); |
shekhar | 23:afadd6b988a2 | 71 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 72 | |
shekhar | 23:afadd6b988a2 | 73 | } |
shekhar | 23:afadd6b988a2 | 74 | } |
shekhar | 23:afadd6b988a2 | 75 | } |
shekhar | 23:afadd6b988a2 | 76 | else |
shekhar | 23:afadd6b988a2 | 77 | { |
shekhar | 23:afadd6b988a2 | 78 | Set_BCN_TX_STATUS(BCN_TX_DISABLED); |
shekhar | 23:afadd6b988a2 | 79 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 80 | } |
shekhar | 23:afadd6b988a2 | 81 | } |
shekhar | 23:afadd6b988a2 | 82 | else |
shekhar | 23:afadd6b988a2 | 83 | { |
shekhar | 23:afadd6b988a2 | 84 | Set_BCN_TX_STATUS(BCN_RF_SILENCE); //Window of RF Silence: None of the Txs should be on. |
shekhar | 23:afadd6b988a2 | 85 | BCN_TX_MAIN_STATUS = 0; |
shekhar | 23:afadd6b988a2 | 86 | } |
shekhar | 23:afadd6b988a2 | 87 | t_i.stop(); |
shekhar | 23:afadd6b988a2 | 88 | int end = t_i.read_us(); |
shekhar | 25:6863457ea3d1 | 89 | pc.printf("The time required for FCTN_BCN_TX_MAIN is %d useconds\r\n", end-begin); |
shekhar | 23:afadd6b988a2 | 90 | |
shekhar | 23:afadd6b988a2 | 91 | } |
shekhar | 23:afadd6b988a2 | 92 | |
shekhar | 23:afadd6b988a2 | 93 | void Set_BCN_TX_STATUS(uint8_t STATUS) |
shekhar | 23:afadd6b988a2 | 94 | { |
shekhar | 23:afadd6b988a2 | 95 | BCN_TX_STATUS = STATUS; |
shekhar | 23:afadd6b988a2 | 96 | } |
shekhar | 23:afadd6b988a2 | 97 | |
shekhar | 23:afadd6b988a2 | 98 | uint8_t check_Temperature() |
shekhar | 23:afadd6b988a2 | 99 | { |
shekhar | 23:afadd6b988a2 | 100 | uint8_t temperature; |
shekhar | 27:eb8c593c4ea8 | 101 | writereg(RF22_REG_0F_ADC_CONFIGURATION,0x00); |
shekhar | 23:afadd6b988a2 | 102 | writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20); |
shekhar | 27:eb8c593c4ea8 | 103 | writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80); |
shekhar | 23:afadd6b988a2 | 104 | wait(0.1); |
shekhar | 23:afadd6b988a2 | 105 | temperature = readreg(RF22_REG_11_ADC_Value); |
shekhar | 23:afadd6b988a2 | 106 | temperature = (float)temperature*0.5 - 64; |
shekhar | 23:afadd6b988a2 | 107 | return temperature; |
shekhar | 23:afadd6b988a2 | 108 | } |
shekhar | 23:afadd6b988a2 | 109 | |
shekhar | 23:afadd6b988a2 | 110 | void SHORT_BCN_TX() |
shekhar | 23:afadd6b988a2 | 111 | { |
shekhar | 23:afadd6b988a2 | 112 | writereg(RF22_REG_6E_TX_DATA_RATE,0x01); |
shekhar | 26:89b6bb0859a1 | 113 | writereg(RF22_REG_6F_TX_DATA_RATE,0x50);//160bps |
shekhar | 23:afadd6b988a2 | 114 | writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length |
shekhar | 23:afadd6b988a2 | 115 | wait(0.02); |
shekhar | 23:afadd6b988a2 | 116 | |
shekhar | 23:afadd6b988a2 | 117 | uint32_t timeout_count = 10e5; |
shekhar | 23:afadd6b988a2 | 118 | //extract values from short_beacon[] |
shekhar | 23:afadd6b988a2 | 119 | |
shekhar | 23:afadd6b988a2 | 120 | struct Short_beacon |
shekhar | 23:afadd6b988a2 | 121 | { |
shekhar | 23:afadd6b988a2 | 122 | uint8_t Voltage[1]; |
shekhar | 23:afadd6b988a2 | 123 | uint8_t AngularSpeed[2]; |
shekhar | 23:afadd6b988a2 | 124 | uint8_t SubsystemStatus[1]; |
shekhar | 23:afadd6b988a2 | 125 | uint8_t Temp[3]; |
shekhar | 23:afadd6b988a2 | 126 | uint8_t ErrorFlag[1]; |
shekhar | 23:afadd6b988a2 | 127 | }Shortbeacon = { {0x88}, {0x99, 0xAA} , {0xAA},{0xAA,0xDD,0xEE}, {0x00} }; |
shekhar | 23:afadd6b988a2 | 128 | |
shekhar | 23:afadd6b988a2 | 129 | //filling hk data |
shekhar | 23:afadd6b988a2 | 130 | //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 | 23:afadd6b988a2 | 131 | 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 | 23:afadd6b988a2 | 132 | |
shekhar | 23:afadd6b988a2 | 133 | //writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode ?? |
shekhar | 23:afadd6b988a2 | 134 | clearTxBuf(); |
shekhar | 23:afadd6b988a2 | 135 | //writing data first time |
shekhar | 23:afadd6b988a2 | 136 | int byte = 0; |
shekhar | 23:afadd6b988a2 | 137 | cs = 0; |
shekhar | 23:afadd6b988a2 | 138 | spi.write(0xFF); |
shekhar | 23:afadd6b988a2 | 139 | |
shekhar | 23:afadd6b988a2 | 140 | for (int byte_counter = 0; byte_counter <15 ; byte_counter++) |
shekhar | 23:afadd6b988a2 | 141 | { |
shekhar | 23:afadd6b988a2 | 142 | for(int j = 3; j >= 0 ; j--) |
shekhar | 23:afadd6b988a2 | 143 | { |
shekhar | 23:afadd6b988a2 | 144 | if((short_beacon[byte_counter] & (uint8_t) pow(2.0,(j*2+1)))!= pow(2.0,(j*2+1))) |
shekhar | 23:afadd6b988a2 | 145 | { |
shekhar | 23:afadd6b988a2 | 146 | byte=0x00; |
shekhar | 23:afadd6b988a2 | 147 | } |
shekhar | 23:afadd6b988a2 | 148 | else |
shekhar | 23:afadd6b988a2 | 149 | { |
shekhar | 23:afadd6b988a2 | 150 | byte=0xF0; |
shekhar | 23:afadd6b988a2 | 151 | } |
shekhar | 23:afadd6b988a2 | 152 | if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j*2))!= pow(2.0,j*2)) |
shekhar | 23:afadd6b988a2 | 153 | { |
shekhar | 23:afadd6b988a2 | 154 | byte=byte | 0x00; |
shekhar | 23:afadd6b988a2 | 155 | } |
shekhar | 23:afadd6b988a2 | 156 | else |
shekhar | 23:afadd6b988a2 | 157 | { |
shekhar | 23:afadd6b988a2 | 158 | byte=byte | 0x0F; |
shekhar | 23:afadd6b988a2 | 159 | } |
shekhar | 23:afadd6b988a2 | 160 | spi.write(byte); |
shekhar | 23:afadd6b988a2 | 161 | } |
shekhar | 23:afadd6b988a2 | 162 | } |
shekhar | 23:afadd6b988a2 | 163 | |
shekhar | 23:afadd6b988a2 | 164 | cs = 1; |
shekhar | 23:afadd6b988a2 | 165 | |
shekhar | 23:afadd6b988a2 | 166 | //Set to Tx mode |
shekhar | 23:afadd6b988a2 | 167 | writereg(RF22_REG_07_OPERATING_MODE1,0x09); |
shekhar | 23:afadd6b988a2 | 168 | wait(0.1); |
shekhar | 23:afadd6b988a2 | 169 | //Check for fifoThresh |
shekhar | 23:afadd6b988a2 | 170 | while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break; |
shekhar | 23:afadd6b988a2 | 171 | timeout_count=10e5; |
shekhar | 23:afadd6b988a2 | 172 | |
shekhar | 23:afadd6b988a2 | 173 | //Check for packet_sent |
shekhar | 23:afadd6b988a2 | 174 | while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break; |
shekhar | 23:afadd6b988a2 | 175 | |
shekhar | 23:afadd6b988a2 | 176 | //pc.printf("Short packet sent\r\n"); |
shekhar | 23:afadd6b988a2 | 177 | |
shekhar | 23:afadd6b988a2 | 178 | writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode |
shekhar | 23:afadd6b988a2 | 179 | |
shekhar | 23:afadd6b988a2 | 180 | } |
shekhar | 23:afadd6b988a2 | 181 | void LONG_BCN_TX() |
shekhar | 23:afadd6b988a2 | 182 | { |
shekhar | 23:afadd6b988a2 | 183 | writereg(RF22_REG_6E_TX_DATA_RATE,0x04); |
shekhar | 23:afadd6b988a2 | 184 | writereg(RF22_REG_6F_TX_DATA_RATE,0xEA);//600 bps |
shekhar | 23:afadd6b988a2 | 185 | writereg(RF22_REG_3E_PACKET_LENGTH,LONG_TX_DATA); //long packet length |
shekhar | 23:afadd6b988a2 | 186 | wait(0.02); |
shekhar | 23:afadd6b988a2 | 187 | uint32_t timeout_count=10e5; |
shekhar | 23:afadd6b988a2 | 188 | |
shekhar | 23:afadd6b988a2 | 189 | //get long_beacon array |
shekhar | 23:afadd6b988a2 | 190 | |
shekhar | 23:afadd6b988a2 | 191 | uint8_t Long_beacon[75]; |
shekhar | 23:afadd6b988a2 | 192 | for(int i = 0;i<75;i++) |
shekhar | 23:afadd6b988a2 | 193 | { |
shekhar | 23:afadd6b988a2 | 194 | Long_beacon[i] = 0xA0; |
shekhar | 23:afadd6b988a2 | 195 | } |
shekhar | 23:afadd6b988a2 | 196 | |
shekhar | 23:afadd6b988a2 | 197 | |
shekhar | 23:afadd6b988a2 | 198 | |
shekhar | 23:afadd6b988a2 | 199 | |
shekhar | 23:afadd6b988a2 | 200 | //setModeIdle(); |
shekhar | 23:afadd6b988a2 | 201 | writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode |
shekhar | 23:afadd6b988a2 | 202 | clearTxBuf(); |
shekhar | 23:afadd6b988a2 | 203 | //writing data first time |
shekhar | 23:afadd6b988a2 | 204 | cs = 0; |
shekhar | 23:afadd6b988a2 | 205 | spi.write(0xFF); |
shekhar | 23:afadd6b988a2 | 206 | for(int i=0; i<60;i++) |
shekhar | 23:afadd6b988a2 | 207 | { |
shekhar | 23:afadd6b988a2 | 208 | spi.write(Long_beacon[i]); |
shekhar | 23:afadd6b988a2 | 209 | } |
shekhar | 23:afadd6b988a2 | 210 | cs = 1; |
shekhar | 23:afadd6b988a2 | 211 | |
shekhar | 23:afadd6b988a2 | 212 | //Set to Tx mode |
shekhar | 23:afadd6b988a2 | 213 | writereg(RF22_REG_07_OPERATING_MODE1,0x09); |
shekhar | 23:afadd6b988a2 | 214 | wait(0.1); |
shekhar | 23:afadd6b988a2 | 215 | |
shekhar | 23:afadd6b988a2 | 216 | //Check for fifoThresh |
shekhar | 23:afadd6b988a2 | 217 | while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break; |
shekhar | 23:afadd6b988a2 | 218 | timeout_count=10e5; |
shekhar | 23:afadd6b988a2 | 219 | |
shekhar | 23:afadd6b988a2 | 220 | cs = 0; |
shekhar | 23:afadd6b988a2 | 221 | spi.write(0xFF); |
shekhar | 23:afadd6b988a2 | 222 | for(int i=60; i<75;i++) |
shekhar | 23:afadd6b988a2 | 223 | { |
shekhar | 23:afadd6b988a2 | 224 | spi.write(Long_beacon[i]); |
shekhar | 23:afadd6b988a2 | 225 | } |
shekhar | 23:afadd6b988a2 | 226 | cs = 1; |
shekhar | 23:afadd6b988a2 | 227 | wait(0.1); |
shekhar | 23:afadd6b988a2 | 228 | //Check for fifoThresh |
shekhar | 23:afadd6b988a2 | 229 | while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break; |
shekhar | 23:afadd6b988a2 | 230 | timeout_count=10e5; |
shekhar | 23:afadd6b988a2 | 231 | |
shekhar | 23:afadd6b988a2 | 232 | //Check for packetsent interrupt |
shekhar | 23:afadd6b988a2 | 233 | while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break; |
shekhar | 23:afadd6b988a2 | 234 | |
shekhar | 23:afadd6b988a2 | 235 | //pc.printf("Long packet sent\r\n"); |
shekhar | 23:afadd6b988a2 | 236 | |
shekhar | 23:afadd6b988a2 | 237 | writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode |
shekhar | 23:afadd6b988a2 | 238 | } |
shekhar | 26:89b6bb0859a1 | 239 | void reset_rfm() |
shekhar | 23:afadd6b988a2 | 240 | { |
shekhar | 23:afadd6b988a2 | 241 | FCTN_BCN_INIT(); |
shekhar | 23:afadd6b988a2 | 242 | } |
shekhar | 23:afadd6b988a2 | 243 | void writereg(uint8_t reg,uint8_t val) |
shekhar | 23:afadd6b988a2 | 244 | { |
shekhar | 23:afadd6b988a2 | 245 | uint8_t count = 0; |
shekhar | 23:afadd6b988a2 | 246 | for(;;count++) |
shekhar | 23:afadd6b988a2 | 247 | { |
shekhar | 23:afadd6b988a2 | 248 | int read_val =0; cs = 0;spi.write(reg | 0x80);spi.write(val);cs = 1; |
shekhar | 23:afadd6b988a2 | 249 | if(reg != 0x7 && reg != 0x58 && reg != 0xF) |
shekhar | 23:afadd6b988a2 | 250 | { |
shekhar | 23:afadd6b988a2 | 251 | read_val = readreg(reg); |
shekhar | 23:afadd6b988a2 | 252 | if (read_val == val) |
shekhar | 23:afadd6b988a2 | 253 | { |
shekhar | 23:afadd6b988a2 | 254 | break; |
shekhar | 23:afadd6b988a2 | 255 | } |
shekhar | 23:afadd6b988a2 | 256 | else if(count == 5) |
shekhar | 23:afadd6b988a2 | 257 | { |
shekhar | 26:89b6bb0859a1 | 258 | reset_rfm(), printf("reg = 0x%X\n",reg);break; |
shekhar | 23:afadd6b988a2 | 259 | } |
shekhar | 23:afadd6b988a2 | 260 | } |
shekhar | 23:afadd6b988a2 | 261 | else |
shekhar | 23:afadd6b988a2 | 262 | break; |
shekhar | 23:afadd6b988a2 | 263 | } |
shekhar | 23:afadd6b988a2 | 264 | } |
shekhar | 23:afadd6b988a2 | 265 | uint8_t readreg(uint8_t reg) |
shekhar | 23:afadd6b988a2 | 266 | { |
shekhar | 23:afadd6b988a2 | 267 | uint8_t val;cs = 0;spi.write(reg & ~0x80);val = spi.write(0);cs = 1;return val; |
shekhar | 23:afadd6b988a2 | 268 | } |
shekhar | 23:afadd6b988a2 | 269 | void clearTxBuf() |
shekhar | 23:afadd6b988a2 | 270 | { |
shekhar | 23:afadd6b988a2 | 271 | writereg(RF22_REG_08_OPERATING_MODE2,0x01); |
shekhar | 23:afadd6b988a2 | 272 | writereg(RF22_REG_08_OPERATING_MODE2,0x00); |
shekhar | 23:afadd6b988a2 | 273 | } |
shekhar | 23:afadd6b988a2 | 274 | uint8_t setFrequency(double centre,float afcPullInRange) |
shekhar | 23:afadd6b988a2 | 275 | { |
shekhar | 23:afadd6b988a2 | 276 | uint8_t fbsel = 0x40; |
shekhar | 23:afadd6b988a2 | 277 | uint8_t afclimiter; |
shekhar | 23:afadd6b988a2 | 278 | if (centre >= 480.0) { |
shekhar | 23:afadd6b988a2 | 279 | centre /= 2; |
shekhar | 23:afadd6b988a2 | 280 | fbsel |= 0x20; |
shekhar | 23:afadd6b988a2 | 281 | afclimiter = afcPullInRange * 1000000.0 / 1250.0; |
shekhar | 23:afadd6b988a2 | 282 | } else { |
shekhar | 23:afadd6b988a2 | 283 | if (afcPullInRange < 0.0 || afcPullInRange > 0.159375) |
shekhar | 23:afadd6b988a2 | 284 | return false; |
shekhar | 23:afadd6b988a2 | 285 | afclimiter = afcPullInRange * 1000000.0 / 625.0; |
shekhar | 23:afadd6b988a2 | 286 | } |
shekhar | 23:afadd6b988a2 | 287 | centre /= 10.0; |
shekhar | 23:afadd6b988a2 | 288 | double integerPart = floor(centre); |
shekhar | 23:afadd6b988a2 | 289 | double fractionalPart = centre - integerPart; |
shekhar | 23:afadd6b988a2 | 290 | |
shekhar | 23:afadd6b988a2 | 291 | uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23 |
shekhar | 23:afadd6b988a2 | 292 | fbsel |= fb; |
shekhar | 23:afadd6b988a2 | 293 | uint16_t fc = fractionalPart * 64000; |
shekhar | 23:afadd6b988a2 | 294 | writereg(RF22_REG_73_FREQUENCY_OFFSET1, 0); // REVISIT |
shekhar | 23:afadd6b988a2 | 295 | writereg(RF22_REG_74_FREQUENCY_OFFSET2, 0); |
shekhar | 23:afadd6b988a2 | 296 | writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel); |
shekhar | 23:afadd6b988a2 | 297 | writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8); |
shekhar | 23:afadd6b988a2 | 298 | writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff); |
shekhar | 23:afadd6b988a2 | 299 | writereg(RF22_REG_2A_AFC_LIMITER, afclimiter); |
shekhar | 23:afadd6b988a2 | 300 | return 0; |
shekhar | 23:afadd6b988a2 | 301 | } |
shekhar | 23:afadd6b988a2 | 302 | |
shekhar | 23:afadd6b988a2 | 303 | |
shekhar | 23:afadd6b988a2 | 304 | void Init_BEACON_HW() |
shekhar | 23:afadd6b988a2 | 305 | { |
shekhar | 23:afadd6b988a2 | 306 | wait(0.1); |
shekhar | 23:afadd6b988a2 | 307 | cs=1; // chip must be deselected |
shekhar | 23:afadd6b988a2 | 308 | wait(0.1); |
shekhar | 23:afadd6b988a2 | 309 | spi.format(8,0); |
shekhar | 23:afadd6b988a2 | 310 | spi.frequency(10000000); //10MHz SCLK |
shekhar | 23:afadd6b988a2 | 311 | |
shekhar | 23:afadd6b988a2 | 312 | //should either have a flag for invalid SPI or discard this for actual case or add reset |
shekhar | 23:afadd6b988a2 | 313 | if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08) |
shekhar | 23:afadd6b988a2 | 314 | pc.printf("spi connection valid\r\n"); |
shekhar | 23:afadd6b988a2 | 315 | else |
shekhar | 23:afadd6b988a2 | 316 | {pc.printf("error in spi connection\r\n"); |
shekhar | 26:89b6bb0859a1 | 317 | reset_rfm(); |
shekhar | 23:afadd6b988a2 | 318 | } |
shekhar | 23:afadd6b988a2 | 319 | |
shekhar | 23:afadd6b988a2 | 320 | writereg(RF22_REG_07_OPERATING_MODE1,0x80); //sw_reset |
shekhar | 23:afadd6b988a2 | 321 | wait(0.1); //takes time to reset |
shekhar | 23:afadd6b988a2 | 322 | |
shekhar | 23:afadd6b988a2 | 323 | clearTxBuf(); |
shekhar | 23:afadd6b988a2 | 324 | |
shekhar | 23:afadd6b988a2 | 325 | writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode |
shekhar | 23:afadd6b988a2 | 326 | |
shekhar | 23:afadd6b988a2 | 327 | //txfifoalmostempty |
shekhar | 23:afadd6b988a2 | 328 | writereg(RF22_REG_7D_TX_FIFO_CONTROL2,30); |
shekhar | 23:afadd6b988a2 | 329 | |
shekhar | 23:afadd6b988a2 | 330 | //Packet-engine registers |
shekhar | 23:afadd6b988a2 | 331 | writereg(RF22_REG_30_DATA_ACCESS_CONTROL,0x00); |
shekhar | 23:afadd6b988a2 | 332 | |
shekhar | 23:afadd6b988a2 | 333 | writereg(RF22_REG_33_HEADER_CONTROL2,0x08); |
shekhar | 23:afadd6b988a2 | 334 | writereg(RF22_REG_34_PREAMBLE_LENGTH,0x00); |
shekhar | 23:afadd6b988a2 | 335 | |
shekhar | 23:afadd6b988a2 | 336 | writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state |
shekhar | 23:afadd6b988a2 | 337 | writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state |
shekhar | 23:afadd6b988a2 | 338 | |
shekhar | 24:3d8e70286f67 | 339 | setFrequency(435.0,0.05); |
shekhar | 23:afadd6b988a2 | 340 | |
shekhar | 23:afadd6b988a2 | 341 | if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00) |
shekhar | 23:afadd6b988a2 | 342 | { |
shekhar | 23:afadd6b988a2 | 343 | pc.printf("frequency not set properly\r\n"); |
shekhar | 26:89b6bb0859a1 | 344 | reset_rfm(); |
shekhar | 23:afadd6b988a2 | 345 | } |
shekhar | 23:afadd6b988a2 | 346 | |
shekhar | 23:afadd6b988a2 | 347 | //set Modem Configuration |
shekhar | 23:afadd6b988a2 | 348 | writereg(RF22_REG_1C_IF_FILTER_BANDWIDTH,0xdf); |
shekhar | 23:afadd6b988a2 | 349 | writereg(RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE,0x03); |
shekhar | 23:afadd6b988a2 | 350 | writereg(RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE,0x39); |
shekhar | 23:afadd6b988a2 | 351 | writereg(RF22_REG_21_CLOCK_RECOVERY_OFFSET2,0x20); |
shekhar | 23:afadd6b988a2 | 352 | 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 | 23:afadd6b988a2 | 353 | writereg(RF22_REG_23_CLOCK_RECOVERY_OFFSET0,0xdc); |
shekhar | 23:afadd6b988a2 | 354 | writereg(RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1,0x00); |
shekhar | 23:afadd6b988a2 | 355 | writereg(RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0,0x6B); |
shekhar | 23:afadd6b988a2 | 356 | writereg(RF22_REG_2C_OOK_COUNTER_VALUE_1,0x2C); |
shekhar | 23:afadd6b988a2 | 357 | writereg(RF22_REG_2D_OOK_COUNTER_VALUE_2,0x11); //not required for fsk (OOK counter value) |
shekhar | 23:afadd6b988a2 | 358 | writereg(RF22_REG_2E_SLICER_PEAK_HOLD,0x2A); //?? |
shekhar | 23:afadd6b988a2 | 359 | writereg(RF22_REG_58,0x80); |
shekhar | 23:afadd6b988a2 | 360 | writereg(RF22_REG_69_AGC_OVERRIDE1,0x60); |
shekhar | 23:afadd6b988a2 | 361 | |
shekhar | 23:afadd6b988a2 | 362 | //Data rate set later |
shekhar | 23:afadd6b988a2 | 363 | |
shekhar | 23:afadd6b988a2 | 364 | writereg(RF22_REG_70_MODULATION_CONTROL1,0x20); |
shekhar | 23:afadd6b988a2 | 365 | writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//ook = 0x21 |
shekhar | 23:afadd6b988a2 | 366 | |
shekhar | 23:afadd6b988a2 | 367 | //set tx power |
shekhar | 23:afadd6b988a2 | 368 | writereg(RF22_REG_6D_TX_POWER,0x07); //20dbm |
shekhar | 23:afadd6b988a2 | 369 | |
shekhar | 23:afadd6b988a2 | 370 | //TX_packet_length written later |
shekhar | 23:afadd6b988a2 | 371 | } |
shekhar | 23:afadd6b988a2 | 372 | bool Check_ACK_RECEIVED() |
shekhar | 23:afadd6b988a2 | 373 | { |
shekhar | 23:afadd6b988a2 | 374 | if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04) |
shekhar | 23:afadd6b988a2 | 375 | { |
shekhar | 23:afadd6b988a2 | 376 | printf("Packet sent: ACK received\r\n"); |
shekhar | 23:afadd6b988a2 | 377 | return 1; |
shekhar | 23:afadd6b988a2 | 378 | } |
shekhar | 23:afadd6b988a2 | 379 | else |
shekhar | 23:afadd6b988a2 | 380 | { |
shekhar | 23:afadd6b988a2 | 381 | pc.printf("Packet not sent\r\n"); |
shekhar | 23:afadd6b988a2 | 382 | return 0; |
shekhar | 23:afadd6b988a2 | 383 | } |
shekhar | 23:afadd6b988a2 | 384 | } |
shekhar | 24:3d8e70286f67 | 385 | int main() |
shekhar | 23:afadd6b988a2 | 386 | { |
shekhar | 23:afadd6b988a2 | 387 | FCTN_BCN_INIT(); |
shekhar | 23:afadd6b988a2 | 388 | |
shekhar | 23:afadd6b988a2 | 389 | loop.attach(&FCTN_BCN_TX_MAIN, 10.0);//in actual case its 30.0 |
shekhar | 23:afadd6b988a2 | 390 | |
shekhar | 23:afadd6b988a2 | 391 | while(1); |
shekhar | 23:afadd6b988a2 | 392 | |
shekhar | 24:3d8e70286f67 | 393 | } |