Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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