Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Thu May 03 14:20:04 2012 +0000
Revision:
22:7ba09c0af0d0
Parent:
9:377560539b74
added 90sec timer and tigger

Who changed what in which revision?

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