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