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