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