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