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