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