Sample of program breaking when a certain set of source files are in a folder, but is fine when it is in the root. In this case, it is tested with RF12B.cpp, RF12B.h and rfdefs
Kalman/RFSRF05/RF12B.cpp@0:349dc9b0984f, 2012-03-25 (annotated)
- Committer:
- narshu
- Date:
- Sun Mar 25 13:39:11 2012 +0000
- Revision:
- 0:349dc9b0984f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narshu | 0:349dc9b0984f | 1 | #include "RF12B.h" |
narshu | 0:349dc9b0984f | 2 | |
narshu | 0:349dc9b0984f | 3 | #include "RF_defs.h" |
narshu | 0:349dc9b0984f | 4 | #include <algorithm> |
narshu | 0:349dc9b0984f | 5 | |
narshu | 0:349dc9b0984f | 6 | DigitalOut DBG3(LED3); |
narshu | 0:349dc9b0984f | 7 | |
narshu | 0:349dc9b0984f | 8 | RF12B::RF12B(PinName _SDI, |
narshu | 0:349dc9b0984f | 9 | PinName _SDO, |
narshu | 0:349dc9b0984f | 10 | PinName _SCK, |
narshu | 0:349dc9b0984f | 11 | PinName _NCS, |
narshu | 0:349dc9b0984f | 12 | PinName _NIRQ):spi(_SDI, _SDO, _SCK), |
narshu | 0:349dc9b0984f | 13 | NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ){// rfled(LED3) { |
narshu | 0:349dc9b0984f | 14 | |
narshu | 0:349dc9b0984f | 15 | /* SPI frequency, word lenght, polarity and phase */ |
narshu | 0:349dc9b0984f | 16 | spi.format(16,0); |
narshu | 0:349dc9b0984f | 17 | spi.frequency(2000000); |
narshu | 0:349dc9b0984f | 18 | |
narshu | 0:349dc9b0984f | 19 | /* Set ~CS high */ |
narshu | 0:349dc9b0984f | 20 | NCS = 1; |
narshu | 0:349dc9b0984f | 21 | |
narshu | 0:349dc9b0984f | 22 | /* Initialise RF Module */ |
narshu | 0:349dc9b0984f | 23 | init(); |
narshu | 0:349dc9b0984f | 24 | |
narshu | 0:349dc9b0984f | 25 | /* Setup interrupt to happen on falling edge of NIRQ */ |
narshu | 0:349dc9b0984f | 26 | NIRQ.fall(this, &RF12B::rxISR); |
narshu | 0:349dc9b0984f | 27 | |
narshu | 0:349dc9b0984f | 28 | rxISR(); |
narshu | 0:349dc9b0984f | 29 | |
narshu | 0:349dc9b0984f | 30 | } |
narshu | 0:349dc9b0984f | 31 | |
narshu | 0:349dc9b0984f | 32 | /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/ |
narshu | 0:349dc9b0984f | 33 | unsigned int RF12B::available() { |
narshu | 0:349dc9b0984f | 34 | return fifo.size(); |
narshu | 0:349dc9b0984f | 35 | } |
narshu | 0:349dc9b0984f | 36 | |
narshu | 0:349dc9b0984f | 37 | /* Reads a packet of data, with length "size" Returns false if read failed. TODO: make a metafifo to isolate packets*/ |
narshu | 0:349dc9b0984f | 38 | bool RF12B::read(unsigned char* data, unsigned int size) { |
narshu | 0:349dc9b0984f | 39 | if (fifo.size() == 0) { |
narshu | 0:349dc9b0984f | 40 | return false; |
narshu | 0:349dc9b0984f | 41 | } else { |
narshu | 0:349dc9b0984f | 42 | unsigned int i = 0; |
narshu | 0:349dc9b0984f | 43 | while (fifo.size() > 0 && i < size) { |
narshu | 0:349dc9b0984f | 44 | data[i++] = fifo.front(); |
narshu | 0:349dc9b0984f | 45 | fifo.pop(); |
narshu | 0:349dc9b0984f | 46 | } |
narshu | 0:349dc9b0984f | 47 | return true; |
narshu | 0:349dc9b0984f | 48 | } |
narshu | 0:349dc9b0984f | 49 | } |
narshu | 0:349dc9b0984f | 50 | |
narshu | 0:349dc9b0984f | 51 | /* Reads a byte of data from the receive buffer */ |
narshu | 0:349dc9b0984f | 52 | unsigned char RF12B::read() { |
narshu | 0:349dc9b0984f | 53 | if (available()) { |
narshu | 0:349dc9b0984f | 54 | unsigned char data = fifo.front(); |
narshu | 0:349dc9b0984f | 55 | fifo.pop(); |
narshu | 0:349dc9b0984f | 56 | return data; |
narshu | 0:349dc9b0984f | 57 | } else { |
narshu | 0:349dc9b0984f | 58 | return 0xFF; // Error val although could also be data... |
narshu | 0:349dc9b0984f | 59 | } |
narshu | 0:349dc9b0984f | 60 | } |
narshu | 0:349dc9b0984f | 61 | |
narshu | 0:349dc9b0984f | 62 | /* Sends a packet of data to the RF module for transmission TODO: Make asych*/ |
narshu | 0:349dc9b0984f | 63 | void RF12B::write(unsigned char *data, unsigned char length) { |
narshu | 0:349dc9b0984f | 64 | unsigned char crc = 0; |
narshu | 0:349dc9b0984f | 65 | |
narshu | 0:349dc9b0984f | 66 | /* Transmitter mode */ |
narshu | 0:349dc9b0984f | 67 | changeMode(TX); |
narshu | 0:349dc9b0984f | 68 | |
narshu | 0:349dc9b0984f | 69 | writeCmd(0x0000); |
narshu | 0:349dc9b0984f | 70 | send(0xAA); // PREAMBLE |
narshu | 0:349dc9b0984f | 71 | send(0xAA); |
narshu | 0:349dc9b0984f | 72 | send(0xAA); |
narshu | 0:349dc9b0984f | 73 | send(0x2D); // SYNC |
narshu | 0:349dc9b0984f | 74 | send(0xD4); |
narshu | 0:349dc9b0984f | 75 | /* Packet Length */ |
narshu | 0:349dc9b0984f | 76 | send(length); |
narshu | 0:349dc9b0984f | 77 | crc = crc8(crc, length); |
narshu | 0:349dc9b0984f | 78 | send(crc); |
narshu | 0:349dc9b0984f | 79 | crc = crc8(crc, crc); |
narshu | 0:349dc9b0984f | 80 | /* Packet Data */ |
narshu | 0:349dc9b0984f | 81 | for (unsigned char i=0; i<length; i++) { |
narshu | 0:349dc9b0984f | 82 | send(data[i]); |
narshu | 0:349dc9b0984f | 83 | crc = crc8(crc, data[i]); |
narshu | 0:349dc9b0984f | 84 | } |
narshu | 0:349dc9b0984f | 85 | send(crc); |
narshu | 0:349dc9b0984f | 86 | send(0xAA); // DUMMY BYTES |
narshu | 0:349dc9b0984f | 87 | send(0xAA); |
narshu | 0:349dc9b0984f | 88 | send(0xAA); |
narshu | 0:349dc9b0984f | 89 | |
narshu | 0:349dc9b0984f | 90 | /* Back to receiver mode */ |
narshu | 0:349dc9b0984f | 91 | changeMode(RX); |
narshu | 0:349dc9b0984f | 92 | status(); |
narshu | 0:349dc9b0984f | 93 | |
narshu | 0:349dc9b0984f | 94 | |
narshu | 0:349dc9b0984f | 95 | } |
narshu | 0:349dc9b0984f | 96 | |
narshu | 0:349dc9b0984f | 97 | /* Transmit a 1-byte data packet */ |
narshu | 0:349dc9b0984f | 98 | void RF12B::write(unsigned char data) { |
narshu | 0:349dc9b0984f | 99 | write(&data, 1); |
narshu | 0:349dc9b0984f | 100 | } |
narshu | 0:349dc9b0984f | 101 | |
narshu | 0:349dc9b0984f | 102 | void RF12B::write(queue<char> &data, int length) { |
narshu | 0:349dc9b0984f | 103 | char crc = 0; |
narshu | 0:349dc9b0984f | 104 | char length_byte = 0; |
narshu | 0:349dc9b0984f | 105 | |
narshu | 0:349dc9b0984f | 106 | /* -1 means try to transmit everything in the queue */ |
narshu | 0:349dc9b0984f | 107 | if(length == -1) { |
narshu | 0:349dc9b0984f | 108 | length = data.size(); |
narshu | 0:349dc9b0984f | 109 | } |
narshu | 0:349dc9b0984f | 110 | |
narshu | 0:349dc9b0984f | 111 | /* max length of packet is 255 */ |
narshu | 0:349dc9b0984f | 112 | length_byte = min(length, 255); |
narshu | 0:349dc9b0984f | 113 | |
narshu | 0:349dc9b0984f | 114 | /* Transmitter mode */ |
narshu | 0:349dc9b0984f | 115 | changeMode(TX); |
narshu | 0:349dc9b0984f | 116 | |
narshu | 0:349dc9b0984f | 117 | writeCmd(0x0000); |
narshu | 0:349dc9b0984f | 118 | send(0xAA); // PREAMBLE |
narshu | 0:349dc9b0984f | 119 | send(0xAA); |
narshu | 0:349dc9b0984f | 120 | send(0xAA); |
narshu | 0:349dc9b0984f | 121 | send(0x2D); // SYNC |
narshu | 0:349dc9b0984f | 122 | send(0xD4); |
narshu | 0:349dc9b0984f | 123 | /* Packet Length */ |
narshu | 0:349dc9b0984f | 124 | send(length_byte); |
narshu | 0:349dc9b0984f | 125 | crc = crc8(crc, length_byte); |
narshu | 0:349dc9b0984f | 126 | send(crc); |
narshu | 0:349dc9b0984f | 127 | crc = crc8(crc, crc); |
narshu | 0:349dc9b0984f | 128 | /* Packet Data */ |
narshu | 0:349dc9b0984f | 129 | for (char i=0; i<length_byte; i++) { |
narshu | 0:349dc9b0984f | 130 | send(data.front()); |
narshu | 0:349dc9b0984f | 131 | crc = crc8(crc, data.front()); |
narshu | 0:349dc9b0984f | 132 | data.pop(); |
narshu | 0:349dc9b0984f | 133 | } |
narshu | 0:349dc9b0984f | 134 | send(crc); |
narshu | 0:349dc9b0984f | 135 | send(0xAA); // DUMMY BYTES |
narshu | 0:349dc9b0984f | 136 | send(0xAA); |
narshu | 0:349dc9b0984f | 137 | send(0xAA); |
narshu | 0:349dc9b0984f | 138 | |
narshu | 0:349dc9b0984f | 139 | /* Back to receiver mode */ |
narshu | 0:349dc9b0984f | 140 | changeMode(RX); |
narshu | 0:349dc9b0984f | 141 | status(); |
narshu | 0:349dc9b0984f | 142 | } |
narshu | 0:349dc9b0984f | 143 | |
narshu | 0:349dc9b0984f | 144 | /********************************************************************** |
narshu | 0:349dc9b0984f | 145 | * PRIVATE FUNCTIONS |
narshu | 0:349dc9b0984f | 146 | *********************************************************************/ |
narshu | 0:349dc9b0984f | 147 | |
narshu | 0:349dc9b0984f | 148 | /* Initialises the RF12B module */ |
narshu | 0:349dc9b0984f | 149 | void RF12B::init() { |
narshu | 0:349dc9b0984f | 150 | /* writeCmd(0x80E7); //EL,EF,868band,12.0pF |
narshu | 0:349dc9b0984f | 151 | changeMode(RX); |
narshu | 0:349dc9b0984f | 152 | writeCmd(0xA640); //frequency select |
narshu | 0:349dc9b0984f | 153 | writeCmd(0xC647); //4.8kbps |
narshu | 0:349dc9b0984f | 154 | writeCmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm |
narshu | 0:349dc9b0984f | 155 | writeCmd(0xC2AC); //AL,!ml,DIG,DQD4 |
narshu | 0:349dc9b0984f | 156 | writeCmd(0xCA81); //FIFO8,SYNC,!ff,DR |
narshu | 0:349dc9b0984f | 157 | writeCmd(0xCED4); //SYNC=2DD4 |
narshu | 0:349dc9b0984f | 158 | writeCmd(0xC483); //@PWR,NO RSTRIC,!st,!fi,OE,EN |
narshu | 0:349dc9b0984f | 159 | writeCmd(0x9850); //!mp,90kHz,MAX OUT |
narshu | 0:349dc9b0984f | 160 | writeCmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDIT�CBW0 |
narshu | 0:349dc9b0984f | 161 | writeCmd(0xE000); //NOT USED |
narshu | 0:349dc9b0984f | 162 | writeCmd(0xC800); //NOT USED |
narshu | 0:349dc9b0984f | 163 | writeCmd(0xC040); //1.66MHz,2.2V */ |
narshu | 0:349dc9b0984f | 164 | |
narshu | 0:349dc9b0984f | 165 | writeCmd( |
narshu | 0:349dc9b0984f | 166 | RFM_CONFIG_EL | |
narshu | 0:349dc9b0984f | 167 | RFM_CONFIG_EF | |
narshu | 0:349dc9b0984f | 168 | RFM_CONFIG_BAND_433 //| |
narshu | 0:349dc9b0984f | 169 | //RFM_CONFIG_X_11_0pf // meh, using default |
narshu | 0:349dc9b0984f | 170 | ); |
narshu | 0:349dc9b0984f | 171 | |
narshu | 0:349dc9b0984f | 172 | // 2. Power Management Command |
narshu | 0:349dc9b0984f | 173 | // leave everything switched off for now |
narshu | 0:349dc9b0984f | 174 | /* |
narshu | 0:349dc9b0984f | 175 | writeCmd( |
narshu | 0:349dc9b0984f | 176 | RFM_POWER_MANAGEMENT // switch all off |
narshu | 0:349dc9b0984f | 177 | ); |
narshu | 0:349dc9b0984f | 178 | */ |
narshu | 0:349dc9b0984f | 179 | |
narshu | 0:349dc9b0984f | 180 | // 3. Frequency Setting Command |
narshu | 0:349dc9b0984f | 181 | writeCmd( |
narshu | 0:349dc9b0984f | 182 | RFM_FREQUENCY | |
narshu | 0:349dc9b0984f | 183 | RFM_FREQ_433Band(435.7) //I totally made this value up... if someone knows where the sweetspots are in this band, tell me! |
narshu | 0:349dc9b0984f | 184 | ); |
narshu | 0:349dc9b0984f | 185 | |
narshu | 0:349dc9b0984f | 186 | |
narshu | 0:349dc9b0984f | 187 | // 4. Data Rate Command |
narshu | 0:349dc9b0984f | 188 | writeCmd(RFM_DATA_RATE_9600); |
narshu | 0:349dc9b0984f | 189 | |
narshu | 0:349dc9b0984f | 190 | |
narshu | 0:349dc9b0984f | 191 | // 5. Receiver Control Command |
narshu | 0:349dc9b0984f | 192 | writeCmd( |
narshu | 0:349dc9b0984f | 193 | RFM_RX_CONTROL_P20_VDI | |
narshu | 0:349dc9b0984f | 194 | RFM_RX_CONTROL_VDI_FAST | |
narshu | 0:349dc9b0984f | 195 | //RFM_RX_CONTROL_BW(RFM_BAUD_RATE) | |
narshu | 0:349dc9b0984f | 196 | RFM_RX_CONTROL_BW_134 | // CHANGE THIS TO 67 TO IMPROVE RANGE! (though the bitrate must then be below 8kbaud, and fsk modulation changed) |
narshu | 0:349dc9b0984f | 197 | RFM_RX_CONTROL_GAIN_0 | |
narshu | 0:349dc9b0984f | 198 | RFM_RX_CONTROL_RSSI_103 // Might need adjustment. Datasheet says around 10^-5 bit error rate at this level and baudrate. |
narshu | 0:349dc9b0984f | 199 | ); |
narshu | 0:349dc9b0984f | 200 | |
narshu | 0:349dc9b0984f | 201 | // 6. Data Filter Command |
narshu | 0:349dc9b0984f | 202 | writeCmd( |
narshu | 0:349dc9b0984f | 203 | RFM_DATA_FILTER_AL | |
narshu | 0:349dc9b0984f | 204 | RFM_DATA_FILTER_ML | |
narshu | 0:349dc9b0984f | 205 | RFM_DATA_FILTER_DIG //| |
narshu | 0:349dc9b0984f | 206 | //RFM_DATA_FILTER_DQD(4) |
narshu | 0:349dc9b0984f | 207 | ); |
narshu | 0:349dc9b0984f | 208 | |
narshu | 0:349dc9b0984f | 209 | // 7. FIFO and Reset Mode Command |
narshu | 0:349dc9b0984f | 210 | writeCmd( |
narshu | 0:349dc9b0984f | 211 | RFM_FIFO_IT(8) | |
narshu | 0:349dc9b0984f | 212 | RFM_FIFO_DR | |
narshu | 0:349dc9b0984f | 213 | 0x8 //turn on 16bit sync word |
narshu | 0:349dc9b0984f | 214 | ); |
narshu | 0:349dc9b0984f | 215 | |
narshu | 0:349dc9b0984f | 216 | // 8. FIFO Syncword |
narshu | 0:349dc9b0984f | 217 | // Leave as default: 0xD4 |
narshu | 0:349dc9b0984f | 218 | |
narshu | 0:349dc9b0984f | 219 | // 9. Receiver FIFO Read |
narshu | 0:349dc9b0984f | 220 | // when the interupt goes high, (and if we can assume that it was a fifo fill interrupt) we can read a byte using: |
narshu | 0:349dc9b0984f | 221 | // result = RFM_READ_FIFO(); |
narshu | 0:349dc9b0984f | 222 | |
narshu | 0:349dc9b0984f | 223 | // 10. AFC Command |
narshu | 0:349dc9b0984f | 224 | writeCmd( |
narshu | 0:349dc9b0984f | 225 | //RFM_AFC_AUTO_VDI | //Note this might be changed to improve range. Refer to datasheet. |
narshu | 0:349dc9b0984f | 226 | RFM_AFC_AUTO_INDEPENDENT | |
narshu | 0:349dc9b0984f | 227 | RFM_AFC_RANGE_LIMIT_7_8 | |
narshu | 0:349dc9b0984f | 228 | RFM_AFC_EN | |
narshu | 0:349dc9b0984f | 229 | RFM_AFC_OE | |
narshu | 0:349dc9b0984f | 230 | RFM_AFC_FI |
narshu | 0:349dc9b0984f | 231 | ); |
narshu | 0:349dc9b0984f | 232 | |
narshu | 0:349dc9b0984f | 233 | // 11. TX Configuration Control Command |
narshu | 0:349dc9b0984f | 234 | writeCmd( |
narshu | 0:349dc9b0984f | 235 | RFM_TX_CONTROL_MOD_60 | |
narshu | 0:349dc9b0984f | 236 | RFM_TX_CONTROL_POW_0 |
narshu | 0:349dc9b0984f | 237 | ); |
narshu | 0:349dc9b0984f | 238 | |
narshu | 0:349dc9b0984f | 239 | |
narshu | 0:349dc9b0984f | 240 | // 12. PLL Setting Command |
narshu | 0:349dc9b0984f | 241 | writeCmd( |
narshu | 0:349dc9b0984f | 242 | 0xCC77 & ~0x01 // Setting the PLL bandwith, less noise, but max bitrate capped at 86.2 |
narshu | 0:349dc9b0984f | 243 | // I think this will slow down the pll's reaction time. Not sure, check with someone! |
narshu | 0:349dc9b0984f | 244 | ); |
narshu | 0:349dc9b0984f | 245 | |
narshu | 0:349dc9b0984f | 246 | changeMode(RX); |
narshu | 0:349dc9b0984f | 247 | resetRX(); |
narshu | 0:349dc9b0984f | 248 | status(); |
narshu | 0:349dc9b0984f | 249 | } |
narshu | 0:349dc9b0984f | 250 | |
narshu | 0:349dc9b0984f | 251 | /* Write a command to the RF Module */ |
narshu | 0:349dc9b0984f | 252 | unsigned int RF12B::writeCmd(unsigned int cmd) { |
narshu | 0:349dc9b0984f | 253 | NCS = 0; |
narshu | 0:349dc9b0984f | 254 | unsigned int recv = spi.write(cmd); |
narshu | 0:349dc9b0984f | 255 | NCS = 1; |
narshu | 0:349dc9b0984f | 256 | return recv; |
narshu | 0:349dc9b0984f | 257 | } |
narshu | 0:349dc9b0984f | 258 | |
narshu | 0:349dc9b0984f | 259 | /* Sends a byte of data across RF */ |
narshu | 0:349dc9b0984f | 260 | void RF12B::send(unsigned char data) { |
narshu | 0:349dc9b0984f | 261 | while (NIRQ); |
narshu | 0:349dc9b0984f | 262 | writeCmd(0xB800 + data); |
narshu | 0:349dc9b0984f | 263 | } |
narshu | 0:349dc9b0984f | 264 | |
narshu | 0:349dc9b0984f | 265 | /* Change the mode of the RF module to Transmitting or Receiving */ |
narshu | 0:349dc9b0984f | 266 | void RF12B::changeMode(rfmode_t _mode) { |
narshu | 0:349dc9b0984f | 267 | mode = _mode; |
narshu | 0:349dc9b0984f | 268 | if (_mode == TX) { |
narshu | 0:349dc9b0984f | 269 | writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC |
narshu | 0:349dc9b0984f | 270 | } else { /* mode == RX */ |
narshu | 0:349dc9b0984f | 271 | writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC |
narshu | 0:349dc9b0984f | 272 | } |
narshu | 0:349dc9b0984f | 273 | } |
narshu | 0:349dc9b0984f | 274 | |
narshu | 0:349dc9b0984f | 275 | /* Interrupt routine for data reception */ |
narshu | 0:349dc9b0984f | 276 | void RF12B::rxISR() { |
narshu | 0:349dc9b0984f | 277 | |
narshu | 0:349dc9b0984f | 278 | printf("junk\r\n"); |
narshu | 0:349dc9b0984f | 279 | DBG3 = 1; |
narshu | 0:349dc9b0984f | 280 | |
narshu | 0:349dc9b0984f | 281 | unsigned int data = 0; |
narshu | 0:349dc9b0984f | 282 | static int i = -2; |
narshu | 0:349dc9b0984f | 283 | static unsigned char packet_length = 0; |
narshu | 0:349dc9b0984f | 284 | static unsigned char crc = 0; |
narshu | 0:349dc9b0984f | 285 | static queue<unsigned char> temp; |
narshu | 0:349dc9b0984f | 286 | |
narshu | 0:349dc9b0984f | 287 | |
narshu | 0:349dc9b0984f | 288 | |
narshu | 0:349dc9b0984f | 289 | //Loop while interrupt is asserted |
narshu | 0:349dc9b0984f | 290 | while (!NIRQ_in && mode == RX) { |
narshu | 0:349dc9b0984f | 291 | |
narshu | 0:349dc9b0984f | 292 | /* Grab the packet's length byte */ |
narshu | 0:349dc9b0984f | 293 | if (i == -2) { |
narshu | 0:349dc9b0984f | 294 | data = writeCmd(0x0000); |
narshu | 0:349dc9b0984f | 295 | if ( (data&0x8000) ) { |
narshu | 0:349dc9b0984f | 296 | data = writeCmd(0xB000); |
narshu | 0:349dc9b0984f | 297 | packet_length = (data&0x00FF); |
narshu | 0:349dc9b0984f | 298 | crc = crc8(crc, packet_length); |
narshu | 0:349dc9b0984f | 299 | i++; |
narshu | 0:349dc9b0984f | 300 | } |
narshu | 0:349dc9b0984f | 301 | } |
narshu | 0:349dc9b0984f | 302 | |
narshu | 0:349dc9b0984f | 303 | //If we exhaust the interrupt, exit |
narshu | 0:349dc9b0984f | 304 | if (NIRQ_in) |
narshu | 0:349dc9b0984f | 305 | break; |
narshu | 0:349dc9b0984f | 306 | |
narshu | 0:349dc9b0984f | 307 | // Check that packet length was correct |
narshu | 0:349dc9b0984f | 308 | if (i == -1) { |
narshu | 0:349dc9b0984f | 309 | data = writeCmd(0x0000); |
narshu | 0:349dc9b0984f | 310 | if ( (data&0x8000) ) { |
narshu | 0:349dc9b0984f | 311 | data = writeCmd(0xB000); |
narshu | 0:349dc9b0984f | 312 | unsigned char crcofsize = (data&0x00FF); |
narshu | 0:349dc9b0984f | 313 | if (crcofsize != crc) { |
narshu | 0:349dc9b0984f | 314 | //It was wrong, start over |
narshu | 0:349dc9b0984f | 315 | i = -2; |
narshu | 0:349dc9b0984f | 316 | packet_length = 0; |
narshu | 0:349dc9b0984f | 317 | crc = 0; |
narshu | 0:349dc9b0984f | 318 | temp = queue<unsigned char>(); |
narshu | 0:349dc9b0984f | 319 | resetRX(); |
narshu | 0:349dc9b0984f | 320 | } else { |
narshu | 0:349dc9b0984f | 321 | crc = crc8(crc, crcofsize); |
narshu | 0:349dc9b0984f | 322 | i++; |
narshu | 0:349dc9b0984f | 323 | } |
narshu | 0:349dc9b0984f | 324 | } |
narshu | 0:349dc9b0984f | 325 | } |
narshu | 0:349dc9b0984f | 326 | |
narshu | 0:349dc9b0984f | 327 | //If we exhaust the interrupt, exit |
narshu | 0:349dc9b0984f | 328 | if (NIRQ_in) |
narshu | 0:349dc9b0984f | 329 | break; |
narshu | 0:349dc9b0984f | 330 | |
narshu | 0:349dc9b0984f | 331 | /* Grab the packet's data */ |
narshu | 0:349dc9b0984f | 332 | if (i >= 0 && i < packet_length) { |
narshu | 0:349dc9b0984f | 333 | data = writeCmd(0x0000); |
narshu | 0:349dc9b0984f | 334 | if ( (data&0x8000) ) { |
narshu | 0:349dc9b0984f | 335 | data = writeCmd(0xB000); |
narshu | 0:349dc9b0984f | 336 | temp.push(data&0x00FF); |
narshu | 0:349dc9b0984f | 337 | crc = crc8(crc, (unsigned char)(data&0x00FF)); |
narshu | 0:349dc9b0984f | 338 | i++; |
narshu | 0:349dc9b0984f | 339 | } |
narshu | 0:349dc9b0984f | 340 | } |
narshu | 0:349dc9b0984f | 341 | |
narshu | 0:349dc9b0984f | 342 | //If we exhaust the interrupt, exit |
narshu | 0:349dc9b0984f | 343 | if (NIRQ_in) |
narshu | 0:349dc9b0984f | 344 | break; |
narshu | 0:349dc9b0984f | 345 | |
narshu | 0:349dc9b0984f | 346 | if (i >= packet_length) { |
narshu | 0:349dc9b0984f | 347 | data = writeCmd(0x0000); |
narshu | 0:349dc9b0984f | 348 | if ( (data&0x8000) ) { |
narshu | 0:349dc9b0984f | 349 | data = writeCmd(0xB000); |
narshu | 0:349dc9b0984f | 350 | if ((unsigned char)(data & 0x00FF) == crc) { |
narshu | 0:349dc9b0984f | 351 | //If the checksum is correct, add our data to the end of the output buffer |
narshu | 0:349dc9b0984f | 352 | while (!temp.empty()) { |
narshu | 0:349dc9b0984f | 353 | fifo.push(temp.front()); |
narshu | 0:349dc9b0984f | 354 | temp.pop(); |
narshu | 0:349dc9b0984f | 355 | } |
narshu | 0:349dc9b0984f | 356 | } |
narshu | 0:349dc9b0984f | 357 | |
narshu | 0:349dc9b0984f | 358 | /* Tell RF Module we are finished, and clean up */ |
narshu | 0:349dc9b0984f | 359 | i = -2; |
narshu | 0:349dc9b0984f | 360 | packet_length = 0; |
narshu | 0:349dc9b0984f | 361 | crc = 0; |
narshu | 0:349dc9b0984f | 362 | temp = queue<unsigned char>(); |
narshu | 0:349dc9b0984f | 363 | resetRX(); |
narshu | 0:349dc9b0984f | 364 | } |
narshu | 0:349dc9b0984f | 365 | } |
narshu | 0:349dc9b0984f | 366 | } |
narshu | 0:349dc9b0984f | 367 | } |
narshu | 0:349dc9b0984f | 368 | |
narshu | 0:349dc9b0984f | 369 | unsigned int RF12B::status() { |
narshu | 0:349dc9b0984f | 370 | return writeCmd(0x0000); |
narshu | 0:349dc9b0984f | 371 | } |
narshu | 0:349dc9b0984f | 372 | |
narshu | 0:349dc9b0984f | 373 | /* Tell the RF Module this packet is received and wait for the next */ |
narshu | 0:349dc9b0984f | 374 | void RF12B::resetRX() { |
narshu | 0:349dc9b0984f | 375 | writeCmd(0xCA81); |
narshu | 0:349dc9b0984f | 376 | writeCmd(0xCA83); |
narshu | 0:349dc9b0984f | 377 | }; |
narshu | 0:349dc9b0984f | 378 | |
narshu | 0:349dc9b0984f | 379 | /* Calculate CRC8 */ |
narshu | 0:349dc9b0984f | 380 | unsigned char RF12B::crc8(unsigned char crc, unsigned char data) { |
narshu | 0:349dc9b0984f | 381 | crc = crc ^ data; |
narshu | 0:349dc9b0984f | 382 | for (int i = 0; i < 8; i++) { |
narshu | 0:349dc9b0984f | 383 | if (crc & 0x01) { |
narshu | 0:349dc9b0984f | 384 | crc = (crc >> 1) ^ 0x8C; |
narshu | 0:349dc9b0984f | 385 | } else { |
narshu | 0:349dc9b0984f | 386 | crc >>= 1; |
narshu | 0:349dc9b0984f | 387 | } |
narshu | 0:349dc9b0984f | 388 | } |
narshu | 0:349dc9b0984f | 389 | return crc; |
narshu | 0:349dc9b0984f | 390 | } |