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