Library to send and receive data using RF12B transceiver modules Big thanks to the tutorial at https://loee.jottit.com/rfm12b_and_avr_-_quick_start and madcowswe

Dependents:   Measure_system Quadcopter_copy

Committer:
harryeakins
Date:
Fri Mar 11 19:30:30 2011 +0000
Revision:
7:9f9e2a63a8a2
Parent:
6:98da0571ec31
Child:
8:6fc24b44e027
Including madcowswe\s changes

Who changed what in which revision?

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