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 // **********************************************************************************
dswood 0:37f3683b3648 2 // Driver definition for HopeRF RFM69W/RFM69HW/RFM69CW/RFM69HCW, Semtech SX1231/1231H
dswood 0:37f3683b3648 3 // **********************************************************************************
dswood 0:37f3683b3648 4 // Copyright Felix Rusu (2014), felix@lowpowerlab.com
dswood 0:37f3683b3648 5 // http://lowpowerlab.com/
dswood 0:37f3683b3648 6 // **********************************************************************************
dswood 0:37f3683b3648 7 // License
dswood 0:37f3683b3648 8 // **********************************************************************************
dswood 0:37f3683b3648 9 // This program is free software; you can redistribute it
dswood 0:37f3683b3648 10 // and/or modify it under the terms of the GNU General
dswood 0:37f3683b3648 11 // Public License as published by the Free Software
dswood 0:37f3683b3648 12 // Foundation; either version 3 of the License, or
dswood 0:37f3683b3648 13 // (at your option) any later version.
dswood 0:37f3683b3648 14 //
dswood 0:37f3683b3648 15 // This program is distributed in the hope that it will
dswood 0:37f3683b3648 16 // be useful, but WITHOUT ANY WARRANTY; without even the
dswood 0:37f3683b3648 17 // implied warranty of MERCHANTABILITY or FITNESS FOR A
dswood 0:37f3683b3648 18 // PARTICULAR PURPOSE. See the GNU General Public
dswood 0:37f3683b3648 19 // License for more details.
dswood 0:37f3683b3648 20 //
dswood 0:37f3683b3648 21 // You should have received a copy of the GNU General
dswood 0:37f3683b3648 22 // Public License along with this program.
dswood 0:37f3683b3648 23 // If not, see <http://www.gnu.org/licenses/>.
dswood 0:37f3683b3648 24 //
dswood 0:37f3683b3648 25 // Licence can be viewed at
dswood 0:37f3683b3648 26 // http://www.gnu.org/licenses/gpl-3.0.txt
dswood 0:37f3683b3648 27 //
dswood 0:37f3683b3648 28 // Please maintain this license information along with authorship
dswood 0:37f3683b3648 29 // and copyright notices in any redistribution of this code
dswood 0:37f3683b3648 30 // **********************************************************************************
dswood 0:37f3683b3648 31 #ifndef RFM69_h
dswood 0:37f3683b3648 32 #define RFM69_h
dswood 0:37f3683b3648 33 #include <mbed.h> // assumes Arduino IDE v1.0 or greater
dswood 0:37f3683b3648 34
dswood 0:37f3683b3648 35 #define RF69_MAX_DATA_LEN 61 // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead - 2 bytes crc)
dswood 0:37f3683b3648 36 //#define RF69_SPI_CS SS // SS is the SPI slave select pin, for instance D10 on ATmega328
dswood 0:37f3683b3648 37
dswood 0:37f3683b3648 38 #define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
dswood 0:37f3683b3648 39 #define RF69_MODE_SLEEP 0 // XTAL OFF
dswood 0:37f3683b3648 40 #define RF69_MODE_STANDBY 1 // XTAL ON
dswood 0:37f3683b3648 41 #define RF69_MODE_SYNTH 2 // PLL ON
dswood 0:37f3683b3648 42 #define RF69_MODE_RX 3 // RX MODE
dswood 0:37f3683b3648 43 #define RF69_MODE_TX 4 // TX MODE
dswood 0:37f3683b3648 44
dswood 0:37f3683b3648 45 // available frequency bands
dswood 0:37f3683b3648 46 #define RF69_315MHZ 31 // non trivial values to avoid misconfiguration
dswood 0:37f3683b3648 47 #define RF69_433MHZ 43
dswood 0:37f3683b3648 48 #define RF69_868MHZ 86
dswood 0:37f3683b3648 49 #define RF69_915MHZ 91
dswood 0:37f3683b3648 50
dswood 0:37f3683b3648 51 #define null 0
dswood 0:37f3683b3648 52 #define COURSE_TEMP_COEF -90 // puts the temperature reading in the ballpark, user can fine tune the returned value
dswood 0:37f3683b3648 53 #define RF69_BROADCAST_ADDR 255
dswood 0:37f3683b3648 54 #define RF69_CSMA_LIMIT_MS 1000
dswood 0:37f3683b3648 55 #define RF69_TX_LIMIT_MS 1000
dswood 0:37f3683b3648 56 #define RF69_FSTEP 61.03515625 // == FXOSC / 2^19 = 32MHz / 2^19 (p13 in datasheet)
dswood 0:37f3683b3648 57
dswood 0:37f3683b3648 58 class RFM69 {
dswood 0:37f3683b3648 59 public:
dswood 0:37f3683b3648 60 static volatile uint8_t DATA[RF69_MAX_DATA_LEN]; // recv/xmit buf, including header & crc bytes
dswood 0:37f3683b3648 61 static volatile uint8_t DATALEN;
dswood 0:37f3683b3648 62 static volatile uint8_t SENDERID;
dswood 0:37f3683b3648 63 static volatile uint8_t TARGETID; // should match _address
dswood 0:37f3683b3648 64 static volatile uint8_t PAYLOADLEN;
dswood 0:37f3683b3648 65 static volatile uint8_t ACK_REQUESTED;
dswood 0:37f3683b3648 66 static volatile uint8_t ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
dswood 0:37f3683b3648 67 static volatile int16_t RSSI; // most accurate RSSI during reception (closest to the reception)
dswood 0:37f3683b3648 68 static volatile uint8_t _mode; // should be protected?
dswood 0:37f3683b3648 69
dswood 0:37f3683b3648 70
dswood 0:37f3683b3648 71 RFM69(PinName mosi, PinName miso, PinName sclk, PinName slaveSelectPin, PinName interrupt);
dswood 0:37f3683b3648 72
dswood 0:37f3683b3648 73 bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1);
dswood 0:37f3683b3648 74 void setAddress(uint8_t addr);
dswood 0:37f3683b3648 75 void setNetwork(uint8_t networkID);
dswood 0:37f3683b3648 76 bool canSend();
dswood 0:37f3683b3648 77 void send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false);
dswood 0:37f3683b3648 78 bool sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries=2, uint8_t retryWaitTime=40); // 40ms roundtrip req for 61byte packets
dswood 0:37f3683b3648 79 bool receiveDone();
dswood 0:37f3683b3648 80 bool ACKReceived(uint8_t fromNodeID);
dswood 0:37f3683b3648 81 bool ACKRequested();
dswood 0:37f3683b3648 82 void sendACK(const void* buffer = "", uint8_t bufferSize=0);
dswood 0:37f3683b3648 83 uint32_t getFrequency();
dswood 0:37f3683b3648 84 void setFrequency(uint32_t freqHz);
dswood 0:37f3683b3648 85 void encrypt(const char* key);
dswood 0:37f3683b3648 86 // void setCS(uint8_t newSPISlaveSelect);
dswood 0:37f3683b3648 87 int16_t readRSSI(bool forceTrigger=false);
dswood 0:37f3683b3648 88 void promiscuous(bool onOff=true);
dswood 0:37f3683b3648 89 void setHighPower(bool onOFF=true); // has to be called after initialize() for RFM69HW
dswood 0:37f3683b3648 90 void setPowerLevel(uint8_t level); // reduce/increase transmit power level
dswood 0:37f3683b3648 91 void sleep(bool onOFF=true);
dswood 0:37f3683b3648 92 uint8_t readTemperature(int8_t calFactor=0); // get CMOS temperature (8bit)
dswood 0:37f3683b3648 93 void rcCalibration(); // calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy]
dswood 0:37f3683b3648 94
dswood 0:37f3683b3648 95 // allow hacking registers by making these public
dswood 0:37f3683b3648 96 uint8_t readReg(uint8_t addr);
dswood 0:37f3683b3648 97 void writeReg(uint8_t addr, uint8_t val);
dswood 0:37f3683b3648 98 void readAllRegs();
dswood 0:37f3683b3648 99 void sendWait();
dswood 0:37f3683b3648 100 void setNodeID(uint8_t nodeID);
dswood 0:37f3683b3648 101
dswood 0:37f3683b3648 102 protected:
dswood 0:37f3683b3648 103 void isr0();
dswood 0:37f3683b3648 104 void virtual interruptHandler();
dswood 0:37f3683b3648 105 void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false);
dswood 0:37f3683b3648 106 void fifoFlush();
dswood 0:37f3683b3648 107
dswood 0:37f3683b3648 108 static RFM69* selfPointer;
dswood 0:37f3683b3648 109 DigitalOut _slaveSelectPin;
dswood 0:37f3683b3648 110 InterruptIn _interrupt;
dswood 0:37f3683b3648 111 uint8_t _address;
dswood 0:37f3683b3648 112 Timer t;
dswood 0:37f3683b3648 113 bool _promiscuousMode;
dswood 0:37f3683b3648 114 uint8_t _powerLevel;
dswood 0:37f3683b3648 115 bool _isRFM69HW;
dswood 0:37f3683b3648 116 SPI _spi;
dswood 0:37f3683b3648 117 void receiveBegin();
dswood 0:37f3683b3648 118 void setMode(uint8_t mode);
dswood 0:37f3683b3648 119 void setHighPowerRegs(bool onOff);
dswood 0:37f3683b3648 120 void select();
dswood 0:37f3683b3648 121 void unselect();
dswood 0:37f3683b3648 122 uint8_t MyNodeID;
dswood 0:37f3683b3648 123 uint8_t MyNetworkID;
dswood 0:37f3683b3648 124 };
dswood 0:37f3683b3648 125
dswood 0:37f3683b3648 126 #endif