Eurobot2012_Beacons

Committer:
narshu
Date:
Wed Oct 17 22:26:33 2012 +0000
Revision:
3:bf8a2e4b8012
Parent:
2:3ff84732828f
Commit before publishing

Who changed what in which revision?

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