Modified to communicate with open energy monitoring platform

Dependents:   Solid_Fuel_Energy_Monitor

Committer:
dswood
Date:
Fri Jan 07 12:15:14 2022 +0000
Revision:
0:37f3683b3648
Heavily modified RFM69 to transmit and receive open energy monitor signals

Who changed what in which revision?

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