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