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
RF12B.cpp@6:98da0571ec31, 2011-03-11 (annotated)
- Committer:
- harryeakins
- Date:
- Fri Mar 11 15:47:06 2011 +0000
- Revision:
- 6:98da0571ec31
- Parent:
- 5:a92c3f6d1711
- Child:
- 7:9f9e2a63a8a2
Added RFSerial
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:2a295db9ba1a | 28 | /* Returns true if receive buffer contains data */ |
harryeakins | 4:2a295db9ba1a | 29 | bool RF12B::available() { |
harryeakins | 4:2a295db9ba1a | 30 | return !fifo.empty(); |
harryeakins | 4:2a295db9ba1a | 31 | } |
harryeakins | 3:e72ad65868ab | 32 | |
harryeakins | 4:2a295db9ba1a | 33 | /* Reads a byte of data from the receive buffer */ |
harryeakins | 3:e72ad65868ab | 34 | unsigned char RF12B::read() { |
harryeakins | 4:2a295db9ba1a | 35 | if (available()) { |
harryeakins | 4:2a295db9ba1a | 36 | unsigned char data = fifo.front(); |
harryeakins | 4:2a295db9ba1a | 37 | fifo.pop(); |
harryeakins | 4:2a295db9ba1a | 38 | return data; |
harryeakins | 4:2a295db9ba1a | 39 | } else { |
harryeakins | 4:2a295db9ba1a | 40 | return 0xFF; // Error val although could also be data... |
harryeakins | 3:e72ad65868ab | 41 | } |
harryeakins | 3:e72ad65868ab | 42 | } |
harryeakins | 3:e72ad65868ab | 43 | |
harryeakins | 5:a92c3f6d1711 | 44 | /* Sends a packet of data to the RF module for transmission */ |
harryeakins | 4:2a295db9ba1a | 45 | void RF12B::write(unsigned char *data, unsigned char length) { |
harryeakins | 6:98da0571ec31 | 46 | unsigned char crc = 0; |
harryeakins | 4:2a295db9ba1a | 47 | /* Transmitter mode */ |
harryeakins | 4:2a295db9ba1a | 48 | changeMode(TX); |
harryeakins | 4:2a295db9ba1a | 49 | |
harryeakins | 3:e72ad65868ab | 50 | writeCmd(0x0000); |
harryeakins | 3:e72ad65868ab | 51 | send(0xAA); // PREAMBLE |
harryeakins | 3:e72ad65868ab | 52 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 53 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 54 | send(0x2D); // SYNC |
harryeakins | 3:e72ad65868ab | 55 | send(0xD4); |
harryeakins | 4:2a295db9ba1a | 56 | /* Packet Length */ |
harryeakins | 4:2a295db9ba1a | 57 | send(length); |
harryeakins | 6:98da0571ec31 | 58 | crc = crc8(crc, length); |
harryeakins | 4:2a295db9ba1a | 59 | /* Packet Data */ |
harryeakins | 4:2a295db9ba1a | 60 | for (unsigned char i=0; i<length; i++) { |
harryeakins | 3:e72ad65868ab | 61 | send(data[i]); |
harryeakins | 6:98da0571ec31 | 62 | crc = crc8(crc, data[i]); |
harryeakins | 3:e72ad65868ab | 63 | } |
harryeakins | 6:98da0571ec31 | 64 | send(crc); |
harryeakins | 3:e72ad65868ab | 65 | send(0xAA); // DUMMY BYTES |
harryeakins | 3:e72ad65868ab | 66 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 67 | send(0xAA); |
harryeakins | 3:e72ad65868ab | 68 | |
harryeakins | 4:2a295db9ba1a | 69 | /* Back to receiver mode */ |
harryeakins | 4:2a295db9ba1a | 70 | changeMode(RX); |
harryeakins | 5:a92c3f6d1711 | 71 | status(); |
harryeakins | 3:e72ad65868ab | 72 | } |
harryeakins | 3:e72ad65868ab | 73 | |
harryeakins | 4:2a295db9ba1a | 74 | /* Transmit a 1-byte data packet */ |
harryeakins | 4:2a295db9ba1a | 75 | void RF12B::write(unsigned char data) { |
harryeakins | 4:2a295db9ba1a | 76 | write(&data, 1); |
harryeakins | 3:e72ad65868ab | 77 | } |
harryeakins | 3:e72ad65868ab | 78 | |
harryeakins | 4:2a295db9ba1a | 79 | /********************************************************************** |
harryeakins | 4:2a295db9ba1a | 80 | * PRIVATE FUNCTIONS |
harryeakins | 4:2a295db9ba1a | 81 | *********************************************************************/ |
harryeakins | 3:e72ad65868ab | 82 | |
harryeakins | 4:2a295db9ba1a | 83 | /* Initialises the RF12B module */ |
harryeakins | 3:e72ad65868ab | 84 | void RF12B::init() { |
harryeakins | 6:98da0571ec31 | 85 | /* writeCmd(0x80E7); //EL,EF,868band,12.0pF |
harryeakins | 6:98da0571ec31 | 86 | changeMode(RX); |
harryeakins | 6:98da0571ec31 | 87 | writeCmd(0xA640); //frequency select |
harryeakins | 6:98da0571ec31 | 88 | writeCmd(0xC647); //4.8kbps |
harryeakins | 6:98da0571ec31 | 89 | writeCmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm |
harryeakins | 6:98da0571ec31 | 90 | writeCmd(0xC2AC); //AL,!ml,DIG,DQD4 |
harryeakins | 6:98da0571ec31 | 91 | writeCmd(0xCA81); //FIFO8,SYNC,!ff,DR |
harryeakins | 6:98da0571ec31 | 92 | writeCmd(0xCED4); //SYNC=2DD4 |
harryeakins | 6:98da0571ec31 | 93 | writeCmd(0xC483); //@PWR,NO RSTRIC,!st,!fi,OE,EN |
harryeakins | 6:98da0571ec31 | 94 | writeCmd(0x9850); //!mp,90kHz,MAX OUT |
harryeakins | 6:98da0571ec31 | 95 | writeCmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDIT�CBW0 |
harryeakins | 6:98da0571ec31 | 96 | writeCmd(0xE000); //NOT USED |
harryeakins | 6:98da0571ec31 | 97 | writeCmd(0xC800); //NOT USED |
harryeakins | 6:98da0571ec31 | 98 | writeCmd(0xC040); //1.66MHz,2.2V */ |
harryeakins | 6:98da0571ec31 | 99 | |
harryeakins | 6:98da0571ec31 | 100 | writeCmd( |
harryeakins | 6:98da0571ec31 | 101 | RFM_CONFIG_EL | |
harryeakins | 6:98da0571ec31 | 102 | RFM_CONFIG_EF | |
harryeakins | 6:98da0571ec31 | 103 | RFM_CONFIG_BAND_433 //| |
harryeakins | 6:98da0571ec31 | 104 | //RFM_CONFIG_X_11_0pf // meh, using default |
harryeakins | 6:98da0571ec31 | 105 | ); |
harryeakins | 6:98da0571ec31 | 106 | |
harryeakins | 6:98da0571ec31 | 107 | // 2. Power Management Command |
harryeakins | 6:98da0571ec31 | 108 | // leave everything switched off for now |
harryeakins | 6:98da0571ec31 | 109 | /* |
harryeakins | 6:98da0571ec31 | 110 | writeCmd( |
harryeakins | 6:98da0571ec31 | 111 | RFM_POWER_MANAGEMENT // switch all off |
harryeakins | 6:98da0571ec31 | 112 | ); |
harryeakins | 6:98da0571ec31 | 113 | */ |
harryeakins | 6:98da0571ec31 | 114 | |
harryeakins | 6:98da0571ec31 | 115 | // 3. Frequency Setting Command |
harryeakins | 6:98da0571ec31 | 116 | writeCmd( |
harryeakins | 6:98da0571ec31 | 117 | RFM_FREQUENCY | |
harryeakins | 6:98da0571ec31 | 118 | 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 | 119 | ); |
harryeakins | 6:98da0571ec31 | 120 | |
harryeakins | 6:98da0571ec31 | 121 | |
harryeakins | 6:98da0571ec31 | 122 | // 4. Data Rate Command |
harryeakins | 6:98da0571ec31 | 123 | writeCmd(RFM_DATA_RATE_9600); |
harryeakins | 6:98da0571ec31 | 124 | |
harryeakins | 6:98da0571ec31 | 125 | |
harryeakins | 6:98da0571ec31 | 126 | // 5. Receiver Control Command |
harryeakins | 6:98da0571ec31 | 127 | writeCmd( |
harryeakins | 6:98da0571ec31 | 128 | RFM_RX_CONTROL_P20_VDI | |
harryeakins | 6:98da0571ec31 | 129 | RFM_RX_CONTROL_VDI_FAST | |
harryeakins | 6:98da0571ec31 | 130 | //RFM_RX_CONTROL_BW(RFM_BAUD_RATE) | |
harryeakins | 6:98da0571ec31 | 131 | 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 | 132 | RFM_RX_CONTROL_GAIN_0 | |
harryeakins | 6:98da0571ec31 | 133 | RFM_RX_CONTROL_RSSI_103 // Might need adjustment. Datasheet says around 10^-5 bit error rate at this level and baudrate. |
harryeakins | 6:98da0571ec31 | 134 | ); |
harryeakins | 6:98da0571ec31 | 135 | |
harryeakins | 6:98da0571ec31 | 136 | // 6. Data Filter Command |
harryeakins | 6:98da0571ec31 | 137 | writeCmd( |
harryeakins | 6:98da0571ec31 | 138 | RFM_DATA_FILTER_AL | |
harryeakins | 6:98da0571ec31 | 139 | RFM_DATA_FILTER_ML | |
harryeakins | 6:98da0571ec31 | 140 | RFM_DATA_FILTER_DIG //| |
harryeakins | 6:98da0571ec31 | 141 | //RFM_DATA_FILTER_DQD(4) |
harryeakins | 6:98da0571ec31 | 142 | ); |
harryeakins | 6:98da0571ec31 | 143 | |
harryeakins | 6:98da0571ec31 | 144 | // 7. FIFO and Reset Mode Command |
harryeakins | 6:98da0571ec31 | 145 | writeCmd( |
harryeakins | 6:98da0571ec31 | 146 | RFM_FIFO_IT(8) | |
harryeakins | 6:98da0571ec31 | 147 | RFM_FIFO_DR | |
harryeakins | 6:98da0571ec31 | 148 | 0x8 //turn on 16bit sync word |
harryeakins | 6:98da0571ec31 | 149 | ); |
harryeakins | 6:98da0571ec31 | 150 | |
harryeakins | 6:98da0571ec31 | 151 | // 8. FIFO Syncword |
harryeakins | 6:98da0571ec31 | 152 | // Leave as default: 0xD4 |
harryeakins | 6:98da0571ec31 | 153 | |
harryeakins | 6:98da0571ec31 | 154 | // 9. Receiver FIFO Read |
harryeakins | 6:98da0571ec31 | 155 | // 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 | 156 | // result = RFM_READ_FIFO(); |
harryeakins | 6:98da0571ec31 | 157 | |
harryeakins | 6:98da0571ec31 | 158 | // 10. AFC Command |
harryeakins | 6:98da0571ec31 | 159 | writeCmd( |
harryeakins | 6:98da0571ec31 | 160 | //RFM_AFC_AUTO_VDI | //Note this might be changed to improve range. Refer to datasheet. |
harryeakins | 6:98da0571ec31 | 161 | RFM_AFC_AUTO_INDEPENDENT | |
harryeakins | 6:98da0571ec31 | 162 | RFM_AFC_RANGE_LIMIT_7_8 | |
harryeakins | 6:98da0571ec31 | 163 | RFM_AFC_EN | |
harryeakins | 6:98da0571ec31 | 164 | RFM_AFC_OE | |
harryeakins | 6:98da0571ec31 | 165 | RFM_AFC_FI |
harryeakins | 6:98da0571ec31 | 166 | ); |
harryeakins | 6:98da0571ec31 | 167 | |
harryeakins | 6:98da0571ec31 | 168 | // 11. TX Configuration Control Command |
harryeakins | 6:98da0571ec31 | 169 | writeCmd( |
harryeakins | 6:98da0571ec31 | 170 | RFM_TX_CONTROL_MOD_60 | |
harryeakins | 6:98da0571ec31 | 171 | RFM_TX_CONTROL_POW_0 |
harryeakins | 6:98da0571ec31 | 172 | ); |
harryeakins | 6:98da0571ec31 | 173 | |
harryeakins | 6:98da0571ec31 | 174 | |
harryeakins | 6:98da0571ec31 | 175 | // 12. PLL Setting Command |
harryeakins | 6:98da0571ec31 | 176 | writeCmd( |
harryeakins | 6:98da0571ec31 | 177 | 0xCC77 & ~0x01 // Setting the PLL bandwith, less noise, but max bitrate capped at 86.2 |
harryeakins | 6:98da0571ec31 | 178 | // I think this will slow down the plls reaction time. Not sure, check with someone! |
harryeakins | 6:98da0571ec31 | 179 | ); |
harryeakins | 5:a92c3f6d1711 | 180 | |
harryeakins | 5:a92c3f6d1711 | 181 | resetRX(); |
harryeakins | 5:a92c3f6d1711 | 182 | status(); |
harryeakins | 4:2a295db9ba1a | 183 | } |
harryeakins | 3:e72ad65868ab | 184 | |
harryeakins | 4:2a295db9ba1a | 185 | /* Write a command to the RF Module */ |
harryeakins | 4:2a295db9ba1a | 186 | unsigned int RF12B::writeCmd(unsigned int cmd) { |
harryeakins | 4:2a295db9ba1a | 187 | NCS = 0; |
harryeakins | 4:2a295db9ba1a | 188 | unsigned int recv = spi.write(cmd); |
harryeakins | 4:2a295db9ba1a | 189 | NCS = 1; |
harryeakins | 4:2a295db9ba1a | 190 | return recv; |
harryeakins | 3:e72ad65868ab | 191 | } |
harryeakins | 3:e72ad65868ab | 192 | |
harryeakins | 4:2a295db9ba1a | 193 | /* Sends a byte of data across RF */ |
harryeakins | 4:2a295db9ba1a | 194 | void RF12B::send(unsigned char data) { |
harryeakins | 4:2a295db9ba1a | 195 | while (NIRQ); |
harryeakins | 4:2a295db9ba1a | 196 | writeCmd(0xB800 + data); |
harryeakins | 4:2a295db9ba1a | 197 | } |
harryeakins | 4:2a295db9ba1a | 198 | |
harryeakins | 4:2a295db9ba1a | 199 | /* Change the mode of the RF module to Transmitting or Receiving */ |
harryeakins | 4:2a295db9ba1a | 200 | void RF12B::changeMode(rfmode_t mode) { |
harryeakins | 4:2a295db9ba1a | 201 | if (mode == TX) { |
harryeakins | 3:e72ad65868ab | 202 | writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC |
harryeakins | 4:2a295db9ba1a | 203 | } else { /* mode == RX */ |
harryeakins | 3:e72ad65868ab | 204 | writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC |
harryeakins | 3:e72ad65868ab | 205 | } |
harryeakins | 3:e72ad65868ab | 206 | } |
harryeakins | 3:e72ad65868ab | 207 | |
harryeakins | 4:2a295db9ba1a | 208 | /* Interrupt routine for data reception */ |
harryeakins | 4:2a295db9ba1a | 209 | void RF12B::rxISR() { |
harryeakins | 4:2a295db9ba1a | 210 | unsigned int data = 0, i = 0; |
harryeakins | 4:2a295db9ba1a | 211 | unsigned char packet_length = 0; |
harryeakins | 6:98da0571ec31 | 212 | unsigned char crc = 0; |
harryeakins | 6:98da0571ec31 | 213 | queue<unsigned char> temp; |
harryeakins | 4:2a295db9ba1a | 214 | |
harryeakins | 4:2a295db9ba1a | 215 | /* Grab the packet's length byte */ |
harryeakins | 4:2a295db9ba1a | 216 | data = writeCmd(0x0000); |
harryeakins | 4:2a295db9ba1a | 217 | if ( (data&0x8000) ) { |
harryeakins | 4:2a295db9ba1a | 218 | data = writeCmd(0xB000); |
harryeakins | 4:2a295db9ba1a | 219 | packet_length = (data&0x00FF); |
harryeakins | 6:98da0571ec31 | 220 | crc = crc8(crc, packet_length); |
harryeakins | 4:2a295db9ba1a | 221 | } |
harryeakins | 5:a92c3f6d1711 | 222 | |
harryeakins | 4:2a295db9ba1a | 223 | /* Grab the packet's data */ |
harryeakins | 5:a92c3f6d1711 | 224 | while (i < packet_length) { |
harryeakins | 5:a92c3f6d1711 | 225 | if (!NIRQ_in) { |
harryeakins | 5:a92c3f6d1711 | 226 | data = writeCmd(0x0000); |
harryeakins | 5:a92c3f6d1711 | 227 | if ( (data&0x8000) ) { |
harryeakins | 5:a92c3f6d1711 | 228 | data = writeCmd(0xB000); |
harryeakins | 6:98da0571ec31 | 229 | temp.push(data&0x00FF); |
harryeakins | 6:98da0571ec31 | 230 | crc = crc8(crc, (unsigned char)(data&0x00FF)); |
harryeakins | 5:a92c3f6d1711 | 231 | i++; |
harryeakins | 5:a92c3f6d1711 | 232 | } |
harryeakins | 4:2a295db9ba1a | 233 | } |
harryeakins | 4:2a295db9ba1a | 234 | } |
harryeakins | 6:98da0571ec31 | 235 | while (1) { |
harryeakins | 6:98da0571ec31 | 236 | if (!NIRQ_in) { |
harryeakins | 6:98da0571ec31 | 237 | data = writeCmd(0x0000); |
harryeakins | 6:98da0571ec31 | 238 | if ( (data&0x8000) ) { |
harryeakins | 6:98da0571ec31 | 239 | data = writeCmd(0xB000); |
harryeakins | 6:98da0571ec31 | 240 | if ((unsigned char)(data & 0x00FF) == crc) { |
harryeakins | 6:98da0571ec31 | 241 | while (!temp.empty()) { |
harryeakins | 6:98da0571ec31 | 242 | fifo.push(temp.front()); |
harryeakins | 6:98da0571ec31 | 243 | temp.pop(); |
harryeakins | 6:98da0571ec31 | 244 | } |
harryeakins | 6:98da0571ec31 | 245 | } |
harryeakins | 6:98da0571ec31 | 246 | break; |
harryeakins | 6:98da0571ec31 | 247 | } |
harryeakins | 6:98da0571ec31 | 248 | } |
harryeakins | 6:98da0571ec31 | 249 | } |
harryeakins | 4:2a295db9ba1a | 250 | /* Tell RF Module we are finished */ |
harryeakins | 4:2a295db9ba1a | 251 | resetRX(); |
harryeakins | 3:e72ad65868ab | 252 | } |
harryeakins | 4:2a295db9ba1a | 253 | |
harryeakins | 5:a92c3f6d1711 | 254 | unsigned int RF12B::status() { |
harryeakins | 5:a92c3f6d1711 | 255 | return writeCmd(0x0000); |
harryeakins | 5:a92c3f6d1711 | 256 | } |
harryeakins | 5:a92c3f6d1711 | 257 | |
harryeakins | 4:2a295db9ba1a | 258 | /* Tell the RF Module this packet is received and wait for the next */ |
harryeakins | 4:2a295db9ba1a | 259 | void RF12B::resetRX() { |
harryeakins | 4:2a295db9ba1a | 260 | writeCmd(0xCA81); |
harryeakins | 4:2a295db9ba1a | 261 | writeCmd(0xCA83); |
harryeakins | 6:98da0571ec31 | 262 | }; |
harryeakins | 6:98da0571ec31 | 263 | |
harryeakins | 6:98da0571ec31 | 264 | /* Calculate CRC8 */ |
harryeakins | 6:98da0571ec31 | 265 | unsigned char RF12B::crc8(unsigned char crc, unsigned char data) { |
harryeakins | 6:98da0571ec31 | 266 | crc = crc ^ data; |
harryeakins | 6:98da0571ec31 | 267 | for (int i = 0; i < 8; i++) { |
harryeakins | 6:98da0571ec31 | 268 | if (crc & 0x01) { |
harryeakins | 6:98da0571ec31 | 269 | crc = (crc >> 1) ^ 0x8C; |
harryeakins | 6:98da0571ec31 | 270 | } else { |
harryeakins | 6:98da0571ec31 | 271 | crc >>= 1; |
harryeakins | 6:98da0571ec31 | 272 | } |
harryeakins | 6:98da0571ec31 | 273 | } |
harryeakins | 6:98da0571ec31 | 274 | return crc; |
harryeakins | 6:98da0571ec31 | 275 | } |