Port of lowpowerlab RFM69 Packet radio library for HopeRF RFM69H & HW (SX1231) modules

Dependents:   testepedro testepedro1 testepedro2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RFM69.h Source File

RFM69.h

00001 // **********************************************************************************
00002 // Driver definition for HopeRF RFM69W/RFM69HW/RFM69CW/RFM69HCW, Semtech SX1231/1231H
00003 // **********************************************************************************
00004 // Copyright Felix Rusu (2014), felix@lowpowerlab.com
00005 // http://lowpowerlab.com/
00006 // **********************************************************************************
00007 // License
00008 // **********************************************************************************
00009 // This program is free software; you can redistribute it 
00010 // and/or modify it under the terms of the GNU General    
00011 // Public License as published by the Free Software       
00012 // Foundation; either version 3 of the License, or        
00013 // (at your option) any later version.                    
00014 //                                                        
00015 // This program is distributed in the hope that it will   
00016 // be useful, but WITHOUT ANY WARRANTY; without even the  
00017 // implied warranty of MERCHANTABILITY or FITNESS FOR A   
00018 // PARTICULAR PURPOSE. See the GNU General Public        
00019 // License for more details.                              
00020 //                                                        
00021 // You should have received a copy of the GNU General    
00022 // Public License along with this program.
00023 // If not, see <http://www.gnu.org/licenses/>.
00024 //                                                        
00025 // Licence can be viewed at                               
00026 // http://www.gnu.org/licenses/gpl-3.0.txt
00027 //
00028 // Please maintain this license information along with authorship
00029 // and copyright notices in any redistribution of this code
00030 // **********************************************************************************
00031 #ifndef RFM69_h
00032 #define RFM69_h
00033 #include <mbed.h>            // assumes Arduino IDE v1.0 or greater
00034 
00035 #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)
00036 //#define RF69_SPI_CS             SS // SS is the SPI slave select pin, for instance D10 on ATmega328
00037 
00038 #define CSMA_LIMIT              -90 // upper RX signal sensitivity threshold in dBm for carrier sense access
00039 #define RF69_MODE_SLEEP         0 // XTAL OFF
00040 #define RF69_MODE_STANDBY       1 // XTAL ON
00041 #define RF69_MODE_SYNTH         2 // PLL ON
00042 #define RF69_MODE_RX            3 // RX MODE
00043 #define RF69_MODE_TX            4 // TX MODE
00044 
00045 // available frequency bands
00046 #define RF69_315MHZ            31 // non trivial values to avoid misconfiguration
00047 #define RF69_433MHZ            43
00048 #define RF69_868MHZ            86
00049 #define RF69_915MHZ            91
00050 
00051 #define null                  0
00052 #define COURSE_TEMP_COEF    -90 // puts the temperature reading in the ballpark, user can fine tune the returned value
00053 #define RF69_BROADCAST_ADDR 255
00054 #define RF69_CSMA_LIMIT_MS 1000
00055 #define RF69_TX_LIMIT_MS   1000
00056 #define RF69_FSTEP  61.03515625 // == FXOSC / 2^19 = 32MHz / 2^19 (p13 in datasheet)
00057 
00058 class RFM69 {
00059   public:
00060     static volatile uint8_t DATA[RF69_MAX_DATA_LEN]; // recv/xmit buf, including header & crc bytes
00061     static volatile uint8_t DATALEN;
00062     static volatile uint8_t SENDERID;
00063     static volatile uint8_t TARGETID; // should match _address
00064     static volatile uint8_t PAYLOADLEN;
00065     static volatile uint8_t ACK_REQUESTED;
00066     static volatile uint8_t ACK_RECEIVED; // should be polled immediately after sending a packet with ACK request
00067     static volatile int16_t RSSI; // most accurate RSSI during reception (closest to the reception)
00068     static volatile uint8_t _mode; // should be protected?  
00069 
00070     RFM69(PinName mosi, PinName miso, PinName sclk, PinName slaveSelectPin, PinName interrupt);
00071 
00072     bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1);
00073     void setAddress(uint8_t addr);
00074     void setNetwork(uint8_t networkID);
00075     bool canSend();
00076     void send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK=false);
00077     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
00078     bool receiveDone();
00079     bool ACKReceived(uint8_t fromNodeID);
00080     bool ACKRequested();
00081     void sendACK(const void* buffer = "", uint8_t bufferSize=0);
00082     uint32_t getFrequency();
00083     void setFrequency(uint32_t freqHz);
00084     void encrypt(const char* key);
00085 //    void setCS(uint8_t newSPISlaveSelect);
00086     int16_t readRSSI(bool forceTrigger=false);
00087     void promiscuous(bool onOff=true);
00088     void setHighPower(bool onOFF=true); // has to be called after initialize() for RFM69HW
00089     void setPowerLevel(uint8_t level); // reduce/increase transmit power level
00090     void sleep();
00091     uint8_t readTemperature(int8_t calFactor=0); // get CMOS temperature (8bit)
00092     void rcCalibration(); // calibrate the internal RC oscillator for use in wide temperature variations - see datasheet section [4.3.5. RC Timer Accuracy]
00093 
00094     // allow hacking registers by making these public
00095     uint8_t readReg(uint8_t addr);
00096     void writeReg(uint8_t addr, uint8_t val);
00097     void readAllRegs();
00098 
00099   protected:
00100     void isr0();
00101     void virtual interruptHandler();
00102     void sendFrame(uint8_t toAddress, const void* buffer, uint8_t size, bool requestACK=false, bool sendACK=false);
00103 
00104     static RFM69* selfPointer;
00105     DigitalOut _slaveSelectPin;
00106     InterruptIn _interrupt;
00107     uint8_t _address;
00108     Timer t;
00109     bool _promiscuousMode;
00110     uint8_t _powerLevel;
00111     bool _isRFM69HW;
00112     SPI _spi;
00113     void receiveBegin();
00114     void setMode(uint8_t mode);
00115     void setHighPowerRegs(bool onOff);
00116     void select();
00117     void unselect();
00118 };
00119 
00120 #endif