Last code used in national competition

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Thu May 05 18:50:17 2022 +0000
Revision:
2:dc50e57d0808
Saving latest code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
einsteingustavo 2:dc50e57d0808 1 //Port of RFM69 from lowpowerlab
einsteingustavo 2:dc50e57d0808 2 //Sync'd Feb. 6, 2015
einsteingustavo 2:dc50e57d0808 3 //spi register read/write routines from Karl Zweimuller's RF22
einsteingustavo 2:dc50e57d0808 4 //
einsteingustavo 2:dc50e57d0808 5 //
einsteingustavo 2:dc50e57d0808 6 //
einsteingustavo 2:dc50e57d0808 7 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 8 // Driver definition for HopeRF RFM69W/RFM69HW/RFM69CW/RFM69HCW, Semtech SX1231/1231H
einsteingustavo 2:dc50e57d0808 9 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 10 // Copyright Felix Rusu (2014), felix@lowpowerlab.com
einsteingustavo 2:dc50e57d0808 11 // http://lowpowerlab.com/
einsteingustavo 2:dc50e57d0808 12 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 13 // License
einsteingustavo 2:dc50e57d0808 14 // **********************************************************************************
einsteingustavo 2:dc50e57d0808 15 // This program is free software; you can redistribute it
einsteingustavo 2:dc50e57d0808 16 // and/or modify it under the terms of the GNU General
einsteingustavo 2:dc50e57d0808 17 // Public License as published by the Free Software
einsteingustavo 2:dc50e57d0808 18 // Foundation; either version 3 of the License, or
einsteingustavo 2:dc50e57d0808 19 // (at your option) any later version.
einsteingustavo 2:dc50e57d0808 20 //
einsteingustavo 2:dc50e57d0808 21 // This program is distributed in the hope that it will
einsteingustavo 2:dc50e57d0808 22 // be useful, but WITHOUT ANY WARRANTY; without even the
einsteingustavo 2:dc50e57d0808 23 // implied warranty of MERCHANTABILITY or FITNESS FOR A
einsteingustavo 2:dc50e57d0808 24 // PARTICULAR PURPOSE. See the GNU General Public
einsteingustavo 2:dc50e57d0808 25 // License for more details.
einsteingustavo 2:dc50e57d0808 26 //
einsteingustavo 2:dc50e57d0808 27 // You should have received a copy of the GNU General
einsteingustavo 2:dc50e57d0808 28 // Public License along with this program.
einsteingustavo 2:dc50e57d0808 29 // If not, see <http://www.gnu.org/licenses/>.
einsteingustavo 2:dc50e57d0808 30 //
einsteingustavo 2:dc50e57d0808 31 // Licence can be viewed at
einsteingustavo 2:dc50e57d0808 32 // http://www.gnu.org/licenses/gpl-3.0.txt
einsteingustavo 2:dc50e57d0808 33 //
einsteingustavo 2:dc50e57d0808 34 // Please maintain this license information along with authorship
einsteingustavo 2:dc50e57d0808 35 // and copyright notices in any redistribution of this code
einsteingustavo 2:dc50e57d0808 36 // **********************************************************************************// RF22.cpp
einsteingustavo 2:dc50e57d0808 37 //
einsteingustavo 2:dc50e57d0808 38 // Copyright (C) 2011 Mike McCauley
einsteingustavo 2:dc50e57d0808 39 // $Id: RF22.cpp,v 1.17 2013/02/06 21:33:56 mikem Exp mikem $
einsteingustavo 2:dc50e57d0808 40 // ported to mbed by Karl Zweimueller
einsteingustavo 2:dc50e57d0808 41
einsteingustavo 2:dc50e57d0808 42
einsteingustavo 2:dc50e57d0808 43 #include "mbed.h"
einsteingustavo 2:dc50e57d0808 44 #include "RFM69.h"
einsteingustavo 2:dc50e57d0808 45 #include <RFM69registers.h>
einsteingustavo 2:dc50e57d0808 46 #include <SPI.h>
einsteingustavo 2:dc50e57d0808 47
einsteingustavo 2:dc50e57d0808 48 volatile uint8_t RFM69::DATA[RF69_MAX_DATA_LEN];
einsteingustavo 2:dc50e57d0808 49 volatile uint8_t RFM69::_mode; // current transceiver state
einsteingustavo 2:dc50e57d0808 50 volatile uint8_t RFM69::DATALEN;
einsteingustavo 2:dc50e57d0808 51 volatile uint8_t RFM69::SENDERID;
einsteingustavo 2:dc50e57d0808 52 volatile uint8_t RFM69::TARGETID; // should match _address
einsteingustavo 2:dc50e57d0808 53 volatile uint8_t RFM69::PAYLOADLEN;
einsteingustavo 2:dc50e57d0808 54 volatile uint8_t RFM69::ACK_REQUESTED;
einsteingustavo 2:dc50e57d0808 55 volatile uint8_t RFM69::ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
einsteingustavo 2:dc50e57d0808 56 volatile int16_t RFM69::RSSI; // most accurate RSSI during reception (closest to the reception)
einsteingustavo 2:dc50e57d0808 57
einsteingustavo 2:dc50e57d0808 58 RFM69::RFM69(PinName mosi, PinName miso, PinName sclk, PinName slaveSelectPin, PinName interrupt):
einsteingustavo 2:dc50e57d0808 59 _slaveSelectPin(slaveSelectPin) , _spi(mosi, miso, sclk), _interrupt(interrupt) {
einsteingustavo 2:dc50e57d0808 60
einsteingustavo 2:dc50e57d0808 61 // Setup the spi for 8 bit data, high steady state clock,
einsteingustavo 2:dc50e57d0808 62 // second edge capture, with a 1MHz clock rate
einsteingustavo 2:dc50e57d0808 63 _spi.format(8,0);
einsteingustavo 2:dc50e57d0808 64 _spi.frequency(4000000);
einsteingustavo 2:dc50e57d0808 65 _mode = RF69_MODE_STANDBY;
einsteingustavo 2:dc50e57d0808 66 _promiscuousMode = false;
einsteingustavo 2:dc50e57d0808 67 _powerLevel = 31;
einsteingustavo 2:dc50e57d0808 68 }
einsteingustavo 2:dc50e57d0808 69
einsteingustavo 2:dc50e57d0808 70 bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID)
einsteingustavo 2:dc50e57d0808 71 {
einsteingustavo 2:dc50e57d0808 72 unsigned long start_to;
einsteingustavo 2:dc50e57d0808 73 const uint8_t CONFIG[][2] =
einsteingustavo 2:dc50e57d0808 74 {
einsteingustavo 2:dc50e57d0808 75 /* 0x01 */ { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY },
einsteingustavo 2:dc50e57d0808 76 /* 0x02 */ { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 }, // no shaping
einsteingustavo 2:dc50e57d0808 77 /* 0x03 */ { REG_BITRATEMSB, RF_BITRATEMSB_4800}, // default: 4.8 KBPS
einsteingustavo 2:dc50e57d0808 78 /* 0x04 */ { REG_BITRATELSB, RF_BITRATELSB_4800},
einsteingustavo 2:dc50e57d0808 79 /* 0x05 */ { REG_FDEVMSB, RF_FDEVMSB_50000}, // default: 5KHz, (FDEV + BitRate / 2 <= 500KHz)
einsteingustavo 2:dc50e57d0808 80 /* 0x06 */ { REG_FDEVLSB, RF_FDEVLSB_50000},
einsteingustavo 2:dc50e57d0808 81
einsteingustavo 2:dc50e57d0808 82 /* 0x07 */ { REG_FRFMSB, (uint8_t) (freqBand==RF69_315MHZ ? RF_FRFMSB_315 : (freqBand==RF69_433MHZ ? RF_FRFMSB_433 : (freqBand==RF69_868MHZ ? RF_FRFMSB_868 : RF_FRFMSB_915))) },
einsteingustavo 2:dc50e57d0808 83 /* 0x08 */ { REG_FRFMID, (uint8_t) (freqBand==RF69_315MHZ ? RF_FRFMID_315 : (freqBand==RF69_433MHZ ? RF_FRFMID_433 : (freqBand==RF69_868MHZ ? RF_FRFMID_868 : RF_FRFMID_915))) },
einsteingustavo 2:dc50e57d0808 84 /* 0x09 */ { REG_FRFLSB, (uint8_t) (freqBand==RF69_315MHZ ? RF_FRFLSB_315 : (freqBand==RF69_433MHZ ? RF_FRFLSB_433 : (freqBand==RF69_868MHZ ? RF_FRFLSB_868 : RF_FRFLSB_915))) },
einsteingustavo 2:dc50e57d0808 85
einsteingustavo 2:dc50e57d0808 86 // looks like PA1 and PA2 are not implemented on RFM69W, hence the max output power is 13dBm
einsteingustavo 2:dc50e57d0808 87 // +17dBm and +20dBm are possible on RFM69HW
einsteingustavo 2:dc50e57d0808 88 // +13dBm formula: Pout = -18 + OutputPower (with PA0 or PA1**)
einsteingustavo 2:dc50e57d0808 89 // +17dBm formula: Pout = -14 + OutputPower (with PA1 and PA2)**
einsteingustavo 2:dc50e57d0808 90 // +20dBm formula: Pout = -11 + OutputPower (with PA1 and PA2)** and high power PA settings (section 3.3.7 in datasheet)
einsteingustavo 2:dc50e57d0808 91 ///* 0x11 */ { REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111},
einsteingustavo 2:dc50e57d0808 92 ///* 0x13 */ { REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95 }, // over current protection (default is 95mA)
einsteingustavo 2:dc50e57d0808 93
einsteingustavo 2:dc50e57d0808 94 // RXBW defaults are { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4KHz)
einsteingustavo 2:dc50e57d0808 95 /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, // (BitRate < 2 * RxBw)
einsteingustavo 2:dc50e57d0808 96 //for BR-19200: /* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3 },
einsteingustavo 2:dc50e57d0808 97 /* 0x25 */ { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01 }, // DIO0 is the only IRQ we're using
einsteingustavo 2:dc50e57d0808 98 /* 0x26 */ { REG_DIOMAPPING2, RF_DIOMAPPING2_CLKOUT_OFF }, // DIO5 ClkOut disable for power saving
einsteingustavo 2:dc50e57d0808 99 /* 0x28 */ { REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN }, // writing to this bit ensures that the FIFO & status flags are reset
einsteingustavo 2:dc50e57d0808 100 /* 0x29 */ { REG_RSSITHRESH, 220 }, // must be set to dBm = (-Sensitivity / 2), default is 0xE4 = 228 so -114dBm
einsteingustavo 2:dc50e57d0808 101 ///* 0x2D */ { REG_PREAMBLELSB, RF_PREAMBLESIZE_LSB_VALUE } // default 3 preamble bytes 0xAAAAAA
einsteingustavo 2:dc50e57d0808 102 /* 0x2E */ { REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0 },
einsteingustavo 2:dc50e57d0808 103 /* 0x2F */ { REG_SYNCVALUE1, 0x2D }, // attempt to make this compatible with sync1 byte of RFM12B lib
einsteingustavo 2:dc50e57d0808 104 /* 0x30 */ { REG_SYNCVALUE2, networkID }, // NETWORK ID
einsteingustavo 2:dc50e57d0808 105 /* 0x37 */ { REG_PACKETCONFIG1, RF_PACKET1_FORMAT_VARIABLE | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_ON | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF },
einsteingustavo 2:dc50e57d0808 106 /* 0x38 */ { REG_PAYLOADLENGTH, 66 }, // in variable length mode: the max frame size, not used in TX
einsteingustavo 2:dc50e57d0808 107 ///* 0x39 */ { REG_NODEADRS, nodeID }, // turned off because we're not using address filtering
einsteingustavo 2:dc50e57d0808 108 /* 0x3C */ { REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | RF_FIFOTHRESH_VALUE }, // TX on FIFO not empty
einsteingustavo 2:dc50e57d0808 109 /* 0x3D */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_2BITS | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, // RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
einsteingustavo 2:dc50e57d0808 110 //for BR-19200: /* 0x3D */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, // RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
einsteingustavo 2:dc50e57d0808 111 /* 0x6F */ { REG_TESTDAGC, RF_DAGC_IMPROVED_LOWBETA0 }, // run DAGC continuously in RX mode for Fading Margin Improvement, recommended default for AfcLowBetaOn=0
einsteingustavo 2:dc50e57d0808 112 {255, 0}
einsteingustavo 2:dc50e57d0808 113 };
einsteingustavo 2:dc50e57d0808 114 // Timer for ms waits
einsteingustavo 2:dc50e57d0808 115 t.start();
einsteingustavo 2:dc50e57d0808 116 _slaveSelectPin = 1;
einsteingustavo 2:dc50e57d0808 117
einsteingustavo 2:dc50e57d0808 118 // Setup the spi for 8 bit data : 1RW-bit 7 adressbit and 8 databit
einsteingustavo 2:dc50e57d0808 119 // second edge capture, with a 10MHz clock rate
einsteingustavo 2:dc50e57d0808 120 _spi.format(8,0);
einsteingustavo 2:dc50e57d0808 121 _spi.frequency(4000000);
einsteingustavo 2:dc50e57d0808 122
einsteingustavo 2:dc50e57d0808 123 #define TIME_OUT 50
einsteingustavo 2:dc50e57d0808 124
einsteingustavo 2:dc50e57d0808 125 start_to = t.read_ms() ;
einsteingustavo 2:dc50e57d0808 126
einsteingustavo 2:dc50e57d0808 127 do writeReg(REG_SYNCVALUE1, 0xaa); while (readReg(REG_SYNCVALUE1) != 0xaa && t.read_ms()-start_to < TIME_OUT);
einsteingustavo 2:dc50e57d0808 128 if (t.read_ms()-start_to >= TIME_OUT) return (false);
einsteingustavo 2:dc50e57d0808 129
einsteingustavo 2:dc50e57d0808 130 // Set time out
einsteingustavo 2:dc50e57d0808 131 start_to = t.read_ms() ;
einsteingustavo 2:dc50e57d0808 132 do writeReg(REG_SYNCVALUE1, 0x55); while (readReg(REG_SYNCVALUE1) != 0x55 && t.read_ms()-start_to < TIME_OUT);
einsteingustavo 2:dc50e57d0808 133 if (t.read_ms()-start_to >= TIME_OUT) return (false);
einsteingustavo 2:dc50e57d0808 134 for (uint8_t i = 0; CONFIG[i][0] != 255; i++)
einsteingustavo 2:dc50e57d0808 135 writeReg(CONFIG[i][0], CONFIG[i][1]);
einsteingustavo 2:dc50e57d0808 136
einsteingustavo 2:dc50e57d0808 137 // Encryption is persistent between resets and can trip you up during debugging.
einsteingustavo 2:dc50e57d0808 138 // Disable it during initialization so we always start from a known state.
einsteingustavo 2:dc50e57d0808 139 encrypt(0);
einsteingustavo 2:dc50e57d0808 140
einsteingustavo 2:dc50e57d0808 141 setHighPower(_isRFM69HW); // called regardless if it's a RFM69W or RFM69HW
einsteingustavo 2:dc50e57d0808 142 setMode(RF69_MODE_STANDBY);
einsteingustavo 2:dc50e57d0808 143 // Set up interrupt handler
einsteingustavo 2:dc50e57d0808 144 start_to = t.read_ms() ;
einsteingustavo 2:dc50e57d0808 145 while (((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) && t.read_ms()-start_to < TIME_OUT); // Wait for ModeReady
einsteingustavo 2:dc50e57d0808 146 if (t.read_ms()-start_to >= TIME_OUT) return (false);
einsteingustavo 2:dc50e57d0808 147
einsteingustavo 2:dc50e57d0808 148 _interrupt.rise(this, &RFM69::isr0);
einsteingustavo 2:dc50e57d0808 149
einsteingustavo 2:dc50e57d0808 150 _address = nodeID;
einsteingustavo 2:dc50e57d0808 151 return true;
einsteingustavo 2:dc50e57d0808 152 }
einsteingustavo 2:dc50e57d0808 153 // return the frequency (in Hz)
einsteingustavo 2:dc50e57d0808 154 uint32_t RFM69::getFrequency()
einsteingustavo 2:dc50e57d0808 155 {
einsteingustavo 2:dc50e57d0808 156 return RF69_FSTEP * (((uint32_t) readReg(REG_FRFMSB) << 16) + ((uint16_t) readReg(REG_FRFMID) << 8) + readReg(REG_FRFLSB));
einsteingustavo 2:dc50e57d0808 157 }
einsteingustavo 2:dc50e57d0808 158
einsteingustavo 2:dc50e57d0808 159 // set the frequency (in Hz)
einsteingustavo 2:dc50e57d0808 160 void RFM69::setFrequency(uint32_t freqHz)
einsteingustavo 2:dc50e57d0808 161 {
einsteingustavo 2:dc50e57d0808 162 uint8_t oldMode = _mode;
einsteingustavo 2:dc50e57d0808 163 if (oldMode == RF69_MODE_TX) {
einsteingustavo 2:dc50e57d0808 164 setMode(RF69_MODE_RX);
einsteingustavo 2:dc50e57d0808 165 }
einsteingustavo 2:dc50e57d0808 166 freqHz /= RF69_FSTEP; // divide down by FSTEP to get FRF
einsteingustavo 2:dc50e57d0808 167 writeReg(REG_FRFMSB, freqHz >> 16);
einsteingustavo 2:dc50e57d0808 168 writeReg(REG_FRFMID, freqHz >> 8);
einsteingustavo 2:dc50e57d0808 169 writeReg(REG_FRFLSB, freqHz);
einsteingustavo 2:dc50e57d0808 170 if (oldMode == RF69_MODE_RX) {
einsteingustavo 2:dc50e57d0808 171 setMode(RF69_MODE_SYNTH);
einsteingustavo 2:dc50e57d0808 172 }
einsteingustavo 2:dc50e57d0808 173 setMode(oldMode);
einsteingustavo 2:dc50e57d0808 174 }
einsteingustavo 2:dc50e57d0808 175
einsteingustavo 2:dc50e57d0808 176 void RFM69::setMode(uint8_t newMode)
einsteingustavo 2:dc50e57d0808 177 {
einsteingustavo 2:dc50e57d0808 178 if (newMode == _mode)
einsteingustavo 2:dc50e57d0808 179 return;
einsteingustavo 2:dc50e57d0808 180
einsteingustavo 2:dc50e57d0808 181 switch (newMode) {
einsteingustavo 2:dc50e57d0808 182 case RF69_MODE_TX:
einsteingustavo 2:dc50e57d0808 183 writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_TRANSMITTER);
einsteingustavo 2:dc50e57d0808 184 if (_isRFM69HW) setHighPowerRegs(true);
einsteingustavo 2:dc50e57d0808 185 break;
einsteingustavo 2:dc50e57d0808 186 case RF69_MODE_RX:
einsteingustavo 2:dc50e57d0808 187 writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_RECEIVER);
einsteingustavo 2:dc50e57d0808 188 if (_isRFM69HW) setHighPowerRegs(false);
einsteingustavo 2:dc50e57d0808 189 break;
einsteingustavo 2:dc50e57d0808 190 case RF69_MODE_SYNTH:
einsteingustavo 2:dc50e57d0808 191 writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SYNTHESIZER);
einsteingustavo 2:dc50e57d0808 192 break;
einsteingustavo 2:dc50e57d0808 193 case RF69_MODE_STANDBY:
einsteingustavo 2:dc50e57d0808 194 writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_STANDBY);
einsteingustavo 2:dc50e57d0808 195 break;
einsteingustavo 2:dc50e57d0808 196 case RF69_MODE_SLEEP:
einsteingustavo 2:dc50e57d0808 197 writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SLEEP);
einsteingustavo 2:dc50e57d0808 198 break;
einsteingustavo 2:dc50e57d0808 199 default:
einsteingustavo 2:dc50e57d0808 200 return;
einsteingustavo 2:dc50e57d0808 201 }
einsteingustavo 2:dc50e57d0808 202
einsteingustavo 2:dc50e57d0808 203 // we are using packet mode, so this check is not really needed
einsteingustavo 2:dc50e57d0808 204 // but waiting for mode ready is necessary when going from sleep because the FIFO may not be immediately available from previous mode
einsteingustavo 2:dc50e57d0808 205 while (_mode == RF69_MODE_SLEEP && (readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
einsteingustavo 2:dc50e57d0808 206 _mode = newMode;
einsteingustavo 2:dc50e57d0808 207 }
einsteingustavo 2:dc50e57d0808 208
einsteingustavo 2:dc50e57d0808 209 void RFM69::sleep() {
einsteingustavo 2:dc50e57d0808 210 setMode(RF69_MODE_SLEEP);
einsteingustavo 2:dc50e57d0808 211 }
einsteingustavo 2:dc50e57d0808 212
einsteingustavo 2:dc50e57d0808 213 void RFM69::setAddress(uint8_t addr)
einsteingustavo 2:dc50e57d0808 214 {
einsteingustavo 2:dc50e57d0808 215 _address = addr;
einsteingustavo 2:dc50e57d0808 216 writeReg(REG_NODEADRS, _address);
einsteingustavo 2:dc50e57d0808 217 }
einsteingustavo 2:dc50e57d0808 218
einsteingustavo 2:dc50e57d0808 219 void RFM69::setNetwork(uint8_t networkID)
einsteingustavo 2:dc50e57d0808 220 {
einsteingustavo 2:dc50e57d0808 221 writeReg(REG_SYNCVALUE2, networkID);
einsteingustavo 2:dc50e57d0808 222 }
einsteingustavo 2:dc50e57d0808 223
einsteingustavo 2:dc50e57d0808 224 // set output power: 0 = min, 31 = max
einsteingustavo 2:dc50e57d0808 225 // this results in a "weaker" transmitted signal, and directly results in a lower RSSI at the receiver
einsteingustavo 2:dc50e57d0808 226 void RFM69::setPowerLevel(uint8_t powerLevel)
einsteingustavo 2:dc50e57d0808 227 {
einsteingustavo 2:dc50e57d0808 228 _powerLevel = powerLevel;
einsteingustavo 2:dc50e57d0808 229 writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0xE0) | (_powerLevel > 31 ? 31 : _powerLevel));
einsteingustavo 2:dc50e57d0808 230 }
einsteingustavo 2:dc50e57d0808 231
einsteingustavo 2:dc50e57d0808 232 bool RFM69::canSend()
einsteingustavo 2:dc50e57d0808 233 {
einsteingustavo 2:dc50e57d0808 234 if (_mode == RF69_MODE_RX && PAYLOADLEN == 0 && readRSSI() < CSMA_LIMIT) // if signal stronger than -100dBm is detected assume channel activity
einsteingustavo 2:dc50e57d0808 235 {
einsteingustavo 2:dc50e57d0808 236 setMode(RF69_MODE_STANDBY);
einsteingustavo 2:dc50e57d0808 237 return true;
einsteingustavo 2:dc50e57d0808 238 }
einsteingustavo 2:dc50e57d0808 239 return false;
einsteingustavo 2:dc50e57d0808 240 }
einsteingustavo 2:dc50e57d0808 241
einsteingustavo 2:dc50e57d0808 242 void RFM69::send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK)
einsteingustavo 2:dc50e57d0808 243 {
einsteingustavo 2:dc50e57d0808 244 printf("aaaaa\r\n");
einsteingustavo 2:dc50e57d0808 245 writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
einsteingustavo 2:dc50e57d0808 246 uint32_t now = t.read_ms();
einsteingustavo 2:dc50e57d0808 247 while (!canSend() && t.read_ms() - now < RF69_CSMA_LIMIT_MS) receiveDone();
einsteingustavo 2:dc50e57d0808 248 printf("before send\r\n");
einsteingustavo 2:dc50e57d0808 249 sendFrame(toAddress, buffer, bufferSize, requestACK, false );
einsteingustavo 2:dc50e57d0808 250 printf("after send\r\n");
einsteingustavo 2:dc50e57d0808 251 }
einsteingustavo 2:dc50e57d0808 252
einsteingustavo 2:dc50e57d0808 253 // to increase the chance of getting a packet across, call this function instead of send
einsteingustavo 2:dc50e57d0808 254 // and it handles all the ACK requesting/retrying for you :)
einsteingustavo 2:dc50e57d0808 255 // The only twist is that you have to manually listen to ACK requests on the other side and send back the ACKs
einsteingustavo 2:dc50e57d0808 256 // The reason for the semi-automaton is that the lib is interrupt driven and
einsteingustavo 2:dc50e57d0808 257 // requires user action to read the received data and decide what to do with it
einsteingustavo 2:dc50e57d0808 258 // replies usually take only 5..8ms at 50kbps@915MHz
einsteingustavo 2:dc50e57d0808 259 bool RFM69::sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries, uint8_t retryWaitTime) {
einsteingustavo 2:dc50e57d0808 260 uint32_t sentTime;
einsteingustavo 2:dc50e57d0808 261 for (uint8_t i = 0; i <= retries; i++)
einsteingustavo 2:dc50e57d0808 262 {
einsteingustavo 2:dc50e57d0808 263 send(toAddress, buffer, bufferSize, true);
einsteingustavo 2:dc50e57d0808 264 sentTime = t.read_ms();
einsteingustavo 2:dc50e57d0808 265 while (t.read_ms() - sentTime < retryWaitTime)
einsteingustavo 2:dc50e57d0808 266 {
einsteingustavo 2:dc50e57d0808 267 if (ACKReceived(toAddress))
einsteingustavo 2:dc50e57d0808 268 {
einsteingustavo 2:dc50e57d0808 269 //Serial.print(" ~ms:"); Serial.print(t.read_ms() - sentTime);
einsteingustavo 2:dc50e57d0808 270 return true;
einsteingustavo 2:dc50e57d0808 271 }
einsteingustavo 2:dc50e57d0808 272 }
einsteingustavo 2:dc50e57d0808 273 //Serial.print(" RETRY#"); Serial.println(i + 1);
einsteingustavo 2:dc50e57d0808 274 }
einsteingustavo 2:dc50e57d0808 275 return false;
einsteingustavo 2:dc50e57d0808 276 }
einsteingustavo 2:dc50e57d0808 277
einsteingustavo 2:dc50e57d0808 278 // should be polled immediately after sending a packet with ACK request
einsteingustavo 2:dc50e57d0808 279 bool RFM69::ACKReceived(uint8_t fromNodeID) {
einsteingustavo 2:dc50e57d0808 280 if (receiveDone())
einsteingustavo 2:dc50e57d0808 281 return (SENDERID == fromNodeID || fromNodeID == RF69_BROADCAST_ADDR) && ACK_RECEIVED;
einsteingustavo 2:dc50e57d0808 282 return false;
einsteingustavo 2:dc50e57d0808 283 }
einsteingustavo 2:dc50e57d0808 284
einsteingustavo 2:dc50e57d0808 285 // check whether an ACK was requested in the last received packet (non-broadcasted packet)
einsteingustavo 2:dc50e57d0808 286 bool RFM69::ACKRequested() {
einsteingustavo 2:dc50e57d0808 287 return ACK_REQUESTED && (TARGETID != RF69_BROADCAST_ADDR);
einsteingustavo 2:dc50e57d0808 288 }
einsteingustavo 2:dc50e57d0808 289
einsteingustavo 2:dc50e57d0808 290 // should be called immediately after reception in case sender wants ACK
einsteingustavo 2:dc50e57d0808 291 void RFM69::sendACK(const void* buffer, uint8_t bufferSize) {
einsteingustavo 2:dc50e57d0808 292 uint8_t sender = SENDERID;
einsteingustavo 2:dc50e57d0808 293 int16_t _RSSI = RSSI; // save payload received RSSI value
einsteingustavo 2:dc50e57d0808 294 writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
einsteingustavo 2:dc50e57d0808 295 uint32_t now = t.read_ms();
einsteingustavo 2:dc50e57d0808 296 while (!canSend() && t.read_ms() - now < RF69_CSMA_LIMIT_MS) receiveDone();
einsteingustavo 2:dc50e57d0808 297 sendFrame(sender, buffer, bufferSize, false, true);
einsteingustavo 2:dc50e57d0808 298 RSSI = _RSSI; // restore payload RSSI
einsteingustavo 2:dc50e57d0808 299 }
einsteingustavo 2:dc50e57d0808 300
einsteingustavo 2:dc50e57d0808 301 void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK)
einsteingustavo 2:dc50e57d0808 302 {
einsteingustavo 2:dc50e57d0808 303 setMode(RF69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo
einsteingustavo 2:dc50e57d0808 304 while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
einsteingustavo 2:dc50e57d0808 305 writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
einsteingustavo 2:dc50e57d0808 306 if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN;
einsteingustavo 2:dc50e57d0808 307
einsteingustavo 2:dc50e57d0808 308 // control byte
einsteingustavo 2:dc50e57d0808 309 uint8_t CTLbyte = 0x00;
einsteingustavo 2:dc50e57d0808 310 if (sendACK)
einsteingustavo 2:dc50e57d0808 311 CTLbyte = 0x80;
einsteingustavo 2:dc50e57d0808 312 else if (requestACK)
einsteingustavo 2:dc50e57d0808 313 CTLbyte = 0x40;
einsteingustavo 2:dc50e57d0808 314
einsteingustavo 2:dc50e57d0808 315 select();
einsteingustavo 2:dc50e57d0808 316 _spi.write(REG_FIFO | 0x80);
einsteingustavo 2:dc50e57d0808 317 _spi.write(bufferSize + 3);
einsteingustavo 2:dc50e57d0808 318 _spi.write(toAddress);
einsteingustavo 2:dc50e57d0808 319 _spi.write(_address);
einsteingustavo 2:dc50e57d0808 320 _spi.write(CTLbyte);
einsteingustavo 2:dc50e57d0808 321
einsteingustavo 2:dc50e57d0808 322 for (uint8_t i = 0; i < bufferSize; i++)
einsteingustavo 2:dc50e57d0808 323 _spi.write(((uint8_t*) buffer)[i]);
einsteingustavo 2:dc50e57d0808 324 unselect();
einsteingustavo 2:dc50e57d0808 325
einsteingustavo 2:dc50e57d0808 326 // no need to wait for transmit mode to be ready since its handled by the radio
einsteingustavo 2:dc50e57d0808 327 setMode(RF69_MODE_TX);
einsteingustavo 2:dc50e57d0808 328 uint32_t txStart = t.read_ms();
einsteingustavo 2:dc50e57d0808 329 while (_interrupt == 0 && t.read_ms() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish
einsteingustavo 2:dc50e57d0808 330 //while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady
einsteingustavo 2:dc50e57d0808 331 setMode(RF69_MODE_STANDBY);
einsteingustavo 2:dc50e57d0808 332 }
einsteingustavo 2:dc50e57d0808 333 // ON = disable filtering to capture all frames on network
einsteingustavo 2:dc50e57d0808 334 // OFF = enable node/broadcast filtering to capture only frames sent to this/broadcast address
einsteingustavo 2:dc50e57d0808 335 void RFM69::promiscuous(bool onOff) {
einsteingustavo 2:dc50e57d0808 336 _promiscuousMode = onOff;
einsteingustavo 2:dc50e57d0808 337 //writeReg(REG_PACKETCONFIG1, (readReg(REG_PACKETCONFIG1) & 0xF9) | (onOff ? RF_PACKET1_ADRSFILTERING_OFF : RF_PACKET1_ADRSFILTERING_NODEBROADCAST));
einsteingustavo 2:dc50e57d0808 338 }
einsteingustavo 2:dc50e57d0808 339
einsteingustavo 2:dc50e57d0808 340 void RFM69::setHighPower(bool onOff) {
einsteingustavo 2:dc50e57d0808 341 _isRFM69HW = onOff;
einsteingustavo 2:dc50e57d0808 342 writeReg(REG_OCP, _isRFM69HW ? RF_OCP_OFF : RF_OCP_ON);
einsteingustavo 2:dc50e57d0808 343 if (_isRFM69HW) // turning ON
einsteingustavo 2:dc50e57d0808 344 writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0x1F) | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON); // enable P1 & P2 amplifier stages
einsteingustavo 2:dc50e57d0808 345 else
einsteingustavo 2:dc50e57d0808 346 writeReg(REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | _powerLevel); // enable P0 only
einsteingustavo 2:dc50e57d0808 347 }
einsteingustavo 2:dc50e57d0808 348
einsteingustavo 2:dc50e57d0808 349 void RFM69::setHighPowerRegs(bool onOff) {
einsteingustavo 2:dc50e57d0808 350 writeReg(REG_TESTPA1, onOff ? 0x5D : 0x55);
einsteingustavo 2:dc50e57d0808 351 writeReg(REG_TESTPA2, onOff ? 0x7C : 0x70);
einsteingustavo 2:dc50e57d0808 352 }
einsteingustavo 2:dc50e57d0808 353
einsteingustavo 2:dc50e57d0808 354 /*
einsteingustavo 2:dc50e57d0808 355 void RFM69::setCS(uint8_t newSPISlaveSelect) {
einsteingustavo 2:dc50e57d0808 356 DigitalOut _slaveSelectPin(newSPISlaveSelect);
einsteingustavo 2:dc50e57d0808 357 _slaveSelectPin = 1;
einsteingustavo 2:dc50e57d0808 358 }
einsteingustavo 2:dc50e57d0808 359 */
einsteingustavo 2:dc50e57d0808 360 // for debugging
einsteingustavo 2:dc50e57d0808 361 void RFM69::readAllRegs()
einsteingustavo 2:dc50e57d0808 362 {
einsteingustavo 2:dc50e57d0808 363 uint8_t regVal,regAddr;
einsteingustavo 2:dc50e57d0808 364
einsteingustavo 2:dc50e57d0808 365 for (regAddr = 1; regAddr <= 0x4F; regAddr++)
einsteingustavo 2:dc50e57d0808 366 {
einsteingustavo 2:dc50e57d0808 367 select();
einsteingustavo 2:dc50e57d0808 368 _spi.write(regAddr & 0x7F); // send address + r/w bit
einsteingustavo 2:dc50e57d0808 369 regVal = _spi.write(0);
einsteingustavo 2:dc50e57d0808 370
einsteingustavo 2:dc50e57d0808 371 /* Serial.print(regAddr, HEX);
einsteingustavo 2:dc50e57d0808 372 Serial.print(" - ");
einsteingustavo 2:dc50e57d0808 373 Serial.print(regVal,HEX);
einsteingustavo 2:dc50e57d0808 374 Serial.print(" - ");
einsteingustavo 2:dc50e57d0808 375 Serial.println(regVal,BIN);*/
einsteingustavo 2:dc50e57d0808 376 }
einsteingustavo 2:dc50e57d0808 377 unselect();
einsteingustavo 2:dc50e57d0808 378 }
einsteingustavo 2:dc50e57d0808 379
einsteingustavo 2:dc50e57d0808 380 uint8_t RFM69::readTemperature(int8_t calFactor) // returns centigrade
einsteingustavo 2:dc50e57d0808 381 {
einsteingustavo 2:dc50e57d0808 382 uint8_t oldMode = _mode;
einsteingustavo 2:dc50e57d0808 383
einsteingustavo 2:dc50e57d0808 384 setMode(RF69_MODE_STANDBY);
einsteingustavo 2:dc50e57d0808 385 writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
einsteingustavo 2:dc50e57d0808 386 while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING));
einsteingustavo 2:dc50e57d0808 387 setMode(oldMode);
einsteingustavo 2:dc50e57d0808 388
einsteingustavo 2:dc50e57d0808 389 return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; // 'complement' corrects the slope, rising temp = rising val
einsteingustavo 2:dc50e57d0808 390 } // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
einsteingustavo 2:dc50e57d0808 391
einsteingustavo 2:dc50e57d0808 392 void RFM69::rcCalibration()
einsteingustavo 2:dc50e57d0808 393 {
einsteingustavo 2:dc50e57d0808 394 writeReg(REG_OSC1, RF_OSC1_RCCAL_START);
einsteingustavo 2:dc50e57d0808 395 while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00);
einsteingustavo 2:dc50e57d0808 396 }
einsteingustavo 2:dc50e57d0808 397 // C++ level interrupt handler for this instance
einsteingustavo 2:dc50e57d0808 398 void RFM69::interruptHandler() {
einsteingustavo 2:dc50e57d0808 399
einsteingustavo 2:dc50e57d0808 400 if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY))
einsteingustavo 2:dc50e57d0808 401 {
einsteingustavo 2:dc50e57d0808 402 setMode(RF69_MODE_STANDBY);
einsteingustavo 2:dc50e57d0808 403 select();
einsteingustavo 2:dc50e57d0808 404
einsteingustavo 2:dc50e57d0808 405 _spi.write(REG_FIFO & 0x7F);
einsteingustavo 2:dc50e57d0808 406 PAYLOADLEN = _spi.write(0);
einsteingustavo 2:dc50e57d0808 407 PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution
einsteingustavo 2:dc50e57d0808 408 TARGETID = _spi.write(0);
einsteingustavo 2:dc50e57d0808 409 if(!(_promiscuousMode || TARGETID == _address || TARGETID == RF69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in promiscuous mode
einsteingustavo 2:dc50e57d0808 410 || PAYLOADLEN < 3) // address situation could receive packets that are malformed and don't fit this libraries extra fields
einsteingustavo 2:dc50e57d0808 411 {
einsteingustavo 2:dc50e57d0808 412 PAYLOADLEN = 0;
einsteingustavo 2:dc50e57d0808 413 unselect();
einsteingustavo 2:dc50e57d0808 414 receiveBegin();
einsteingustavo 2:dc50e57d0808 415 return;
einsteingustavo 2:dc50e57d0808 416 }
einsteingustavo 2:dc50e57d0808 417
einsteingustavo 2:dc50e57d0808 418 DATALEN = PAYLOADLEN - 3;
einsteingustavo 2:dc50e57d0808 419 SENDERID = _spi.write(0);
einsteingustavo 2:dc50e57d0808 420 uint8_t CTLbyte = _spi.write(0);
einsteingustavo 2:dc50e57d0808 421
einsteingustavo 2:dc50e57d0808 422 ACK_RECEIVED = CTLbyte & 0x80; // extract ACK-received flag
einsteingustavo 2:dc50e57d0808 423 ACK_REQUESTED = CTLbyte & 0x40; // extract ACK-requested flag
einsteingustavo 2:dc50e57d0808 424
einsteingustavo 2:dc50e57d0808 425 for (uint8_t i = 0; i < DATALEN; i++)
einsteingustavo 2:dc50e57d0808 426 {
einsteingustavo 2:dc50e57d0808 427 DATA[i] = _spi.write(0);
einsteingustavo 2:dc50e57d0808 428 }
einsteingustavo 2:dc50e57d0808 429 if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string
einsteingustavo 2:dc50e57d0808 430 unselect();
einsteingustavo 2:dc50e57d0808 431 setMode(RF69_MODE_RX);
einsteingustavo 2:dc50e57d0808 432 }
einsteingustavo 2:dc50e57d0808 433 RSSI = readRSSI();
einsteingustavo 2:dc50e57d0808 434 }
einsteingustavo 2:dc50e57d0808 435
einsteingustavo 2:dc50e57d0808 436
einsteingustavo 2:dc50e57d0808 437 // These are low level functions that call the interrupt handler for the correct instance of RFM69.
einsteingustavo 2:dc50e57d0808 438 void RFM69::isr0()
einsteingustavo 2:dc50e57d0808 439 {
einsteingustavo 2:dc50e57d0808 440 interruptHandler();
einsteingustavo 2:dc50e57d0808 441 }
einsteingustavo 2:dc50e57d0808 442 void RFM69::receiveBegin() {
einsteingustavo 2:dc50e57d0808 443 DATALEN = 0;
einsteingustavo 2:dc50e57d0808 444 SENDERID = 0;
einsteingustavo 2:dc50e57d0808 445 TARGETID = 0;
einsteingustavo 2:dc50e57d0808 446 PAYLOADLEN = 0;
einsteingustavo 2:dc50e57d0808 447 ACK_REQUESTED = 0;
einsteingustavo 2:dc50e57d0808 448 ACK_RECEIVED = 0;
einsteingustavo 2:dc50e57d0808 449 RSSI = 0;
einsteingustavo 2:dc50e57d0808 450 if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)
einsteingustavo 2:dc50e57d0808 451 writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
einsteingustavo 2:dc50e57d0808 452 writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); // set DIO0 to "PAYLOADREADY" in receive mode
einsteingustavo 2:dc50e57d0808 453 setMode(RF69_MODE_RX);
einsteingustavo 2:dc50e57d0808 454 _interrupt.enable_irq();
einsteingustavo 2:dc50e57d0808 455 }
einsteingustavo 2:dc50e57d0808 456
einsteingustavo 2:dc50e57d0808 457 bool RFM69::receiveDone() {
einsteingustavo 2:dc50e57d0808 458 _interrupt.disable_irq(); // re-enabled in unselect() via setMode() or via receiveBegin()
einsteingustavo 2:dc50e57d0808 459 if (_mode == RF69_MODE_RX && PAYLOADLEN > 0)
einsteingustavo 2:dc50e57d0808 460 {
einsteingustavo 2:dc50e57d0808 461 setMode(RF69_MODE_STANDBY); // enables interrupts
einsteingustavo 2:dc50e57d0808 462 return true;
einsteingustavo 2:dc50e57d0808 463 }
einsteingustavo 2:dc50e57d0808 464 else if (_mode == RF69_MODE_RX) // already in RX no payload yet
einsteingustavo 2:dc50e57d0808 465 {
einsteingustavo 2:dc50e57d0808 466 _interrupt.enable_irq(); // explicitly re-enable interrupts
einsteingustavo 2:dc50e57d0808 467 return false;
einsteingustavo 2:dc50e57d0808 468 }
einsteingustavo 2:dc50e57d0808 469 receiveBegin();
einsteingustavo 2:dc50e57d0808 470 return false;
einsteingustavo 2:dc50e57d0808 471 }
einsteingustavo 2:dc50e57d0808 472
einsteingustavo 2:dc50e57d0808 473 // To enable encryption: radio.encrypt("ABCDEFGHIJKLMNOP");
einsteingustavo 2:dc50e57d0808 474 // To disable encryption: radio.encrypt(null) or radio.encrypt(0)
einsteingustavo 2:dc50e57d0808 475 // KEY HAS TO BE 16 bytes !!!
einsteingustavo 2:dc50e57d0808 476 void RFM69::encrypt(const char* key) {
einsteingustavo 2:dc50e57d0808 477 setMode(RF69_MODE_STANDBY);
einsteingustavo 2:dc50e57d0808 478 if (key != 0)
einsteingustavo 2:dc50e57d0808 479 {
einsteingustavo 2:dc50e57d0808 480 select();
einsteingustavo 2:dc50e57d0808 481 _spi.write(REG_AESKEY1 | 0x80);
einsteingustavo 2:dc50e57d0808 482 for (uint8_t i = 0; i < 16; i++)
einsteingustavo 2:dc50e57d0808 483 _spi.write(key[i]);
einsteingustavo 2:dc50e57d0808 484 unselect();
einsteingustavo 2:dc50e57d0808 485 }
einsteingustavo 2:dc50e57d0808 486 writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFE) | (key ? 1 : 0));
einsteingustavo 2:dc50e57d0808 487 }
einsteingustavo 2:dc50e57d0808 488
einsteingustavo 2:dc50e57d0808 489 int16_t RFM69::readRSSI(bool forceTrigger) {
einsteingustavo 2:dc50e57d0808 490 int16_t rssi = 0;
einsteingustavo 2:dc50e57d0808 491 if (forceTrigger)
einsteingustavo 2:dc50e57d0808 492 {
einsteingustavo 2:dc50e57d0808 493 // RSSI trigger not needed if DAGC is in continuous mode
einsteingustavo 2:dc50e57d0808 494 writeReg(REG_RSSICONFIG, RF_RSSI_START);
einsteingustavo 2:dc50e57d0808 495 while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00); // wait for RSSI_Ready
einsteingustavo 2:dc50e57d0808 496 }
einsteingustavo 2:dc50e57d0808 497 rssi = -readReg(REG_RSSIVALUE);
einsteingustavo 2:dc50e57d0808 498 rssi >>= 1;
einsteingustavo 2:dc50e57d0808 499 return rssi;
einsteingustavo 2:dc50e57d0808 500 }
einsteingustavo 2:dc50e57d0808 501
einsteingustavo 2:dc50e57d0808 502 uint8_t RFM69::readReg(uint8_t addr)
einsteingustavo 2:dc50e57d0808 503 {
einsteingustavo 2:dc50e57d0808 504 select();
einsteingustavo 2:dc50e57d0808 505 _spi.write(addr & 0x7F); // Send the address with the write mask off
einsteingustavo 2:dc50e57d0808 506 uint8_t val = _spi.write(0); // The written value is ignored, reg value is read
einsteingustavo 2:dc50e57d0808 507 unselect();
einsteingustavo 2:dc50e57d0808 508 return val;
einsteingustavo 2:dc50e57d0808 509 }
einsteingustavo 2:dc50e57d0808 510
einsteingustavo 2:dc50e57d0808 511 void RFM69::writeReg(uint8_t addr, uint8_t value)
einsteingustavo 2:dc50e57d0808 512 {
einsteingustavo 2:dc50e57d0808 513 select();
einsteingustavo 2:dc50e57d0808 514 _spi.write(addr | 0x80); // Send the address with the write mask on
einsteingustavo 2:dc50e57d0808 515 _spi.write(value); // New value follows
einsteingustavo 2:dc50e57d0808 516 unselect();
einsteingustavo 2:dc50e57d0808 517 }
einsteingustavo 2:dc50e57d0808 518
einsteingustavo 2:dc50e57d0808 519 // select the transceiver
einsteingustavo 2:dc50e57d0808 520 void RFM69::select() {
einsteingustavo 2:dc50e57d0808 521 _interrupt.disable_irq(); // Disable Interrupts
einsteingustavo 2:dc50e57d0808 522 /* // set RFM69 SPI settings
einsteingustavo 2:dc50e57d0808 523 SPI.setDataMode(SPI_MODE0);
einsteingustavo 2:dc50e57d0808 524 SPI.setBitOrder(MSBFIRST);
einsteingustavo 2:dc50e57d0808 525 SPI.setClockDivider(SPI_CLOCK_DIV4); // decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present */
einsteingustavo 2:dc50e57d0808 526 _slaveSelectPin = 0;
einsteingustavo 2:dc50e57d0808 527 }
einsteingustavo 2:dc50e57d0808 528
einsteingustavo 2:dc50e57d0808 529 // UNselect the transceiver chip
einsteingustavo 2:dc50e57d0808 530 void RFM69::unselect() {
einsteingustavo 2:dc50e57d0808 531 _slaveSelectPin = 1;
einsteingustavo 2:dc50e57d0808 532 _interrupt.enable_irq(); // Enable Interrupts
einsteingustavo 2:dc50e57d0808 533 }
einsteingustavo 2:dc50e57d0808 534